p12_kiss.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 1999-2023 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. #include "crypto/x509.h" /* for ossl_x509_add_cert_new() */
  13. /* Simplified PKCS#12 routines */
  14. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  15. EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
  16. static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  17. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts,
  18. OSSL_LIB_CTX *libctx, const char *propq);
  19. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  20. EVP_PKEY **pkey, STACK_OF(X509) *ocerts,
  21. OSSL_LIB_CTX *libctx, const char *propq);
  22. /*
  23. * Parse and decrypt a PKCS#12 structure returning user key, user cert and
  24. * other (CA) certs. Note either ca should be NULL, *ca should be NULL, or it
  25. * should point to a valid STACK structure. pkey and/or cert may be NULL;
  26. * if non-NULL the variables they point to can be passed uninitialised.
  27. */
  28. int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
  29. STACK_OF(X509) **ca)
  30. {
  31. STACK_OF(X509) *ocerts = NULL;
  32. X509 *x = NULL;
  33. if (pkey != NULL)
  34. *pkey = NULL;
  35. if (cert != NULL)
  36. *cert = NULL;
  37. /* Check for NULL PKCS12 structure */
  38. if (p12 == NULL) {
  39. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_PKCS12_POINTER);
  40. return 0;
  41. }
  42. /* Check the mac */
  43. if (PKCS12_mac_present(p12)) {
  44. /*
  45. * If password is zero length or NULL then try verifying both cases to
  46. * determine which password is correct. The reason for this is that under
  47. * PKCS#12 password based encryption no password and a zero length
  48. * password are two different things...
  49. */
  50. if (pass == NULL || *pass == '\0') {
  51. if (PKCS12_verify_mac(p12, NULL, 0))
  52. pass = NULL;
  53. else if (PKCS12_verify_mac(p12, "", 0))
  54. pass = "";
  55. else {
  56. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE);
  57. goto err;
  58. }
  59. } else if (!PKCS12_verify_mac(p12, pass, -1)) {
  60. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE);
  61. goto err;
  62. }
  63. } else if (pass == NULL || *pass == '\0') {
  64. pass = NULL;
  65. }
  66. /* If needed, allocate stack for other certificates */
  67. if ((cert != NULL || ca != NULL)
  68. && (ocerts = sk_X509_new_null()) == NULL) {
  69. ERR_raise(ERR_LIB_PKCS12, ERR_R_CRYPTO_LIB);
  70. goto err;
  71. }
  72. if (!parse_pk12(p12, pass, -1, pkey, ocerts)) {
  73. int err = ERR_peek_last_error();
  74. if (ERR_GET_LIB(err) != ERR_LIB_EVP
  75. && ERR_GET_REASON(err) != EVP_R_UNSUPPORTED_ALGORITHM)
  76. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR);
  77. goto err;
  78. }
  79. /* Split the certs in ocerts over *cert and *ca as far as requested */
  80. while ((x = sk_X509_shift(ocerts)) != NULL) {
  81. if (pkey != NULL && *pkey != NULL
  82. && cert != NULL && *cert == NULL) {
  83. int match;
  84. ERR_set_mark();
  85. match = X509_check_private_key(x, *pkey);
  86. ERR_pop_to_mark();
  87. if (match) {
  88. *cert = x;
  89. continue;
  90. }
  91. }
  92. if (ca != NULL) {
  93. if (!ossl_x509_add_cert_new(ca, x, X509_ADD_FLAG_DEFAULT))
  94. goto err;
  95. continue;
  96. }
  97. X509_free(x);
  98. }
  99. sk_X509_free(ocerts);
  100. return 1;
  101. err:
  102. if (pkey != NULL) {
  103. EVP_PKEY_free(*pkey);
  104. *pkey = NULL;
  105. }
  106. if (cert != NULL) {
  107. X509_free(*cert);
  108. *cert = NULL;
  109. }
  110. X509_free(x);
  111. OSSL_STACK_OF_X509_free(ocerts);
  112. return 0;
  113. }
  114. /* Parse the outer PKCS#12 structure */
  115. /* pkey and/or ocerts may be NULL */
  116. static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
  117. EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
  118. {
  119. STACK_OF(PKCS7) *asafes;
  120. STACK_OF(PKCS12_SAFEBAG) *bags;
  121. int i, bagnid;
  122. PKCS7 *p7;
  123. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  124. return 0;
  125. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  126. p7 = sk_PKCS7_value(asafes, i);
  127. bagnid = OBJ_obj2nid(p7->type);
  128. if (bagnid == NID_pkcs7_data) {
  129. bags = PKCS12_unpack_p7data(p7);
  130. } else if (bagnid == NID_pkcs7_encrypted) {
  131. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  132. } else
  133. continue;
  134. if (!bags) {
  135. sk_PKCS7_pop_free(asafes, PKCS7_free);
  136. return 0;
  137. }
  138. if (!parse_bags(bags, pass, passlen, pkey, ocerts,
  139. p7->ctx.libctx, p7->ctx.propq)) {
  140. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  141. sk_PKCS7_pop_free(asafes, PKCS7_free);
  142. return 0;
  143. }
  144. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  145. }
  146. sk_PKCS7_pop_free(asafes, PKCS7_free);
  147. return 1;
  148. }
  149. /* pkey and/or ocerts may be NULL */
  150. static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
  151. int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts,
  152. OSSL_LIB_CTX *libctx, const char *propq)
  153. {
  154. int i;
  155. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  156. if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i),
  157. pass, passlen, pkey, ocerts,
  158. libctx, propq))
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. /* pkey and/or ocerts may be NULL */
  164. static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
  165. EVP_PKEY **pkey, STACK_OF(X509) *ocerts,
  166. OSSL_LIB_CTX *libctx, const char *propq)
  167. {
  168. PKCS8_PRIV_KEY_INFO *p8;
  169. X509 *x509;
  170. const ASN1_TYPE *attrib;
  171. ASN1_BMPSTRING *fname = NULL;
  172. ASN1_OCTET_STRING *lkid = NULL;
  173. if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)))
  174. fname = attrib->value.bmpstring;
  175. if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)))
  176. lkid = attrib->value.octet_string;
  177. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  178. case NID_keyBag:
  179. if (pkey == NULL || *pkey != NULL)
  180. return 1;
  181. *pkey = EVP_PKCS82PKEY_ex(PKCS12_SAFEBAG_get0_p8inf(bag),
  182. libctx, propq);
  183. if (*pkey == NULL)
  184. return 0;
  185. break;
  186. case NID_pkcs8ShroudedKeyBag:
  187. if (pkey == NULL || *pkey != NULL)
  188. return 1;
  189. if ((p8 = PKCS12_decrypt_skey_ex(bag, pass, passlen,
  190. libctx, propq)) == NULL)
  191. return 0;
  192. *pkey = EVP_PKCS82PKEY_ex(p8, libctx, propq);
  193. PKCS8_PRIV_KEY_INFO_free(p8);
  194. if (!(*pkey))
  195. return 0;
  196. break;
  197. case NID_certBag:
  198. if (ocerts == NULL
  199. || PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  200. return 1;
  201. if ((x509 = PKCS12_SAFEBAG_get1_cert_ex(bag, libctx, propq)) == NULL)
  202. return 0;
  203. if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) {
  204. X509_free(x509);
  205. return 0;
  206. }
  207. if (fname) {
  208. int len, r;
  209. unsigned char *data;
  210. len = ASN1_STRING_to_UTF8(&data, fname);
  211. if (len >= 0) {
  212. r = X509_alias_set1(x509, data, len);
  213. OPENSSL_free(data);
  214. if (!r) {
  215. X509_free(x509);
  216. return 0;
  217. }
  218. }
  219. }
  220. if (!sk_X509_push(ocerts, x509)) {
  221. X509_free(x509);
  222. return 0;
  223. }
  224. break;
  225. case NID_safeContentsBag:
  226. return parse_bags(PKCS12_SAFEBAG_get0_safes(bag), pass, passlen, pkey,
  227. ocerts, libctx, propq);
  228. default:
  229. return 1;
  230. }
  231. return 1;
  232. }