p12_sbag.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/pkcs12.h>
  12. #include "p12_local.h"
  13. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  14. ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid)
  15. {
  16. return PKCS12_get_attr_gen(bag->attrib, attr_nid);
  17. }
  18. #endif
  19. const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag,
  20. int attr_nid)
  21. {
  22. return PKCS12_get_attr_gen(bag->attrib, attr_nid);
  23. }
  24. ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid)
  25. {
  26. return PKCS12_get_attr_gen(PKCS8_pkey_get0_attrs(p8), attr_nid);
  27. }
  28. const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag)
  29. {
  30. if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag)
  31. return NULL;
  32. return bag->value.keybag;
  33. }
  34. const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag)
  35. {
  36. if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag)
  37. return NULL;
  38. return bag->value.shkeybag;
  39. }
  40. const STACK_OF(PKCS12_SAFEBAG) *
  41. PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag)
  42. {
  43. if (OBJ_obj2nid(bag->type) != NID_safeContentsBag)
  44. return NULL;
  45. return bag->value.safes;
  46. }
  47. const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag)
  48. {
  49. return bag->type;
  50. }
  51. int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag)
  52. {
  53. return OBJ_obj2nid(bag->type);
  54. }
  55. int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag)
  56. {
  57. int btype = PKCS12_SAFEBAG_get_nid(bag);
  58. if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag)
  59. return -1;
  60. return OBJ_obj2nid(bag->value.bag->type);
  61. }
  62. const ASN1_OBJECT *PKCS12_SAFEBAG_get0_bag_type(const PKCS12_SAFEBAG *bag)
  63. {
  64. return bag->value.bag->type;
  65. }
  66. const ASN1_TYPE *PKCS12_SAFEBAG_get0_bag_obj(const PKCS12_SAFEBAG *bag)
  67. {
  68. return bag->value.bag->value.other;
  69. }
  70. X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag)
  71. {
  72. if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag)
  73. return NULL;
  74. if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
  75. return NULL;
  76. return ASN1_item_unpack(bag->value.bag->value.octet,
  77. ASN1_ITEM_rptr(X509));
  78. }
  79. X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag)
  80. {
  81. if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag)
  82. return NULL;
  83. if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl)
  84. return NULL;
  85. return ASN1_item_unpack(bag->value.bag->value.octet,
  86. ASN1_ITEM_rptr(X509_CRL));
  87. }
  88. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509)
  89. {
  90. return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
  91. NID_x509Certificate, NID_certBag);
  92. }
  93. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl)
  94. {
  95. return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
  96. NID_x509Crl, NID_crlBag);
  97. }
  98. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_secret(int type, int vtype, const unsigned char *value, int len)
  99. {
  100. PKCS12_BAGS *bag;
  101. PKCS12_SAFEBAG *safebag;
  102. if ((bag = PKCS12_BAGS_new()) == NULL) {
  103. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  104. return NULL;
  105. }
  106. bag->type = OBJ_nid2obj(type);
  107. switch (vtype) {
  108. case V_ASN1_OCTET_STRING:
  109. {
  110. ASN1_OCTET_STRING *strtmp = ASN1_OCTET_STRING_new();
  111. if (strtmp == NULL) {
  112. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  113. goto err;
  114. }
  115. /* Pack data into an octet string */
  116. if (!ASN1_OCTET_STRING_set(strtmp, value, len)) {
  117. ASN1_OCTET_STRING_free(strtmp);
  118. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
  119. goto err;
  120. }
  121. bag->value.other = ASN1_TYPE_new();
  122. if (bag->value.other == NULL) {
  123. ASN1_OCTET_STRING_free(strtmp);
  124. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  125. goto err;
  126. }
  127. ASN1_TYPE_set(bag->value.other, vtype, strtmp);
  128. }
  129. break;
  130. default:
  131. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_TYPE);
  132. goto err;
  133. }
  134. if ((safebag = PKCS12_SAFEBAG_new()) == NULL) {
  135. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  136. goto err;
  137. }
  138. safebag->value.bag = bag;
  139. safebag->type = OBJ_nid2obj(NID_secretBag);
  140. return safebag;
  141. err:
  142. PKCS12_BAGS_free(bag);
  143. return NULL;
  144. }
  145. /* Turn PKCS8 object into a keybag */
  146. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8)
  147. {
  148. PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
  149. if (bag == NULL) {
  150. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  151. return NULL;
  152. }
  153. bag->type = OBJ_nid2obj(NID_keyBag);
  154. bag->value.keybag = p8;
  155. return bag;
  156. }
  157. /* Turn PKCS8 object into a shrouded keybag */
  158. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8)
  159. {
  160. PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
  161. /* Set up the safe bag */
  162. if (bag == NULL) {
  163. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  164. return NULL;
  165. }
  166. bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag);
  167. bag->value.shkeybag = p8;
  168. return bag;
  169. }
  170. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(int pbe_nid,
  171. const char *pass,
  172. int passlen,
  173. unsigned char *salt,
  174. int saltlen, int iter,
  175. PKCS8_PRIV_KEY_INFO *p8inf,
  176. OSSL_LIB_CTX *ctx,
  177. const char *propq)
  178. {
  179. PKCS12_SAFEBAG *bag = NULL;
  180. const EVP_CIPHER *pbe_ciph = NULL;
  181. EVP_CIPHER *pbe_ciph_fetch = NULL;
  182. X509_SIG *p8;
  183. ERR_set_mark();
  184. pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq);
  185. if (pbe_ciph == NULL)
  186. pbe_ciph = EVP_get_cipherbynid(pbe_nid);
  187. ERR_pop_to_mark();
  188. if (pbe_ciph != NULL)
  189. pbe_nid = -1;
  190. p8 = PKCS8_encrypt_ex(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter,
  191. p8inf, ctx, propq);
  192. if (p8 == NULL)
  193. goto err;
  194. bag = PKCS12_SAFEBAG_create0_pkcs8(p8);
  195. if (bag == NULL)
  196. X509_SIG_free(p8);
  197. err:
  198. EVP_CIPHER_free(pbe_ciph_fetch);
  199. return bag;
  200. }
  201. PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid,
  202. const char *pass,
  203. int passlen,
  204. unsigned char *salt,
  205. int saltlen, int iter,
  206. PKCS8_PRIV_KEY_INFO *p8inf)
  207. {
  208. return PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(pbe_nid, pass, passlen,
  209. salt, saltlen, iter, p8inf,
  210. NULL, NULL);
  211. }