p12_add.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 1999-2024 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 "internal/cryptlib.h"
  11. #include <openssl/core.h>
  12. #include <openssl/core_names.h>
  13. #include <openssl/pkcs12.h>
  14. #include "p12_local.h"
  15. #include "crypto/pkcs7/pk7_local.h"
  16. /* Pack an object into an OCTET STRING and turn into a safebag */
  17. PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it,
  18. int nid1, int nid2)
  19. {
  20. PKCS12_BAGS *bag;
  21. PKCS12_SAFEBAG *safebag;
  22. if ((bag = PKCS12_BAGS_new()) == NULL) {
  23. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  24. return NULL;
  25. }
  26. bag->type = OBJ_nid2obj(nid1);
  27. if (!ASN1_item_pack(obj, it, &bag->value.octet)) {
  28. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  29. goto err;
  30. }
  31. if ((safebag = PKCS12_SAFEBAG_new()) == NULL) {
  32. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  33. goto err;
  34. }
  35. safebag->value.bag = bag;
  36. safebag->type = OBJ_nid2obj(nid2);
  37. return safebag;
  38. err:
  39. PKCS12_BAGS_free(bag);
  40. return NULL;
  41. }
  42. /* Turn a stack of SAFEBAGS into a PKCS#7 data Contentinfo */
  43. PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk)
  44. {
  45. PKCS7 *p7;
  46. if ((p7 = PKCS7_new()) == NULL) {
  47. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  48. return NULL;
  49. }
  50. p7->type = OBJ_nid2obj(NID_pkcs7_data);
  51. if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL) {
  52. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  53. goto err;
  54. }
  55. if (!ASN1_item_pack(sk, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), &p7->d.data)) {
  56. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CANT_PACK_STRUCTURE);
  57. goto err;
  58. }
  59. return p7;
  60. err:
  61. PKCS7_free(p7);
  62. return NULL;
  63. }
  64. /* Unpack SAFEBAGS from PKCS#7 data ContentInfo */
  65. STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7)
  66. {
  67. if (!PKCS7_type_is_data(p7)) {
  68. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
  69. return NULL;
  70. }
  71. if (p7->d.data == NULL) {
  72. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
  73. return NULL;
  74. }
  75. return ASN1_item_unpack_ex(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
  76. ossl_pkcs7_ctx_get0_libctx(&p7->ctx),
  77. ossl_pkcs7_ctx_get0_propq(&p7->ctx));
  78. }
  79. /* Turn a stack of SAFEBAGS into a PKCS#7 encrypted data ContentInfo */
  80. PKCS7 *PKCS12_pack_p7encdata_ex(int pbe_nid, const char *pass, int passlen,
  81. unsigned char *salt, int saltlen, int iter,
  82. STACK_OF(PKCS12_SAFEBAG) *bags,
  83. OSSL_LIB_CTX *ctx, const char *propq)
  84. {
  85. PKCS7 *p7;
  86. X509_ALGOR *pbe;
  87. const EVP_CIPHER *pbe_ciph = NULL;
  88. EVP_CIPHER *pbe_ciph_fetch = NULL;
  89. if ((p7 = PKCS7_new_ex(ctx, propq)) == NULL) {
  90. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  91. return NULL;
  92. }
  93. if (!PKCS7_set_type(p7, NID_pkcs7_encrypted)) {
  94. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE);
  95. goto err;
  96. }
  97. ERR_set_mark();
  98. pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq);
  99. if (pbe_ciph == NULL)
  100. pbe_ciph = EVP_get_cipherbynid(pbe_nid);
  101. ERR_pop_to_mark();
  102. if (pbe_ciph != NULL) {
  103. pbe = PKCS5_pbe2_set_iv_ex(pbe_ciph, iter, salt, saltlen, NULL, -1, ctx);
  104. } else {
  105. pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, ctx);
  106. }
  107. if (pbe == NULL) {
  108. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  109. goto err;
  110. }
  111. X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm);
  112. p7->d.encrypted->enc_data->algorithm = pbe;
  113. ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data);
  114. if (!(p7->d.encrypted->enc_data->enc_data =
  115. PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass,
  116. passlen, bags, 1, ctx, propq))) {
  117. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
  118. goto err;
  119. }
  120. EVP_CIPHER_free(pbe_ciph_fetch);
  121. return p7;
  122. err:
  123. PKCS7_free(p7);
  124. EVP_CIPHER_free(pbe_ciph_fetch);
  125. return NULL;
  126. }
  127. PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen,
  128. unsigned char *salt, int saltlen, int iter,
  129. STACK_OF(PKCS12_SAFEBAG) *bags)
  130. {
  131. return PKCS12_pack_p7encdata_ex(pbe_nid, pass, passlen, salt, saltlen,
  132. iter, bags, NULL, NULL);
  133. }
  134. STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass,
  135. int passlen)
  136. {
  137. if (!PKCS7_type_is_encrypted(p7))
  138. return NULL;
  139. if (p7->d.encrypted == NULL) {
  140. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
  141. return NULL;
  142. }
  143. return PKCS12_item_decrypt_d2i_ex(p7->d.encrypted->enc_data->algorithm,
  144. ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
  145. pass, passlen,
  146. p7->d.encrypted->enc_data->enc_data, 1,
  147. p7->ctx.libctx, p7->ctx.propq);
  148. }
  149. PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey_ex(const PKCS12_SAFEBAG *bag,
  150. const char *pass, int passlen,
  151. OSSL_LIB_CTX *ctx, const char *propq)
  152. {
  153. return PKCS8_decrypt_ex(bag->value.shkeybag, pass, passlen, ctx, propq);
  154. }
  155. PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag,
  156. const char *pass, int passlen)
  157. {
  158. return PKCS12_decrypt_skey_ex(bag, pass, passlen, NULL, NULL);
  159. }
  160. int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes)
  161. {
  162. if (ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
  163. &p12->authsafes->d.data))
  164. return 1;
  165. return 0;
  166. }
  167. STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12)
  168. {
  169. STACK_OF(PKCS7) *p7s;
  170. PKCS7_CTX *p7ctx;
  171. PKCS7 *p7;
  172. int i;
  173. if (!PKCS7_type_is_data(p12->authsafes)) {
  174. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
  175. return NULL;
  176. }
  177. if (p12->authsafes->d.data == NULL) {
  178. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
  179. return NULL;
  180. }
  181. p7ctx = &p12->authsafes->ctx;
  182. p7s = ASN1_item_unpack_ex(p12->authsafes->d.data,
  183. ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
  184. ossl_pkcs7_ctx_get0_libctx(p7ctx),
  185. ossl_pkcs7_ctx_get0_propq(p7ctx));
  186. if (p7s != NULL) {
  187. for (i = 0; i < sk_PKCS7_num(p7s); i++) {
  188. p7 = sk_PKCS7_value(p7s, i);
  189. if (!ossl_pkcs7_ctx_propagate(p12->authsafes, p7))
  190. goto err;
  191. }
  192. }
  193. return p7s;
  194. err:
  195. sk_PKCS7_free(p7s);
  196. return NULL;
  197. }