Configurations/unix-Makefile.tmpl: make cleanup kinder
[openssl.git] / crypto / pkcs7 / pk7_lib.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/objects.h>
13 #include <openssl/x509.h>
14 #include "crypto/asn1.h"
15 #include "crypto/evp.h"
16
17 DEFINE_STACK_OF(X509)
18 DEFINE_STACK_OF(X509_CRL)
19 DEFINE_STACK_OF(X509_ALGOR)
20 DEFINE_STACK_OF(PKCS7_RECIP_INFO)
21 DEFINE_STACK_OF(PKCS7_SIGNER_INFO)
22
23 long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
24 {
25     int nid;
26     long ret;
27
28     nid = OBJ_obj2nid(p7->type);
29
30     switch (cmd) {
31     /* NOTE(emilia): does not support detached digested data. */
32     case PKCS7_OP_SET_DETACHED_SIGNATURE:
33         if (nid == NID_pkcs7_signed) {
34             ret = p7->detached = (int)larg;
35             if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
36                 ASN1_OCTET_STRING *os;
37                 os = p7->d.sign->contents->d.data;
38                 ASN1_OCTET_STRING_free(os);
39                 p7->d.sign->contents->d.data = NULL;
40             }
41         } else {
42             PKCS7err(PKCS7_F_PKCS7_CTRL,
43                      PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
44             ret = 0;
45         }
46         break;
47     case PKCS7_OP_GET_DETACHED_SIGNATURE:
48         if (nid == NID_pkcs7_signed) {
49             if (p7->d.sign == NULL || p7->d.sign->contents->d.ptr == NULL)
50                 ret = 1;
51             else
52                 ret = 0;
53
54             p7->detached = ret;
55         } else {
56             PKCS7err(PKCS7_F_PKCS7_CTRL,
57                      PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
58             ret = 0;
59         }
60
61         break;
62     default:
63         PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION);
64         ret = 0;
65     }
66     return ret;
67 }
68
69 int PKCS7_content_new(PKCS7 *p7, int type)
70 {
71     PKCS7 *ret = NULL;
72
73     if ((ret = PKCS7_new()) == NULL)
74         goto err;
75     if (!PKCS7_set_type(ret, type))
76         goto err;
77     if (!PKCS7_set_content(p7, ret))
78         goto err;
79
80     return 1;
81  err:
82     PKCS7_free(ret);
83     return 0;
84 }
85
86 int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
87 {
88     int i;
89
90     i = OBJ_obj2nid(p7->type);
91     switch (i) {
92     case NID_pkcs7_signed:
93         PKCS7_free(p7->d.sign->contents);
94         p7->d.sign->contents = p7_data;
95         break;
96     case NID_pkcs7_digest:
97         PKCS7_free(p7->d.digest->contents);
98         p7->d.digest->contents = p7_data;
99         break;
100     case NID_pkcs7_data:
101     case NID_pkcs7_enveloped:
102     case NID_pkcs7_signedAndEnveloped:
103     case NID_pkcs7_encrypted:
104     default:
105         PKCS7err(PKCS7_F_PKCS7_SET_CONTENT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
106         goto err;
107     }
108     return 1;
109  err:
110     return 0;
111 }
112
113 int PKCS7_set_type(PKCS7 *p7, int type)
114 {
115     ASN1_OBJECT *obj;
116
117     /*
118      * PKCS7_content_free(p7);
119      */
120     obj = OBJ_nid2obj(type);    /* will not fail */
121
122     switch (type) {
123     case NID_pkcs7_signed:
124         p7->type = obj;
125         if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
126             goto err;
127         if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
128             PKCS7_SIGNED_free(p7->d.sign);
129             p7->d.sign = NULL;
130             goto err;
131         }
132         break;
133     case NID_pkcs7_data:
134         p7->type = obj;
135         if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL)
136             goto err;
137         break;
138     case NID_pkcs7_signedAndEnveloped:
139         p7->type = obj;
140         if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
141             == NULL)
142             goto err;
143         if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
144             goto err;
145         p7->d.signed_and_enveloped->enc_data->content_type
146             = OBJ_nid2obj(NID_pkcs7_data);
147         break;
148     case NID_pkcs7_enveloped:
149         p7->type = obj;
150         if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
151             == NULL)
152             goto err;
153         if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
154             goto err;
155         p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
156         break;
157     case NID_pkcs7_encrypted:
158         p7->type = obj;
159         if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
160             == NULL)
161             goto err;
162         if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
163             goto err;
164         p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
165         break;
166
167     case NID_pkcs7_digest:
168         p7->type = obj;
169         if ((p7->d.digest = PKCS7_DIGEST_new())
170             == NULL)
171             goto err;
172         if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
173             goto err;
174         break;
175     default:
176         PKCS7err(PKCS7_F_PKCS7_SET_TYPE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
177         goto err;
178     }
179     return 1;
180  err:
181     return 0;
182 }
183
184 int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
185 {
186     p7->type = OBJ_nid2obj(type);
187     p7->d.other = other;
188     return 1;
189 }
190
191 int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
192 {
193     int i, j, nid;
194     X509_ALGOR *alg;
195     STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
196     STACK_OF(X509_ALGOR) *md_sk;
197
198     i = OBJ_obj2nid(p7->type);
199     switch (i) {
200     case NID_pkcs7_signed:
201         signer_sk = p7->d.sign->signer_info;
202         md_sk = p7->d.sign->md_algs;
203         break;
204     case NID_pkcs7_signedAndEnveloped:
205         signer_sk = p7->d.signed_and_enveloped->signer_info;
206         md_sk = p7->d.signed_and_enveloped->md_algs;
207         break;
208     default:
209         PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, PKCS7_R_WRONG_CONTENT_TYPE);
210         return 0;
211     }
212
213     nid = OBJ_obj2nid(psi->digest_alg->algorithm);
214
215     /* If the digest is not currently listed, add it */
216     j = 0;
217     for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
218         alg = sk_X509_ALGOR_value(md_sk, i);
219         if (OBJ_obj2nid(alg->algorithm) == nid) {
220             j = 1;
221             break;
222         }
223     }
224     if (!j) {                   /* we need to add another algorithm */
225         if ((alg = X509_ALGOR_new()) == NULL
226             || (alg->parameter = ASN1_TYPE_new()) == NULL) {
227             X509_ALGOR_free(alg);
228             PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
229             return 0;
230         }
231         alg->algorithm = OBJ_nid2obj(nid);
232         alg->parameter->type = V_ASN1_NULL;
233         if (!sk_X509_ALGOR_push(md_sk, alg)) {
234             X509_ALGOR_free(alg);
235             return 0;
236         }
237     }
238
239     if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
240         return 0;
241     return 1;
242 }
243
244 int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
245 {
246     int i;
247     STACK_OF(X509) **sk;
248
249     i = OBJ_obj2nid(p7->type);
250     switch (i) {
251     case NID_pkcs7_signed:
252         sk = &(p7->d.sign->cert);
253         break;
254     case NID_pkcs7_signedAndEnveloped:
255         sk = &(p7->d.signed_and_enveloped->cert);
256         break;
257     default:
258         PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, PKCS7_R_WRONG_CONTENT_TYPE);
259         return 0;
260     }
261
262     if (*sk == NULL)
263         *sk = sk_X509_new_null();
264     if (*sk == NULL) {
265         PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
266         return 0;
267     }
268     X509_up_ref(x509);
269     if (!sk_X509_push(*sk, x509)) {
270         X509_free(x509);
271         return 0;
272     }
273     return 1;
274 }
275
276 int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
277 {
278     int i;
279     STACK_OF(X509_CRL) **sk;
280
281     i = OBJ_obj2nid(p7->type);
282     switch (i) {
283     case NID_pkcs7_signed:
284         sk = &(p7->d.sign->crl);
285         break;
286     case NID_pkcs7_signedAndEnveloped:
287         sk = &(p7->d.signed_and_enveloped->crl);
288         break;
289     default:
290         PKCS7err(PKCS7_F_PKCS7_ADD_CRL, PKCS7_R_WRONG_CONTENT_TYPE);
291         return 0;
292     }
293
294     if (*sk == NULL)
295         *sk = sk_X509_CRL_new_null();
296     if (*sk == NULL) {
297         PKCS7err(PKCS7_F_PKCS7_ADD_CRL, ERR_R_MALLOC_FAILURE);
298         return 0;
299     }
300
301     X509_CRL_up_ref(crl);
302     if (!sk_X509_CRL_push(*sk, crl)) {
303         X509_CRL_free(crl);
304         return 0;
305     }
306     return 1;
307 }
308
309 int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
310                           const EVP_MD *dgst)
311 {
312     int ret;
313
314     /* We now need to add another PKCS7_SIGNER_INFO entry */
315     if (!ASN1_INTEGER_set(p7i->version, 1))
316         goto err;
317     if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
318                        X509_get_issuer_name(x509)))
319         goto err;
320
321     /*
322      * because ASN1_INTEGER_set is used to set a 'long' we will do things the
323      * ugly way.
324      */
325     ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
326     if (!(p7i->issuer_and_serial->serial =
327           ASN1_INTEGER_dup(X509_get0_serialNumber(x509))))
328         goto err;
329
330     /* lets keep the pkey around for a while */
331     EVP_PKEY_up_ref(pkey);
332     p7i->pkey = pkey;
333
334     /* Set the algorithms */
335
336     X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)),
337                     V_ASN1_NULL, NULL);
338
339     if (pkey->ameth && pkey->ameth->pkey_ctrl) {
340         ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 0, p7i);
341         if (ret > 0)
342             return 1;
343         if (ret != -2) {
344             PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
345                      PKCS7_R_SIGNING_CTRL_FAILURE);
346             return 0;
347         }
348     }
349     PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
350              PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
351  err:
352     return 0;
353 }
354
355 PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
356                                        const EVP_MD *dgst)
357 {
358     PKCS7_SIGNER_INFO *si = NULL;
359
360     if (dgst == NULL) {
361         int def_nid;
362         if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
363             goto err;
364         dgst = EVP_get_digestbynid(def_nid);
365         if (dgst == NULL) {
366             PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, PKCS7_R_NO_DEFAULT_DIGEST);
367             goto err;
368         }
369     }
370
371     if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
372         goto err;
373     if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
374         goto err;
375     if (!PKCS7_add_signer(p7, si))
376         goto err;
377     return si;
378  err:
379     PKCS7_SIGNER_INFO_free(si);
380     return NULL;
381 }
382
383 int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
384 {
385     if (PKCS7_type_is_digest(p7)) {
386         if ((p7->d.digest->md->parameter = ASN1_TYPE_new()) == NULL) {
387             PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, ERR_R_MALLOC_FAILURE);
388             return 0;
389         }
390         p7->d.digest->md->parameter->type = V_ASN1_NULL;
391         p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
392         return 1;
393     }
394
395     PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE);
396     return 1;
397 }
398
399 STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
400 {
401     if (p7 == NULL || p7->d.ptr == NULL)
402         return NULL;
403     if (PKCS7_type_is_signed(p7)) {
404         return p7->d.sign->signer_info;
405     } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
406         return p7->d.signed_and_enveloped->signer_info;
407     } else
408         return NULL;
409 }
410
411 void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
412                                  X509_ALGOR **pdig, X509_ALGOR **psig)
413 {
414     if (pk)
415         *pk = si->pkey;
416     if (pdig)
417         *pdig = si->digest_alg;
418     if (psig)
419         *psig = si->digest_enc_alg;
420 }
421
422 void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
423 {
424     if (penc)
425         *penc = ri->key_enc_algor;
426 }
427
428 PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
429 {
430     PKCS7_RECIP_INFO *ri;
431
432     if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
433         goto err;
434     if (!PKCS7_RECIP_INFO_set(ri, x509))
435         goto err;
436     if (!PKCS7_add_recipient_info(p7, ri))
437         goto err;
438     return ri;
439  err:
440     PKCS7_RECIP_INFO_free(ri);
441     return NULL;
442 }
443
444 int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
445 {
446     int i;
447     STACK_OF(PKCS7_RECIP_INFO) *sk;
448
449     i = OBJ_obj2nid(p7->type);
450     switch (i) {
451     case NID_pkcs7_signedAndEnveloped:
452         sk = p7->d.signed_and_enveloped->recipientinfo;
453         break;
454     case NID_pkcs7_enveloped:
455         sk = p7->d.enveloped->recipientinfo;
456         break;
457     default:
458         PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,
459                  PKCS7_R_WRONG_CONTENT_TYPE);
460         return 0;
461     }
462
463     if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
464         return 0;
465     return 1;
466 }
467
468 int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
469 {
470     int ret;
471     EVP_PKEY *pkey = NULL;
472     if (!ASN1_INTEGER_set(p7i->version, 0))
473         return 0;
474     if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
475                        X509_get_issuer_name(x509)))
476         return 0;
477
478     ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
479     if (!(p7i->issuer_and_serial->serial =
480           ASN1_INTEGER_dup(X509_get0_serialNumber(x509))))
481         return 0;
482
483     pkey = X509_get0_pubkey(x509);
484
485     if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) {
486         PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
487                  PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
488         goto err;
489     }
490
491     ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 0, p7i);
492     if (ret == -2) {
493         PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
494                  PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
495         goto err;
496     }
497     if (ret <= 0) {
498         PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
499                  PKCS7_R_ENCRYPTION_CTRL_FAILURE);
500         goto err;
501     }
502
503     X509_up_ref(x509);
504     p7i->cert = x509;
505
506     return 1;
507
508  err:
509     return 0;
510 }
511
512 X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
513 {
514     if (PKCS7_type_is_signed(p7))
515         return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
516                                                si->issuer_and_serial->issuer,
517                                                si->
518                                                issuer_and_serial->serial));
519     else
520         return NULL;
521 }
522
523 int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
524 {
525     int i;
526     PKCS7_ENC_CONTENT *ec;
527
528     i = OBJ_obj2nid(p7->type);
529     switch (i) {
530     case NID_pkcs7_signedAndEnveloped:
531         ec = p7->d.signed_and_enveloped->enc_data;
532         break;
533     case NID_pkcs7_enveloped:
534         ec = p7->d.enveloped->enc_data;
535         break;
536     default:
537         PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE);
538         return 0;
539     }
540
541     /* Check cipher OID exists and has data in it */
542     i = EVP_CIPHER_type(cipher);
543     if (i == NID_undef) {
544         PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,
545                  PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
546         return 0;
547     }
548
549     ec->cipher = cipher;
550     return 1;
551 }
552
553 /* unfortunately cannot constify BIO_new_NDEF() due to this and CMS_stream() */
554 int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
555 {
556     ASN1_OCTET_STRING *os = NULL;
557
558     switch (OBJ_obj2nid(p7->type)) {
559     case NID_pkcs7_data:
560         os = p7->d.data;
561         break;
562
563     case NID_pkcs7_signedAndEnveloped:
564         os = p7->d.signed_and_enveloped->enc_data->enc_data;
565         if (os == NULL) {
566             os = ASN1_OCTET_STRING_new();
567             p7->d.signed_and_enveloped->enc_data->enc_data = os;
568         }
569         break;
570
571     case NID_pkcs7_enveloped:
572         os = p7->d.enveloped->enc_data->enc_data;
573         if (os == NULL) {
574             os = ASN1_OCTET_STRING_new();
575             p7->d.enveloped->enc_data->enc_data = os;
576         }
577         break;
578
579     case NID_pkcs7_signed:
580         os = p7->d.sign->contents->d.data;
581         break;
582
583     default:
584         os = NULL;
585         break;
586     }
587
588     if (os == NULL)
589         return 0;
590
591     os->flags |= ASN1_STRING_FLAG_NDEF;
592     *boundary = &os->data;
593
594     return 1;
595 }