p12_npas.c 7.3 KB

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