Coverage Report

Created: 2022-08-24 06:30

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