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