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