/** * rsa_parse_pub_key() - extracts an rsa private key from BER encoded buffer * and stores it in the provided struct rsa_key * * @rsa_key: struct rsa_key key representation * @key: key in BER format * @key_len: length of key * * Return: 0 on success or error code in case of error */ int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key, unsigned int key_len) { int ret; free_mpis(rsa_key); ret = asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len); if (ret < 0) goto error; return 0; error: free_mpis(rsa_key); return ret; }
static libspectrum_error create_key( gcry_sexp_t *s_key, libspectrum_rzx_dsa_key *key, int secret_key ) { gcry_error_t error; size_t i; gcry_mpi_t mpis[MPI_COUNT]; const char *format; for( i=0; i<MPI_COUNT; i++ ) mpis[i] = NULL; error = gcry_mpi_scan( &mpis[0], GCRYMPI_FMT_HEX, (unsigned char*)key->p, 0, NULL ); if( !error ) error = gcry_mpi_scan( &mpis[1], GCRYMPI_FMT_HEX, (unsigned char*)key->q, 0, NULL ); if( !error ) error = gcry_mpi_scan( &mpis[2], GCRYMPI_FMT_HEX, (unsigned char*)key->g, 0, NULL ); if( !error ) error = gcry_mpi_scan( &mpis[3], GCRYMPI_FMT_HEX, (unsigned char*)key->y, 0, NULL ); if( !error && secret_key ) error = gcry_mpi_scan( &mpis[4], GCRYMPI_FMT_HEX, (unsigned char*)key->x, 0, NULL ); if( error ) { libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC, "create_key: error creating MPIs: %s", gcry_strerror( error ) ); free_mpis( mpis, MPI_COUNT ); return LIBSPECTRUM_ERROR_LOGIC; } format = secret_key ? private_key_format : public_key_format; error = gcry_sexp_build( s_key, NULL, format, mpis[0], mpis[1], mpis[2], mpis[3], mpis[4] ); if( error ) { libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC, "create_key: error creating key: %s", gcry_strerror( error ) ); free_mpis( mpis, MPI_COUNT ); return LIBSPECTRUM_ERROR_LOGIC; } free_mpis( mpis, MPI_COUNT ); /* FIXME: Test public keys as well once gcry_pk_testkey acquires this functionality */ if( secret_key ) { error = gcry_pk_testkey( *s_key ); if( error ) { libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC, "create_key: key is not sane: %s", gcry_strerror( error ) ); return LIBSPECTRUM_ERROR_LOGIC; } } return LIBSPECTRUM_ERROR_NONE; }
/** * rsa_free_key() - frees rsa key allocated by rsa_parse_key() * * @rsa_key: struct rsa_key key representation */ void rsa_free_key(struct rsa_key *key) { free_mpis(key); }