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