/** * cdk_listkey_next: * @ctx: list key context * @r_key: the pointer to the new key node object * * Retrieve the next key from the pattern of the key list context. **/ cdk_error_t cdk_listkey_next( cdk_listkey_t ctx, cdk_kbnode_t * ret_key ) { if( !ctx || !ret_key ) return CDK_Inv_Value; if( !ctx->init ) return CDK_Inv_Mode; if( ctx->type && ctx->u.patt[0] == '*' ) return cdk_keydb_get_keyblock( ctx->inp, ret_key ); else if( ctx->type ) { cdk_kbnode_t node; struct cdk_dbsearch_s ks; int rc; for( ;; ) { rc = cdk_keydb_get_keyblock( ctx->inp, &node ); if( rc ) return rc; memset( &ks, 0, sizeof ks ); ks.type = CDK_DBSEARCH_SUBSTR; ks.u.pattern = ctx->u.patt; if( find_by_pattern( node, &ks ) ) { *ret_key = node; return 0; } cdk_kbnode_release( node ); node = NULL; } } else { if( !ctx->t ) ctx->t = ctx->u.fpatt; else if( ctx->t->next ) ctx->t = ctx->t->next; else return CDK_EOF; return cdk_keydb_get_bypattern( ctx->db, ctx->t->d, ret_key ); } return CDK_General_Error; }
/** * gnutls_openpgp_key_import - This function will import a RAW or BASE64 encoded key * @key: The structure to store the parsed key. * @data: The RAW or BASE64 encoded key. * @format: One of gnutls_openpgp_key_fmt_t elements. * * This function will convert the given RAW or Base64 encoded key * to the native gnutls_openpgp_key_t format. The output will be stored in 'key'. * * Returns 0 on success. * **/ int gnutls_openpgp_key_import (gnutls_openpgp_key_t key, const gnutls_datum_t * data, gnutls_openpgp_key_fmt_t format) { int rc; if (format == GNUTLS_OPENPGP_FMT_RAW) { rc = cdk_kbnode_read_from_mem (&key->knode, data->data, data->size); if (rc) { rc = _gnutls_map_cdk_rc (rc); gnutls_assert (); return rc; } } else { /* base64 */ key->inp = cdk_stream_tmp_from_mem (data->data, data->size); if (key->inp == NULL) { gnutls_assert (); return GNUTLS_E_INTERNAL_ERROR; } rc = cdk_stream_set_armor_flag (key->inp, 0); if (rc) { rc = _gnutls_map_cdk_rc (rc); gnutls_assert (); return rc; } rc = cdk_keydb_get_keyblock (key->inp, &key->knode); if (rc) { rc = _gnutls_map_cdk_rc (rc); gnutls_assert (); return rc; } } return 0; }
/** * cdk_kbnode_read_from_mem: * @ret_node: the new key node * @buf: the buffer which stores the key sequence * @buflen: the length of the buffer * * Tries to read a key node from the memory buffer @buf. **/ cdk_error_t cdk_kbnode_read_from_mem (cdk_kbnode_t * ret_node, const byte * buf, size_t buflen) { cdk_stream_t inp; cdk_error_t rc; if (!ret_node || !buf) return CDK_Inv_Value; *ret_node = NULL; if (!buflen) return CDK_Too_Short; rc = cdk_stream_tmp_from_mem (buf, buflen, &inp); if (rc) return rc; rc = cdk_keydb_get_keyblock (inp, ret_node); if (rc) gnutls_assert (); cdk_stream_close (inp); return rc; }
/** * gnutls_openpgp_privkey_import: * @key: The structure to store the parsed key. * @data: The RAW or BASE64 encoded key. * @format: One of #gnutls_openpgp_crt_fmt_t elements. * @password: not used for now * @flags: should be zero * * This function will convert the given RAW or Base64 encoded key to * the native gnutls_openpgp_privkey_t format. The output will be * stored in 'key'. * * Returns: %GNUTLS_E_SUCCESS on success, or an error code. **/ int gnutls_openpgp_privkey_import (gnutls_openpgp_privkey_t key, const gnutls_datum_t * data, gnutls_openpgp_crt_fmt_t format, const char *password, unsigned int flags) { cdk_stream_t inp; cdk_packet_t pkt; int rc; if (data->data == NULL || data->size == 0) { gnutls_assert (); return GNUTLS_E_OPENPGP_GETKEY_FAILED; } if (format == GNUTLS_OPENPGP_FMT_RAW) { rc = cdk_kbnode_read_from_mem (&key->knode, data->data, data->size); if (rc != 0) { rc = _gnutls_map_cdk_rc (rc); gnutls_assert (); return rc; } } else { rc = cdk_stream_tmp_from_mem (data->data, data->size, &inp); if (rc != 0) { rc = _gnutls_map_cdk_rc (rc); gnutls_assert (); return rc; } if (cdk_armor_filter_use (inp)) { rc = cdk_stream_set_armor_flag (inp, 0); if (rc != 0) { rc = _gnutls_map_cdk_rc (rc); cdk_stream_close (inp); gnutls_assert (); return rc; } } rc = cdk_keydb_get_keyblock (inp, &key->knode); cdk_stream_close (inp); if (rc != 0) { rc = _gnutls_map_cdk_rc (rc); gnutls_assert (); return rc; } } /* Test if the import was successful. */ pkt = cdk_kbnode_find_packet (key->knode, CDK_PKT_SECRET_KEY); if (pkt == NULL) { gnutls_assert (); return GNUTLS_E_OPENPGP_GETKEY_FAILED; } return 0; }
/** * cdk_keydb_search: * @hd: the keydb object * @ks: the keydb search object * @ret_key: kbnode object to store the key * * Search for a key in the given keyring. The search mode is handled * via @ks. If the key was found, @ret_key contains the key data. **/ cdk_error_t cdk_keydb_search( cdk_keydb_hd_t hd, cdk_kbnode_t * ret_key ) { cdk_stream_t kr = NULL; cdk_kbnode_t knode = NULL; cdk_dbsearch_t ks; u32 off = 0; size_t pos = 0; int key_found = 0, cache_hit = 0; int rc = 0; if( !hd || !ret_key ) return CDK_Inv_Value; *ret_key = NULL; hd->search = 1; rc = cdk_keydb_open( hd, &kr ); if( rc ) return rc; rc = keydb_pos_from_cache( hd, hd->dbs, &cache_hit, &off ); if( rc ) return rc; ks = hd->dbs; while( !key_found && !rc ) { if( cache_hit && ks->type != CDK_DBSEARCH_NEXT ) cdk_stream_seek( kr, off ); pos = cdk_stream_tell( kr ); rc = cdk_keydb_get_keyblock( kr, &knode ); if( rc ) { if( rc == CDK_EOF && knode ) rc = 0; if( !knode && rc == CDK_EOF ) rc = CDK_Error_No_Key; if( rc ) break; } switch( ks->type ) { case CDK_DBSEARCH_SHORT_KEYID: case CDK_DBSEARCH_KEYID: key_found = find_by_keyid( knode, ks ); break; case CDK_DBSEARCH_FPR: key_found = find_by_fpr( knode, ks ); break; case CDK_DBSEARCH_EXACT: case CDK_DBSEARCH_SUBSTR: key_found = find_by_pattern( knode, ks ); break; case CDK_DBSEARCH_NEXT: key_found = knode? 1 : 0; break; } if( key_found ) { if( !keydb_cache_find( hd->cache, ks ) ) keydb_cache_add( hd, ks, pos ); break; } cdk_kbnode_release( knode ); knode = NULL; } hd->search = 0; *ret_key = key_found? knode : NULL; return rc; }