pkcs8.h (12245B)
1 // Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef OPENSSL_HEADER_PKCS8_H 16 #define OPENSSL_HEADER_PKCS8_H 17 18 #include <openssl/base.h> // IWYU pragma: export 19 #include <openssl/x509.h> 20 21 22 #if defined(__cplusplus) 23 extern "C" { 24 #endif 25 26 27 // PKCS8_encrypt serializes and encrypts a PKCS8_PRIV_KEY_INFO with PBES1 or 28 // PBES2 as defined in PKCS #5. Only pbeWithSHAAnd128BitRC4, 29 // pbeWithSHAAnd3-KeyTripleDES-CBC and pbeWithSHA1And40BitRC2, defined in PKCS 30 // #12, and PBES2, are supported. PBES2 is selected by setting |cipher| and 31 // passing -1 for |pbe_nid|. Otherwise, PBES1 is used and |cipher| is ignored. 32 // 33 // |pass| is used as the password. If a PBES1 scheme from PKCS #12 is used, this 34 // will be converted to a raw byte string as specified in B.1 of PKCS #12. If 35 // |pass| is NULL, it will be encoded as the empty byte string rather than two 36 // zero bytes, the PKCS #12 encoding of the empty string. 37 // 38 // If |salt| is NULL, a random salt of |salt_len| bytes is generated. If 39 // |salt_len| is zero, a default salt length is used instead. 40 // 41 // The resulting structure is stored in an |X509_SIG| which must be freed by the 42 // caller. 43 OPENSSL_EXPORT X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, 44 const char *pass, int pass_len, 45 const uint8_t *salt, size_t salt_len, 46 int iterations, 47 PKCS8_PRIV_KEY_INFO *p8inf); 48 49 // PKCS8_marshal_encrypted_private_key behaves like |PKCS8_encrypt| but encrypts 50 // an |EVP_PKEY| and writes the serialized EncryptedPrivateKeyInfo to |out|. It 51 // returns one on success and zero on error. 52 OPENSSL_EXPORT int PKCS8_marshal_encrypted_private_key( 53 CBB *out, int pbe_nid, const EVP_CIPHER *cipher, const char *pass, 54 size_t pass_len, const uint8_t *salt, size_t salt_len, int iterations, 55 const EVP_PKEY *pkey); 56 57 // PKCS8_decrypt decrypts and decodes a PKCS8_PRIV_KEY_INFO with PBES1 or PBES2 58 // as defined in PKCS #5. Only pbeWithSHAAnd128BitRC4, 59 // pbeWithSHAAnd3-KeyTripleDES-CBC and pbeWithSHA1And40BitRC2, and PBES2, 60 // defined in PKCS #12, are supported. 61 // 62 // |pass| is used as the password. If a PBES1 scheme from PKCS #12 is used, this 63 // will be converted to a raw byte string as specified in B.1 of PKCS #12. If 64 // |pass| is NULL, it will be encoded as the empty byte string rather than two 65 // zero bytes, the PKCS #12 encoding of the empty string. 66 // 67 // The resulting structure must be freed by the caller. 68 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, 69 const char *pass, 70 int pass_len); 71 72 // PKCS8_parse_encrypted_private_key behaves like |PKCS8_decrypt| but it parses 73 // the EncryptedPrivateKeyInfo structure from |cbs| and advances |cbs|. It 74 // returns a newly-allocated |EVP_PKEY| on success and zero on error. 75 OPENSSL_EXPORT EVP_PKEY *PKCS8_parse_encrypted_private_key(CBS *cbs, 76 const char *pass, 77 size_t pass_len); 78 79 // PKCS12_get_key_and_certs parses a PKCS#12 structure from |in|, authenticates 80 // and decrypts it using |password|, sets |*out_key| to the included private 81 // key and appends the included certificates to |out_certs|. It returns one on 82 // success and zero on error. The caller takes ownership of the outputs. 83 // Any friendlyName attributes (RFC 2985) in the PKCS#12 structure will be 84 // returned on the |X509| objects as aliases. See also |X509_alias_get0|. 85 OPENSSL_EXPORT int PKCS12_get_key_and_certs(EVP_PKEY **out_key, 86 STACK_OF(X509) *out_certs, 87 CBS *in, const char *password); 88 89 90 // Deprecated functions. 91 92 // PKCS12_PBE_add does nothing. It exists for compatibility with OpenSSL. 93 OPENSSL_EXPORT void PKCS12_PBE_add(void); 94 95 // d2i_PKCS12 is a dummy function that copies |*ber_bytes| into a 96 // |PKCS12| structure. The |out_p12| argument should be NULL(✝). On exit, 97 // |*ber_bytes| will be advanced by |ber_len|. It returns a fresh |PKCS12| 98 // structure or NULL on error. 99 // 100 // Note: unlike other d2i functions, |d2i_PKCS12| will always consume |ber_len| 101 // bytes. 102 // 103 // (✝) If |out_p12| is not NULL and the function is successful, |*out_p12| will 104 // be freed if not NULL itself and the result will be written to |*out_p12|. 105 // New code should not depend on this. 106 OPENSSL_EXPORT PKCS12 *d2i_PKCS12(PKCS12 **out_p12, const uint8_t **ber_bytes, 107 size_t ber_len); 108 109 // d2i_PKCS12_bio acts like |d2i_PKCS12| but reads from a |BIO|. 110 OPENSSL_EXPORT PKCS12* d2i_PKCS12_bio(BIO *bio, PKCS12 **out_p12); 111 112 // d2i_PKCS12_fp acts like |d2i_PKCS12| but reads from a |FILE|. 113 OPENSSL_EXPORT PKCS12* d2i_PKCS12_fp(FILE *fp, PKCS12 **out_p12); 114 115 // i2d_PKCS12 is a dummy function which copies the contents of |p12|. If |out| 116 // is not NULL then the result is written to |*out| and |*out| is advanced just 117 // past the output. It returns the number of bytes in the result, whether 118 // written or not, or a negative value on error. 119 OPENSSL_EXPORT int i2d_PKCS12(const PKCS12 *p12, uint8_t **out); 120 121 // i2d_PKCS12_bio writes the contents of |p12| to |bio|. It returns one on 122 // success and zero on error. 123 OPENSSL_EXPORT int i2d_PKCS12_bio(BIO *bio, const PKCS12 *p12); 124 125 // i2d_PKCS12_fp writes the contents of |p12| to |fp|. It returns one on 126 // success and zero on error. 127 OPENSSL_EXPORT int i2d_PKCS12_fp(FILE *fp, const PKCS12 *p12); 128 129 // PKCS12_parse calls |PKCS12_get_key_and_certs| on the ASN.1 data stored in 130 // |p12|. The |out_pkey| and |out_cert| arguments must not be NULL and, on 131 // successful exit, the private key and matching certificate will be stored in 132 // them. The |out_ca_certs| argument may be NULL but, if not, then any extra 133 // certificates will be appended to |*out_ca_certs|. If |*out_ca_certs| is NULL 134 // then it will be set to a freshly allocated stack containing the extra certs. 135 // 136 // Note if |p12| does not contain a private key, both |*out_pkey| and 137 // |*out_cert| will be set to NULL and all certificates will be returned via 138 // |*out_ca_certs|. Also note this function differs from OpenSSL in that extra 139 // certificates are returned in the order they appear in the file. OpenSSL 1.1.1 140 // returns them in reverse order, but this will be fixed in OpenSSL 3.0. 141 // 142 // It returns one on success and zero on error. 143 // 144 // Use |PKCS12_get_key_and_certs| instead. 145 OPENSSL_EXPORT int PKCS12_parse(const PKCS12 *p12, const char *password, 146 EVP_PKEY **out_pkey, X509 **out_cert, 147 STACK_OF(X509) **out_ca_certs); 148 149 // PKCS12_verify_mac returns one if |password| is a valid password for |p12| 150 // and zero otherwise. Since |PKCS12_parse| doesn't take a length parameter, 151 // it's not actually possible to use a non-NUL-terminated password to actually 152 // get anything from a |PKCS12|. Thus |password| and |password_len| may be 153 // |NULL| and zero, respectively, or else |password_len| may be -1, or else 154 // |password[password_len]| must be zero and no other NUL bytes may appear in 155 // |password|. If the |password_len| checks fail, zero is returned 156 // immediately. 157 OPENSSL_EXPORT int PKCS12_verify_mac(const PKCS12 *p12, const char *password, 158 int password_len); 159 160 // PKCS12_DEFAULT_ITER is the default number of KDF iterations used when 161 // creating a |PKCS12| object. 162 #define PKCS12_DEFAULT_ITER 2048 163 164 // PKCS12_create returns a newly-allocated |PKCS12| object containing |pkey|, 165 // |cert|, and |chain|, encrypted with the specified password. |name|, if not 166 // NULL, specifies a user-friendly name to encode with the key and 167 // certificate. The key and certificates are encrypted with |key_nid| and 168 // |cert_nid|, respectively, using |iterations| iterations in the 169 // KDF. |mac_iterations| is the number of iterations when deriving the MAC 170 // key. |key_type| must be zero. |pkey| and |cert| may be NULL to omit them. 171 // 172 // Each of |key_nid|, |cert_nid|, |iterations|, and |mac_iterations| may be zero 173 // to use defaults, which are |NID_aes_256_cbc|, |NID_aes_256_cbc|, 174 // |PKCS12_DEFAULT_ITER|, and |PKCS12_DEFAULT_ITER|, respectively. 175 // 176 // |key_nid| and |cert_nid| are then interpreted as follows: 177 // 178 // * If the NID is a cipher that is supported with PBES2, e.g. 179 // |NID_aes_256_cbc|, this function will use it with PBES2 and a default KDF 180 // (currently PBKDF2 with HMAC-SHA256). There is no way to specify the KDF in 181 // this function. 182 // 183 // * If the NID is a PBES1 suite, e.g. |NID_pbe_WithSHA1And3_Key_TripleDES_CBC|, 184 // this function will use the specified suite. 185 // 186 // * If the NID is -1, this function will disable encryption for the key or 187 // certificate. This option is not recommended and is only implemented for 188 // compatibility with external packages. Note the output still requires a 189 // password for the MAC. Unencrypted keys in PKCS#12 are also not widely 190 // supported and may not open in other implementations. 191 // 192 // WARNING: This differs from other functions in this module, which use a pair 193 // of NID and |EVP_CIPHER| parameters to pick between PBES1 and PBES2 schemes. 194 // 195 // If |cert| or |chain| have associated aliases (see |X509_alias_set1|), they 196 // will be included in the output as friendlyName attributes (RFC 2985). It is 197 // an error to specify both an alias on |cert| and a non-NULL |name| 198 // parameter. 199 OPENSSL_EXPORT PKCS12 *PKCS12_create(const char *password, const char *name, 200 const EVP_PKEY *pkey, X509 *cert, 201 const STACK_OF(X509) *chain, int key_nid, 202 int cert_nid, int iterations, 203 int mac_iterations, int key_type); 204 205 // PKCS12_free frees |p12| and its contents. 206 OPENSSL_EXPORT void PKCS12_free(PKCS12 *p12); 207 208 209 #if defined(__cplusplus) 210 } // extern C 211 212 extern "C++" { 213 214 BSSL_NAMESPACE_BEGIN 215 216 BORINGSSL_MAKE_DELETER(PKCS12, PKCS12_free) 217 BORINGSSL_MAKE_DELETER(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free) 218 219 BSSL_NAMESPACE_END 220 221 } // extern C++ 222 223 #endif 224 225 #define PKCS8_R_BAD_PKCS12_DATA 100 226 #define PKCS8_R_BAD_PKCS12_VERSION 101 227 #define PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 102 228 #define PKCS8_R_CRYPT_ERROR 103 229 #define PKCS8_R_DECODE_ERROR 104 230 #define PKCS8_R_ENCODE_ERROR 105 231 #define PKCS8_R_ENCRYPT_ERROR 106 232 #define PKCS8_R_ERROR_SETTING_CIPHER_PARAMS 107 233 #define PKCS8_R_INCORRECT_PASSWORD 108 234 #define PKCS8_R_KEYGEN_FAILURE 109 235 #define PKCS8_R_KEY_GEN_ERROR 110 236 #define PKCS8_R_METHOD_NOT_SUPPORTED 111 237 #define PKCS8_R_MISSING_MAC 112 238 #define PKCS8_R_MULTIPLE_PRIVATE_KEYS_IN_PKCS12 113 239 #define PKCS8_R_PKCS12_PUBLIC_KEY_INTEGRITY_NOT_SUPPORTED 114 240 #define PKCS8_R_PKCS12_TOO_DEEPLY_NESTED 115 241 #define PKCS8_R_PRIVATE_KEY_DECODE_ERROR 116 242 #define PKCS8_R_PRIVATE_KEY_ENCODE_ERROR 117 243 #define PKCS8_R_TOO_LONG 118 244 #define PKCS8_R_UNKNOWN_ALGORITHM 119 245 #define PKCS8_R_UNKNOWN_CIPHER 120 246 #define PKCS8_R_UNKNOWN_CIPHER_ALGORITHM 121 247 #define PKCS8_R_UNKNOWN_DIGEST 122 248 #define PKCS8_R_UNKNOWN_HASH 123 249 #define PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM 124 250 #define PKCS8_R_UNSUPPORTED_KEYLENGTH 125 251 #define PKCS8_R_UNSUPPORTED_SALT_TYPE 126 252 #define PKCS8_R_UNSUPPORTED_CIPHER 127 253 #define PKCS8_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION 128 254 #define PKCS8_R_BAD_ITERATION_COUNT 129 255 #define PKCS8_R_UNSUPPORTED_PRF 130 256 #define PKCS8_R_INVALID_CHARACTERS 131 257 #define PKCS8_R_UNSUPPORTED_OPTIONS 132 258 #define PKCS8_R_AMBIGUOUS_FRIENDLY_NAME 133 259 260 #endif // OPENSSL_HEADER_PKCS8_H