p12_add.c 6.3 KB

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