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