Coverage Report

Created: 2023-06-08 06:40

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