Initial support for enveloped data decrypt. Extent runex.pl to cover these
[openssl.git] / crypto / cms / cms_env.c
1 /* crypto/cms/cms_env.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2008 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
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 #include "asn1_locl.h"
63
64 /* CMS EnvelopedData Utilities */
65
66 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
67 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
68 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
69
70 DECLARE_STACK_OF(CMS_RecipientInfo)
71
72 static CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
73         {
74         if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped)
75                 {
76                 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
77                                 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
78                 return NULL;
79                 }
80         return cms->d.envelopedData;
81         }
82
83 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
84         {
85         if (cms->d.other == NULL)
86                 {
87                 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
88                 if (!cms->d.envelopedData)
89                         {
90                         CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT,
91                                                         ERR_R_MALLOC_FAILURE);
92                         return NULL;
93                         }
94                 cms->d.envelopedData->version = 0;
95                 cms->d.envelopedData->encryptedContentInfo->contentType =
96                                                 OBJ_nid2obj(NID_pkcs7_data);
97                 ASN1_OBJECT_free(cms->contentType);
98                 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
99                 return cms->d.envelopedData;
100                 }
101         return cms_get0_enveloped(cms);
102         }
103
104 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
105         {
106         CMS_EnvelopedData *env;
107         env = cms_enveloped_data_init(cms);
108         if (!env)
109                 return NULL;
110         return env->recipientInfos;
111         }
112
113 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
114         {
115         return ri->type;
116         }
117
118 /* Add a recipient certificate. For now only handle key transport.
119  * If we ever handle key agreement will need updating.
120  */
121
122 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
123                                         X509 *recip, unsigned int flags)
124         {
125         CMS_RecipientInfo *ri = NULL;
126         CMS_KeyTransRecipientInfo *ktri;
127         CMS_EnvelopedData *env;
128         EVP_PKEY *pk = NULL;
129         int i, type;
130         /* Init enveloped data */
131         env = cms_enveloped_data_init(cms);
132         if (!env)
133                 goto err;
134
135         /* Initialized recipient info */
136         ri = M_ASN1_new_of(CMS_RecipientInfo);
137         if (!ri)
138                 goto merr;
139
140         /* Initialize and add key transrport recipient info */
141
142         ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
143         if (!ri->d.ktri)
144                 goto merr;
145         ri->type = CMS_RECIPINFO_TRANS;
146
147         ktri = ri->d.ktri;
148
149         X509_check_purpose(recip, -1, -1);
150         pk = X509_get_pubkey(recip);
151         if (!pk)
152                 {
153                 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
154                                 CMS_R_ERROR_GETTING_PUBLIC_KEY);
155                 goto err;
156                 }
157         CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
158         CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
159         ktri->pkey = pk;
160         ktri->recip = recip;
161
162         if (flags & CMS_USE_KEYID)
163                 {
164                 ktri->version = 2;
165                 type = CMS_RECIPINFO_KEYIDENTIFIER;
166                 }
167         else
168                 {
169                 ktri->version = 0;
170                 type = CMS_RECIPINFO_ISSUER_SERIAL;
171                 }
172
173         /* Not a typo: RecipientIdentifier and SignerIdentifier are the
174          * same structure.
175          */
176
177         if (!cms_set1_SignerIdentifier(ktri->rid, recip, type))
178                 goto err;
179
180         if (pk->ameth && pk->ameth->pkey_ctrl)
181                 {
182                 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_ENVELOPE,
183                                                 0, ri);
184                 if (i == -2)
185                         {
186                         CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
187                                 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
188                         goto err;
189                         }
190                 if (i <= 0)
191                         {
192                         CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
193                                 CMS_R_CTRL_FAILURE);
194                         goto err;
195                         }
196                 }
197
198         if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
199                 goto merr;
200
201         return ri;
202
203         merr:
204         CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
205         err:
206         if (ri)
207                 M_ASN1_free_of(ri, CMS_RecipientInfo);
208         return NULL;
209
210         }
211
212 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
213                                         EVP_PKEY **pk, X509 **recip,
214                                         X509_ALGOR **palg)
215         {
216         CMS_KeyTransRecipientInfo *ktri;
217         if (ri->type != CMS_RECIPINFO_TRANS)
218                 {
219                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
220                         CMS_R_NOT_KEY_TRANSPORT);
221                 return 0;
222                 }
223
224         ktri = ri->d.ktri;
225
226         if (pk)
227                 *pk = ktri->pkey;
228         if (recip)
229                 *recip = ktri->recip;
230         if (palg)
231                 *palg = ktri->keyEncryptionAlgorithm;
232         return 1;
233         }
234
235 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
236                                         ASN1_OCTET_STRING **keyid,
237                                         X509_NAME **issuer, ASN1_INTEGER **sno)
238         {
239         CMS_KeyTransRecipientInfo *ktri;
240         if (ri->type != CMS_RECIPINFO_TRANS)
241                 {
242                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
243                         CMS_R_NOT_KEY_TRANSPORT);
244                 return 0;
245                 }
246         ktri = ri->d.ktri;
247
248         return cms_SignerIdentifier_get0_signer_id(ktri->rid,
249                                                         keyid, issuer, sno);
250         }
251
252 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
253         {
254         if (ri->type != CMS_RECIPINFO_TRANS)
255                 {
256                 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
257                         CMS_R_NOT_KEY_TRANSPORT);
258                 return -2;
259                 }
260
261         return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
262         }
263
264 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
265                                EVP_PKEY *pkey)
266         {
267         CMS_KeyTransRecipientInfo *ktri;
268         EVP_PKEY_CTX *pctx = NULL;
269         unsigned char *ek = NULL;
270         size_t eklen;
271
272         int ret = 0;
273
274         if (ri->type != CMS_RECIPINFO_TRANS)
275                 {
276                 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
277                         CMS_R_NOT_KEY_TRANSPORT);
278                 return 0;
279                 }
280         ktri = ri->d.ktri;
281
282         pctx = EVP_PKEY_CTX_new(pkey, NULL);
283         if (!pctx)
284                 return 0;
285
286         if (EVP_PKEY_decrypt_init(pctx) <= 0)
287                 goto err;
288
289         if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
290                                 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0)
291                 {
292                 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, CMS_R_CTRL_ERROR);
293                 goto err;
294                 }
295
296         if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
297                                 ktri->encryptedKey->data,
298                                 ktri->encryptedKey->length) <= 0)
299                 goto err;
300
301         ek = OPENSSL_malloc(eklen);
302
303         if (ek == NULL)
304                 {
305                 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, ERR_R_MALLOC_FAILURE);
306                 goto err;
307                 }
308
309         if (EVP_PKEY_decrypt(pctx, ek, &eklen,
310                                 ktri->encryptedKey->data,
311                                 ktri->encryptedKey->length) <= 0)
312                 {
313                 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, CMS_R_CMS_LIB);
314                 goto err;
315                 }
316
317         ret = 1;
318
319         cms->d.envelopedData->encryptedContentInfo->key = ek;
320         cms->d.envelopedData->encryptedContentInfo->keylen = eklen;
321
322         err:
323         if (pctx)
324                 EVP_PKEY_CTX_free(pctx);
325         if (!ret && ek)
326                 OPENSSL_free(ek);
327
328         return ret;
329         }
330
331 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
332         {
333         CMS_EncryptedContentInfo *ec;
334         ec = cms->d.envelopedData->encryptedContentInfo;
335         return cms_EncryptedContent_init_bio(ec);
336         }