2
0

p12_kiss.c 8.0 KB

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