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.

257 lines
7.4 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 "crypto/x509.h" /* for X509_add_cert_new() */
  13. DEFINE_STACK_OF(PKCS12_SAFEBAG)
  14. /* Simplified PKCS#12 routines */
  15. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  16. EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
  17. static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  18. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
  19. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  20. EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
  21. /*
  22. * Parse and decrypt a PKCS#12 structure returning user key, user cert and
  23. * other (CA) certs. Note either ca should be NULL, *ca should be NULL, or it
  24. * should point to a valid STACK structure. pkey and/or cert may be NULL;
  25. * if non-NULL the variables they point to can be passed uninitialised.
  26. */
  27. int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
  28. STACK_OF(X509) **ca)
  29. {
  30. STACK_OF(X509) *ocerts = NULL;
  31. X509 *x = NULL;
  32. if (pkey != NULL)
  33. *pkey = NULL;
  34. if (cert != NULL)
  35. *cert = NULL;
  36. /* Check for NULL PKCS12 structure */
  37. if (p12 == NULL) {
  38. PKCS12err(PKCS12_F_PKCS12_PARSE,
  39. PKCS12_R_INVALID_NULL_PKCS12_POINTER);
  40. return 0;
  41. }
  42. /* Check the mac */
  43. /*
  44. * If password is zero length or NULL then try verifying both cases to
  45. * determine which password is correct. The reason for this is that under
  46. * PKCS#12 password based encryption no password and a zero length
  47. * password are two different things...
  48. */
  49. if (pass == NULL || *pass == '\0') {
  50. if (PKCS12_verify_mac(p12, NULL, 0))
  51. pass = NULL;
  52. else if (PKCS12_verify_mac(p12, "", 0))
  53. pass = "";
  54. else {
  55. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
  56. goto err;
  57. }
  58. } else if (!PKCS12_verify_mac(p12, pass, -1)) {
  59. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
  60. goto err;
  61. }
  62. /* If needed, allocate stack for other certificates */
  63. if ((cert != NULL || ca != NULL)
  64. && (ocerts = sk_X509_new_null()) == NULL) {
  65. PKCS12err(PKCS12_F_PKCS12_PARSE, ERR_R_MALLOC_FAILURE);
  66. goto err;
  67. }
  68. if (!parse_pk12(p12, pass, -1, pkey, ocerts)) {
  69. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_PARSE_ERROR);
  70. goto err;
  71. }
  72. /* Split the certs in ocerts over *cert and *ca as far as requested */
  73. while ((x = sk_X509_shift(ocerts)) != NULL) {
  74. if (pkey != NULL && *pkey != NULL
  75. && cert != NULL && *cert == NULL) {
  76. int match;
  77. ERR_set_mark();
  78. match = X509_check_private_key(x, *pkey);
  79. ERR_pop_to_mark();
  80. if (match) {
  81. *cert = x;
  82. continue;
  83. }
  84. }
  85. if (ca != NULL) {
  86. if (!X509_add_cert_new(ca, x, X509_ADD_FLAG_DEFAULT))
  87. goto err;
  88. continue;
  89. }
  90. X509_free(x);
  91. }
  92. sk_X509_free(ocerts);
  93. return 1;
  94. err:
  95. if (pkey != NULL) {
  96. EVP_PKEY_free(*pkey);
  97. *pkey = NULL;
  98. }
  99. if (cert != NULL) {
  100. X509_free(*cert);
  101. *cert = NULL;
  102. }
  103. X509_free(x);
  104. sk_X509_pop_free(ocerts, X509_free);
  105. return 0;
  106. }
  107. /* Parse the outer PKCS#12 structure */
  108. /* pkey and/or ocerts may be NULL */
  109. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  110. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  111. {
  112. STACK_OF(PKCS7) *asafes;
  113. STACK_OF(PKCS12_SAFEBAG) *bags;
  114. int i, bagnid;
  115. PKCS7 *p7;
  116. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  117. return 0;
  118. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  119. p7 = sk_PKCS7_value(asafes, i);
  120. bagnid = OBJ_obj2nid(p7->type);
  121. if (bagnid == NID_pkcs7_data) {
  122. bags = PKCS12_unpack_p7data(p7);
  123. } else if (bagnid == NID_pkcs7_encrypted) {
  124. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  125. } else
  126. continue;
  127. if (!bags) {
  128. sk_PKCS7_pop_free(asafes, PKCS7_free);
  129. return 0;
  130. }
  131. if (!parse_bags(bags, pass, passlen, pkey, ocerts)) {
  132. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  133. sk_PKCS7_pop_free(asafes, PKCS7_free);
  134. return 0;
  135. }
  136. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  137. }
  138. sk_PKCS7_pop_free(asafes, PKCS7_free);
  139. return 1;
  140. }
  141. /* pkey and/or ocerts may be NULL */
  142. static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  143. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  144. {
  145. int i;
  146. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  147. if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i),
  148. pass, passlen, pkey, ocerts))
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. /* pkey and/or ocerts may be NULL */
  154. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  155. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  156. {
  157. PKCS8_PRIV_KEY_INFO *p8;
  158. X509 *x509;
  159. const ASN1_TYPE *attrib;
  160. ASN1_BMPSTRING *fname = NULL;
  161. ASN1_OCTET_STRING *lkid = NULL;
  162. if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)))
  163. fname = attrib->value.bmpstring;
  164. if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)))
  165. lkid = attrib->value.octet_string;
  166. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  167. case NID_keyBag:
  168. if (pkey == NULL || *pkey != NULL)
  169. return 1;
  170. *pkey = EVP_PKCS82PKEY(PKCS12_SAFEBAG_get0_p8inf(bag));
  171. if (*pkey == NULL)
  172. return 0;
  173. break;
  174. case NID_pkcs8ShroudedKeyBag:
  175. if (pkey == NULL || *pkey != NULL)
  176. return 1;
  177. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  178. return 0;
  179. *pkey = EVP_PKCS82PKEY(p8);
  180. PKCS8_PRIV_KEY_INFO_free(p8);
  181. if (!(*pkey))
  182. return 0;
  183. break;
  184. case NID_certBag:
  185. if (ocerts == NULL
  186. || PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  187. return 1;
  188. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  189. return 0;
  190. if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) {
  191. X509_free(x509);
  192. return 0;
  193. }
  194. if (fname) {
  195. int len, r;
  196. unsigned char *data;
  197. len = ASN1_STRING_to_UTF8(&data, fname);
  198. if (len >= 0) {
  199. r = X509_alias_set1(x509, data, len);
  200. OPENSSL_free(data);
  201. if (!r) {
  202. X509_free(x509);
  203. return 0;
  204. }
  205. }
  206. }
  207. if (!sk_X509_push(ocerts, x509)) {
  208. X509_free(x509);
  209. return 0;
  210. }
  211. break;
  212. case NID_safeContentsBag:
  213. return parse_bags(PKCS12_SAFEBAG_get0_safes(bag), pass, passlen, pkey,
  214. ocerts);
  215. default:
  216. return 1;
  217. }
  218. return 1;
  219. }