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_CRYPTO_PKCS8_INTERNAL_H
16 #define OPENSSL_HEADER_CRYPTO_PKCS8_INTERNAL_H
17 
18 #include <openssl/base.h>
19 #include <openssl/stack.h>
20 
21 #if defined(__cplusplus)
22 extern "C" {
23 #endif
24 
25 
26 struct pkcs8_priv_key_info_st {
27   ASN1_INTEGER *version;
28   X509_ALGOR *pkeyalg;
29   ASN1_OCTET_STRING *pkey;
30   STACK_OF(X509_ATTRIBUTE) *attributes;
31 };
32 
33 // pkcs8_pbe_decrypt decrypts |in| using the PBE scheme described by
34 // |algorithm|, which should be a serialized AlgorithmIdentifier structure. On
35 // success, it sets |*out| to a newly-allocated buffer containing the decrypted
36 // result and returns one. Otherwise, it returns zero.
37 int pkcs8_pbe_decrypt(uint8_t **out, size_t *out_len, CBS *algorithm,
38                       const char *pass, size_t pass_len, const uint8_t *in,
39                       size_t in_len);
40 
41 #define PKCS12_KEY_ID 1
42 #define PKCS12_IV_ID 2
43 #define PKCS12_MAC_ID 3
44 
45 // pkcs12_key_gen runs the PKCS#12 key derivation function as specified in
46 // RFC 7292, appendix B. On success, it writes the resulting |out_len| bytes of
47 // key material to |out| and returns one. Otherwise, it returns zero. |id|
48 // should be one of the |PKCS12_*_ID| values.
49 int pkcs12_key_gen(const char *pass, size_t pass_len, const uint8_t *salt,
50                    size_t salt_len, uint8_t id, uint32_t iterations,
51                    size_t out_len, uint8_t *out, const EVP_MD *md);
52 
53 // pkcs12_pbe_encrypt_init configures |ctx| for encrypting with a PBES1 scheme
54 // defined in PKCS#12, or a PBES2 scheme defined in PKCS#5. The algorithm is
55 // determined as in |PKCS8_encrypt|. It writes the corresponding
56 // AlgorithmIdentifier to |out|.
57 int pkcs12_pbe_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, int alg_nid,
58                             const EVP_CIPHER *alg_cipher, uint32_t iterations,
59                             const char *pass, size_t pass_len,
60                             const uint8_t *salt, size_t salt_len);
61 
62 struct pbe_suite {
63   int pbe_nid;
64   uint8_t oid[10];
65   uint8_t oid_len;
66   const EVP_CIPHER *(*cipher_func)(void);
67   const EVP_MD *(*md_func)(void);
68   // decrypt_init initialize |ctx| for decrypting. The password is specified by
69   // |pass| and |pass_len|. |param| contains the serialized parameters field of
70   // the AlgorithmIdentifier.
71   //
72   // It returns one on success and zero on error.
73   int (*decrypt_init)(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx,
74                       const char *pass, size_t pass_len, CBS *param);
75 };
76 
77 #define PKCS5_SALT_LEN 16
78 
79 // pkcs5_pbe2_nid_to_cipher returns the |EVP_CIPHER| for |nid| if |nid| is
80 // supported with PKCS#5 PBES2, and nullptr otherwise.
81 const EVP_CIPHER *pkcs5_pbe2_nid_to_cipher(int nid);
82 
83 int PKCS5_pbe2_decrypt_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx,
84                             const char *pass, size_t pass_len, CBS *param);
85 
86 // PKCS5_pbe2_encrypt_init configures |ctx| for encrypting with PKCS #5 PBES2,
87 // as defined in RFC 2998, with the specified parameters. It writes the
88 // corresponding AlgorithmIdentifier to |out|.
89 int PKCS5_pbe2_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx,
90                             const EVP_CIPHER *cipher, uint32_t iterations,
91                             const char *pass, size_t pass_len,
92                             const uint8_t *salt, size_t salt_len);
93 
94 // pkcs12_iterations_acceptable returns one if |iterations| is a reasonable
95 // number of PBKDF2 iterations and zero otherwise.
96 int pkcs12_iterations_acceptable(uint64_t iterations);
97 
98 
99 #if defined(__cplusplus)
100 }  // extern C
101 #endif
102 
103 #endif  // OPENSSL_HEADER_CRYPTO_PKCS8_INTERNAL_H
104