Update default CFLAGS and dependencies.
[oweals/openssl.git] / crypto / cms / cms_sd.c
1 /* crypto/cms/cms_sd.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 "cms_lcl.h"
61
62 /* CMS SignedData Utilities */
63
64 DECLARE_ASN1_ITEM(CMS_SignedData)
65
66 static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
67         {
68         if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed)
69                 {
70                 CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
71                 return NULL;
72                 }
73         return cms->d.signedData;
74         }
75
76 static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
77         {
78         if (cms->d.other == NULL)
79                 {
80                 cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
81                 if (!cms->d.signedData)
82                         {
83                         CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
84                         return NULL;
85                         }
86                 cms->d.signedData->version = 1;
87                 cms->d.signedData->encapContentInfo->eContentType =
88                                                 OBJ_nid2obj(NID_pkcs7_data);
89                 cms->d.signedData->encapContentInfo->partial = 1;
90                 ASN1_OBJECT_free(cms->contentType);
91                 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
92                 return cms->d.signedData;
93                 }
94         return cms_get0_signed(cms);
95         }
96
97 /* Just initialize SignedData e.g. for certs only structure */
98
99 int CMS_SignedData_init(CMS_ContentInfo *cms)
100         {
101         if (cms_signed_data_init(cms))
102                 return 1;
103         else
104                 return 0;
105         }
106
107 /* Check structures and fixup version numbers (if necessary) */
108
109 static void cms_sd_set_version(CMS_SignedData *sd)
110         {
111         int i;
112         CMS_CertificateChoices *cch;
113         CMS_RevocationInfoChoice *rch;
114         CMS_SignerInfo *si;
115
116         for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++)
117                 {
118                 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
119                 if (cch->type == CMS_CERTCHOICE_OTHER)
120                         {
121                         if (sd->version < 5)
122                                 sd->version = 5;
123                         }
124                 else if (cch->type == CMS_CERTCHOICE_V2ACERT)
125                         {
126                         if (sd->version < 4)
127                                 sd->version = 4;
128                         }
129                 else if (cch->type == CMS_CERTCHOICE_V1ACERT)
130                         {
131                         if (sd->version < 3)
132                                 sd->version = 3;
133                         }
134                 }
135
136         for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++)
137                 {
138                 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
139                 if (rch->type == CMS_REVCHOICE_OTHER)
140                         {
141                         if (sd->version < 5)
142                                 sd->version = 5;
143                         }
144                 }
145
146         if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
147                         && (sd->version < 3))
148                 sd->version = 3;
149
150         for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++)
151                 {
152                 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
153                 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
154                         {
155                         if (si->version < 3)
156                                 si->version = 3;
157                         if (sd->version < 3)
158                                 sd->version = 3;
159                         }
160                 else
161                         sd->version = 1;
162                 }
163
164         if (sd->version < 1)
165                 sd->version = 1;
166
167         }
168         
169 /* Copy an existing messageDigest value */
170
171 static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
172         {
173         STACK_OF(CMS_SignerInfo) *sinfos;
174         CMS_SignerInfo *sitmp;
175         int i;
176         sinfos = CMS_get0_SignerInfos(cms);
177         for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
178                 {
179                 ASN1_OCTET_STRING *messageDigest;
180                 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
181                 if (sitmp == si)
182                         continue;
183                 if (CMS_signed_get_attr_count(sitmp) < 0)
184                         continue;
185                 if (OBJ_cmp(si->digestAlgorithm->algorithm,
186                                 sitmp->digestAlgorithm->algorithm))
187                         continue;
188                 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
189                                         OBJ_nid2obj(NID_pkcs9_messageDigest),
190                                         -3, V_ASN1_OCTET_STRING);
191                 if (!messageDigest)
192                         {
193                         CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
194                                 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
195                         return 0;
196                         }
197
198                 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
199                                                 V_ASN1_OCTET_STRING,
200                                                 messageDigest, -1))
201                         return 1;
202                 else
203                         return 0;
204                 }
205                 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
206                 return 0;
207         }
208
209 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
210         {
211         switch(type)
212                 {
213                 case CMS_SIGNERINFO_ISSUER_SERIAL:
214                 sid->d.issuerAndSerialNumber =
215                         M_ASN1_new_of(CMS_IssuerAndSerialNumber);
216                 if (!sid->d.issuerAndSerialNumber)
217                         goto merr;
218                 if (!X509_NAME_set(&sid->d.issuerAndSerialNumber->issuer,
219                                         X509_get_issuer_name(cert)))
220                         goto merr;
221                 ASN1_STRING_free(sid->d.issuerAndSerialNumber->serialNumber);
222                 sid->d.issuerAndSerialNumber->serialNumber =
223                                 ASN1_STRING_dup(X509_get_serialNumber(cert));
224                 if(!sid->d.issuerAndSerialNumber->serialNumber)
225                         goto merr;
226                 break;
227
228                 case CMS_SIGNERINFO_KEYIDENTIFIER:
229                 if (!cert->skid)
230                         {
231                         CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER,
232                                         CMS_R_CERTIFICATE_HAS_NO_KEYID);
233                         return 0;
234                         }
235                 sid->d.subjectKeyIdentifier = ASN1_STRING_dup(cert->skid);
236                 if (!sid->d.subjectKeyIdentifier)
237                         goto merr;
238                 break;
239
240                 default:
241                 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
242                 return 0;
243                 }
244
245         sid->type = type;
246
247         return 1;
248
249         merr:
250         CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, ERR_R_MALLOC_FAILURE);
251         return 0;
252
253         }
254
255 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
256                                         ASN1_OCTET_STRING **keyid,
257                                         X509_NAME **issuer, ASN1_INTEGER **sno)
258         {
259         if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
260                 {
261                 if (issuer)
262                         *issuer = sid->d.issuerAndSerialNumber->issuer;
263                 if (sno)
264                         *sno = sid->d.issuerAndSerialNumber->serialNumber;
265                 }
266         else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
267                 {
268                 if (keyid)
269                         *keyid = sid->d.subjectKeyIdentifier;
270                 }
271         else
272                 return 0;
273         return 1;
274         }
275
276 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
277         {
278         int ret;
279         if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
280                 {
281                 ret = X509_NAME_cmp(sid->d.issuerAndSerialNumber->issuer,
282                                         X509_get_issuer_name(cert));
283                 if (ret)
284                         return ret;
285                 return ASN1_INTEGER_cmp(sid->d.issuerAndSerialNumber->serialNumber,
286                                         X509_get_serialNumber(cert));
287                 }
288         else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
289                 {
290                 X509_check_purpose(cert, -1, -1);
291                 if (!cert->skid)
292                         return -1;
293                 return ASN1_OCTET_STRING_cmp(sid->d.subjectKeyIdentifier,
294                                                         cert->skid);
295                 }
296         else
297                 return -1;
298         }
299
300 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
301                         X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
302                         unsigned int flags)
303         {
304         CMS_SignedData *sd;
305         CMS_SignerInfo *si = NULL;
306         X509_ALGOR *alg;
307         int i, type;
308         if(!X509_check_private_key(signer, pk))
309                 {
310                 CMSerr(CMS_F_CMS_ADD1_SIGNER,
311                         CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
312                 return NULL;
313                 }
314         sd = cms_signed_data_init(cms);
315         if (!sd)
316                 goto err;
317         si = M_ASN1_new_of(CMS_SignerInfo);
318         if (!si)
319                 goto merr;
320         X509_check_purpose(signer, -1, -1);
321
322         CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
323         CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
324
325         si->pkey = pk;
326         si->signer = signer;
327
328         if (flags & CMS_USE_KEYID)
329                 {
330                 si->version = 3;
331                 if (sd->version < 3)
332                         sd->version = 3;
333                 type = CMS_SIGNERINFO_KEYIDENTIFIER;
334                 }
335         else
336                 {
337                 type = CMS_SIGNERINFO_ISSUER_SERIAL;
338                 si->version = 1;
339                 }
340
341         if (!cms_set1_SignerIdentifier(si->sid, signer, type))
342                 goto err;
343
344         /* Since no EVP_PKEY_METHOD in 0.9.8 hard code SHA1 as default */
345         if (md == NULL)
346                 md = EVP_sha1();
347
348         /* OpenSSL 0.9.8 only supports SHA1 with non-RSA keys */
349
350         if ((pk->type != EVP_PKEY_RSA) && (EVP_MD_type(md) != NID_sha1))
351                 {
352                 CMSerr(CMS_F_CMS_ADD1_SIGNER,
353                                 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
354                 goto err;
355                 }
356
357         cms_DigestAlgorithm_set(si->digestAlgorithm, md);
358
359         /* See if digest is present in digestAlgorithms */
360         for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)
361                 {
362                 ASN1_OBJECT *aoid;
363                 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
364                 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
365                 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
366                         break;
367                 }
368
369         if (i == sk_X509_ALGOR_num(sd->digestAlgorithms))
370                 {
371                 alg = X509_ALGOR_new();
372                 if (!alg)
373                         goto merr;
374                 cms_DigestAlgorithm_set(alg, md);
375                 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg))
376                         {
377                         X509_ALGOR_free(alg);
378                         goto merr;
379                         }
380                 }
381
382         /* Since we have no EVP_PKEY_ASN1_METHOD in OpenSSL 0.9.8,
383          * hard code algorithm parameters.
384          */
385
386         switch (pk->type)
387                 {
388
389                 case EVP_PKEY_RSA:
390                 X509_ALGOR_set0(si->signatureAlgorithm,
391                                         OBJ_nid2obj(NID_rsaEncryption),
392                                         V_ASN1_NULL, 0);
393                 break;
394
395                 case EVP_PKEY_DSA:
396                 X509_ALGOR_set0(si->signatureAlgorithm,
397                                         OBJ_nid2obj(NID_dsaWithSHA1),
398                                         V_ASN1_UNDEF, 0);
399                 break;
400
401
402                 case EVP_PKEY_EC:
403                 X509_ALGOR_set0(si->signatureAlgorithm,
404                                         OBJ_nid2obj(NID_ecdsa_with_SHA1),
405                                         V_ASN1_UNDEF, 0);
406                 break;
407
408                 default:
409                 CMSerr(CMS_F_CMS_ADD1_SIGNER,
410                                 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
411                 goto err;
412
413                 }
414
415         if (!(flags & CMS_NOATTR))
416                 {
417                 /* Copy content type across */
418                 ASN1_OBJECT *ctype =
419                                 OBJ_dup(sd->encapContentInfo->eContentType); 
420                 if (!ctype)
421                         goto merr;
422                 i = CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
423                                                 V_ASN1_OBJECT, ctype, -1);
424                 ASN1_OBJECT_free(ctype);
425                 if (i <= 0)
426                         goto merr;
427                 if (!(flags & CMS_NOSMIMECAP))
428                         {
429                         STACK_OF(X509_ALGOR) *smcap = NULL;
430                         i = CMS_add_standard_smimecap(&smcap);
431                         if (i)
432                                 i = CMS_add_smimecap(si, smcap);
433                         sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
434                         if (!i)
435                                 goto merr;
436                         }
437                 if (flags & CMS_REUSE_DIGEST)
438                         {
439                         if (!cms_copy_messageDigest(cms, si))
440                                 goto err;
441                         if (!(flags & CMS_PARTIAL) &&
442                                         !CMS_SignerInfo_sign(si))
443                                 goto err;
444                         }
445                 }
446
447         if (!(flags & CMS_NOCERTS))
448                 {
449                 /* NB ignore -1 return for duplicate cert */
450                 if (!CMS_add1_cert(cms, signer))
451                         goto merr;
452                 }
453
454         if (!sd->signerInfos)
455                 sd->signerInfos = sk_CMS_SignerInfo_new_null();
456         if (!sd->signerInfos ||
457                 !sk_CMS_SignerInfo_push(sd->signerInfos, si))
458                 goto merr;
459
460         return si;
461
462         merr:
463         CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
464         err:
465         if (si)
466                 M_ASN1_free_of(si, CMS_SignerInfo);
467         return NULL;
468
469         }
470
471 static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
472         {
473         ASN1_TIME *tt;
474         int r = 0;
475         if (t)
476                 tt = t;
477         else
478                 tt = X509_gmtime_adj(NULL, 0);
479
480         if (!tt)
481                 goto merr;
482
483         if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
484                                                 tt->type, tt, -1) <= 0)
485                 goto merr;
486
487         r = 1;
488
489         merr:
490
491         if (!t)
492                 ASN1_TIME_free(tt);
493
494         if (!r)
495                 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
496
497         return r;
498
499         }
500
501 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
502         {
503         CMS_SignedData *sd;
504         sd = cms_get0_signed(cms);
505         if (!sd)
506                 return NULL;
507         return sd->signerInfos;
508         }
509
510 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
511         {
512         STACK_OF(X509) *signers = NULL;
513         STACK_OF(CMS_SignerInfo) *sinfos;
514         CMS_SignerInfo *si;
515         int i;
516         sinfos = CMS_get0_SignerInfos(cms);
517         for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
518                 {
519                 si = sk_CMS_SignerInfo_value(sinfos, i);
520                 if (si->signer)
521                         {
522                         if (!signers)
523                                 {
524                                 signers = sk_X509_new_null();
525                                 if (!signers)
526                                         return NULL;
527                                 }
528                         if (!sk_X509_push(signers, si->signer))
529                                 {
530                                 sk_X509_free(signers);
531                                 return NULL;
532                                 }
533                         }
534                 }
535         return signers;
536         }
537
538 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
539         {
540         if (signer)
541                 {
542                 CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
543                 if (si->pkey)
544                         EVP_PKEY_free(si->pkey);
545                 si->pkey = X509_get_pubkey(signer);
546                 }
547         if (si->signer)
548                 X509_free(si->signer);
549         si->signer = signer;
550         }
551
552 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
553                                         ASN1_OCTET_STRING **keyid,
554                                         X509_NAME **issuer, ASN1_INTEGER **sno)
555         {
556         return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
557         }
558
559 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
560         {
561         return cms_SignerIdentifier_cert_cmp(si->sid, cert);
562         }
563
564 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
565                                 unsigned int flags)
566         {
567         CMS_SignedData *sd;
568         CMS_SignerInfo *si;
569         CMS_CertificateChoices *cch;
570         STACK_OF(CMS_CertificateChoices) *certs;
571         X509 *x;
572         int i, j;
573         int ret = 0;
574         sd = cms_get0_signed(cms);
575         if (!sd)
576                 return -1;
577         certs = sd->certificates;
578         for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++)
579                 {
580                 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
581                 if (si->signer)
582                         continue;
583
584                 for (j = 0; j < sk_X509_num(scerts); j++)
585                         {
586                         x = sk_X509_value(scerts, j);
587                         if (CMS_SignerInfo_cert_cmp(si, x) == 0)
588                                 {
589                                 CMS_SignerInfo_set1_signer_cert(si, x);
590                                 ret++;
591                                 break;
592                                 }
593                         }
594
595                 if (si->signer || (flags & CMS_NOINTERN))
596                         continue;
597
598                 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++)
599                         {
600                         cch = sk_CMS_CertificateChoices_value(certs, j);
601                         if (cch->type != 0)
602                                 continue;
603                         x = cch->d.certificate;
604                         if (CMS_SignerInfo_cert_cmp(si, x) == 0)
605                                 {
606                                 CMS_SignerInfo_set1_signer_cert(si, x);
607                                 ret++;
608                                 break;
609                                 }
610                         }
611                 }
612         return ret;
613         }
614
615 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, X509 **signer,
616                                         X509_ALGOR **pdig, X509_ALGOR **psig)
617         {
618         if (pk)
619                 *pk = si->pkey;
620         if (signer)
621                 *signer = si->signer;
622         if (pdig)
623                 *pdig = si->digestAlgorithm;
624         if (psig)
625                 *psig = si->signatureAlgorithm;
626         }
627
628 /* In OpenSSL 0.9.8 we have the link between digest types and public
629  * key types so we need to fixup the digest type if the public key
630  * type is not appropriate.
631  */
632
633 static void cms_fixup_mctx(EVP_MD_CTX *mctx, EVP_PKEY *pkey)
634         {
635         if (EVP_MD_CTX_type(mctx) != NID_sha1)
636                 return;
637 #ifndef OPENSSL_NO_DSA
638         if (pkey->type == EVP_PKEY_DSA)
639                 mctx->digest = EVP_dss1();      
640 #endif
641 #ifndef OPENSSL_NO_ECDSA
642         if (pkey->type == EVP_PKEY_EC)
643                 mctx->digest = EVP_ecdsa();     
644 #endif
645         }
646
647 static int cms_SignerInfo_content_sign(CMS_SignerInfo *si, BIO *chain)
648         {
649         EVP_MD_CTX mctx;
650         int r = 0;
651         EVP_MD_CTX_init(&mctx);
652
653
654         if (!si->pkey)
655                 {
656                 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
657                 return 0;
658                 }
659
660         if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
661                 goto err;
662
663         /* If any signed attributes calculate and add messageDigest attribute */
664
665         if (CMS_signed_get_attr_count(si) >= 0)
666                 {
667                 unsigned char md[EVP_MAX_MD_SIZE];
668                 unsigned int mdlen;
669                 EVP_DigestFinal_ex(&mctx, md, &mdlen);
670                 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
671                                                 V_ASN1_OCTET_STRING,
672                                                 md, mdlen))
673                         goto err;
674                 if (!CMS_SignerInfo_sign(si))
675                         goto err;
676                 }
677         else
678                 {
679                 unsigned char *sig;
680                 unsigned int siglen;
681                 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
682                 if (!sig)
683                         {
684                         CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,
685                                         ERR_R_MALLOC_FAILURE);
686                         goto err;
687                         }
688                 cms_fixup_mctx(&mctx, si->pkey);
689                 if (!EVP_SignFinal(&mctx, sig, &siglen, si->pkey))
690                         {
691                         CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,
692                                         CMS_R_SIGNFINAL_ERROR);
693                         OPENSSL_free(sig);
694                         goto err;
695                         }
696                 ASN1_STRING_set0(si->signature, sig, siglen);
697                 }
698
699         r = 1;
700
701         err:
702         EVP_MD_CTX_cleanup(&mctx);
703         return r;
704
705         }
706
707 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
708         {
709         STACK_OF(CMS_SignerInfo) *sinfos;
710         CMS_SignerInfo *si;
711         int i;
712         sinfos = CMS_get0_SignerInfos(cms);
713         for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
714                 {
715                 si = sk_CMS_SignerInfo_value(sinfos, i);
716                 if (!cms_SignerInfo_content_sign(si, chain))
717                         return 0;
718                 }
719         cms->d.signedData->encapContentInfo->partial = 0;
720         return 1;
721         }
722
723 int CMS_SignerInfo_sign(CMS_SignerInfo *si)
724         {
725         EVP_MD_CTX mctx;
726         unsigned char *abuf = NULL;
727         int alen;
728         unsigned int siglen;
729         const EVP_MD *md = NULL;
730
731         md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
732         if (md == NULL)
733                 return 0;
734
735         EVP_MD_CTX_init(&mctx);
736
737         if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0)
738                 {
739                 if (!cms_add1_signingTime(si, NULL))
740                         goto err;
741                 }
742
743         if (EVP_SignInit_ex(&mctx, md, NULL) <= 0)
744                 goto err;
745
746 #if 0
747         if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
748                                 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0)
749                 {
750                 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
751                 goto err;
752                 }
753 #endif
754
755         alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,
756                                 ASN1_ITEM_rptr(CMS_Attributes_Sign));
757         if(!abuf)
758                 goto err;
759         if (EVP_SignUpdate(&mctx, abuf, alen) <= 0)
760                 goto err;
761         siglen = EVP_PKEY_size(si->pkey);
762         OPENSSL_free(abuf);
763         abuf = OPENSSL_malloc(siglen);
764         if(!abuf)
765                 goto err;
766         cms_fixup_mctx(&mctx, si->pkey);
767         if (EVP_SignFinal(&mctx, abuf, &siglen, si->pkey) <= 0)
768                 goto err;
769 #if 0
770         if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
771                                 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0)
772                 {
773                 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
774                 goto err;
775                 }
776 #endif
777         EVP_MD_CTX_cleanup(&mctx);
778
779         ASN1_STRING_set0(si->signature, abuf, siglen);
780
781         return 1;
782
783         err:
784         if (abuf)
785                 OPENSSL_free(abuf);
786         EVP_MD_CTX_cleanup(&mctx);
787         return 0;
788
789         }
790
791 int CMS_SignerInfo_verify(CMS_SignerInfo *si)
792         {
793         EVP_MD_CTX mctx;
794         unsigned char *abuf = NULL;
795         int alen, r = -1;
796         const EVP_MD *md = NULL;
797
798         if (!si->pkey)
799                 {
800                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
801                 return -1;
802                 }
803
804         md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
805         if (md == NULL)
806                 return -1;
807         EVP_MD_CTX_init(&mctx);
808         if (EVP_VerifyInit_ex(&mctx, md, NULL) <= 0)
809                 goto err;
810
811         alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,
812                                 ASN1_ITEM_rptr(CMS_Attributes_Verify));
813         if(!abuf)
814                 goto err;
815         r = EVP_VerifyUpdate(&mctx, abuf, alen);
816         OPENSSL_free(abuf);
817         if (r <= 0)
818                 {
819                 r = -1;
820                 goto err;
821                 }
822         cms_fixup_mctx(&mctx, si->pkey);
823         r = EVP_VerifyFinal(&mctx,
824                         si->signature->data, si->signature->length, si->pkey);
825         if (!r)
826                 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
827         err:
828         EVP_MD_CTX_cleanup(&mctx);
829         return r;
830         }
831
832 /* Create a chain of digest BIOs from a CMS ContentInfo */
833
834 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
835         {
836         int i;
837         CMS_SignedData *sd;
838         BIO *chain = NULL;
839         sd = cms_get0_signed(cms);
840         if (!sd)
841                 return NULL;
842         if (cms->d.signedData->encapContentInfo->partial)
843                 cms_sd_set_version(sd);
844         for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)
845                 {
846                 X509_ALGOR *digestAlgorithm;
847                 BIO *mdbio;
848                 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
849                 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
850                 if (!mdbio)
851                         goto err;       
852                 if (chain)
853                          BIO_push(chain, mdbio);
854                 else
855                         chain = mdbio;
856                 }
857         return chain;
858         err:
859         if (chain)
860                 BIO_free_all(chain);
861         return NULL;
862         }
863
864 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
865         {
866         ASN1_OCTET_STRING *os = NULL;
867         EVP_MD_CTX mctx;
868         int r = -1;
869         EVP_MD_CTX_init(&mctx);
870         /* If we have any signed attributes look for messageDigest value */
871         if (CMS_signed_get_attr_count(si) >= 0)
872                 {
873                 os = CMS_signed_get0_data_by_OBJ(si,
874                                         OBJ_nid2obj(NID_pkcs9_messageDigest),
875                                         -3, V_ASN1_OCTET_STRING);
876                 if (!os)
877                         {
878                         CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
879                                 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
880                         goto err;
881                         }
882                 }
883
884         if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
885                 goto err;
886
887         /* If messageDigest found compare it */
888
889         if (os)
890                 {
891                 unsigned char mval[EVP_MAX_MD_SIZE];
892                 unsigned int mlen;
893                 if (EVP_DigestFinal_ex(&mctx, mval, &mlen) <= 0)
894                         {
895                         CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
896                                 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
897                         goto err;
898                         }
899                 if (mlen != (unsigned int)os->length)
900                         {
901                         CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
902                                 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
903                         goto err;
904                         }
905
906                 if (memcmp(mval, os->data, mlen))
907                         {
908                         CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
909                                 CMS_R_VERIFICATION_FAILURE);
910                         r = 0;
911                         }
912                 else
913                         r = 1;
914                 }
915         else
916                 {
917                 cms_fixup_mctx(&mctx, si->pkey);
918                 r = EVP_VerifyFinal(&mctx, si->signature->data,
919                                         si->signature->length, si->pkey);
920                 if (r <= 0)
921                         {
922                         CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
923                                 CMS_R_VERIFICATION_FAILURE);
924                         r = 0;
925                         }
926                 }
927
928         err:
929         EVP_MD_CTX_cleanup(&mctx);
930         return r;
931
932         }
933
934 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
935         {
936         unsigned char *smder = NULL;
937         int smderlen, r;
938         smderlen = i2d_X509_ALGORS(algs, &smder);
939         if (smderlen <= 0)
940                 return 0;
941         r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
942                                         V_ASN1_SEQUENCE, smder, smderlen);
943         OPENSSL_free(smder);
944         return r;
945         }
946
947 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
948                                 int algnid, int keysize)
949         {
950         X509_ALGOR *alg;
951         ASN1_INTEGER *key = NULL;
952         if (keysize > 0)
953                 {
954                 key = ASN1_INTEGER_new();
955                 if (!key || !ASN1_INTEGER_set(key, keysize))
956                         return 0;
957                 }
958         alg = X509_ALGOR_new();
959         if (!alg)
960                 {
961                 if (key)
962                         ASN1_INTEGER_free(key);
963                 return 0;
964                 }
965                 
966         X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
967                                 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
968         if (!*algs)
969                 *algs = sk_X509_ALGOR_new_null();
970         if (!*algs || !sk_X509_ALGOR_push(*algs, alg))
971                 {
972                 X509_ALGOR_free(alg);
973                 return 0;
974                 }
975         return 1;
976         }
977
978 /* Check to see if a cipher exists and if so add S/MIME capabilities */
979
980 static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
981         {
982         if (EVP_get_cipherbynid(nid))
983                 return CMS_add_simple_smimecap(sk, nid, arg);
984         return 1;
985         }
986 #if 0
987 static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
988         {
989         if (EVP_get_digestbynid(nid))
990                 return CMS_add_simple_smimecap(sk, nid, arg);
991         return 1;
992         }
993 #endif
994 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
995         {
996         if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
997                 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
998                 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
999                 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
1000                 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
1001                 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
1002                 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
1003                 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
1004                 return 0;
1005         return 1;
1006         }