p12_kiss.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "internal/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. if (X509_check_private_key(x, *pkey)) {
  123. *cert = x;
  124. x = NULL;
  125. }
  126. }
  127. if (ca && x) {
  128. if (!*ca)
  129. *ca = sk_X509_new_null();
  130. if (!*ca)
  131. goto err;
  132. if (!sk_X509_push(*ca, x))
  133. goto err;
  134. x = NULL;
  135. }
  136. X509_free(x);
  137. }
  138. sk_X509_pop_free(ocerts, X509_free);
  139. return 1;
  140. err:
  141. if (pkey)
  142. EVP_PKEY_free(*pkey);
  143. if (cert)
  144. X509_free(*cert);
  145. X509_free(x);
  146. sk_X509_pop_free(ocerts, X509_free);
  147. return 0;
  148. }
  149. /* Parse the outer PKCS#12 structure */
  150. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  151. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  152. {
  153. STACK_OF(PKCS7) *asafes;
  154. STACK_OF(PKCS12_SAFEBAG) *bags;
  155. int i, bagnid;
  156. PKCS7 *p7;
  157. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  158. return 0;
  159. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  160. p7 = sk_PKCS7_value(asafes, i);
  161. bagnid = OBJ_obj2nid(p7->type);
  162. if (bagnid == NID_pkcs7_data) {
  163. bags = PKCS12_unpack_p7data(p7);
  164. } else if (bagnid == NID_pkcs7_encrypted) {
  165. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  166. } else
  167. continue;
  168. if (!bags) {
  169. sk_PKCS7_pop_free(asafes, PKCS7_free);
  170. return 0;
  171. }
  172. if (!parse_bags(bags, pass, passlen, pkey, ocerts)) {
  173. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  174. sk_PKCS7_pop_free(asafes, PKCS7_free);
  175. return 0;
  176. }
  177. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  178. }
  179. sk_PKCS7_pop_free(asafes, PKCS7_free);
  180. return 1;
  181. }
  182. static int parse_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  183. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  184. {
  185. int i;
  186. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  187. if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i),
  188. pass, passlen, pkey, ocerts))
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  194. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  195. {
  196. PKCS8_PRIV_KEY_INFO *p8;
  197. X509 *x509;
  198. ASN1_TYPE *attrib;
  199. ASN1_BMPSTRING *fname = NULL;
  200. ASN1_OCTET_STRING *lkid = NULL;
  201. if ((attrib = PKCS12_get_attr(bag, NID_friendlyName)))
  202. fname = attrib->value.bmpstring;
  203. if ((attrib = PKCS12_get_attr(bag, NID_localKeyID)))
  204. lkid = attrib->value.octet_string;
  205. switch (M_PKCS12_bag_type(bag)) {
  206. case NID_keyBag:
  207. if (!pkey || *pkey)
  208. return 1;
  209. if ((*pkey = EVP_PKCS82PKEY(bag->value.keybag)) == NULL)
  210. return 0;
  211. break;
  212. case NID_pkcs8ShroudedKeyBag:
  213. if (!pkey || *pkey)
  214. return 1;
  215. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  216. return 0;
  217. *pkey = EVP_PKCS82PKEY(p8);
  218. PKCS8_PRIV_KEY_INFO_free(p8);
  219. if (!(*pkey))
  220. return 0;
  221. break;
  222. case NID_certBag:
  223. if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
  224. return 1;
  225. if ((x509 = PKCS12_certbag2x509(bag)) == NULL)
  226. return 0;
  227. if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) {
  228. X509_free(x509);
  229. return 0;
  230. }
  231. if (fname) {
  232. int len, r;
  233. unsigned char *data;
  234. len = ASN1_STRING_to_UTF8(&data, fname);
  235. if (len >= 0) {
  236. r = X509_alias_set1(x509, data, len);
  237. OPENSSL_free(data);
  238. if (!r) {
  239. X509_free(x509);
  240. return 0;
  241. }
  242. }
  243. }
  244. if (!sk_X509_push(ocerts, x509)) {
  245. X509_free(x509);
  246. return 0;
  247. }
  248. break;
  249. case NID_safeContentsBag:
  250. return parse_bags(bag->value.safes, pass, passlen, pkey, ocerts);
  251. default:
  252. return 1;
  253. }
  254. return 1;
  255. }