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