p12_npas.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 1999-2020 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. static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
  21. const char *newpass);
  22. static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter,
  23. int *psaltlen);
  24. /*
  25. * Change the password on a PKCS#12 structure.
  26. */
  27. int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass)
  28. {
  29. /* Check for NULL PKCS12 structure */
  30. if (p12 == NULL) {
  31. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_PKCS12_POINTER);
  32. return 0;
  33. }
  34. /* Check the mac */
  35. if (!PKCS12_verify_mac(p12, oldpass, -1)) {
  36. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE);
  37. return 0;
  38. }
  39. if (!newpass_p12(p12, oldpass, newpass)) {
  40. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR);
  41. return 0;
  42. }
  43. return 1;
  44. }
  45. /* Parse the outer PKCS#12 structure */
  46. static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
  47. {
  48. STACK_OF(PKCS7) *asafes = NULL, *newsafes = NULL;
  49. STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
  50. int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0;
  51. PKCS7 *p7, *p7new;
  52. ASN1_OCTET_STRING *p12_data_tmp = NULL, *macoct = NULL;
  53. unsigned char mac[EVP_MAX_MD_SIZE];
  54. unsigned int maclen;
  55. int rv = 0;
  56. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  57. goto err;
  58. if ((newsafes = sk_PKCS7_new_null()) == NULL)
  59. goto err;
  60. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  61. p7 = sk_PKCS7_value(asafes, i);
  62. bagnid = OBJ_obj2nid(p7->type);
  63. if (bagnid == NID_pkcs7_data) {
  64. bags = PKCS12_unpack_p7data(p7);
  65. } else if (bagnid == NID_pkcs7_encrypted) {
  66. bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
  67. if (!alg_get(p7->d.encrypted->enc_data->algorithm,
  68. &pbe_nid, &pbe_iter, &pbe_saltlen))
  69. goto err;
  70. } else {
  71. continue;
  72. }
  73. if (bags == NULL)
  74. goto err;
  75. if (!newpass_bags(bags, oldpass, newpass))
  76. goto err;
  77. /* Repack bag in same form with new password */
  78. if (bagnid == NID_pkcs7_data)
  79. p7new = PKCS12_pack_p7data(bags);
  80. else
  81. p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL,
  82. pbe_saltlen, pbe_iter, bags);
  83. if (p7new == NULL || !sk_PKCS7_push(newsafes, p7new))
  84. goto err;
  85. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  86. bags = NULL;
  87. }
  88. /* Repack safe: save old safe in case of error */
  89. p12_data_tmp = p12->authsafes->d.data;
  90. if ((p12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL)
  91. goto err;
  92. if (!PKCS12_pack_authsafes(p12, newsafes))
  93. goto err;
  94. if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
  95. goto err;
  96. X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
  97. if (!ASN1_OCTET_STRING_set(macoct, mac, maclen))
  98. goto err;
  99. rv = 1;
  100. err:
  101. /* Restore old safe if necessary */
  102. if (rv == 1) {
  103. ASN1_OCTET_STRING_free(p12_data_tmp);
  104. } else if (p12_data_tmp != NULL) {
  105. ASN1_OCTET_STRING_free(p12->authsafes->d.data);
  106. p12->authsafes->d.data = p12_data_tmp;
  107. }
  108. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  109. sk_PKCS7_pop_free(asafes, PKCS7_free);
  110. sk_PKCS7_pop_free(newsafes, PKCS7_free);
  111. return rv;
  112. }
  113. static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
  114. const char *newpass)
  115. {
  116. int i;
  117. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  118. if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), oldpass, newpass))
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. /* Change password of safebag: only needs handle shrouded keybags */
  124. static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
  125. const char *newpass)
  126. {
  127. PKCS8_PRIV_KEY_INFO *p8;
  128. X509_SIG *p8new;
  129. int p8_nid, p8_saltlen, p8_iter;
  130. const X509_ALGOR *shalg;
  131. if (PKCS12_SAFEBAG_get_nid(bag) != NID_pkcs8ShroudedKeyBag)
  132. return 1;
  133. if ((p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1)) == NULL)
  134. return 0;
  135. X509_SIG_get0(bag->value.shkeybag, &shalg, NULL);
  136. if (!alg_get(shalg, &p8_nid, &p8_iter, &p8_saltlen)) {
  137. PKCS8_PRIV_KEY_INFO_free(p8);
  138. return 0;
  139. }
  140. p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen,
  141. p8_iter, p8);
  142. PKCS8_PRIV_KEY_INFO_free(p8);
  143. if (p8new == NULL)
  144. return 0;
  145. X509_SIG_free(bag->value.shkeybag);
  146. bag->value.shkeybag = p8new;
  147. return 1;
  148. }
  149. static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter,
  150. int *psaltlen)
  151. {
  152. PBEPARAM *pbe;
  153. pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), alg->parameter);
  154. if (pbe == NULL)
  155. return 0;
  156. *pnid = OBJ_obj2nid(alg->algorithm);
  157. *piter = ASN1_INTEGER_get(pbe->iter);
  158. *psaltlen = pbe->salt->length;
  159. PBEPARAM_free(pbe);
  160. return 1;
  161. }