p12_npas.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/pem.h>
  13. #include <openssl/err.h>
  14. #include <openssl/pkcs12.h>
  15. #include "p12_local.h"
  16. /* PKCS#12 password change routine */
  17. static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass);
  18. static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
  19. const char *newpass,
  20. OSSL_LIB_CTX *libctx, const char *propq);
  21. static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
  22. const char *newpass,
  23. OSSL_LIB_CTX *libctx, const char *propq);
  24. static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter,
  25. int *psaltlen, int *cipherid);
  26. /*
  27. * Change the password on a PKCS#12 structure.
  28. */
  29. int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass)
  30. {
  31. /* Check for NULL PKCS12 structure */
  32. if (p12 == NULL) {
  33. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_PKCS12_POINTER);
  34. return 0;
  35. }
  36. /* Check the mac */
  37. if (p12->mac != NULL) {
  38. if (!PKCS12_verify_mac(p12, oldpass, -1)) {
  39. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE);
  40. return 0;
  41. }
  42. }
  43. if (!newpass_p12(p12, oldpass, newpass)) {
  44. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR);
  45. return 0;
  46. }
  47. return 1;
  48. }
  49. /* Parse the outer PKCS#12 structure */
  50. static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
  51. {
  52. STACK_OF(PKCS7) *asafes = NULL, *newsafes = NULL;
  53. STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
  54. int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0, cipherid = NID_undef;
  55. PKCS7 *p7, *p7new;
  56. ASN1_OCTET_STRING *p12_data_tmp = NULL, *macoct = NULL;
  57. unsigned char mac[EVP_MAX_MD_SIZE];
  58. unsigned int maclen;
  59. int rv = 0;
  60. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  61. goto err;
  62. if ((newsafes = sk_PKCS7_new_null()) == NULL)
  63. goto err;
  64. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  65. p7 = sk_PKCS7_value(asafes, i);
  66. bagnid = OBJ_obj2nid(p7->type);
  67. if (bagnid == NID_pkcs7_data) {
  68. bags = PKCS12_unpack_p7data(p7);
  69. } else if (bagnid == NID_pkcs7_encrypted) {
  70. bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
  71. if (!alg_get(p7->d.encrypted->enc_data->algorithm,
  72. &pbe_nid, &pbe_iter, &pbe_saltlen, &cipherid))
  73. goto err;
  74. } else {
  75. continue;
  76. }
  77. if (bags == NULL)
  78. goto err;
  79. if (!newpass_bags(bags, oldpass, newpass,
  80. p7->ctx.libctx, p7->ctx.propq))
  81. goto err;
  82. /* Repack bag in same form with new password */
  83. if (bagnid == NID_pkcs7_data)
  84. p7new = PKCS12_pack_p7data(bags);
  85. else
  86. p7new = PKCS12_pack_p7encdata_ex(pbe_nid, newpass, -1, NULL,
  87. pbe_saltlen, pbe_iter, bags,
  88. p7->ctx.libctx, p7->ctx.propq);
  89. if (p7new == NULL || !sk_PKCS7_push(newsafes, p7new))
  90. goto err;
  91. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  92. bags = NULL;
  93. }
  94. /* Repack safe: save old safe in case of error */
  95. p12_data_tmp = p12->authsafes->d.data;
  96. if ((p12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL)
  97. goto err;
  98. if (!PKCS12_pack_authsafes(p12, newsafes))
  99. goto err;
  100. if (p12->mac != NULL) {
  101. if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
  102. goto err;
  103. X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
  104. if (!ASN1_OCTET_STRING_set(macoct, mac, maclen))
  105. goto err;
  106. }
  107. rv = 1;
  108. err:
  109. /* Restore old safe if necessary */
  110. if (rv == 1) {
  111. ASN1_OCTET_STRING_free(p12_data_tmp);
  112. } else if (p12_data_tmp != NULL) {
  113. ASN1_OCTET_STRING_free(p12->authsafes->d.data);
  114. p12->authsafes->d.data = p12_data_tmp;
  115. }
  116. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  117. sk_PKCS7_pop_free(asafes, PKCS7_free);
  118. sk_PKCS7_pop_free(newsafes, PKCS7_free);
  119. return rv;
  120. }
  121. static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
  122. const char *newpass,
  123. OSSL_LIB_CTX *libctx, const char *propq)
  124. {
  125. int i;
  126. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  127. if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), oldpass, newpass,
  128. libctx, propq))
  129. return 0;
  130. }
  131. return 1;
  132. }
  133. /* Change password of safebag: only needs handle shrouded keybags */
  134. static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
  135. const char *newpass,
  136. OSSL_LIB_CTX *libctx, const char *propq)
  137. {
  138. EVP_CIPHER *cipher = NULL;
  139. PKCS8_PRIV_KEY_INFO *p8;
  140. X509_SIG *p8new;
  141. int p8_nid, p8_saltlen, p8_iter, cipherid = 0;
  142. const X509_ALGOR *shalg;
  143. if (PKCS12_SAFEBAG_get_nid(bag) != NID_pkcs8ShroudedKeyBag)
  144. return 1;
  145. if ((p8 = PKCS8_decrypt_ex(bag->value.shkeybag, oldpass, -1,
  146. libctx, propq)) == NULL)
  147. return 0;
  148. X509_SIG_get0(bag->value.shkeybag, &shalg, NULL);
  149. if (!alg_get(shalg, &p8_nid, &p8_iter, &p8_saltlen, &cipherid)) {
  150. PKCS8_PRIV_KEY_INFO_free(p8);
  151. return 0;
  152. }
  153. if (cipherid != NID_undef) {
  154. cipher = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(cipherid), propq);
  155. if (cipher == NULL) {
  156. PKCS8_PRIV_KEY_INFO_free(p8);
  157. return 0;
  158. }
  159. }
  160. p8new = PKCS8_encrypt_ex(p8_nid, cipher, newpass, -1, NULL, p8_saltlen,
  161. p8_iter, p8, libctx, propq);
  162. PKCS8_PRIV_KEY_INFO_free(p8);
  163. EVP_CIPHER_free(cipher);
  164. if (p8new == NULL)
  165. return 0;
  166. X509_SIG_free(bag->value.shkeybag);
  167. bag->value.shkeybag = p8new;
  168. return 1;
  169. }
  170. static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter,
  171. int *psaltlen, int *cipherid)
  172. {
  173. int ret = 0, pbenid, aparamtype;
  174. int encnid, prfnid;
  175. const ASN1_OBJECT *aoid;
  176. const void *aparam;
  177. PBEPARAM *pbe = NULL;
  178. PBE2PARAM *pbe2 = NULL;
  179. PBKDF2PARAM *kdf = NULL;
  180. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
  181. pbenid = OBJ_obj2nid(aoid);
  182. switch (pbenid) {
  183. case NID_pbes2:
  184. if (aparamtype == V_ASN1_SEQUENCE)
  185. pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
  186. if (pbe2 == NULL)
  187. goto done;
  188. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
  189. pbenid = OBJ_obj2nid(aoid);
  190. X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
  191. encnid = OBJ_obj2nid(aoid);
  192. if (aparamtype == V_ASN1_SEQUENCE)
  193. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
  194. if (kdf == NULL)
  195. goto done;
  196. /* Only OCTET_STRING is supported */
  197. if (kdf->salt->type != V_ASN1_OCTET_STRING)
  198. goto done;
  199. if (kdf->prf == NULL) {
  200. prfnid = NID_hmacWithSHA1;
  201. } else {
  202. X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
  203. prfnid = OBJ_obj2nid(aoid);
  204. }
  205. *psaltlen = kdf->salt->value.octet_string->length;
  206. *piter = ASN1_INTEGER_get(kdf->iter);
  207. *pnid = prfnid;
  208. *cipherid = encnid;
  209. break;
  210. default:
  211. pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), alg->parameter);
  212. if (pbe == NULL)
  213. goto done;
  214. *pnid = OBJ_obj2nid(alg->algorithm);
  215. *piter = ASN1_INTEGER_get(pbe->iter);
  216. *psaltlen = pbe->salt->length;
  217. *cipherid = NID_undef;
  218. ret = 1;
  219. break;
  220. }
  221. ret = 1;
  222. done:
  223. if (kdf != NULL)
  224. PBKDF2PARAM_free(kdf);
  225. if (pbe2 != NULL)
  226. PBE2PARAM_free(pbe2);
  227. if (pbe != NULL)
  228. PBEPARAM_free(pbe);
  229. return ret;
  230. }