p12_kiss.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* p12_kiss.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/pkcs12.h>
  62. /* Simplified PKCS#12 routines */
  63. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  64. EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
  65. static int parse_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  66. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
  67. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  68. EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
  69. /*
  70. * Parse and decrypt a PKCS#12 structure returning user key, user cert and
  71. * other (CA) certs. Note either ca should be NULL, *ca should be NULL, or it
  72. * should point to a valid STACK structure. pkey and cert can be passed
  73. * unitialised.
  74. */
  75. int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
  76. STACK_OF(X509) **ca)
  77. {
  78. STACK_OF(X509) *ocerts = NULL;
  79. X509 *x = NULL;
  80. /* Check for NULL PKCS12 structure */
  81. if (!p12) {
  82. PKCS12err(PKCS12_F_PKCS12_PARSE,
  83. PKCS12_R_INVALID_NULL_PKCS12_POINTER);
  84. return 0;
  85. }
  86. if (pkey)
  87. *pkey = NULL;
  88. if (cert)
  89. *cert = NULL;
  90. /* Check the mac */
  91. /*
  92. * If password is zero length or NULL then try verifying both cases to
  93. * determine which password is correct. The reason for this is that under
  94. * PKCS#12 password based encryption no password and a zero length
  95. * password are two different things...
  96. */
  97. if (!pass || !*pass) {
  98. if (PKCS12_verify_mac(p12, NULL, 0))
  99. pass = NULL;
  100. else if (PKCS12_verify_mac(p12, "", 0))
  101. pass = "";
  102. else {
  103. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
  104. goto err;
  105. }
  106. } else if (!PKCS12_verify_mac(p12, pass, -1)) {
  107. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
  108. goto err;
  109. }
  110. /* Allocate stack for other certificates */
  111. ocerts = sk_X509_new_null();
  112. if (!ocerts) {
  113. PKCS12err(PKCS12_F_PKCS12_PARSE, ERR_R_MALLOC_FAILURE);
  114. return 0;
  115. }
  116. if (!parse_pk12(p12, pass, -1, pkey, ocerts)) {
  117. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_PARSE_ERROR);
  118. goto err;
  119. }
  120. while ((x = sk_X509_pop(ocerts))) {
  121. if (pkey && *pkey && cert && !*cert) {
  122. ERR_set_mark();
  123. if (X509_check_private_key(x, *pkey)) {
  124. *cert = x;
  125. x = NULL;
  126. }
  127. ERR_pop_to_mark();
  128. }
  129. if (ca && x) {
  130. if (!*ca)
  131. *ca = sk_X509_new_null();
  132. if (!*ca)
  133. goto err;
  134. if (!sk_X509_push(*ca, x))
  135. goto err;
  136. x = NULL;
  137. }
  138. if (x)
  139. X509_free(x);
  140. }
  141. if (ocerts)
  142. sk_X509_pop_free(ocerts, X509_free);
  143. return 1;
  144. err:
  145. if (pkey && *pkey)
  146. EVP_PKEY_free(*pkey);
  147. if (cert && *cert)
  148. X509_free(*cert);
  149. if (x)
  150. X509_free(x);
  151. if (ocerts)
  152. sk_X509_pop_free(ocerts, X509_free);
  153. return 0;
  154. }
  155. /* Parse the outer PKCS#12 structure */
  156. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  157. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  158. {
  159. STACK_OF(PKCS7) *asafes;
  160. STACK_OF(PKCS12_SAFEBAG) *bags;
  161. int i, bagnid;
  162. PKCS7 *p7;
  163. if (!(asafes = PKCS12_unpack_authsafes(p12)))
  164. return 0;
  165. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  166. p7 = sk_PKCS7_value(asafes, i);
  167. bagnid = OBJ_obj2nid(p7->type);
  168. if (bagnid == NID_pkcs7_data) {
  169. bags = PKCS12_unpack_p7data(p7);
  170. } else if (bagnid == NID_pkcs7_encrypted) {
  171. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  172. } else
  173. continue;
  174. if (!bags) {
  175. sk_PKCS7_pop_free(asafes, PKCS7_free);
  176. return 0;
  177. }
  178. if (!parse_bags(bags, pass, passlen, pkey, ocerts)) {
  179. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  180. sk_PKCS7_pop_free(asafes, PKCS7_free);
  181. return 0;
  182. }
  183. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  184. }
  185. sk_PKCS7_pop_free(asafes, PKCS7_free);
  186. return 1;
  187. }
  188. static int parse_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  189. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  190. {
  191. int i;
  192. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  193. if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i),
  194. pass, passlen, pkey, ocerts))
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  200. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  201. {
  202. PKCS8_PRIV_KEY_INFO *p8;
  203. X509 *x509;
  204. ASN1_TYPE *attrib;
  205. ASN1_BMPSTRING *fname = NULL;
  206. ASN1_OCTET_STRING *lkid = NULL;
  207. if ((attrib = PKCS12_get_attr(bag, NID_friendlyName)))
  208. fname = attrib->value.bmpstring;
  209. if ((attrib = PKCS12_get_attr(bag, NID_localKeyID)))
  210. lkid = attrib->value.octet_string;
  211. switch (M_PKCS12_bag_type(bag)) {
  212. case NID_keyBag:
  213. if (!pkey || *pkey)
  214. return 1;
  215. if (!(*pkey = EVP_PKCS82PKEY(bag->value.keybag)))
  216. return 0;
  217. break;
  218. case NID_pkcs8ShroudedKeyBag:
  219. if (!pkey || *pkey)
  220. return 1;
  221. if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
  222. return 0;
  223. *pkey = EVP_PKCS82PKEY(p8);
  224. PKCS8_PRIV_KEY_INFO_free(p8);
  225. if (!(*pkey))
  226. return 0;
  227. break;
  228. case NID_certBag:
  229. if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
  230. return 1;
  231. if (!(x509 = PKCS12_certbag2x509(bag)))
  232. return 0;
  233. if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) {
  234. X509_free(x509);
  235. return 0;
  236. }
  237. if (fname) {
  238. int len, r;
  239. unsigned char *data;
  240. len = ASN1_STRING_to_UTF8(&data, fname);
  241. if (len >= 0) {
  242. r = X509_alias_set1(x509, data, len);
  243. OPENSSL_free(data);
  244. if (!r) {
  245. X509_free(x509);
  246. return 0;
  247. }
  248. }
  249. }
  250. if (!sk_X509_push(ocerts, x509)) {
  251. X509_free(x509);
  252. return 0;
  253. }
  254. break;
  255. case NID_safeContentsBag:
  256. return parse_bags(bag->value.safes, pass, passlen, pkey, ocerts);
  257. break;
  258. default:
  259. return 1;
  260. break;
  261. }
  262. return 1;
  263. }