cms_pwri.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* crypto/cms/cms_pwri.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2009 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 <openssl/aes.h>
  61. #include "cms_lcl.h"
  62. #include "asn1_locl.h"
  63. int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
  64. unsigned char *pass, ossl_ssize_t passlen)
  65. {
  66. CMS_PasswordRecipientInfo *pwri;
  67. if (ri->type != CMS_RECIPINFO_PASS)
  68. {
  69. CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD, CMS_R_NOT_PWRI);
  70. return 0;
  71. }
  72. pwri = ri->d.pwri;
  73. pwri->pass = pass;
  74. if (pass && passlen < 0)
  75. passlen = strlen((char *)pass);
  76. pwri->passlen = passlen;
  77. return 1;
  78. }
  79. CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
  80. int iter, int wrap_nid, int pbe_nid,
  81. unsigned char *pass,
  82. ossl_ssize_t passlen,
  83. const EVP_CIPHER *kekciph)
  84. {
  85. CMS_RecipientInfo *ri = NULL;
  86. CMS_EnvelopedData *env;
  87. CMS_PasswordRecipientInfo *pwri;
  88. EVP_CIPHER_CTX ctx;
  89. X509_ALGOR *encalg = NULL;
  90. unsigned char iv[EVP_MAX_IV_LENGTH];
  91. int ivlen;
  92. env = cms_get0_enveloped(cms);
  93. if (!env)
  94. goto err;
  95. if (wrap_nid <= 0)
  96. wrap_nid = NID_id_alg_PWRI_KEK;
  97. if (pbe_nid <= 0)
  98. pbe_nid = NID_id_pbkdf2;
  99. /* Get from enveloped data */
  100. if (kekciph == NULL)
  101. kekciph = env->encryptedContentInfo->cipher;
  102. if (kekciph == NULL)
  103. {
  104. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER);
  105. return NULL;
  106. }
  107. if (wrap_nid != NID_id_alg_PWRI_KEK)
  108. {
  109. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
  110. CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
  111. return NULL;
  112. }
  113. /* Setup algorithm identifier for cipher */
  114. encalg = X509_ALGOR_new();
  115. EVP_CIPHER_CTX_init(&ctx);
  116. if (EVP_EncryptInit_ex(&ctx, kekciph, NULL, NULL, NULL) <= 0)
  117. {
  118. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
  119. goto err;
  120. }
  121. ivlen = EVP_CIPHER_CTX_iv_length(&ctx);
  122. if (ivlen > 0)
  123. {
  124. if (RAND_pseudo_bytes(iv, ivlen) <= 0)
  125. goto err;
  126. if (EVP_EncryptInit_ex(&ctx, NULL, NULL, NULL, iv) <= 0)
  127. {
  128. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
  129. ERR_R_EVP_LIB);
  130. goto err;
  131. }
  132. encalg->parameter = ASN1_TYPE_new();
  133. if (!encalg->parameter)
  134. {
  135. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
  136. ERR_R_MALLOC_FAILURE);
  137. goto err;
  138. }
  139. if (EVP_CIPHER_param_to_asn1(&ctx, encalg->parameter) <= 0)
  140. {
  141. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
  142. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  143. goto err;
  144. }
  145. }
  146. encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(&ctx));
  147. EVP_CIPHER_CTX_cleanup(&ctx);
  148. /* Initialize recipient info */
  149. ri = M_ASN1_new_of(CMS_RecipientInfo);
  150. if (!ri)
  151. goto merr;
  152. ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
  153. if (!ri->d.pwri)
  154. goto merr;
  155. ri->type = CMS_RECIPINFO_PASS;
  156. pwri = ri->d.pwri;
  157. /* Since this is overwritten, free up empty structure already there */
  158. X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
  159. pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
  160. if (!pwri->keyEncryptionAlgorithm)
  161. goto merr;
  162. pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
  163. pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
  164. if (!pwri->keyEncryptionAlgorithm->parameter)
  165. goto merr;
  166. if(!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
  167. &pwri->keyEncryptionAlgorithm->parameter->value.sequence))
  168. goto merr;
  169. pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
  170. X509_ALGOR_free(encalg);
  171. encalg = NULL;
  172. /* Setup PBE algorithm */
  173. pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1);
  174. if (!pwri->keyDerivationAlgorithm)
  175. goto err;
  176. CMS_RecipientInfo_set0_password(ri, pass, passlen);
  177. pwri->version = 0;
  178. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  179. goto merr;
  180. return ri;
  181. merr:
  182. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
  183. err:
  184. EVP_CIPHER_CTX_cleanup(&ctx);
  185. if (ri)
  186. M_ASN1_free_of(ri, CMS_RecipientInfo);
  187. if (encalg)
  188. X509_ALGOR_free(encalg);
  189. return NULL;
  190. }
  191. /* This is an implementation of the key wrapping mechanism in RFC3211,
  192. * at some point this should go into EVP.
  193. */
  194. static int kek_unwrap_key(unsigned char *out, size_t *outlen,
  195. const unsigned char *in, size_t inlen, EVP_CIPHER_CTX *ctx)
  196. {
  197. size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
  198. unsigned char *tmp;
  199. int outl, rv = 0;
  200. if (inlen < 2 * blocklen)
  201. {
  202. /* too small */
  203. return 0;
  204. }
  205. if (inlen % blocklen)
  206. {
  207. /* Invalid size */
  208. return 0;
  209. }
  210. tmp = OPENSSL_malloc(inlen);
  211. /* setup IV by decrypting last two blocks */
  212. EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
  213. in + inlen - 2 * blocklen, blocklen * 2);
  214. /* Do a decrypt of last decrypted block to set IV to correct value
  215. * output it to start of buffer so we don't corrupt decrypted block
  216. * this works because buffer is at least two block lengths long.
  217. */
  218. EVP_DecryptUpdate(ctx, tmp, &outl,
  219. tmp + inlen - blocklen, blocklen);
  220. /* Can now decrypt first n - 1 blocks */
  221. EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen);
  222. /* Reset IV to original value */
  223. EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);
  224. /* Decrypt again */
  225. EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen);
  226. /* Check check bytes */
  227. if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff)
  228. {
  229. /* Check byte failure */
  230. goto err;
  231. }
  232. if (inlen < (size_t)(tmp[0] - 4 ))
  233. {
  234. /* Invalid length value */
  235. goto err;
  236. }
  237. *outlen = (size_t)tmp[0];
  238. memcpy(out, tmp + 4, *outlen);
  239. rv = 1;
  240. err:
  241. OPENSSL_cleanse(tmp, inlen);
  242. OPENSSL_free(tmp);
  243. return rv;
  244. }
  245. static int kek_wrap_key(unsigned char *out, size_t *outlen,
  246. const unsigned char *in, size_t inlen, EVP_CIPHER_CTX *ctx)
  247. {
  248. size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
  249. size_t olen;
  250. int dummy;
  251. /* First decide length of output buffer: need header and round up to
  252. * multiple of block length.
  253. */
  254. olen = (inlen + 4 + blocklen - 1)/blocklen;
  255. olen *= blocklen;
  256. if (olen < 2 * blocklen)
  257. {
  258. /* Key too small */
  259. return 0;
  260. }
  261. if (inlen > 0xFF)
  262. {
  263. /* Key too large */
  264. return 0;
  265. }
  266. if (out)
  267. {
  268. /* Set header */
  269. out[0] = (unsigned char)inlen;
  270. out[1] = in[0] ^ 0xFF;
  271. out[2] = in[1] ^ 0xFF;
  272. out[3] = in[2] ^ 0xFF;
  273. memcpy(out + 4, in, inlen);
  274. /* Add random padding to end */
  275. if (olen > inlen + 4)
  276. RAND_pseudo_bytes(out + 4 + inlen, olen - 4 - inlen);
  277. /* Encrypt twice */
  278. EVP_EncryptUpdate(ctx, out, &dummy, out, olen);
  279. EVP_EncryptUpdate(ctx, out, &dummy, out, olen);
  280. }
  281. *outlen = olen;
  282. return 1;
  283. }
  284. /* Encrypt/Decrypt content key in PWRI recipient info */
  285. int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
  286. int en_de)
  287. {
  288. CMS_EncryptedContentInfo *ec;
  289. CMS_PasswordRecipientInfo *pwri;
  290. const unsigned char *p = NULL;
  291. int plen;
  292. int r = 0;
  293. X509_ALGOR *algtmp, *kekalg = NULL;
  294. EVP_CIPHER_CTX kekctx;
  295. const EVP_CIPHER *kekcipher;
  296. unsigned char *key = NULL;
  297. size_t keylen;
  298. ec = cms->d.envelopedData->encryptedContentInfo;
  299. pwri = ri->d.pwri;
  300. EVP_CIPHER_CTX_init(&kekctx);
  301. if (!pwri->pass)
  302. {
  303. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);
  304. return 0;
  305. }
  306. algtmp = pwri->keyEncryptionAlgorithm;
  307. if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK)
  308. {
  309. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  310. CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
  311. return 0;
  312. }
  313. if (algtmp->parameter->type == V_ASN1_SEQUENCE)
  314. {
  315. p = algtmp->parameter->value.sequence->data;
  316. plen = algtmp->parameter->value.sequence->length;
  317. kekalg = d2i_X509_ALGOR(NULL, &p, plen);
  318. }
  319. if (kekalg == NULL)
  320. {
  321. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  322. CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
  323. return 0;
  324. }
  325. kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
  326. if(!kekcipher)
  327. {
  328. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  329. CMS_R_UNKNOWN_CIPHER);
  330. goto err;
  331. }
  332. /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
  333. if (!EVP_CipherInit_ex(&kekctx, kekcipher, NULL, NULL, NULL, en_de))
  334. goto err;
  335. EVP_CIPHER_CTX_set_padding(&kekctx, 0);
  336. if(EVP_CIPHER_asn1_to_param(&kekctx, kekalg->parameter) < 0)
  337. {
  338. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  339. CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
  340. goto err;
  341. }
  342. algtmp = pwri->keyDerivationAlgorithm;
  343. /* Finish password based key derivation to setup key in "ctx" */
  344. if (EVP_PBE_CipherInit(algtmp->algorithm,
  345. (char *)pwri->pass, pwri->passlen,
  346. algtmp->parameter, &kekctx, en_de) < 0)
  347. {
  348. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);
  349. goto err;
  350. }
  351. /* Finally wrap/unwrap the key */
  352. if (en_de)
  353. {
  354. if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, &kekctx))
  355. goto err;
  356. key = OPENSSL_malloc(keylen);
  357. if (!key)
  358. goto err;
  359. if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, &kekctx))
  360. goto err;
  361. pwri->encryptedKey->data = key;
  362. pwri->encryptedKey->length = keylen;
  363. }
  364. else
  365. {
  366. key = OPENSSL_malloc(pwri->encryptedKey->length);
  367. if (!key)
  368. {
  369. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  370. ERR_R_MALLOC_FAILURE);
  371. goto err;
  372. }
  373. if (!kek_unwrap_key(key, &keylen,
  374. pwri->encryptedKey->data,
  375. pwri->encryptedKey->length, &kekctx))
  376. {
  377. CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
  378. CMS_R_UNWRAP_FAILURE);
  379. goto err;
  380. }
  381. ec->key = key;
  382. ec->keylen = keylen;
  383. }
  384. r = 1;
  385. err:
  386. EVP_CIPHER_CTX_cleanup(&kekctx);
  387. if (!r && key)
  388. OPENSSL_free(key);
  389. X509_ALGOR_free(kekalg);
  390. return r;
  391. }