p12_npas.c 5.6 KB

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