p12_add.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 1999-2023 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. return ASN1_item_unpack_ex(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
  72. ossl_pkcs7_ctx_get0_libctx(&p7->ctx),
  73. ossl_pkcs7_ctx_get0_propq(&p7->ctx));
  74. }
  75. /* Turn a stack of SAFEBAGS into a PKCS#7 encrypted data ContentInfo */
  76. PKCS7 *PKCS12_pack_p7encdata_ex(int pbe_nid, const char *pass, int passlen,
  77. unsigned char *salt, int saltlen, int iter,
  78. STACK_OF(PKCS12_SAFEBAG) *bags,
  79. OSSL_LIB_CTX *ctx, const char *propq)
  80. {
  81. PKCS7 *p7;
  82. X509_ALGOR *pbe;
  83. const EVP_CIPHER *pbe_ciph = NULL;
  84. EVP_CIPHER *pbe_ciph_fetch = NULL;
  85. if ((p7 = PKCS7_new_ex(ctx, propq)) == NULL) {
  86. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  87. return NULL;
  88. }
  89. if (!PKCS7_set_type(p7, NID_pkcs7_encrypted)) {
  90. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE);
  91. goto err;
  92. }
  93. ERR_set_mark();
  94. pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq);
  95. if (pbe_ciph == NULL)
  96. pbe_ciph = EVP_get_cipherbynid(pbe_nid);
  97. ERR_pop_to_mark();
  98. if (pbe_ciph != NULL) {
  99. pbe = PKCS5_pbe2_set_iv_ex(pbe_ciph, iter, salt, saltlen, NULL, -1, ctx);
  100. } else {
  101. pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, ctx);
  102. }
  103. if (pbe == NULL) {
  104. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  105. goto err;
  106. }
  107. X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm);
  108. p7->d.encrypted->enc_data->algorithm = pbe;
  109. ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data);
  110. if (!(p7->d.encrypted->enc_data->enc_data =
  111. PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass,
  112. passlen, bags, 1, ctx, propq))) {
  113. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
  114. goto err;
  115. }
  116. EVP_CIPHER_free(pbe_ciph_fetch);
  117. return p7;
  118. err:
  119. PKCS7_free(p7);
  120. EVP_CIPHER_free(pbe_ciph_fetch);
  121. return NULL;
  122. }
  123. PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen,
  124. unsigned char *salt, int saltlen, int iter,
  125. STACK_OF(PKCS12_SAFEBAG) *bags)
  126. {
  127. return PKCS12_pack_p7encdata_ex(pbe_nid, pass, passlen, salt, saltlen,
  128. iter, bags, NULL, NULL);
  129. }
  130. STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass,
  131. int passlen)
  132. {
  133. if (!PKCS7_type_is_encrypted(p7))
  134. return NULL;
  135. return PKCS12_item_decrypt_d2i_ex(p7->d.encrypted->enc_data->algorithm,
  136. ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
  137. pass, passlen,
  138. p7->d.encrypted->enc_data->enc_data, 1,
  139. p7->ctx.libctx, p7->ctx.propq);
  140. }
  141. PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey_ex(const PKCS12_SAFEBAG *bag,
  142. const char *pass, int passlen,
  143. OSSL_LIB_CTX *ctx, const char *propq)
  144. {
  145. return PKCS8_decrypt_ex(bag->value.shkeybag, pass, passlen, ctx, propq);
  146. }
  147. PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag,
  148. const char *pass, int passlen)
  149. {
  150. return PKCS12_decrypt_skey_ex(bag, pass, passlen, NULL, NULL);
  151. }
  152. int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes)
  153. {
  154. if (ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
  155. &p12->authsafes->d.data))
  156. return 1;
  157. return 0;
  158. }
  159. STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12)
  160. {
  161. STACK_OF(PKCS7) *p7s;
  162. PKCS7_CTX *p7ctx;
  163. PKCS7 *p7;
  164. int i;
  165. if (!PKCS7_type_is_data(p12->authsafes)) {
  166. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
  167. return NULL;
  168. }
  169. p7ctx = &p12->authsafes->ctx;
  170. p7s = ASN1_item_unpack_ex(p12->authsafes->d.data,
  171. ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
  172. ossl_pkcs7_ctx_get0_libctx(p7ctx),
  173. ossl_pkcs7_ctx_get0_propq(p7ctx));
  174. if (p7s != NULL) {
  175. for (i = 0; i < sk_PKCS7_num(p7s); i++) {
  176. p7 = sk_PKCS7_value(p7s, i);
  177. if (!ossl_pkcs7_ctx_propagate(p12->authsafes, p7))
  178. goto err;
  179. }
  180. }
  181. return p7s;
  182. err:
  183. sk_PKCS7_free(p7s);
  184. return NULL;
  185. }