p12_kiss.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 == NULL) {
  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 == NULL || *pass == '\0') {
  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 != NULL && *pkey != NULL
  72. && cert != NULL && *cert == NULL) {
  73. ERR_set_mark();
  74. if (X509_check_private_key(x, *pkey)) {
  75. *cert = x;
  76. x = NULL;
  77. }
  78. ERR_pop_to_mark();
  79. }
  80. if (ca && x) {
  81. if (*ca == NULL)
  82. *ca = sk_X509_new_null();
  83. if (*ca == NULL)
  84. goto err;
  85. if (!sk_X509_push(*ca, x))
  86. goto err;
  87. x = NULL;
  88. }
  89. X509_free(x);
  90. }
  91. sk_X509_pop_free(ocerts, X509_free);
  92. return 1;
  93. err:
  94. if (pkey) {
  95. EVP_PKEY_free(*pkey);
  96. *pkey = NULL;
  97. }
  98. if (cert) {
  99. X509_free(*cert);
  100. *cert = NULL;
  101. }
  102. X509_free(x);
  103. sk_X509_pop_free(ocerts, X509_free);
  104. return 0;
  105. }
  106. /* Parse the outer PKCS#12 structure */
  107. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  108. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  109. {
  110. STACK_OF(PKCS7) *asafes;
  111. STACK_OF(PKCS12_SAFEBAG) *bags;
  112. int i, bagnid;
  113. PKCS7 *p7;
  114. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  115. return 0;
  116. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  117. p7 = sk_PKCS7_value(asafes, i);
  118. bagnid = OBJ_obj2nid(p7->type);
  119. if (bagnid == NID_pkcs7_data) {
  120. bags = PKCS12_unpack_p7data(p7);
  121. } else if (bagnid == NID_pkcs7_encrypted) {
  122. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  123. } else
  124. continue;
  125. if (!bags) {
  126. sk_PKCS7_pop_free(asafes, PKCS7_free);
  127. return 0;
  128. }
  129. if (!parse_bags(bags, pass, passlen, pkey, ocerts)) {
  130. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  131. sk_PKCS7_pop_free(asafes, PKCS7_free);
  132. return 0;
  133. }
  134. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  135. }
  136. sk_PKCS7_pop_free(asafes, PKCS7_free);
  137. return 1;
  138. }
  139. static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  140. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  141. {
  142. int i;
  143. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  144. if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i),
  145. pass, passlen, pkey, ocerts))
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  151. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  152. {
  153. PKCS8_PRIV_KEY_INFO *p8;
  154. X509 *x509;
  155. const ASN1_TYPE *attrib;
  156. ASN1_BMPSTRING *fname = NULL;
  157. ASN1_OCTET_STRING *lkid = NULL;
  158. if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)))
  159. fname = attrib->value.bmpstring;
  160. if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)))
  161. lkid = attrib->value.octet_string;
  162. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  163. case NID_keyBag:
  164. if (pkey == NULL || *pkey != NULL)
  165. return 1;
  166. *pkey = EVP_PKCS82PKEY(PKCS12_SAFEBAG_get0_p8inf(bag));
  167. if (*pkey == NULL)
  168. return 0;
  169. break;
  170. case NID_pkcs8ShroudedKeyBag:
  171. if (pkey == NULL || *pkey != NULL)
  172. return 1;
  173. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  174. return 0;
  175. *pkey = EVP_PKCS82PKEY(p8);
  176. PKCS8_PRIV_KEY_INFO_free(p8);
  177. if (!(*pkey))
  178. return 0;
  179. break;
  180. case NID_certBag:
  181. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  182. return 1;
  183. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  184. return 0;
  185. if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) {
  186. X509_free(x509);
  187. return 0;
  188. }
  189. if (fname) {
  190. int len, r;
  191. unsigned char *data;
  192. len = ASN1_STRING_to_UTF8(&data, fname);
  193. if (len >= 0) {
  194. r = X509_alias_set1(x509, data, len);
  195. OPENSSL_free(data);
  196. if (!r) {
  197. X509_free(x509);
  198. return 0;
  199. }
  200. }
  201. }
  202. if (!sk_X509_push(ocerts, x509)) {
  203. X509_free(x509);
  204. return 0;
  205. }
  206. break;
  207. case NID_safeContentsBag:
  208. return parse_bags(PKCS12_SAFEBAG_get0_safes(bag), pass, passlen, pkey,
  209. ocerts);
  210. default:
  211. return 1;
  212. }
  213. return 1;
  214. }