cms_enc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* crypto/cms/cms_enc.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. #include "cryptlib.h"
  55. #include <openssl/asn1t.h>
  56. #include <openssl/pem.h>
  57. #include <openssl/x509v3.h>
  58. #include <openssl/err.h>
  59. #include <openssl/cms.h>
  60. #include <openssl/rand.h>
  61. #include "cms_lcl.h"
  62. /* CMS EncryptedData Utilities */
  63. DECLARE_ASN1_ITEM(CMS_EncryptedData)
  64. /* Return BIO based on EncryptedContentInfo and key */
  65. BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
  66. {
  67. BIO *b;
  68. EVP_CIPHER_CTX *ctx;
  69. const EVP_CIPHER *ciph;
  70. X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
  71. unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
  72. unsigned char *tkey = NULL;
  73. size_t tkeylen = 0;
  74. int ok = 0;
  75. int enc, keep_key = 0;
  76. enc = ec->cipher ? 1 : 0;
  77. b = BIO_new(BIO_f_cipher());
  78. if (!b) {
  79. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  80. return NULL;
  81. }
  82. BIO_get_cipher_ctx(b, &ctx);
  83. if (enc) {
  84. ciph = ec->cipher;
  85. /*
  86. * If not keeping key set cipher to NULL so subsequent calls decrypt.
  87. */
  88. if (ec->key)
  89. ec->cipher = NULL;
  90. } else {
  91. ciph = EVP_get_cipherbyobj(calg->algorithm);
  92. if (!ciph) {
  93. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);
  94. goto err;
  95. }
  96. }
  97. if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) {
  98. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  99. CMS_R_CIPHER_INITIALISATION_ERROR);
  100. goto err;
  101. }
  102. if (enc) {
  103. int ivlen;
  104. calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
  105. /* Generate a random IV if we need one */
  106. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  107. if (ivlen > 0) {
  108. if (RAND_bytes(iv, ivlen) <= 0)
  109. goto err;
  110. piv = iv;
  111. }
  112. } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {
  113. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  114. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  115. goto err;
  116. }
  117. tkeylen = EVP_CIPHER_CTX_key_length(ctx);
  118. /* Generate random session key */
  119. if (!enc || !ec->key) {
  120. tkey = OPENSSL_malloc(tkeylen);
  121. if (!tkey) {
  122. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  123. goto err;
  124. }
  125. if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
  126. goto err;
  127. }
  128. if (!ec->key) {
  129. ec->key = tkey;
  130. ec->keylen = tkeylen;
  131. tkey = NULL;
  132. if (enc)
  133. keep_key = 1;
  134. else
  135. ERR_clear_error();
  136. }
  137. if (ec->keylen != tkeylen) {
  138. /* If necessary set key length */
  139. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
  140. /*
  141. * Only reveal failure if debugging so we don't leak information
  142. * which may be useful in MMA.
  143. */
  144. if (enc || ec->debug) {
  145. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  146. CMS_R_INVALID_KEY_LENGTH);
  147. goto err;
  148. } else {
  149. /* Use random key */
  150. OPENSSL_cleanse(ec->key, ec->keylen);
  151. OPENSSL_free(ec->key);
  152. ec->key = tkey;
  153. ec->keylen = tkeylen;
  154. tkey = NULL;
  155. ERR_clear_error();
  156. }
  157. }
  158. }
  159. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
  160. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  161. CMS_R_CIPHER_INITIALISATION_ERROR);
  162. goto err;
  163. }
  164. if (enc) {
  165. calg->parameter = ASN1_TYPE_new();
  166. if (calg->parameter == NULL) {
  167. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  168. goto err;
  169. }
  170. if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) {
  171. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  172. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  173. goto err;
  174. }
  175. /* If parameter type not set omit parameter */
  176. if (calg->parameter->type == V_ASN1_UNDEF) {
  177. ASN1_TYPE_free(calg->parameter);
  178. calg->parameter = NULL;
  179. }
  180. }
  181. ok = 1;
  182. err:
  183. if (ec->key && (!keep_key || !ok)) {
  184. OPENSSL_cleanse(ec->key, ec->keylen);
  185. OPENSSL_free(ec->key);
  186. ec->key = NULL;
  187. }
  188. if (tkey) {
  189. OPENSSL_cleanse(tkey, tkeylen);
  190. OPENSSL_free(tkey);
  191. }
  192. if (ok)
  193. return b;
  194. BIO_free(b);
  195. return NULL;
  196. }
  197. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  198. const EVP_CIPHER *cipher,
  199. const unsigned char *key, size_t keylen)
  200. {
  201. ec->cipher = cipher;
  202. if (key) {
  203. ec->key = OPENSSL_malloc(keylen);
  204. if (!ec->key)
  205. return 0;
  206. memcpy(ec->key, key, keylen);
  207. }
  208. ec->keylen = keylen;
  209. if (cipher)
  210. ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
  211. return 1;
  212. }
  213. int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
  214. const unsigned char *key, size_t keylen)
  215. {
  216. CMS_EncryptedContentInfo *ec;
  217. if (!key || !keylen) {
  218. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY);
  219. return 0;
  220. }
  221. if (ciph) {
  222. cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
  223. if (!cms->d.encryptedData) {
  224. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, ERR_R_MALLOC_FAILURE);
  225. return 0;
  226. }
  227. cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
  228. cms->d.encryptedData->version = 0;
  229. } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
  230. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NOT_ENCRYPTED_DATA);
  231. return 0;
  232. }
  233. ec = cms->d.encryptedData->encryptedContentInfo;
  234. return cms_EncryptedContent_init(ec, ciph, key, keylen);
  235. }
  236. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
  237. {
  238. CMS_EncryptedData *enc = cms->d.encryptedData;
  239. if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
  240. enc->version = 2;
  241. return cms_EncryptedContent_init_bio(enc->encryptedContentInfo);
  242. }