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