pkcs12.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #ifndef HEADER_PKCS12_H
  10. # define HEADER_PKCS12_H
  11. # include <openssl/bio.h>
  12. # include <openssl/x509.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. # define PKCS12_KEY_ID 1
  17. # define PKCS12_IV_ID 2
  18. # define PKCS12_MAC_ID 3
  19. /* Default iteration count */
  20. # ifndef PKCS12_DEFAULT_ITER
  21. # define PKCS12_DEFAULT_ITER PKCS5_DEFAULT_ITER
  22. # endif
  23. # define PKCS12_MAC_KEY_LENGTH 20
  24. # define PKCS12_SALT_LEN 8
  25. /* It's not clear if these are actually needed... */
  26. # define PKCS12_key_gen PKCS12_key_gen_utf8
  27. # define PKCS12_add_friendlyname PKCS12_add_friendlyname_utf8
  28. /* MS key usage constants */
  29. # define KEY_EX 0x10
  30. # define KEY_SIG 0x80
  31. typedef struct PKCS12_MAC_DATA_st PKCS12_MAC_DATA;
  32. typedef struct PKCS12_st PKCS12;
  33. typedef struct PKCS12_SAFEBAG_st PKCS12_SAFEBAG;
  34. DEFINE_STACK_OF(PKCS12_SAFEBAG)
  35. typedef struct pkcs12_bag_st PKCS12_BAGS;
  36. # define PKCS12_ERROR 0
  37. # define PKCS12_OK 1
  38. /* Compatibility macros */
  39. #if OPENSSL_API_COMPAT < 0x10100000L
  40. # define M_PKCS12_bag_type PKCS12_bag_type
  41. # define M_PKCS12_cert_bag_type PKCS12_cert_bag_type
  42. # define M_PKCS12_crl_bag_type PKCS12_cert_bag_type
  43. # define PKCS12_certbag2x509 PKCS12_SAFEBAG_get1_cert
  44. # define PKCS12_certbag2scrl PKCS12_SAFEBAG_get1_crl
  45. # define PKCS12_bag_type PKCS12_SAFEBAG_get_nid
  46. # define PKCS12_cert_bag_type PKCS12_SAFEBAG_get_bag_nid
  47. # define PKCS12_x5092certbag PKCS12_SAFEBAG_create_cert
  48. # define PKCS12_x509crl2certbag PKCS12_SAFEBAG_create_crl
  49. # define PKCS12_MAKE_KEYBAG PKCS12_SAFEBAG_create0_p8inf
  50. # define PKCS12_MAKE_SHKEYBAG PKCS12_SAFEBAG_create_pkcs8_encrypt
  51. #endif
  52. DEPRECATEDIN_1_1_0(ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid))
  53. ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid);
  54. int PKCS12_mac_present(const PKCS12 *p12);
  55. void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac,
  56. const X509_ALGOR **pmacalg,
  57. const ASN1_OCTET_STRING **psalt,
  58. const ASN1_INTEGER **piter,
  59. const PKCS12 *p12);
  60. const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag,
  61. int attr_nid);
  62. const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag);
  63. int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag);
  64. int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag);
  65. X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag);
  66. X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag);
  67. const STACK_OF(PKCS12_SAFEBAG) *
  68. PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag);
  69. const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag);
  70. const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag);
  71. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509);
  72. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl);
  73. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8);
  74. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8);
  75. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid,
  76. const char *pass,
  77. int passlen,
  78. unsigned char *salt,
  79. int saltlen, int iter,
  80. PKCS8_PRIV_KEY_INFO *p8inf);
  81. PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it,
  82. int nid1, int nid2);
  83. PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(const X509_SIG *p8, const char *pass,
  84. int passlen);
  85. PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag,
  86. const char *pass, int passlen);
  87. X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher,
  88. const char *pass, int passlen, unsigned char *salt,
  89. int saltlen, int iter, PKCS8_PRIV_KEY_INFO *p8);
  90. X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen,
  91. PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe);
  92. PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk);
  93. STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7);
  94. PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen,
  95. unsigned char *salt, int saltlen, int iter,
  96. STACK_OF(PKCS12_SAFEBAG) *bags);
  97. STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass,
  98. int passlen);
  99. int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes);
  100. STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12);
  101. int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name,
  102. int namelen);
  103. int PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name,
  104. int namelen);
  105. int PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag, const char *name,
  106. int namelen);
  107. int PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name,
  108. int namelen);
  109. int PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag,
  110. const unsigned char *name, int namelen);
  111. int PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage);
  112. ASN1_TYPE *PKCS12_get_attr_gen(const STACK_OF(X509_ATTRIBUTE) *attrs,
  113. int attr_nid);
  114. char *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag);
  115. const STACK_OF(X509_ATTRIBUTE) *
  116. PKCS12_SAFEBAG_get0_attrs(const PKCS12_SAFEBAG *bag);
  117. unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
  118. const char *pass, int passlen,
  119. const unsigned char *in, int inlen,
  120. unsigned char **data, int *datalen,
  121. int en_de);
  122. void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
  123. const char *pass, int passlen,
  124. const ASN1_OCTET_STRING *oct, int zbuf);
  125. ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
  126. const ASN1_ITEM *it,
  127. const char *pass, int passlen,
  128. void *obj, int zbuf);
  129. PKCS12 *PKCS12_init(int mode);
  130. int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
  131. int saltlen, int id, int iter, int n,
  132. unsigned char *out, const EVP_MD *md_type);
  133. int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
  134. int saltlen, int id, int iter, int n,
  135. unsigned char *out, const EVP_MD *md_type);
  136. int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt,
  137. int saltlen, int id, int iter, int n,
  138. unsigned char *out, const EVP_MD *md_type);
  139. int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
  140. ASN1_TYPE *param, const EVP_CIPHER *cipher,
  141. const EVP_MD *md_type, int en_de);
  142. int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
  143. unsigned char *mac, unsigned int *maclen);
  144. int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen);
  145. int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
  146. unsigned char *salt, int saltlen, int iter,
  147. const EVP_MD *md_type);
  148. int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt,
  149. int saltlen, const EVP_MD *md_type);
  150. unsigned char *OPENSSL_asc2uni(const char *asc, int asclen,
  151. unsigned char **uni, int *unilen);
  152. char *OPENSSL_uni2asc(const unsigned char *uni, int unilen);
  153. unsigned char *OPENSSL_utf82uni(const char *asc, int asclen,
  154. unsigned char **uni, int *unilen);
  155. char *OPENSSL_uni2utf8(const unsigned char *uni, int unilen);
  156. DECLARE_ASN1_FUNCTIONS(PKCS12)
  157. DECLARE_ASN1_FUNCTIONS(PKCS12_MAC_DATA)
  158. DECLARE_ASN1_FUNCTIONS(PKCS12_SAFEBAG)
  159. DECLARE_ASN1_FUNCTIONS(PKCS12_BAGS)
  160. DECLARE_ASN1_ITEM(PKCS12_SAFEBAGS)
  161. DECLARE_ASN1_ITEM(PKCS12_AUTHSAFES)
  162. void PKCS12_PBE_add(void);
  163. int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
  164. STACK_OF(X509) **ca);
  165. PKCS12 *PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey,
  166. X509 *cert, STACK_OF(X509) *ca, int nid_key, int nid_cert,
  167. int iter, int mac_iter, int keytype);
  168. PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert);
  169. PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
  170. EVP_PKEY *key, int key_usage, int iter,
  171. int key_nid, const char *pass);
  172. int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
  173. int safe_nid, int iter, const char *pass);
  174. PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int p7_nid);
  175. int i2d_PKCS12_bio(BIO *bp, PKCS12 *p12);
  176. # ifndef OPENSSL_NO_STDIO
  177. int i2d_PKCS12_fp(FILE *fp, PKCS12 *p12);
  178. # endif
  179. PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12);
  180. # ifndef OPENSSL_NO_STDIO
  181. PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12);
  182. # endif
  183. int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass);
  184. /* BEGIN ERROR CODES */
  185. /*
  186. * The following lines are auto generated by the script mkerr.pl. Any changes
  187. * made after this point may be overwritten when the script is next run.
  188. */
  189. int ERR_load_PKCS12_strings(void);
  190. /* Error codes for the PKCS12 functions. */
  191. /* Function codes. */
  192. # define PKCS12_F_PKCS12_CREATE 105
  193. # define PKCS12_F_PKCS12_GEN_MAC 107
  194. # define PKCS12_F_PKCS12_INIT 109
  195. # define PKCS12_F_PKCS12_ITEM_DECRYPT_D2I 106
  196. # define PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT 108
  197. # define PKCS12_F_PKCS12_ITEM_PACK_SAFEBAG 117
  198. # define PKCS12_F_PKCS12_KEY_GEN_ASC 110
  199. # define PKCS12_F_PKCS12_KEY_GEN_UNI 111
  200. # define PKCS12_F_PKCS12_KEY_GEN_UTF8 116
  201. # define PKCS12_F_PKCS12_NEWPASS 128
  202. # define PKCS12_F_PKCS12_PACK_P7DATA 114
  203. # define PKCS12_F_PKCS12_PACK_P7ENCDATA 115
  204. # define PKCS12_F_PKCS12_PARSE 118
  205. # define PKCS12_F_PKCS12_PBE_CRYPT 119
  206. # define PKCS12_F_PKCS12_PBE_KEYIVGEN 120
  207. # define PKCS12_F_PKCS12_SAFEBAG_CREATE0_P8INF 112
  208. # define PKCS12_F_PKCS12_SAFEBAG_CREATE0_PKCS8 113
  209. # define PKCS12_F_PKCS12_SAFEBAG_CREATE_PKCS8_ENCRYPT 133
  210. # define PKCS12_F_PKCS12_SETUP_MAC 122
  211. # define PKCS12_F_PKCS12_SET_MAC 123
  212. # define PKCS12_F_PKCS12_UNPACK_AUTHSAFES 130
  213. # define PKCS12_F_PKCS12_UNPACK_P7DATA 131
  214. # define PKCS12_F_PKCS12_VERIFY_MAC 126
  215. # define PKCS12_F_PKCS8_ENCRYPT 125
  216. # define PKCS12_F_PKCS8_SET0_PBE 132
  217. /* Reason codes. */
  218. # define PKCS12_R_CANT_PACK_STRUCTURE 100
  219. # define PKCS12_R_CONTENT_TYPE_NOT_DATA 121
  220. # define PKCS12_R_DECODE_ERROR 101
  221. # define PKCS12_R_ENCODE_ERROR 102
  222. # define PKCS12_R_ENCRYPT_ERROR 103
  223. # define PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE 120
  224. # define PKCS12_R_INVALID_NULL_ARGUMENT 104
  225. # define PKCS12_R_INVALID_NULL_PKCS12_POINTER 105
  226. # define PKCS12_R_IV_GEN_ERROR 106
  227. # define PKCS12_R_KEY_GEN_ERROR 107
  228. # define PKCS12_R_MAC_ABSENT 108
  229. # define PKCS12_R_MAC_GENERATION_ERROR 109
  230. # define PKCS12_R_MAC_SETUP_ERROR 110
  231. # define PKCS12_R_MAC_STRING_SET_ERROR 111
  232. # define PKCS12_R_MAC_VERIFY_FAILURE 113
  233. # define PKCS12_R_PARSE_ERROR 114
  234. # define PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR 115
  235. # define PKCS12_R_PKCS12_CIPHERFINAL_ERROR 116
  236. # define PKCS12_R_PKCS12_PBE_CRYPT_ERROR 117
  237. # define PKCS12_R_UNKNOWN_DIGEST_ALGORITHM 118
  238. # define PKCS12_R_UNSUPPORTED_PKCS12_MODE 119
  239. # ifdef __cplusplus
  240. }
  241. # endif
  242. #endif