15#ifndef OPENSSL_PKCS12_H
16# define OPENSSL_PKCS12_H
19# include <openssl/macros.h>
20# ifndef OPENSSL_NO_DEPRECATED_3_0
21# define HEADER_PKCS12_H
24# include <openssl/bio.h>
25# include <openssl/core.h>
26# include <openssl/x509.h>
27# include <openssl/pkcs12err.h>
33# define PKCS12_KEY_ID 1
34# define PKCS12_IV_ID 2
35# define PKCS12_MAC_ID 3
38# ifndef PKCS12_DEFAULT_ITER
39# define PKCS12_DEFAULT_ITER PKCS5_DEFAULT_ITER
42# define PKCS12_MAC_KEY_LENGTH 20
44# define PKCS12_SALT_LEN 8
47# define PKCS12_key_gen PKCS12_key_gen_utf8
48# define PKCS12_add_friendlyname PKCS12_add_friendlyname_utf8
55typedef struct PKCS12_MAC_DATA_st PKCS12_MAC_DATA;
57typedef struct PKCS12_st PKCS12;
59typedef struct PKCS12_SAFEBAG_st PKCS12_SAFEBAG;
61SKM_DEFINE_STACK_OF_INTERNAL(PKCS12_SAFEBAG, PKCS12_SAFEBAG, PKCS12_SAFEBAG)
62#define sk_PKCS12_SAFEBAG_num(sk) OPENSSL_sk_num(ossl_check_const_PKCS12_SAFEBAG_sk_type(sk))
63#define sk_PKCS12_SAFEBAG_value(sk, idx) ((PKCS12_SAFEBAG *)OPENSSL_sk_value(ossl_check_const_PKCS12_SAFEBAG_sk_type(sk), (idx)))
64#define sk_PKCS12_SAFEBAG_new(cmp) ((STACK_OF(PKCS12_SAFEBAG) *)OPENSSL_sk_new(ossl_check_PKCS12_SAFEBAG_compfunc_type(cmp)))
65#define sk_PKCS12_SAFEBAG_new_null() ((STACK_OF(PKCS12_SAFEBAG) *)OPENSSL_sk_new_null())
66#define sk_PKCS12_SAFEBAG_new_reserve(cmp, n) ((STACK_OF(PKCS12_SAFEBAG) *)OPENSSL_sk_new_reserve(ossl_check_PKCS12_SAFEBAG_compfunc_type(cmp), (n)))
67#define sk_PKCS12_SAFEBAG_reserve(sk, n) OPENSSL_sk_reserve(ossl_check_PKCS12_SAFEBAG_sk_type(sk), (n))
68#define sk_PKCS12_SAFEBAG_free(sk) OPENSSL_sk_free(ossl_check_PKCS12_SAFEBAG_sk_type(sk))
69#define sk_PKCS12_SAFEBAG_zero(sk) OPENSSL_sk_zero(ossl_check_PKCS12_SAFEBAG_sk_type(sk))
70#define sk_PKCS12_SAFEBAG_delete(sk, i) ((PKCS12_SAFEBAG *)OPENSSL_sk_delete(ossl_check_PKCS12_SAFEBAG_sk_type(sk), (i)))
71#define sk_PKCS12_SAFEBAG_delete_ptr(sk, ptr) ((PKCS12_SAFEBAG *)OPENSSL_sk_delete_ptr(ossl_check_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_type(ptr)))
72#define sk_PKCS12_SAFEBAG_push(sk, ptr) OPENSSL_sk_push(ossl_check_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_type(ptr))
73#define sk_PKCS12_SAFEBAG_unshift(sk, ptr) OPENSSL_sk_unshift(ossl_check_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_type(ptr))
74#define sk_PKCS12_SAFEBAG_pop(sk) ((PKCS12_SAFEBAG *)OPENSSL_sk_pop(ossl_check_PKCS12_SAFEBAG_sk_type(sk)))
75#define sk_PKCS12_SAFEBAG_shift(sk) ((PKCS12_SAFEBAG *)OPENSSL_sk_shift(ossl_check_PKCS12_SAFEBAG_sk_type(sk)))
76#define sk_PKCS12_SAFEBAG_pop_free(sk, freefunc) OPENSSL_sk_pop_free(ossl_check_PKCS12_SAFEBAG_sk_type(sk),ossl_check_PKCS12_SAFEBAG_freefunc_type(freefunc))
77#define sk_PKCS12_SAFEBAG_insert(sk, ptr, idx) OPENSSL_sk_insert(ossl_check_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_type(ptr), (idx))
78#define sk_PKCS12_SAFEBAG_set(sk, idx, ptr) ((PKCS12_SAFEBAG *)OPENSSL_sk_set(ossl_check_PKCS12_SAFEBAG_sk_type(sk), (idx), ossl_check_PKCS12_SAFEBAG_type(ptr)))
79#define sk_PKCS12_SAFEBAG_find(sk, ptr) OPENSSL_sk_find(ossl_check_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_type(ptr))
80#define sk_PKCS12_SAFEBAG_find_ex(sk, ptr) OPENSSL_sk_find_ex(ossl_check_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_type(ptr))
81#define sk_PKCS12_SAFEBAG_find_all(sk, ptr, pnum) OPENSSL_sk_find_all(ossl_check_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_type(ptr), pnum)
82#define sk_PKCS12_SAFEBAG_sort(sk) OPENSSL_sk_sort(ossl_check_PKCS12_SAFEBAG_sk_type(sk))
83#define sk_PKCS12_SAFEBAG_is_sorted(sk) OPENSSL_sk_is_sorted(ossl_check_const_PKCS12_SAFEBAG_sk_type(sk))
84#define sk_PKCS12_SAFEBAG_dup(sk) ((STACK_OF(PKCS12_SAFEBAG) *)OPENSSL_sk_dup(ossl_check_const_PKCS12_SAFEBAG_sk_type(sk)))
85#define sk_PKCS12_SAFEBAG_deep_copy(sk, copyfunc, freefunc) ((STACK_OF(PKCS12_SAFEBAG) *)OPENSSL_sk_deep_copy(ossl_check_const_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_copyfunc_type(copyfunc), ossl_check_PKCS12_SAFEBAG_freefunc_type(freefunc)))
86#define sk_PKCS12_SAFEBAG_set_cmp_func(sk, cmp) ((sk_PKCS12_SAFEBAG_compfunc)OPENSSL_sk_set_cmp_func(ossl_check_PKCS12_SAFEBAG_sk_type(sk), ossl_check_PKCS12_SAFEBAG_compfunc_type(cmp)))
89typedef struct pkcs12_bag_st PKCS12_BAGS;
91# define PKCS12_ERROR 0
96#ifndef OPENSSL_NO_DEPRECATED_1_1_0
98# define M_PKCS12_bag_type PKCS12_bag_type
99# define M_PKCS12_cert_bag_type PKCS12_cert_bag_type
100# define M_PKCS12_crl_bag_type PKCS12_cert_bag_type
102# define PKCS12_certbag2x509 PKCS12_SAFEBAG_get1_cert
103# define PKCS12_certbag2scrl PKCS12_SAFEBAG_get1_crl
104# define PKCS12_bag_type PKCS12_SAFEBAG_get_nid
105# define PKCS12_cert_bag_type PKCS12_SAFEBAG_get_bag_nid
106# define PKCS12_x5092certbag PKCS12_SAFEBAG_create_cert
107# define PKCS12_x509crl2certbag PKCS12_SAFEBAG_create_crl
108# define PKCS12_MAKE_KEYBAG PKCS12_SAFEBAG_create0_p8inf
109# define PKCS12_MAKE_SHKEYBAG PKCS12_SAFEBAG_create_pkcs8_encrypt
112#ifndef OPENSSL_NO_DEPRECATED_1_1_0
113OSSL_DEPRECATEDIN_1_1_0 ASN1_TYPE *PKCS12_get_attr(
const PKCS12_SAFEBAG *bag,
117ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8,
int attr_nid);
118int PKCS12_mac_present(
const PKCS12 *p12);
119void PKCS12_get0_mac(
const ASN1_OCTET_STRING **pmac,
120 const X509_ALGOR **pmacalg,
121 const ASN1_OCTET_STRING **psalt,
122 const ASN1_INTEGER **piter,
125const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(
const PKCS12_SAFEBAG *bag,
127const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(
const PKCS12_SAFEBAG *bag);
128int PKCS12_SAFEBAG_get_nid(
const PKCS12_SAFEBAG *bag);
129int PKCS12_SAFEBAG_get_bag_nid(
const PKCS12_SAFEBAG *bag);
130const ASN1_TYPE *PKCS12_SAFEBAG_get0_bag_obj(
const PKCS12_SAFEBAG *bag);
131const ASN1_OBJECT *PKCS12_SAFEBAG_get0_bag_type(
const PKCS12_SAFEBAG *bag);
133X509 *PKCS12_SAFEBAG_get1_cert(
const PKCS12_SAFEBAG *bag);
134X509_CRL *PKCS12_SAFEBAG_get1_crl(
const PKCS12_SAFEBAG *bag);
135const STACK_OF(PKCS12_SAFEBAG) *
136PKCS12_SAFEBAG_get0_safes(
const PKCS12_SAFEBAG *bag);
137const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(
const PKCS12_SAFEBAG *bag);
138const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(
const PKCS12_SAFEBAG *bag);
140PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509);
141PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl);
142PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_secret(
int type,
int vtype,
const unsigned char *value,
int len);
143PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8);
144PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8);
145PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(
int pbe_nid,
149 int saltlen,
int iter,
150 PKCS8_PRIV_KEY_INFO *p8inf);
151PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(
int pbe_nid,
155 int saltlen,
int iter,
156 PKCS8_PRIV_KEY_INFO *p8inf,
160PKCS12_SAFEBAG *PKCS12_item_pack_safebag(
void *obj,
const ASN1_ITEM *it,
162PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(
const X509_SIG *p8,
const char *pass,
164PKCS8_PRIV_KEY_INFO *PKCS8_decrypt_ex(
const X509_SIG *p8,
const char *pass,
165 int passlen, OSSL_LIB_CTX *ctx,
167PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(
const PKCS12_SAFEBAG *bag,
168 const char *pass,
int passlen);
169PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey_ex(
const PKCS12_SAFEBAG *bag,
170 const char *pass,
int passlen,
173X509_SIG *PKCS8_encrypt(
int pbe_nid,
const EVP_CIPHER *cipher,
174 const char *pass,
int passlen,
unsigned char *salt,
175 int saltlen,
int iter, PKCS8_PRIV_KEY_INFO *p8);
176X509_SIG *PKCS8_encrypt_ex(
int pbe_nid,
const EVP_CIPHER *cipher,
177 const char *pass,
int passlen,
unsigned char *salt,
178 int saltlen,
int iter, PKCS8_PRIV_KEY_INFO *p8,
179 OSSL_LIB_CTX *ctx,
const char *propq);
180X509_SIG *PKCS8_set0_pbe(
const char *pass,
int passlen,
181 PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe);
182X509_SIG *PKCS8_set0_pbe_ex(
const char *pass,
int passlen,
183 PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe,
184 OSSL_LIB_CTX *ctx,
const char *propq);
185PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk);
186STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(
PKCS7 *p7);
187PKCS7 *PKCS12_pack_p7encdata(
int pbe_nid,
const char *pass,
int passlen,
188 unsigned char *salt,
int saltlen,
int iter,
189 STACK_OF(PKCS12_SAFEBAG) *bags);
190PKCS7 *PKCS12_pack_p7encdata_ex(
int pbe_nid,
const char *pass,
int passlen,
191 unsigned char *salt,
int saltlen,
int iter,
192 STACK_OF(PKCS12_SAFEBAG) *bags,
193 OSSL_LIB_CTX *ctx,
const char *propq);
195STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(
PKCS7 *p7,
const char *pass,
198int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(
PKCS7) *safes);
199STACK_OF(
PKCS7) *PKCS12_unpack_authsafes(
const PKCS12 *p12);
201int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag,
unsigned char *name,
203int PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag,
const char *name,
205int PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag,
const char *name,
207int PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag,
const char *name,
209int PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag,
210 const unsigned char *name,
int namelen);
211int PKCS12_add1_attr_by_NID(PKCS12_SAFEBAG *bag,
int nid,
int type,
212 const unsigned char *bytes,
int len);
213int PKCS12_add1_attr_by_txt(PKCS12_SAFEBAG *bag,
const char *attrname,
int type,
214 const unsigned char *bytes,
int len);
215int PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8,
int usage);
216ASN1_TYPE *PKCS12_get_attr_gen(
const STACK_OF(X509_ATTRIBUTE) *attrs,
218char *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag);
219const STACK_OF(X509_ATTRIBUTE) *
220PKCS12_SAFEBAG_get0_attrs(
const PKCS12_SAFEBAG *bag);
221unsigned char *PKCS12_pbe_crypt(
const X509_ALGOR *algor,
222 const char *pass,
int passlen,
223 const unsigned char *in,
int inlen,
224 unsigned char **data,
int *datalen,
226unsigned char *PKCS12_pbe_crypt_ex(
const X509_ALGOR *algor,
227 const char *pass,
int passlen,
228 const unsigned char *in,
int inlen,
229 unsigned char **data,
int *datalen,
230 int en_de, OSSL_LIB_CTX *libctx,
232void *PKCS12_item_decrypt_d2i(
const X509_ALGOR *algor,
const ASN1_ITEM *it,
233 const char *pass,
int passlen,
234 const ASN1_OCTET_STRING *oct,
int zbuf);
235void *PKCS12_item_decrypt_d2i_ex(
const X509_ALGOR *algor,
const ASN1_ITEM *it,
236 const char *pass,
int passlen,
237 const ASN1_OCTET_STRING *oct,
int zbuf,
238 OSSL_LIB_CTX *libctx,
240ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
242 const char *pass,
int passlen,
243 void *obj,
int zbuf);
244ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt_ex(X509_ALGOR *algor,
246 const char *pass,
int passlen,
250PKCS12 *PKCS12_init(
int mode);
251PKCS12 *PKCS12_init_ex(
int mode, OSSL_LIB_CTX *ctx,
const char *propq);
253int PKCS12_key_gen_asc(
const char *pass,
int passlen,
unsigned char *salt,
254 int saltlen,
int id,
int iter,
int n,
255 unsigned char *out,
const EVP_MD *md_type);
256int PKCS12_key_gen_asc_ex(
const char *pass,
int passlen,
unsigned char *salt,
257 int saltlen,
int id,
int iter,
int n,
258 unsigned char *out,
const EVP_MD *md_type,
259 OSSL_LIB_CTX *ctx,
const char *propq);
260int PKCS12_key_gen_uni(
unsigned char *pass,
int passlen,
unsigned char *salt,
261 int saltlen,
int id,
int iter,
int n,
262 unsigned char *out,
const EVP_MD *md_type);
263int PKCS12_key_gen_uni_ex(
unsigned char *pass,
int passlen,
unsigned char *salt,
264 int saltlen,
int id,
int iter,
int n,
265 unsigned char *out,
const EVP_MD *md_type,
266 OSSL_LIB_CTX *ctx,
const char *propq);
267int PKCS12_key_gen_utf8(
const char *pass,
int passlen,
unsigned char *salt,
268 int saltlen,
int id,
int iter,
int n,
269 unsigned char *out,
const EVP_MD *md_type);
270int PKCS12_key_gen_utf8_ex(
const char *pass,
int passlen,
unsigned char *salt,
271 int saltlen,
int id,
int iter,
int n,
272 unsigned char *out,
const EVP_MD *md_type,
273 OSSL_LIB_CTX *ctx,
const char *propq);
275int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx,
const char *pass,
int passlen,
276 ASN1_TYPE *param,
const EVP_CIPHER *cipher,
277 const EVP_MD *md_type,
int en_de);
278int PKCS12_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx,
const char *pass,
int passlen,
279 ASN1_TYPE *param,
const EVP_CIPHER *cipher,
280 const EVP_MD *md_type,
int en_de,
281 OSSL_LIB_CTX *libctx,
const char *propq);
282int PKCS12_gen_mac(PKCS12 *p12,
const char *pass,
int passlen,
283 unsigned char *mac,
unsigned int *maclen);
284int PKCS12_verify_mac(PKCS12 *p12,
const char *pass,
int passlen);
285int PKCS12_set_mac(PKCS12 *p12,
const char *pass,
int passlen,
286 unsigned char *salt,
int saltlen,
int iter,
287 const EVP_MD *md_type);
288int PKCS12_setup_mac(PKCS12 *p12,
int iter,
unsigned char *salt,
289 int saltlen,
const EVP_MD *md_type);
290unsigned char *OPENSSL_asc2uni(
const char *asc,
int asclen,
291 unsigned char **uni,
int *unilen);
292char *OPENSSL_uni2asc(
const unsigned char *uni,
int unilen);
293unsigned char *OPENSSL_utf82uni(
const char *asc,
int asclen,
294 unsigned char **uni,
int *unilen);
295char *OPENSSL_uni2utf8(
const unsigned char *uni,
int unilen);
297DECLARE_ASN1_FUNCTIONS(PKCS12)
298DECLARE_ASN1_FUNCTIONS(PKCS12_MAC_DATA)
299DECLARE_ASN1_FUNCTIONS(PKCS12_SAFEBAG)
300DECLARE_ASN1_FUNCTIONS(PKCS12_BAGS)
302DECLARE_ASN1_ITEM(PKCS12_SAFEBAGS)
303DECLARE_ASN1_ITEM(PKCS12_AUTHSAFES)
305void PKCS12_PBE_add(
void);
306int PKCS12_parse(PKCS12 *p12,
const char *pass, EVP_PKEY **pkey, X509 **cert,
307 STACK_OF(X509) **ca);
308PKCS12 *PKCS12_create(
const char *pass,
const char *name, EVP_PKEY *pkey,
309 X509 *cert, STACK_OF(X509) *ca,
int nid_key,
int nid_cert,
310 int iter,
int mac_iter,
int keytype);
311PKCS12 *PKCS12_create_ex(
const char *pass,
const char *name, EVP_PKEY *pkey,
312 X509 *cert, STACK_OF(X509) *ca,
int nid_key,
int nid_cert,
313 int iter,
int mac_iter,
int keytype,
314 OSSL_LIB_CTX *ctx,
const char *propq);
316PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert);
317PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
318 EVP_PKEY *key,
int key_usage,
int iter,
319 int key_nid,
const char *pass);
320PKCS12_SAFEBAG *PKCS12_add_key_ex(STACK_OF(PKCS12_SAFEBAG) **pbags,
321 EVP_PKEY *key,
int key_usage,
int iter,
322 int key_nid,
const char *pass,
323 OSSL_LIB_CTX *ctx,
const char *propq);
325PKCS12_SAFEBAG *PKCS12_add_secret(STACK_OF(PKCS12_SAFEBAG) **pbags,
326 int nid_type,
const unsigned char *value,
int len);
327int PKCS12_add_safe(STACK_OF(
PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
328 int safe_nid,
int iter,
const char *pass);
329int PKCS12_add_safe_ex(STACK_OF(
PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
330 int safe_nid,
int iter,
const char *pass,
331 OSSL_LIB_CTX *ctx,
const char *propq);
333PKCS12 *PKCS12_add_safes(STACK_OF(
PKCS7) *safes,
int p7_nid);
334PKCS12 *PKCS12_add_safes_ex(STACK_OF(
PKCS7) *safes,
int p7_nid,
335 OSSL_LIB_CTX *ctx,
const char *propq);
337int i2d_PKCS12_bio(BIO *bp,
const PKCS12 *p12);
338# ifndef OPENSSL_NO_STDIO
339int i2d_PKCS12_fp(FILE *fp,
const PKCS12 *p12);
341PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12);
342# ifndef OPENSSL_NO_STDIO
343PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12);
345int PKCS12_newpass(PKCS12 *p12,
const char *oldpass,
const char *newpass);