FreeBSD/crypto/openssh/authfd.c
/*
 * Fetch list of identities held by the agent.
 */
Fetch list of identities held by the agent.
Fetch list of identities held by the agent .
int
ssh_fetch_identitylist(int sock, int version, struct ssh_identitylist **idlp)
{
	u_char type, code1 = 0, code2 = 0;
	u_int32_t num, i;
	struct sshbuf *msg;
	struct ssh_identitylist *idl = NULL;
	int r;
	switch (version) {
	case 1:
		code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
		code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER;
		break;
	case 2:
		code1 = SSH2_AGENTC_REQUEST_IDENTITIES;
		code2 = SSH2_AGENT_IDENTITIES_ANSWER;
		break;
	default:
		return SSH_ERR_INVALID_ARGUMENT;
	}
	if ((msg = sshbuf_new()) == NULL)
		return SSH_ERR_ALLOC_FAIL;
	if ((r = sshbuf_put_u8(msg, code1)) != 0)
		goto out;
	if ((r = ssh_request_reply(sock, msg, msg)) != 0)
		goto out;
	if ((r = sshbuf_get_u8(msg, &type)) != 0)
		goto out;
	if (agent_failed(type)) {
		r = SSH_ERR_AGENT_FAILURE;
		goto out;
	} else if (type != code2) {
		r = SSH_ERR_INVALID_FORMAT;
		goto out;
	}
	if ((r = sshbuf_get_u32(msg, &num)) != 0)
		goto out;
	if (num > MAX_AGENT_IDENTITIES) {
		r = SSH_ERR_INVALID_FORMAT;
		goto out;
	}
	if (num == 0) {
		r = SSH_ERR_AGENT_NO_IDENTITIES;
		goto out;
	}
	if ((idl = calloc(1, sizeof(*idl))) == NULL ||
	    (idl->keys = calloc(num, sizeof(*idl->keys))) == NULL ||
	    (idl->comments = calloc(num, sizeof(*idl->comments))) == NULL) {
		r = SSH_ERR_ALLOC_FAIL;
		goto out;
	}
	for (i = 0; i < num;) {
		switch (version) {
		case 1:
#ifdef WITH_SSH1
			if ((r = deserialise_identity1(msg,
			    &(idl->keys[i]), &(idl->comments[i]))) != 0)
				goto out;
#endif
			break;
		case 2:
			if ((r = deserialise_identity2(msg,
			    &(idl->keys[i]), &(idl->comments[i]))) != 0) {
				if (r == SSH_ERR_KEY_TYPE_UNKNOWN) {
					num--;
					continue;
				} else
					goto out;
			}
			break;
		}
		i++;
	}
	idl->nkeys = num;
	*idlp = idl;
	idl = NULL;
	r = 0;
 out:
	sshbuf_free(msg);
	if (idl != NULL)
		ssh_free_identitylist(idl);
	return r;
}
int ssh_fetch_identitylist ( int sock , int version , struct ssh_identitylist * * idlp ) { u_char type , code1 = 0 , code2 = 0 ; u_int32_t num , i ; struct sshbuf * msg ; struct ssh_identitylist * idl = NULL ; int r ; switch ( version ) { case 1 : code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES ; code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER ; break ; case 2 : code1 = SSH2_AGENTC_REQUEST_IDENTITIES ; code2 = SSH2_AGENT_IDENTITIES_ANSWER ; break ; default : return SSH_ERR_INVALID_ARGUMENT ; } if ( ( msg = sshbuf_new ( ) ) == NULL ) return SSH_ERR_ALLOC_FAIL ; if ( ( r = sshbuf_put_u8 ( msg , code1 ) ) != 0 ) goto out ; if ( ( r = ssh_request_reply ( sock , msg , msg ) ) != 0 ) goto out ; if ( ( r = sshbuf_get_u8 ( msg , & type ) ) != 0 ) goto out ; if ( agent_failed ( type ) ) { r = SSH_ERR_AGENT_FAILURE ; goto out ; } else if ( type != code2 ) { r = SSH_ERR_INVALID_FORMAT ; goto out ; } if ( ( r = sshbuf_get_u32 ( msg , & num ) ) != 0 ) goto out ; if ( num > MAX_AGENT_IDENTITIES ) { r = SSH_ERR_INVALID_FORMAT ; goto out ; } if ( num == 0 ) { r = SSH_ERR_AGENT_NO_IDENTITIES ; goto out ; } if ( ( idl = calloc ( 1 , sizeof ( * idl ) ) ) == NULL || ( idl -> keys = calloc ( num , sizeof ( * idl -> keys ) ) ) == NULL || ( idl -> comments = calloc ( num , sizeof ( * idl -> comments ) ) ) == NULL ) { r = SSH_ERR_ALLOC_FAIL ; goto out ; } for ( i = 0 ; i < num ; ) { switch ( version ) { case 1 : #ifdef WITH_SSH1 if ( ( r = deserialise_identity1 ( msg , & ( idl -> keys [ i ] ) , & ( idl -> comments [ i ] ) ) ) != 0 ) goto out ; #endif break ; case 2 : if ( ( r = deserialise_identity2 ( msg , & ( idl -> keys [ i ] ) , & ( idl -> comments [ i ] ) ) ) != 0 ) { if ( r == SSH_ERR_KEY_TYPE_UNKNOWN ) { num -- ; continue ; } else goto out ; } break ; } i ++ ; } idl -> nkeys = num ; * idlp = idl ; idl = NULL ; r = 0 ; out : sshbuf_free ( msg ) ; if ( idl != NULL ) ssh_free_identitylist ( idl ) ; return r ; }
Fetch list of identities held by the agent.
Fetch list of identities held by the agent .
/* Determine request and expected response types */ /*
	 * Send a message to the agent requesting for a list of the
	 * identities it can represent.
	 */ /* Get message type, and verify that we got a proper answer. */ /* Get the number of entries in the response and check it for sanity. */ /* Deserialise the response into a list of keys/comments */ /* Gracefully skip unknown key types */