p12_kiss.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* p12_kiss.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  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. if (pkey)
  81. *pkey = NULL;
  82. if (cert)
  83. *cert = NULL;
  84. /* Check for NULL PKCS12 structure */
  85. if (!p12) {
  86. PKCS12err(PKCS12_F_PKCS12_PARSE,
  87. PKCS12_R_INVALID_NULL_PKCS12_POINTER);
  88. return 0;
  89. }
  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 ||
  98. (!pass[0]
  99. #if defined(WINSCP) && defined(PBE_UNICODE)
  100. && !pass[1]
  101. #endif
  102. )) {
  103. if (PKCS12_verify_mac(p12, NULL, 0))
  104. pass = NULL;
  105. #if defined(WINSCP) && defined(PBE_UNICODE)
  106. else if (PKCS12_verify_mac(p12, "\0", -1))
  107. pass = "\0"; // two NULLs
  108. #else
  109. else if (PKCS12_verify_mac(p12, "", 0))
  110. pass = "";
  111. #endif
  112. else {
  113. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
  114. goto err;
  115. }
  116. } else if (!PKCS12_verify_mac(p12, pass, -1)) {
  117. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
  118. goto err;
  119. }
  120. /* Allocate stack for other certificates */
  121. ocerts = sk_X509_new_null();
  122. if (!ocerts) {
  123. PKCS12err(PKCS12_F_PKCS12_PARSE, ERR_R_MALLOC_FAILURE);
  124. goto err;
  125. }
  126. if (!parse_pk12(p12, pass, -1, pkey, ocerts)) {
  127. PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_PARSE_ERROR);
  128. goto err;
  129. }
  130. while ((x = sk_X509_pop(ocerts))) {
  131. if (pkey && *pkey && cert && !*cert) {
  132. ERR_set_mark();
  133. if (X509_check_private_key(x, *pkey)) {
  134. *cert = x;
  135. x = NULL;
  136. }
  137. ERR_pop_to_mark();
  138. }
  139. if (ca && x) {
  140. if (!*ca)
  141. *ca = sk_X509_new_null();
  142. if (!*ca)
  143. goto err;
  144. if (!sk_X509_push(*ca, x))
  145. goto err;
  146. x = NULL;
  147. }
  148. if (x)
  149. X509_free(x);
  150. }
  151. if (ocerts)
  152. sk_X509_pop_free(ocerts, X509_free);
  153. return 1;
  154. err:
  155. if (pkey) {
  156. EVP_PKEY_free(*pkey);
  157. *pkey = NULL;
  158. }
  159. if (cert) {
  160. X509_free(*cert);
  161. *cert = NULL;
  162. }
  163. if (x)
  164. X509_free(x);
  165. if (ocerts)
  166. sk_X509_pop_free(ocerts, X509_free);
  167. return 0;
  168. }
  169. /* Parse the outer PKCS#12 structure */
  170. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  171. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  172. {
  173. STACK_OF(PKCS7) *asafes;
  174. STACK_OF(PKCS12_SAFEBAG) *bags;
  175. int i, bagnid;
  176. PKCS7 *p7;
  177. if (!(asafes = PKCS12_unpack_authsafes(p12)))
  178. return 0;
  179. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  180. p7 = sk_PKCS7_value(asafes, i);
  181. bagnid = OBJ_obj2nid(p7->type);
  182. if (bagnid == NID_pkcs7_data) {
  183. bags = PKCS12_unpack_p7data(p7);
  184. } else if (bagnid == NID_pkcs7_encrypted) {
  185. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  186. } else
  187. continue;
  188. if (!bags) {
  189. sk_PKCS7_pop_free(asafes, PKCS7_free);
  190. return 0;
  191. }
  192. if (!parse_bags(bags, pass, passlen, pkey, ocerts)) {
  193. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  194. sk_PKCS7_pop_free(asafes, PKCS7_free);
  195. return 0;
  196. }
  197. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  198. }
  199. sk_PKCS7_pop_free(asafes, PKCS7_free);
  200. return 1;
  201. }
  202. static int parse_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  203. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  204. {
  205. int i;
  206. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  207. if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i),
  208. pass, passlen, pkey, ocerts))
  209. return 0;
  210. }
  211. return 1;
  212. }
  213. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  214. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  215. {
  216. PKCS8_PRIV_KEY_INFO *p8;
  217. X509 *x509;
  218. ASN1_TYPE *attrib;
  219. ASN1_BMPSTRING *fname = NULL;
  220. ASN1_OCTET_STRING *lkid = NULL;
  221. if ((attrib = PKCS12_get_attr(bag, NID_friendlyName)))
  222. fname = attrib->value.bmpstring;
  223. if ((attrib = PKCS12_get_attr(bag, NID_localKeyID)))
  224. lkid = attrib->value.octet_string;
  225. switch (M_PKCS12_bag_type(bag)) {
  226. case NID_keyBag:
  227. if (!pkey || *pkey)
  228. return 1;
  229. if (!(*pkey = EVP_PKCS82PKEY(bag->value.keybag)))
  230. return 0;
  231. break;
  232. case NID_pkcs8ShroudedKeyBag:
  233. if (!pkey || *pkey)
  234. return 1;
  235. if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
  236. return 0;
  237. *pkey = EVP_PKCS82PKEY(p8);
  238. PKCS8_PRIV_KEY_INFO_free(p8);
  239. if (!(*pkey))
  240. return 0;
  241. break;
  242. case NID_certBag:
  243. if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
  244. return 1;
  245. if (!(x509 = PKCS12_certbag2x509(bag)))
  246. return 0;
  247. if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) {
  248. X509_free(x509);
  249. return 0;
  250. }
  251. if (fname) {
  252. int len, r;
  253. unsigned char *data;
  254. len = ASN1_STRING_to_UTF8(&data, fname);
  255. if (len >= 0) {
  256. r = X509_alias_set1(x509, data, len);
  257. OPENSSL_free(data);
  258. if (!r) {
  259. X509_free(x509);
  260. return 0;
  261. }
  262. }
  263. }
  264. if (!sk_X509_push(ocerts, x509)) {
  265. X509_free(x509);
  266. return 0;
  267. }
  268. break;
  269. case NID_safeContentsBag:
  270. return parse_bags(bag->value.safes, pass, passlen, pkey, ocerts);
  271. break;
  272. default:
  273. return 1;
  274. break;
  275. }
  276. return 1;
  277. }