cms_kari.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* crypto/cms/cms_kari.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2013 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 <openssl/aes.h>
  62. #include "cms_lcl.h"
  63. #include "asn1_locl.h"
  64. DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
  65. DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
  66. DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
  67. DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
  68. /* Key Agreement Recipient Info (KARI) routines */
  69. int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
  70. X509_ALGOR **palg,
  71. ASN1_OCTET_STRING **pukm)
  72. {
  73. if (ri->type != CMS_RECIPINFO_AGREE) {
  74. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG,
  75. CMS_R_NOT_KEY_AGREEMENT);
  76. return 0;
  77. }
  78. if (palg)
  79. *palg = ri->d.kari->keyEncryptionAlgorithm;
  80. if (pukm)
  81. *pukm = ri->d.kari->ukm;
  82. return 1;
  83. }
  84. /* Retrieve recipient encrypted keys from a kari */
  85. STACK_OF(CMS_RecipientEncryptedKey)
  86. *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri)
  87. {
  88. if (ri->type != CMS_RECIPINFO_AGREE) {
  89. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS,
  90. CMS_R_NOT_KEY_AGREEMENT);
  91. return NULL;
  92. }
  93. return ri->d.kari->recipientEncryptedKeys;
  94. }
  95. int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
  96. X509_ALGOR **pubalg,
  97. ASN1_BIT_STRING **pubkey,
  98. ASN1_OCTET_STRING **keyid,
  99. X509_NAME **issuer,
  100. ASN1_INTEGER **sno)
  101. {
  102. CMS_OriginatorIdentifierOrKey *oik;
  103. if (ri->type != CMS_RECIPINFO_AGREE) {
  104. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID,
  105. CMS_R_NOT_KEY_AGREEMENT);
  106. return 0;
  107. }
  108. oik = ri->d.kari->originator;
  109. if (issuer)
  110. *issuer = NULL;
  111. if (sno)
  112. *sno = NULL;
  113. if (keyid)
  114. *keyid = NULL;
  115. if (pubalg)
  116. *pubalg = NULL;
  117. if (pubkey)
  118. *pubkey = NULL;
  119. if (oik->type == CMS_OIK_ISSUER_SERIAL) {
  120. if (issuer)
  121. *issuer = oik->d.issuerAndSerialNumber->issuer;
  122. if (sno)
  123. *sno = oik->d.issuerAndSerialNumber->serialNumber;
  124. } else if (oik->type == CMS_OIK_KEYIDENTIFIER) {
  125. if (keyid)
  126. *keyid = oik->d.subjectKeyIdentifier;
  127. } else if (oik->type == CMS_OIK_PUBKEY) {
  128. if (pubalg)
  129. *pubalg = oik->d.originatorKey->algorithm;
  130. if (pubkey)
  131. *pubkey = oik->d.originatorKey->publicKey;
  132. } else
  133. return 0;
  134. return 1;
  135. }
  136. int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert)
  137. {
  138. CMS_OriginatorIdentifierOrKey *oik;
  139. if (ri->type != CMS_RECIPINFO_AGREE) {
  140. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP,
  141. CMS_R_NOT_KEY_AGREEMENT);
  142. return -2;
  143. }
  144. oik = ri->d.kari->originator;
  145. if (oik->type == CMS_OIK_ISSUER_SERIAL)
  146. return cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert);
  147. else if (oik->type == CMS_OIK_KEYIDENTIFIER)
  148. return cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert);
  149. return -1;
  150. }
  151. int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek,
  152. ASN1_OCTET_STRING **keyid,
  153. ASN1_GENERALIZEDTIME **tm,
  154. CMS_OtherKeyAttribute **other,
  155. X509_NAME **issuer, ASN1_INTEGER **sno)
  156. {
  157. CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
  158. if (rid->type == CMS_REK_ISSUER_SERIAL) {
  159. if (issuer)
  160. *issuer = rid->d.issuerAndSerialNumber->issuer;
  161. if (sno)
  162. *sno = rid->d.issuerAndSerialNumber->serialNumber;
  163. if (keyid)
  164. *keyid = NULL;
  165. if (tm)
  166. *tm = NULL;
  167. if (other)
  168. *other = NULL;
  169. } else if (rid->type == CMS_REK_KEYIDENTIFIER) {
  170. if (keyid)
  171. *keyid = rid->d.rKeyId->subjectKeyIdentifier;
  172. if (tm)
  173. *tm = rid->d.rKeyId->date;
  174. if (other)
  175. *other = rid->d.rKeyId->other;
  176. if (issuer)
  177. *issuer = NULL;
  178. if (sno)
  179. *sno = NULL;
  180. } else
  181. return 0;
  182. return 1;
  183. }
  184. int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek,
  185. X509 *cert)
  186. {
  187. CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
  188. if (rid->type == CMS_REK_ISSUER_SERIAL)
  189. return cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert);
  190. else if (rid->type == CMS_REK_KEYIDENTIFIER)
  191. return cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, cert);
  192. else
  193. return -1;
  194. }
  195. int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
  196. {
  197. EVP_PKEY_CTX *pctx;
  198. CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
  199. if (kari->pctx) {
  200. EVP_PKEY_CTX_free(kari->pctx);
  201. kari->pctx = NULL;
  202. }
  203. if (!pk)
  204. return 1;
  205. pctx = EVP_PKEY_CTX_new(pk, NULL);
  206. if (!pctx || !EVP_PKEY_derive_init(pctx))
  207. goto err;
  208. kari->pctx = pctx;
  209. return 1;
  210. err:
  211. if (pctx)
  212. EVP_PKEY_CTX_free(pctx);
  213. return 0;
  214. }
  215. EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
  216. {
  217. if (ri->type == CMS_RECIPINFO_AGREE)
  218. return &ri->d.kari->ctx;
  219. return NULL;
  220. }
  221. /*
  222. * Derive KEK and decrypt/encrypt with it to produce either the original CEK
  223. * or the encrypted CEK.
  224. */
  225. static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
  226. const unsigned char *in, size_t inlen,
  227. CMS_KeyAgreeRecipientInfo *kari, int enc)
  228. {
  229. /* Key encryption key */
  230. unsigned char kek[EVP_MAX_KEY_LENGTH];
  231. size_t keklen;
  232. int rv = 0;
  233. unsigned char *out = NULL;
  234. int outlen;
  235. keklen = EVP_CIPHER_CTX_key_length(&kari->ctx);
  236. if (keklen > EVP_MAX_KEY_LENGTH)
  237. return 0;
  238. /* Derive KEK */
  239. if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
  240. goto err;
  241. /* Set KEK in context */
  242. if (!EVP_CipherInit_ex(&kari->ctx, NULL, NULL, kek, NULL, enc))
  243. goto err;
  244. /* obtain output length of ciphered key */
  245. if (!EVP_CipherUpdate(&kari->ctx, NULL, &outlen, in, inlen))
  246. goto err;
  247. out = OPENSSL_malloc(outlen);
  248. if (!out)
  249. goto err;
  250. if (!EVP_CipherUpdate(&kari->ctx, out, &outlen, in, inlen))
  251. goto err;
  252. *pout = out;
  253. *poutlen = (size_t)outlen;
  254. rv = 1;
  255. err:
  256. OPENSSL_cleanse(kek, keklen);
  257. if (!rv && out)
  258. OPENSSL_free(out);
  259. EVP_CIPHER_CTX_cleanup(&kari->ctx);
  260. EVP_PKEY_CTX_free(kari->pctx);
  261. kari->pctx = NULL;
  262. return rv;
  263. }
  264. int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
  265. CMS_RecipientInfo *ri,
  266. CMS_RecipientEncryptedKey *rek)
  267. {
  268. int rv = 0;
  269. unsigned char *enckey = NULL, *cek = NULL;
  270. size_t enckeylen;
  271. size_t ceklen;
  272. CMS_EncryptedContentInfo *ec;
  273. enckeylen = rek->encryptedKey->length;
  274. enckey = rek->encryptedKey->data;
  275. /* Setup all parameters to derive KEK */
  276. if (!cms_env_asn1_ctrl(ri, 1))
  277. goto err;
  278. /* Attempt to decrypt CEK */
  279. if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
  280. goto err;
  281. ec = cms->d.envelopedData->encryptedContentInfo;
  282. if (ec->key) {
  283. OPENSSL_cleanse(ec->key, ec->keylen);
  284. OPENSSL_free(ec->key);
  285. }
  286. ec->key = cek;
  287. ec->keylen = ceklen;
  288. cek = NULL;
  289. rv = 1;
  290. err:
  291. if (cek)
  292. OPENSSL_free(cek);
  293. return rv;
  294. }
  295. /* Create ephemeral key and initialise context based on it */
  296. static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
  297. EVP_PKEY *pk)
  298. {
  299. EVP_PKEY_CTX *pctx = NULL;
  300. EVP_PKEY *ekey = NULL;
  301. int rv = 0;
  302. pctx = EVP_PKEY_CTX_new(pk, NULL);
  303. if (!pctx)
  304. goto err;
  305. if (EVP_PKEY_keygen_init(pctx) <= 0)
  306. goto err;
  307. if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
  308. goto err;
  309. EVP_PKEY_CTX_free(pctx);
  310. pctx = EVP_PKEY_CTX_new(ekey, NULL);
  311. if (!pctx)
  312. goto err;
  313. if (EVP_PKEY_derive_init(pctx) <= 0)
  314. goto err;
  315. kari->pctx = pctx;
  316. rv = 1;
  317. err:
  318. if (!rv && pctx)
  319. EVP_PKEY_CTX_free(pctx);
  320. if (ekey)
  321. EVP_PKEY_free(ekey);
  322. return rv;
  323. }
  324. /* Initialise a ktri based on passed certificate and key */
  325. int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
  326. EVP_PKEY *pk, unsigned int flags)
  327. {
  328. CMS_KeyAgreeRecipientInfo *kari;
  329. CMS_RecipientEncryptedKey *rek = NULL;
  330. ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
  331. if (!ri->d.kari)
  332. return 0;
  333. ri->type = CMS_RECIPINFO_AGREE;
  334. kari = ri->d.kari;
  335. kari->version = 3;
  336. rek = M_ASN1_new_of(CMS_RecipientEncryptedKey);
  337. if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) {
  338. M_ASN1_free_of(rek, CMS_RecipientEncryptedKey);
  339. return 0;
  340. }
  341. if (flags & CMS_USE_KEYID) {
  342. rek->rid->type = CMS_REK_KEYIDENTIFIER;
  343. rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier);
  344. if (rek->rid->d.rKeyId == NULL)
  345. return 0;
  346. if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
  347. return 0;
  348. } else {
  349. rek->rid->type = CMS_REK_ISSUER_SERIAL;
  350. if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip))
  351. return 0;
  352. }
  353. /* Create ephemeral key */
  354. if (!cms_kari_create_ephemeral_key(kari, pk))
  355. return 0;
  356. CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
  357. rek->pkey = pk;
  358. return 1;
  359. }
  360. static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
  361. const EVP_CIPHER *cipher)
  362. {
  363. EVP_CIPHER_CTX *ctx = &kari->ctx;
  364. const EVP_CIPHER *kekcipher;
  365. int keylen = EVP_CIPHER_key_length(cipher);
  366. /* If a suitable wrap algorithm is already set nothing to do */
  367. kekcipher = EVP_CIPHER_CTX_cipher(ctx);
  368. if (kekcipher) {
  369. if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE)
  370. return 0;
  371. return 1;
  372. }
  373. /*
  374. * Pick a cipher based on content encryption cipher. If it is DES3 use
  375. * DES3 wrap otherwise use AES wrap similar to key size.
  376. */
  377. #ifndef OPENSSL_NO_DES
  378. if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc)
  379. kekcipher = EVP_des_ede3_wrap();
  380. else
  381. #endif
  382. if (keylen <= 16)
  383. kekcipher = EVP_aes_128_wrap();
  384. else if (keylen <= 24)
  385. kekcipher = EVP_aes_192_wrap();
  386. else
  387. kekcipher = EVP_aes_256_wrap();
  388. return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
  389. }
  390. /* Encrypt content key in key agreement recipient info */
  391. int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
  392. CMS_RecipientInfo *ri)
  393. {
  394. CMS_KeyAgreeRecipientInfo *kari;
  395. CMS_EncryptedContentInfo *ec;
  396. CMS_RecipientEncryptedKey *rek;
  397. STACK_OF(CMS_RecipientEncryptedKey) *reks;
  398. int i;
  399. if (ri->type != CMS_RECIPINFO_AGREE) {
  400. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT);
  401. return 0;
  402. }
  403. kari = ri->d.kari;
  404. reks = kari->recipientEncryptedKeys;
  405. ec = cms->d.envelopedData->encryptedContentInfo;
  406. /* Initialise wrap algorithm parameters */
  407. if (!cms_wrap_init(kari, ec->cipher))
  408. return 0;
  409. /*
  410. * If no orignator key set up initialise for ephemeral key the public key
  411. * ASN1 structure will set the actual public key value.
  412. */
  413. if (kari->originator->type == -1) {
  414. CMS_OriginatorIdentifierOrKey *oik = kari->originator;
  415. oik->type = CMS_OIK_PUBKEY;
  416. oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
  417. if (!oik->d.originatorKey)
  418. return 0;
  419. }
  420. /* Initialise KDF algorithm */
  421. if (!cms_env_asn1_ctrl(ri, 0))
  422. return 0;
  423. /* For each rek, derive KEK, encrypt CEK */
  424. for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
  425. unsigned char *enckey;
  426. size_t enckeylen;
  427. rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
  428. if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
  429. return 0;
  430. if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
  431. kari, 1))
  432. return 0;
  433. ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);
  434. }
  435. return 1;
  436. }