cms_enc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "internal/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. /* 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. unsigned char *tkey = NULL;
  72. size_t tkeylen = 0;
  73. int ok = 0;
  74. int enc, keep_key = 0;
  75. enc = ec->cipher ? 1 : 0;
  76. b = BIO_new(BIO_f_cipher());
  77. if (b == NULL) {
  78. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  79. return NULL;
  80. }
  81. BIO_get_cipher_ctx(b, &ctx);
  82. if (enc) {
  83. ciph = ec->cipher;
  84. /*
  85. * If not keeping key set cipher to NULL so subsequent calls decrypt.
  86. */
  87. if (ec->key)
  88. ec->cipher = NULL;
  89. } else {
  90. ciph = EVP_get_cipherbyobj(calg->algorithm);
  91. if (!ciph) {
  92. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);
  93. goto err;
  94. }
  95. }
  96. if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) {
  97. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  98. CMS_R_CIPHER_INITIALISATION_ERROR);
  99. goto err;
  100. }
  101. if (enc) {
  102. int ivlen;
  103. calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
  104. /* Generate a random IV if we need one */
  105. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  106. if (ivlen > 0) {
  107. if (RAND_bytes(iv, ivlen) <= 0)
  108. goto err;
  109. piv = iv;
  110. }
  111. } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {
  112. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  113. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  114. goto err;
  115. }
  116. tkeylen = EVP_CIPHER_CTX_key_length(ctx);
  117. /* Generate random session key */
  118. if (!enc || !ec->key) {
  119. tkey = OPENSSL_malloc(tkeylen);
  120. if (tkey == NULL) {
  121. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  122. goto err;
  123. }
  124. if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
  125. goto err;
  126. }
  127. if (!ec->key) {
  128. ec->key = tkey;
  129. ec->keylen = tkeylen;
  130. tkey = NULL;
  131. if (enc)
  132. keep_key = 1;
  133. else
  134. ERR_clear_error();
  135. }
  136. if (ec->keylen != tkeylen) {
  137. /* If necessary set key length */
  138. if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
  139. /*
  140. * Only reveal failure if debugging so we don't leak information
  141. * which may be useful in MMA.
  142. */
  143. if (enc || ec->debug) {
  144. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  145. CMS_R_INVALID_KEY_LENGTH);
  146. goto err;
  147. } else {
  148. /* Use random key */
  149. OPENSSL_clear_free(ec->key, ec->keylen);
  150. ec->key = tkey;
  151. ec->keylen = tkeylen;
  152. tkey = NULL;
  153. ERR_clear_error();
  154. }
  155. }
  156. }
  157. if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
  158. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
  159. CMS_R_CIPHER_INITIALISATION_ERROR);
  160. goto err;
  161. }
  162. if (piv) {
  163. calg->parameter = ASN1_TYPE_new();
  164. if (calg->parameter == NULL) {
  165. CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
  166. goto err;
  167. }
  168. if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) {
  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 (!keep_key || !ok) {
  177. OPENSSL_clear_free(ec->key, ec->keylen);
  178. ec->key = NULL;
  179. }
  180. OPENSSL_clear_free(tkey, tkeylen);
  181. if (ok)
  182. return b;
  183. BIO_free(b);
  184. return NULL;
  185. }
  186. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  187. const EVP_CIPHER *cipher,
  188. const unsigned char *key, size_t keylen)
  189. {
  190. ec->cipher = cipher;
  191. if (key) {
  192. ec->key = OPENSSL_malloc(keylen);
  193. if (ec->key == NULL)
  194. return 0;
  195. memcpy(ec->key, key, keylen);
  196. }
  197. ec->keylen = keylen;
  198. if (cipher)
  199. ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
  200. return 1;
  201. }
  202. int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
  203. const unsigned char *key, size_t keylen)
  204. {
  205. CMS_EncryptedContentInfo *ec;
  206. if (!key || !keylen) {
  207. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY);
  208. return 0;
  209. }
  210. if (ciph) {
  211. cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
  212. if (!cms->d.encryptedData) {
  213. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, ERR_R_MALLOC_FAILURE);
  214. return 0;
  215. }
  216. cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
  217. cms->d.encryptedData->version = 0;
  218. } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
  219. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NOT_ENCRYPTED_DATA);
  220. return 0;
  221. }
  222. ec = cms->d.encryptedData->encryptedContentInfo;
  223. return cms_EncryptedContent_init(ec, ciph, key, keylen);
  224. }
  225. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
  226. {
  227. CMS_EncryptedData *enc = cms->d.encryptedData;
  228. if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
  229. enc->version = 2;
  230. return cms_EncryptedContent_init_bio(enc->encryptedContentInfo);
  231. }