p12_npas.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* p12_npas.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <openssl/pem.h>
  63. #include <openssl/err.h>
  64. #include <openssl/pkcs12.h>
  65. /* PKCS#12 password change routine */
  66. static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass);
  67. static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, char *oldpass,
  68. char *newpass);
  69. static int newpass_bag(PKCS12_SAFEBAG *bag, char *oldpass, char *newpass);
  70. static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen);
  71. /*
  72. * Change the password on a PKCS#12 structure.
  73. */
  74. int PKCS12_newpass(PKCS12 *p12, char *oldpass, char *newpass)
  75. {
  76. /* Check for NULL PKCS12 structure */
  77. if (!p12) {
  78. PKCS12err(PKCS12_F_PKCS12_NEWPASS,
  79. PKCS12_R_INVALID_NULL_PKCS12_POINTER);
  80. return 0;
  81. }
  82. /* Check the mac */
  83. if (!PKCS12_verify_mac(p12, oldpass, -1)) {
  84. PKCS12err(PKCS12_F_PKCS12_NEWPASS, PKCS12_R_MAC_VERIFY_FAILURE);
  85. return 0;
  86. }
  87. if (!newpass_p12(p12, oldpass, newpass)) {
  88. PKCS12err(PKCS12_F_PKCS12_NEWPASS, PKCS12_R_PARSE_ERROR);
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. /* Parse the outer PKCS#12 structure */
  94. static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)
  95. {
  96. STACK_OF(PKCS7) *asafes, *newsafes;
  97. STACK_OF(PKCS12_SAFEBAG) *bags;
  98. int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0;
  99. PKCS7 *p7, *p7new;
  100. ASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL;
  101. unsigned char mac[EVP_MAX_MD_SIZE];
  102. unsigned int maclen;
  103. if (!(asafes = PKCS12_unpack_authsafes(p12)))
  104. return 0;
  105. if (!(newsafes = sk_PKCS7_new_null()))
  106. return 0;
  107. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  108. p7 = sk_PKCS7_value(asafes, i);
  109. bagnid = OBJ_obj2nid(p7->type);
  110. if (bagnid == NID_pkcs7_data) {
  111. bags = PKCS12_unpack_p7data(p7);
  112. } else if (bagnid == NID_pkcs7_encrypted) {
  113. bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
  114. if (!alg_get(p7->d.encrypted->enc_data->algorithm,
  115. &pbe_nid, &pbe_iter, &pbe_saltlen)) {
  116. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  117. bags = NULL;
  118. }
  119. } else
  120. continue;
  121. if (!bags) {
  122. sk_PKCS7_pop_free(asafes, PKCS7_free);
  123. return 0;
  124. }
  125. if (!newpass_bags(bags, oldpass, newpass)) {
  126. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  127. sk_PKCS7_pop_free(asafes, PKCS7_free);
  128. return 0;
  129. }
  130. /* Repack bag in same form with new password */
  131. if (bagnid == NID_pkcs7_data)
  132. p7new = PKCS12_pack_p7data(bags);
  133. else
  134. p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL,
  135. pbe_saltlen, pbe_iter, bags);
  136. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  137. if (!p7new) {
  138. sk_PKCS7_pop_free(asafes, PKCS7_free);
  139. return 0;
  140. }
  141. sk_PKCS7_push(newsafes, p7new);
  142. }
  143. sk_PKCS7_pop_free(asafes, PKCS7_free);
  144. /* Repack safe: save old safe in case of error */
  145. p12_data_tmp = p12->authsafes->d.data;
  146. if (!(p12->authsafes->d.data = ASN1_OCTET_STRING_new()))
  147. goto saferr;
  148. if (!PKCS12_pack_authsafes(p12, newsafes))
  149. goto saferr;
  150. if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
  151. goto saferr;
  152. if (!(macnew = ASN1_OCTET_STRING_new()))
  153. goto saferr;
  154. if (!ASN1_OCTET_STRING_set(macnew, mac, maclen))
  155. goto saferr;
  156. ASN1_OCTET_STRING_free(p12->mac->dinfo->digest);
  157. p12->mac->dinfo->digest = macnew;
  158. ASN1_OCTET_STRING_free(p12_data_tmp);
  159. return 1;
  160. saferr:
  161. /* Restore old safe */
  162. ASN1_OCTET_STRING_free(p12->authsafes->d.data);
  163. ASN1_OCTET_STRING_free(macnew);
  164. p12->authsafes->d.data = p12_data_tmp;
  165. return 0;
  166. }
  167. static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, char *oldpass,
  168. char *newpass)
  169. {
  170. int i;
  171. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  172. if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), oldpass, newpass))
  173. return 0;
  174. }
  175. return 1;
  176. }
  177. /* Change password of safebag: only needs handle shrouded keybags */
  178. static int newpass_bag(PKCS12_SAFEBAG *bag, char *oldpass, char *newpass)
  179. {
  180. PKCS8_PRIV_KEY_INFO *p8;
  181. X509_SIG *p8new;
  182. int p8_nid, p8_saltlen, p8_iter;
  183. if (M_PKCS12_bag_type(bag) != NID_pkcs8ShroudedKeyBag)
  184. return 1;
  185. if (!(p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1)))
  186. return 0;
  187. if (!alg_get(bag->value.shkeybag->algor, &p8_nid, &p8_iter, &p8_saltlen))
  188. return 0;
  189. if (!(p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen,
  190. p8_iter, p8)))
  191. return 0;
  192. X509_SIG_free(bag->value.shkeybag);
  193. bag->value.shkeybag = p8new;
  194. return 1;
  195. }
  196. static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen)
  197. {
  198. PBEPARAM *pbe;
  199. const unsigned char *p;
  200. p = alg->parameter->value.sequence->data;
  201. pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
  202. if (!pbe)
  203. return 0;
  204. *pnid = OBJ_obj2nid(alg->algorithm);
  205. *piter = ASN1_INTEGER_get(pbe->iter);
  206. *psaltlen = pbe->salt->length;
  207. PBEPARAM_free(pbe);
  208. return 1;
  209. }