2
0

p12_kiss.c 6.9 KB

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