A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.2 KiB

  1. /*
  2. * Copyright 1999-2020 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. PKCS12err(0, ERR_R_MALLOC_FAILURE);
  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. PKCS12err(0, ERR_R_MALLOC_FAILURE);
  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. PKCS12err(0, 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. PKCS12err(0, ERR_R_MALLOC_FAILURE);
  125. goto err;
  126. }
  127. ASN1_TYPE_set(bag->value.other, vtype, strtmp);
  128. }
  129. break;
  130. default:
  131. PKCS12err(0, PKCS12_R_INVALID_TYPE);
  132. goto err;
  133. }
  134. if ((safebag = PKCS12_SAFEBAG_new()) == NULL) {
  135. PKCS12err(0, ERR_R_MALLOC_FAILURE);
  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. PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_P8INF, ERR_R_MALLOC_FAILURE);
  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. PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_PKCS8, ERR_R_MALLOC_FAILURE);
  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(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. {
  177. PKCS12_SAFEBAG *bag;
  178. const EVP_CIPHER *pbe_ciph;
  179. X509_SIG *p8;
  180. pbe_ciph = EVP_get_cipherbynid(pbe_nid);
  181. if (pbe_ciph)
  182. pbe_nid = -1;
  183. p8 = PKCS8_encrypt(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter,
  184. p8inf);
  185. if (p8 == NULL)
  186. return NULL;
  187. bag = PKCS12_SAFEBAG_create0_pkcs8(p8);
  188. if (bag == NULL)
  189. X509_SIG_free(p8);
  190. return bag;
  191. }