1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5*b077aed3SPierre Proncheryd2i_PrivateKey_ex, d2i_PrivateKey, d2i_PublicKey, d2i_KeyParams, 6*b077aed3SPierre Proncheryd2i_AutoPrivateKey_ex, d2i_AutoPrivateKey, i2d_PrivateKey, i2d_PublicKey, 7*b077aed3SPierre Proncheryi2d_KeyParams, i2d_KeyParams_bio, d2i_PrivateKey_ex_bio, d2i_PrivateKey_bio, 8*b077aed3SPierre Proncheryd2i_PrivateKey_ex_fp, d2i_PrivateKey_fp, d2i_KeyParams_bio, i2d_PrivateKey_bio, 9*b077aed3SPierre Proncheryi2d_PrivateKey_fp 10e71b7053SJung-uk Kim- decode and encode functions for reading and saving EVP_PKEY structures 11e71b7053SJung-uk Kim 12e71b7053SJung-uk Kim=head1 SYNOPSIS 13e71b7053SJung-uk Kim 14e71b7053SJung-uk Kim #include <openssl/evp.h> 15e71b7053SJung-uk Kim 16*b077aed3SPierre Pronchery EVP_PKEY *d2i_PrivateKey_ex(int type, EVP_PKEY **a, const unsigned char **pp, 17*b077aed3SPierre Pronchery long length, OSSL_LIB_CTX *libctx, 18*b077aed3SPierre Pronchery const char *propq); 19e71b7053SJung-uk Kim EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, 20e71b7053SJung-uk Kim long length); 21e71b7053SJung-uk Kim EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp, 22e71b7053SJung-uk Kim long length); 23*b077aed3SPierre Pronchery EVP_PKEY *d2i_KeyParams(int type, EVP_PKEY **a, const unsigned char **pp, 24*b077aed3SPierre Pronchery long length); 25*b077aed3SPierre Pronchery EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp, 26*b077aed3SPierre Pronchery long length, OSSL_LIB_CTX *libctx, 27*b077aed3SPierre Pronchery const char *propq); 28e71b7053SJung-uk Kim EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp, 29e71b7053SJung-uk Kim long length); 30e71b7053SJung-uk Kim 31*b077aed3SPierre Pronchery int i2d_PrivateKey(const EVP_PKEY *a, unsigned char **pp); 32*b077aed3SPierre Pronchery int i2d_PublicKey(const EVP_PKEY *a, unsigned char **pp); 33*b077aed3SPierre Pronchery int i2d_KeyParams(const EVP_PKEY *a, unsigned char **pp); 34*b077aed3SPierre Pronchery int i2d_KeyParams_bio(BIO *bp, const EVP_PKEY *pkey); 35*b077aed3SPierre Pronchery EVP_PKEY *d2i_KeyParams_bio(int type, EVP_PKEY **a, BIO *in); 36*b077aed3SPierre Pronchery 37*b077aed3SPierre Pronchery 38*b077aed3SPierre Pronchery #include <openssl/x509.h> 39*b077aed3SPierre Pronchery 40*b077aed3SPierre Pronchery EVP_PKEY *d2i_PrivateKey_ex_bio(BIO *bp, EVP_PKEY **a, OSSL_LIB_CTX *libctx, 41*b077aed3SPierre Pronchery const char *propq); 42e71b7053SJung-uk Kim EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a); 43*b077aed3SPierre Pronchery EVP_PKEY *d2i_PrivateKey_ex_fp(FILE *fp, EVP_PKEY **a, OSSL_LIB_CTX *libctx, 44*b077aed3SPierre Pronchery const char *propq); 45*b077aed3SPierre Pronchery EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a); 46*b077aed3SPierre Pronchery 47*b077aed3SPierre Pronchery int i2d_PrivateKey_bio(BIO *bp, const EVP_PKEY *pkey); 48*b077aed3SPierre Pronchery int i2d_PrivateKey_fp(FILE *fp, const EVP_PKEY *pkey); 49e71b7053SJung-uk Kim 50e71b7053SJung-uk Kim=head1 DESCRIPTION 51e71b7053SJung-uk Kim 52*b077aed3SPierre Proncheryd2i_PrivateKey_ex() decodes a private key using algorithm I<type>. It attempts 53*b077aed3SPierre Proncheryto use any key-specific format or PKCS#8 unencrypted PrivateKeyInfo format. 54*b077aed3SPierre ProncheryThe I<type> parameter should be a public key algorithm constant such as 55*b077aed3SPierre ProncheryB<EVP_PKEY_RSA>. An error occurs if the decoded key does not match I<type>. Some 56*b077aed3SPierre Proncheryprivate key decoding implementations may use cryptographic algorithms (for 57*b077aed3SPierre Proncheryexample to automatically derive the public key if it is not explicitly 58*b077aed3SPierre Proncheryincluded in the encoding). In this case the supplied library context I<libctx> 59*b077aed3SPierre Proncheryand property query string I<propq> are used. 60*b077aed3SPierre ProncheryIf successful and the I<a> parameter is not NULL the function assigns the 61*b077aed3SPierre Proncheryreturned B<EVP_PKEY> structure pointer to I<*a>, overwriting any previous value. 62*b077aed3SPierre Pronchery 63*b077aed3SPierre Proncheryd2i_PrivateKey() does the same as d2i_PrivateKey_ex() except that the default 64*b077aed3SPierre Proncherylibrary context and property query string are used. 65e71b7053SJung-uk Kimd2i_PublicKey() does the same for public keys. 66*b077aed3SPierre Proncheryd2i_KeyParams() does the same for key parameters. 67e71b7053SJung-uk Kim 68*b077aed3SPierre ProncheryThe d2i_PrivateKey_ex_bio() and d2i_PrivateKey_bio() functions are similar to 69*b077aed3SPierre Proncheryd2i_PrivateKey_ex() and d2i_PrivateKey() respectively except that they decode 70*b077aed3SPierre Proncherythe data read from the given BIO. The d2i_PrivateKey_ex_fp() and 71*b077aed3SPierre Proncheryd2i_PrivateKey_fp() functions are the same except that they read the data from 72*b077aed3SPierre Proncherythe given FILE. 73e71b7053SJung-uk Kim 74*b077aed3SPierre Proncheryd2i_AutoPrivateKey_ex() and d2i_AutoPrivateKey() are similar to 75*b077aed3SPierre Proncheryd2i_PrivateKey_ex() and d2i_PrivateKey() respectively except that they attempt 76*b077aed3SPierre Proncheryto automatically detect the private key format. 77*b077aed3SPierre Pronchery 78*b077aed3SPierre Proncheryi2d_PrivateKey() encodes I<a>. It uses a key specific format or, if none is 79e71b7053SJung-uk Kimdefined for that key type, PKCS#8 unencrypted PrivateKeyInfo format. 80e71b7053SJung-uk Kimi2d_PublicKey() does the same for public keys. 81*b077aed3SPierre Proncheryi2d_KeyParams() does the same for key parameters. 82e71b7053SJung-uk KimThese functions are similar to the d2i_X509() functions; see L<d2i_X509(3)>. 83*b077aed3SPierre Proncheryi2d_PrivateKey_bio() and i2d_PrivateKey_fp() do the same thing except that they 84*b077aed3SPierre Proncheryencode to a B<BIO> or B<FILE> respectively. Again, these work similarly to the 85*b077aed3SPierre Proncheryfunctions described in L<d2i_X509(3)>. 86e71b7053SJung-uk Kim 87e71b7053SJung-uk Kim=head1 NOTES 88e71b7053SJung-uk Kim 899a3ae0cdSJung-uk KimAll the functions that operate on data in memory update the data pointer I<*pp> 909a3ae0cdSJung-uk Kimafter a successful operation, just like the other d2i and i2d functions; 919a3ae0cdSJung-uk Kimsee L<d2i_X509(3)>. 929a3ae0cdSJung-uk Kim 93e71b7053SJung-uk KimAll these functions use DER format and unencrypted keys. Applications wishing 94e71b7053SJung-uk Kimto encrypt or decrypt private keys should use other functions such as 95e71b7053SJung-uk Kimd2i_PKCS8PrivateKey() instead. 96e71b7053SJung-uk Kim 97*b077aed3SPierre ProncheryTo decode a key with type B<EVP_PKEY_EC>, d2i_PublicKey() requires I<*a> to be 986935a639SJung-uk Kima non-NULL EVP_PKEY structure assigned an EC_KEY structure referencing the proper 996935a639SJung-uk KimEC_GROUP. 1006935a639SJung-uk Kim 101e71b7053SJung-uk Kim=head1 RETURN VALUES 102e71b7053SJung-uk Kim 103*b077aed3SPierre ProncheryThe d2i_PrivateKey_ex(), d2i_PrivateKey(), d2i_AutoPrivateKey_ex(), 104*b077aed3SPierre Proncheryd2i_AutoPrivateKey(), d2i_PrivateKey_ex_bio(), d2i_PrivateKey_bio(), 105*b077aed3SPierre Proncheryd2i_PrivateKey_ex_fp(), d2i_PrivateKey_fp(), d2i_PublicKey(), d2i_KeyParams() 106*b077aed3SPierre Proncheryand d2i_KeyParams_bio() functions return a valid B<EVP_PKEY> structure or NULL if 107*b077aed3SPierre Proncheryan error occurs. The error code can be obtained by calling L<ERR_get_error(3)>. 108e71b7053SJung-uk Kim 109*b077aed3SPierre Proncheryi2d_PrivateKey(), i2d_PublicKey() and i2d_KeyParams() return the number of 110*b077aed3SPierre Proncherybytes successfully encoded or a negative value if an error occurs. The error 111*b077aed3SPierre Proncherycode can be obtained by calling L<ERR_get_error(3)>. 112*b077aed3SPierre Pronchery 113*b077aed3SPierre Proncheryi2d_PrivateKey_bio(), i2d_PrivateKey_fp() and i2d_KeyParams_bio() return 1 if 114*b077aed3SPierre Proncherysuccessfully encoded or zero if an error occurs. 115e71b7053SJung-uk Kim 116e71b7053SJung-uk Kim=head1 SEE ALSO 117e71b7053SJung-uk Kim 118e71b7053SJung-uk KimL<crypto(7)>, 119e71b7053SJung-uk KimL<d2i_PKCS8PrivateKey_bio(3)> 120e71b7053SJung-uk Kim 121*b077aed3SPierre Pronchery=head1 HISTORY 122*b077aed3SPierre Pronchery 123*b077aed3SPierre Proncheryd2i_PrivateKey_ex(), d2i_PrivateKey_ex_bio(), d2i_PrivateKey_ex_fp(), and 124*b077aed3SPierre Proncheryd2i_AutoPrivateKey_ex() were added in OpenSSL 3.0. 125*b077aed3SPierre Pronchery 126e71b7053SJung-uk Kim=head1 COPYRIGHT 127e71b7053SJung-uk Kim 1289a3ae0cdSJung-uk KimCopyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. 129e71b7053SJung-uk Kim 130*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 131e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 132e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 133e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 134e71b7053SJung-uk Kim 135e71b7053SJung-uk Kim=cut 136