cms_enc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* crypto/cms/cms_enc.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2008 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. #include "cryptlib.h"
  54. #include <openssl/asn1t.h>
  55. #include <openssl/pem.h>
  56. #include <openssl/x509v3.h>
  57. #include <openssl/err.h>
  58. #include <openssl/cms.h>
  59. #include <openssl/rand.h>
  60. #include "cms_lcl.h"
  61. /* CMS EncryptedData Utilities */
  62. DECLARE_ASN1_ITEM(CMS_EncryptedData)
  63. /* Return BIO based on EncryptedContentInfo and key */
  64. BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
  65. {
  66. BIO *b;
  67. EVP_CIPHER_CTX *ctx;
  68. const EVP_CIPHER *ciph;
  69. X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
  70. unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
  71. int ok = 0;
  72. int enc, keep_key = 0;
  73. enc = ec->cipher ? 1 : 0;
  74. b = BIO_new(BIO_f_cipher());
  75. if (!b)
  76. {
  77. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  78. ERR_R_MALLOC_FAILURE);
  79. return NULL;
  80. }
  81. BIO_get_cipher_ctx(b, &ctx);
  82. if (enc)
  83. {
  84. ciph = ec->cipher;
  85. /* If not keeping key set cipher to NULL so subsequent calls
  86. * decrypt.
  87. */
  88. if (ec->key)
  89. ec->cipher = NULL;
  90. }
  91. else
  92. {
  93. ciph = EVP_get_cipherbyobj(calg->algorithm);
  94. if (!ciph)
  95. {
  96. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  97. CMS_R_UNKNOWN_CIPHER);
  98. goto err;
  99. }
  100. }
  101. if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0)
  102. {
  103. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  104. CMS_R_CIPHER_INITIALISATION_ERROR);
  105. goto err;
  106. }
  107. if (enc)
  108. {
  109. int ivlen;
  110. calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
  111. /* Generate a random IV if we need one */
  112. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  113. if (ivlen > 0)
  114. {
  115. if (RAND_pseudo_bytes(iv, ivlen) <= 0)
  116. goto err;
  117. piv = iv;
  118. }
  119. }
  120. else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0)
  121. {
  122. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  123. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  124. goto err;
  125. }
  126. if (enc && !ec->key)
  127. {
  128. /* Generate random key */
  129. if (!ec->keylen)
  130. ec->keylen = EVP_CIPHER_CTX_key_length(ctx);
  131. ec->key = OPENSSL_malloc(ec->keylen);
  132. if (!ec->key)
  133. {
  134. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  135. ERR_R_MALLOC_FAILURE);
  136. goto err;
  137. }
  138. if (EVP_CIPHER_CTX_rand_key(ctx, ec->key) <= 0)
  139. goto err;
  140. keep_key = 1;
  141. }
  142. else if (ec->keylen != (unsigned int)EVP_CIPHER_CTX_key_length(ctx))
  143. {
  144. /* If necessary set key length */
  145. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0)
  146. {
  147. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  148. CMS_R_INVALID_KEY_LENGTH);
  149. goto err;
  150. }
  151. }
  152. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0)
  153. {
  154. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  155. CMS_R_CIPHER_INITIALISATION_ERROR);
  156. goto err;
  157. }
  158. if (piv)
  159. {
  160. calg->parameter = ASN1_TYPE_new();
  161. if (!calg->parameter)
  162. {
  163. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  164. ERR_R_MALLOC_FAILURE);
  165. goto err;
  166. }
  167. if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0)
  168. {
  169. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  170. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  171. goto err;
  172. }
  173. }
  174. ok = 1;
  175. err:
  176. if (ec->key && !keep_key)
  177. {
  178. OPENSSL_cleanse(ec->key, ec->keylen);
  179. OPENSSL_free(ec->key);
  180. ec->key = NULL;
  181. }
  182. if (ok)
  183. return b;
  184. BIO_free(b);
  185. return NULL;
  186. }
  187. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  188. const EVP_CIPHER *cipher,
  189. const unsigned char *key, size_t keylen)
  190. {
  191. ec->cipher = cipher;
  192. if (key)
  193. {
  194. ec->key = OPENSSL_malloc(keylen);
  195. if (!ec->key)
  196. return 0;
  197. memcpy(ec->key, key, keylen);
  198. }
  199. ec->keylen = keylen;
  200. if (cipher)
  201. ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
  202. return 1;
  203. }
  204. int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
  205. const unsigned char *key, size_t keylen)
  206. {
  207. CMS_EncryptedContentInfo *ec;
  208. if (!key || !keylen)
  209. {
  210. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY);
  211. return 0;
  212. }
  213. if (ciph)
  214. {
  215. cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
  216. if (!cms->d.encryptedData)
  217. {
  218. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY,
  219. ERR_R_MALLOC_FAILURE);
  220. return 0;
  221. }
  222. cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
  223. cms->d.encryptedData->version = 0;
  224. }
  225. else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted)
  226. {
  227. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY,
  228. CMS_R_NOT_ENCRYPTED_DATA);
  229. return 0;
  230. }
  231. ec = cms->d.encryptedData->encryptedContentInfo;
  232. return cms_EncryptedContent_init(ec, ciph, key, keylen);
  233. }
  234. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
  235. {
  236. CMS_EncryptedData *enc = cms->d.encryptedData;
  237. if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
  238. enc->version = 2;
  239. return cms_EncryptedContent_init_bio(enc->encryptedContentInfo);
  240. }