Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/pkcs7/pk7_doit.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/rand.h>
13
#include <openssl/objects.h>
14
#include <openssl/x509.h>
15
#include <openssl/x509v3.h>
16
#include <openssl/err.h>
17
18
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
19
                         void *value);
20
static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid);
21
22
static int PKCS7_type_is_other(PKCS7 *p7)
23
0
{
24
0
    int isOther = 1;
25
0
26
0
    int nid = OBJ_obj2nid(p7->type);
27
0
28
0
    switch (nid) {
29
0
    case NID_pkcs7_data:
30
0
    case NID_pkcs7_signed:
31
0
    case NID_pkcs7_enveloped:
32
0
    case NID_pkcs7_signedAndEnveloped:
33
0
    case NID_pkcs7_digest:
34
0
    case NID_pkcs7_encrypted:
35
0
        isOther = 0;
36
0
        break;
37
0
    default:
38
0
        isOther = 1;
39
0
    }
40
0
41
0
    return isOther;
42
0
43
0
}
44
45
static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
46
0
{
47
0
    if (PKCS7_type_is_data(p7))
48
0
        return p7->d.data;
49
0
    if (PKCS7_type_is_other(p7) && p7->d.other
50
0
        && (p7->d.other->type == V_ASN1_OCTET_STRING))
51
0
        return p7->d.other->value.octet_string;
52
0
    return NULL;
53
0
}
54
55
static int PKCS7_bio_add_digest(BIO **pbio, X509_ALGOR *alg)
56
0
{
57
0
    BIO *btmp;
58
0
    const EVP_MD *md;
59
0
    if ((btmp = BIO_new(BIO_f_md())) == NULL) {
60
0
        PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
61
0
        goto err;
62
0
    }
63
0
64
0
    md = EVP_get_digestbyobj(alg->algorithm);
65
0
    if (md == NULL) {
66
0
        PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, PKCS7_R_UNKNOWN_DIGEST_TYPE);
67
0
        goto err;
68
0
    }
69
0
70
0
    BIO_set_md(btmp, md);
71
0
    if (*pbio == NULL)
72
0
        *pbio = btmp;
73
0
    else if (!BIO_push(*pbio, btmp)) {
74
0
        PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
75
0
        goto err;
76
0
    }
77
0
    btmp = NULL;
78
0
79
0
    return 1;
80
0
81
0
 err:
82
0
    BIO_free(btmp);
83
0
    return 0;
84
0
85
0
}
86
87
static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
88
                              unsigned char *key, int keylen)
89
0
{
90
0
    EVP_PKEY_CTX *pctx = NULL;
91
0
    EVP_PKEY *pkey = NULL;
92
0
    unsigned char *ek = NULL;
93
0
    int ret = 0;
94
0
    size_t eklen;
95
0
96
0
    pkey = X509_get0_pubkey(ri->cert);
97
0
98
0
    if (!pkey)
99
0
        return 0;
100
0
101
0
    pctx = EVP_PKEY_CTX_new(pkey, NULL);
102
0
    if (!pctx)
103
0
        return 0;
104
0
105
0
    if (EVP_PKEY_encrypt_init(pctx) <= 0)
106
0
        goto err;
107
0
108
0
    if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
109
0
                          EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) {
110
0
        PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR);
111
0
        goto err;
112
0
    }
113
0
114
0
    if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
115
0
        goto err;
116
0
117
0
    ek = OPENSSL_malloc(eklen);
118
0
119
0
    if (ek == NULL) {
120
0
        PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE);
121
0
        goto err;
122
0
    }
123
0
124
0
    if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
125
0
        goto err;
126
0
127
0
    ASN1_STRING_set0(ri->enc_key, ek, eklen);
128
0
    ek = NULL;
129
0
130
0
    ret = 1;
131
0
132
0
 err:
133
0
    EVP_PKEY_CTX_free(pctx);
134
0
    OPENSSL_free(ek);
135
0
    return ret;
136
0
137
0
}
138
139
static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
140
                               PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey)
141
0
{
142
0
    EVP_PKEY_CTX *pctx = NULL;
143
0
    unsigned char *ek = NULL;
144
0
    size_t eklen;
145
0
146
0
    int ret = -1;
147
0
148
0
    pctx = EVP_PKEY_CTX_new(pkey, NULL);
149
0
    if (!pctx)
150
0
        return -1;
151
0
152
0
    if (EVP_PKEY_decrypt_init(pctx) <= 0)
153
0
        goto err;
154
0
155
0
    if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
156
0
                          EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) {
157
0
        PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR);
158
0
        goto err;
159
0
    }
160
0
161
0
    if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
162
0
                         ri->enc_key->data, ri->enc_key->length) <= 0)
163
0
        goto err;
164
0
165
0
    ek = OPENSSL_malloc(eklen);
166
0
167
0
    if (ek == NULL) {
168
0
        PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE);
169
0
        goto err;
170
0
    }
171
0
172
0
    if (EVP_PKEY_decrypt(pctx, ek, &eklen,
173
0
                         ri->enc_key->data, ri->enc_key->length) <= 0) {
174
0
        ret = 0;
175
0
        PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB);
176
0
        goto err;
177
0
    }
178
0
179
0
    ret = 1;
180
0
181
0
    OPENSSL_clear_free(*pek, *peklen);
182
0
    *pek = ek;
183
0
    *peklen = eklen;
184
0
185
0
 err:
186
0
    EVP_PKEY_CTX_free(pctx);
187
0
    if (!ret)
188
0
        OPENSSL_free(ek);
189
0
190
0
    return ret;
191
0
}
192
193
BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
194
0
{
195
0
    int i;
196
0
    BIO *out = NULL, *btmp = NULL;
197
0
    X509_ALGOR *xa = NULL;
198
0
    const EVP_CIPHER *evp_cipher = NULL;
199
0
    STACK_OF(X509_ALGOR) *md_sk = NULL;
200
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
201
0
    X509_ALGOR *xalg = NULL;
202
0
    PKCS7_RECIP_INFO *ri = NULL;
203
0
    ASN1_OCTET_STRING *os = NULL;
204
0
205
0
    if (p7 == NULL) {
206
0
        PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_INVALID_NULL_POINTER);
207
0
        return NULL;
208
0
    }
209
0
    /*
210
0
     * The content field in the PKCS7 ContentInfo is optional, but that really
211
0
     * only applies to inner content (precisely, detached signatures).
212
0
     *
213
0
     * When reading content, missing outer content is therefore treated as an
214
0
     * error.
215
0
     *
216
0
     * When creating content, PKCS7_content_new() must be called before
217
0
     * calling this method, so a NULL p7->d is always an error.
218
0
     */
219
0
    if (p7->d.ptr == NULL) {
220
0
        PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_NO_CONTENT);
221
0
        return NULL;
222
0
    }
223
0
224
0
    i = OBJ_obj2nid(p7->type);
225
0
    p7->state = PKCS7_S_HEADER;
226
0
227
0
    switch (i) {
228
0
    case NID_pkcs7_signed:
229
0
        md_sk = p7->d.sign->md_algs;
230
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
231
0
        break;
232
0
    case NID_pkcs7_signedAndEnveloped:
233
0
        rsk = p7->d.signed_and_enveloped->recipientinfo;
234
0
        md_sk = p7->d.signed_and_enveloped->md_algs;
235
0
        xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
236
0
        evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
237
0
        if (evp_cipher == NULL) {
238
0
            PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
239
0
            goto err;
240
0
        }
241
0
        break;
242
0
    case NID_pkcs7_enveloped:
243
0
        rsk = p7->d.enveloped->recipientinfo;
244
0
        xalg = p7->d.enveloped->enc_data->algorithm;
245
0
        evp_cipher = p7->d.enveloped->enc_data->cipher;
246
0
        if (evp_cipher == NULL) {
247
0
            PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
248
0
            goto err;
249
0
        }
250
0
        break;
251
0
    case NID_pkcs7_digest:
252
0
        xa = p7->d.digest->md;
253
0
        os = PKCS7_get_octet_string(p7->d.digest->contents);
254
0
        break;
255
0
    case NID_pkcs7_data:
256
0
        break;
257
0
    default:
258
0
        PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
259
0
        goto err;
260
0
    }
261
0
262
0
    for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
263
0
        if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i)))
264
0
            goto err;
265
0
266
0
    if (xa && !PKCS7_bio_add_digest(&out, xa))
267
0
        goto err;
268
0
269
0
    if (evp_cipher != NULL) {
270
0
        unsigned char key[EVP_MAX_KEY_LENGTH];
271
0
        unsigned char iv[EVP_MAX_IV_LENGTH];
272
0
        int keylen, ivlen;
273
0
        EVP_CIPHER_CTX *ctx;
274
0
275
0
        if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
276
0
            PKCS7err(PKCS7_F_PKCS7_DATAINIT, ERR_R_BIO_LIB);
277
0
            goto err;
278
0
        }
279
0
        BIO_get_cipher_ctx(btmp, &ctx);
280
0
        keylen = EVP_CIPHER_key_length(evp_cipher);
281
0
        ivlen = EVP_CIPHER_iv_length(evp_cipher);
282
0
        xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher));
283
0
        if (ivlen > 0)
284
0
            if (RAND_bytes(iv, ivlen) <= 0)
285
0
                goto err;
286
0
        if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1) <= 0)
287
0
            goto err;
288
0
        if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
289
0
            goto err;
290
0
        if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
291
0
            goto err;
292
0
293
0
        if (ivlen > 0) {
294
0
            if (xalg->parameter == NULL) {
295
0
                xalg->parameter = ASN1_TYPE_new();
296
0
                if (xalg->parameter == NULL)
297
0
                    goto err;
298
0
            }
299
0
            if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)
300
0
                goto err;
301
0
        }
302
0
303
0
        /* Lets do the pub key stuff :-) */
304
0
        for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
305
0
            ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
306
0
            if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
307
0
                goto err;
308
0
        }
309
0
        OPENSSL_cleanse(key, keylen);
310
0
311
0
        if (out == NULL)
312
0
            out = btmp;
313
0
        else
314
0
            BIO_push(out, btmp);
315
0
        btmp = NULL;
316
0
    }
317
0
318
0
    if (bio == NULL) {
319
0
        if (PKCS7_is_detached(p7)) {
320
0
            bio = BIO_new(BIO_s_null());
321
0
        } else if (os && os->length > 0) {
322
0
            bio = BIO_new_mem_buf(os->data, os->length);
323
0
        } else {
324
0
            bio = BIO_new(BIO_s_mem());
325
0
            if (bio == NULL)
326
0
                goto err;
327
0
            BIO_set_mem_eof_return(bio, 0);
328
0
        }
329
0
        if (bio == NULL)
330
0
            goto err;
331
0
    }
332
0
    if (out)
333
0
        BIO_push(out, bio);
334
0
    else
335
0
        out = bio;
336
0
    return out;
337
0
338
0
 err:
339
0
    BIO_free_all(out);
340
0
    BIO_free_all(btmp);
341
0
    return NULL;
342
0
}
343
344
static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
345
0
{
346
0
    int ret;
347
0
    ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
348
0
                        X509_get_issuer_name(pcert));
349
0
    if (ret)
350
0
        return ret;
351
0
    return ASN1_INTEGER_cmp(X509_get_serialNumber(pcert),
352
0
                            ri->issuer_and_serial->serial);
353
0
}
354
355
/* int */
356
BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
357
0
{
358
0
    int i, j;
359
0
    BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
360
0
    X509_ALGOR *xa;
361
0
    ASN1_OCTET_STRING *data_body = NULL;
362
0
    const EVP_MD *evp_md;
363
0
    const EVP_CIPHER *evp_cipher = NULL;
364
0
    EVP_CIPHER_CTX *evp_ctx = NULL;
365
0
    X509_ALGOR *enc_alg = NULL;
366
0
    STACK_OF(X509_ALGOR) *md_sk = NULL;
367
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
368
0
    PKCS7_RECIP_INFO *ri = NULL;
369
0
    unsigned char *ek = NULL, *tkey = NULL;
370
0
    int eklen = 0, tkeylen = 0;
371
0
372
0
    if (p7 == NULL) {
373
0
        PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_INVALID_NULL_POINTER);
374
0
        return NULL;
375
0
    }
376
0
377
0
    if (p7->d.ptr == NULL) {
378
0
        PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
379
0
        return NULL;
380
0
    }
381
0
382
0
    i = OBJ_obj2nid(p7->type);
383
0
    p7->state = PKCS7_S_HEADER;
384
0
385
0
    switch (i) {
386
0
    case NID_pkcs7_signed:
387
0
        /*
388
0
         * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
389
0
         * field and optional content.
390
0
         * data_body is NULL if that structure has no (=detached) content
391
0
         * or if the contentType is wrong (i.e., not "data").
392
0
         */
393
0
        data_body = PKCS7_get_octet_string(p7->d.sign->contents);
394
0
        if (!PKCS7_is_detached(p7) && data_body == NULL) {
395
0
            PKCS7err(PKCS7_F_PKCS7_DATADECODE,
396
0
                     PKCS7_R_INVALID_SIGNED_DATA_TYPE);
397
0
            goto err;
398
0
        }
399
0
        md_sk = p7->d.sign->md_algs;
400
0
        break;
401
0
    case NID_pkcs7_signedAndEnveloped:
402
0
        rsk = p7->d.signed_and_enveloped->recipientinfo;
403
0
        md_sk = p7->d.signed_and_enveloped->md_algs;
404
0
        /* data_body is NULL if the optional EncryptedContent is missing. */
405
0
        data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
406
0
        enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
407
0
        evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm);
408
0
        if (evp_cipher == NULL) {
409
0
            PKCS7err(PKCS7_F_PKCS7_DATADECODE,
410
0
                     PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
411
0
            goto err;
412
0
        }
413
0
        break;
414
0
    case NID_pkcs7_enveloped:
415
0
        rsk = p7->d.enveloped->recipientinfo;
416
0
        enc_alg = p7->d.enveloped->enc_data->algorithm;
417
0
        /* data_body is NULL if the optional EncryptedContent is missing. */
418
0
        data_body = p7->d.enveloped->enc_data->enc_data;
419
0
        evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm);
420
0
        if (evp_cipher == NULL) {
421
0
            PKCS7err(PKCS7_F_PKCS7_DATADECODE,
422
0
                     PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
423
0
            goto err;
424
0
        }
425
0
        break;
426
0
    default:
427
0
        PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
428
0
        goto err;
429
0
    }
430
0
431
0
    /* Detached content must be supplied via in_bio instead. */
432
0
    if (data_body == NULL && in_bio == NULL) {
433
0
        PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
434
0
        goto err;
435
0
    }
436
0
437
0
    /* We will be checking the signature */
438
0
    if (md_sk != NULL) {
439
0
        for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
440
0
            xa = sk_X509_ALGOR_value(md_sk, i);
441
0
            if ((btmp = BIO_new(BIO_f_md())) == NULL) {
442
0
                PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
443
0
                goto err;
444
0
            }
445
0
446
0
            j = OBJ_obj2nid(xa->algorithm);
447
0
            evp_md = EVP_get_digestbynid(j);
448
0
            if (evp_md == NULL) {
449
0
                PKCS7err(PKCS7_F_PKCS7_DATADECODE,
450
0
                         PKCS7_R_UNKNOWN_DIGEST_TYPE);
451
0
                goto err;
452
0
            }
453
0
454
0
            BIO_set_md(btmp, evp_md);
455
0
            if (out == NULL)
456
0
                out = btmp;
457
0
            else
458
0
                BIO_push(out, btmp);
459
0
            btmp = NULL;
460
0
        }
461
0
    }
462
0
463
0
    if (evp_cipher != NULL) {
464
0
        if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
465
0
            PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
466
0
            goto err;
467
0
        }
468
0
469
0
        /*
470
0
         * It was encrypted, we need to decrypt the secret key with the
471
0
         * private key
472
0
         */
473
0
474
0
        /*
475
0
         * Find the recipientInfo which matches the passed certificate (if
476
0
         * any)
477
0
         */
478
0
479
0
        if (pcert) {
480
0
            for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
481
0
                ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
482
0
                if (!pkcs7_cmp_ri(ri, pcert))
483
0
                    break;
484
0
                ri = NULL;
485
0
            }
486
0
            if (ri == NULL) {
487
0
                PKCS7err(PKCS7_F_PKCS7_DATADECODE,
488
0
                         PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
489
0
                goto err;
490
0
            }
491
0
        }
492
0
493
0
        /* If we haven't got a certificate try each ri in turn */
494
0
        if (pcert == NULL) {
495
0
            /*
496
0
             * Always attempt to decrypt all rinfo even after success as a
497
0
             * defence against MMA timing attacks.
498
0
             */
499
0
            for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
500
0
                ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
501
0
502
0
                if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0)
503
0
                    goto err;
504
0
                ERR_clear_error();
505
0
            }
506
0
        } else {
507
0
            /* Only exit on fatal errors, not decrypt failure */
508
0
            if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0)
509
0
                goto err;
510
0
            ERR_clear_error();
511
0
        }
512
0
513
0
        evp_ctx = NULL;
514
0
        BIO_get_cipher_ctx(etmp, &evp_ctx);
515
0
        if (EVP_CipherInit_ex(evp_ctx, evp_cipher, NULL, NULL, NULL, 0) <= 0)
516
0
            goto err;
517
0
        if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) < 0)
518
0
            goto err;
519
0
        /* Generate random key as MMA defence */
520
0
        tkeylen = EVP_CIPHER_CTX_key_length(evp_ctx);
521
0
        tkey = OPENSSL_malloc(tkeylen);
522
0
        if (tkey == NULL)
523
0
            goto err;
524
0
        if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
525
0
            goto err;
526
0
        if (ek == NULL) {
527
0
            ek = tkey;
528
0
            eklen = tkeylen;
529
0
            tkey = NULL;
530
0
        }
531
0
532
0
        if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) {
533
0
            /*
534
0
             * Some S/MIME clients don't use the same key and effective key
535
0
             * length. The key length is determined by the size of the
536
0
             * decrypted RSA key.
537
0
             */
538
0
            if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) {
539
0
                /* Use random key as MMA defence */
540
0
                OPENSSL_clear_free(ek, eklen);
541
0
                ek = tkey;
542
0
                eklen = tkeylen;
543
0
                tkey = NULL;
544
0
            }
545
0
        }
546
0
        /* Clear errors so we don't leak information useful in MMA */
547
0
        ERR_clear_error();
548
0
        if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
549
0
            goto err;
550
0
551
0
        OPENSSL_clear_free(ek, eklen);
552
0
        ek = NULL;
553
0
        OPENSSL_clear_free(tkey, tkeylen);
554
0
        tkey = NULL;
555
0
556
0
        if (out == NULL)
557
0
            out = etmp;
558
0
        else
559
0
            BIO_push(out, etmp);
560
0
        etmp = NULL;
561
0
    }
562
0
    if (in_bio != NULL) {
563
0
        bio = in_bio;
564
0
    } else {
565
0
        if (data_body->length > 0)
566
0
            bio = BIO_new_mem_buf(data_body->data, data_body->length);
567
0
        else {
568
0
            bio = BIO_new(BIO_s_mem());
569
0
            if (bio == NULL)
570
0
                goto err;
571
0
            BIO_set_mem_eof_return(bio, 0);
572
0
        }
573
0
        if (bio == NULL)
574
0
            goto err;
575
0
    }
576
0
    BIO_push(out, bio);
577
0
    bio = NULL;
578
0
    return out;
579
0
580
0
 err:
581
0
    OPENSSL_clear_free(ek, eklen);
582
0
    OPENSSL_clear_free(tkey, tkeylen);
583
0
    BIO_free_all(out);
584
0
    BIO_free_all(btmp);
585
0
    BIO_free_all(etmp);
586
0
    BIO_free_all(bio);
587
0
    return NULL;
588
0
}
589
590
static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
591
0
{
592
0
    for (;;) {
593
0
        bio = BIO_find_type(bio, BIO_TYPE_MD);
594
0
        if (bio == NULL) {
595
0
            PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,
596
0
                     PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
597
0
            return NULL;
598
0
        }
599
0
        BIO_get_md_ctx(bio, pmd);
600
0
        if (*pmd == NULL) {
601
0
            PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, ERR_R_INTERNAL_ERROR);
602
0
            return NULL;
603
0
        }
604
0
        if (EVP_MD_CTX_type(*pmd) == nid)
605
0
            return bio;
606
0
        bio = BIO_next(bio);
607
0
    }
608
0
    return NULL;
609
0
}
610
611
static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
612
0
{
613
0
    unsigned char md_data[EVP_MAX_MD_SIZE];
614
0
    unsigned int md_len;
615
0
616
0
    /* Add signing time if not already present */
617
0
    if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
618
0
        if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
619
0
            PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
620
0
            return 0;
621
0
        }
622
0
    }
623
0
624
0
    /* Add digest */
625
0
    if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
626
0
        PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
627
0
        return 0;
628
0
    }
629
0
    if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
630
0
        PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
631
0
        return 0;
632
0
    }
633
0
634
0
    /* Now sign the attributes */
635
0
    if (!PKCS7_SIGNER_INFO_sign(si))
636
0
        return 0;
637
0
638
0
    return 1;
639
0
}
640
641
int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
642
0
{
643
0
    int ret = 0;
644
0
    int i, j;
645
0
    BIO *btmp;
646
0
    PKCS7_SIGNER_INFO *si;
647
0
    EVP_MD_CTX *mdc, *ctx_tmp;
648
0
    STACK_OF(X509_ATTRIBUTE) *sk;
649
0
    STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
650
0
    ASN1_OCTET_STRING *os = NULL;
651
0
652
0
    if (p7 == NULL) {
653
0
        PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_INVALID_NULL_POINTER);
654
0
        return 0;
655
0
    }
656
0
657
0
    if (p7->d.ptr == NULL) {
658
0
        PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
659
0
        return 0;
660
0
    }
661
0
662
0
    ctx_tmp = EVP_MD_CTX_new();
663
0
    if (ctx_tmp == NULL) {
664
0
        PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
665
0
        return 0;
666
0
    }
667
0
668
0
    i = OBJ_obj2nid(p7->type);
669
0
    p7->state = PKCS7_S_HEADER;
670
0
671
0
    switch (i) {
672
0
    case NID_pkcs7_data:
673
0
        os = p7->d.data;
674
0
        break;
675
0
    case NID_pkcs7_signedAndEnveloped:
676
0
        /* XXXXXXXXXXXXXXXX */
677
0
        si_sk = p7->d.signed_and_enveloped->signer_info;
678
0
        os = p7->d.signed_and_enveloped->enc_data->enc_data;
679
0
        if (os == NULL) {
680
0
            os = ASN1_OCTET_STRING_new();
681
0
            if (os == NULL) {
682
0
                PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
683
0
                goto err;
684
0
            }
685
0
            p7->d.signed_and_enveloped->enc_data->enc_data = os;
686
0
        }
687
0
        break;
688
0
    case NID_pkcs7_enveloped:
689
0
        /* XXXXXXXXXXXXXXXX */
690
0
        os = p7->d.enveloped->enc_data->enc_data;
691
0
        if (os == NULL) {
692
0
            os = ASN1_OCTET_STRING_new();
693
0
            if (os == NULL) {
694
0
                PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
695
0
                goto err;
696
0
            }
697
0
            p7->d.enveloped->enc_data->enc_data = os;
698
0
        }
699
0
        break;
700
0
    case NID_pkcs7_signed:
701
0
        si_sk = p7->d.sign->signer_info;
702
0
        os = PKCS7_get_octet_string(p7->d.sign->contents);
703
0
        /* If detached data then the content is excluded */
704
0
        if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
705
0
            ASN1_OCTET_STRING_free(os);
706
0
            os = NULL;
707
0
            p7->d.sign->contents->d.data = NULL;
708
0
        }
709
0
        break;
710
0
711
0
    case NID_pkcs7_digest:
712
0
        os = PKCS7_get_octet_string(p7->d.digest->contents);
713
0
        /* If detached data then the content is excluded */
714
0
        if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
715
0
            ASN1_OCTET_STRING_free(os);
716
0
            os = NULL;
717
0
            p7->d.digest->contents->d.data = NULL;
718
0
        }
719
0
        break;
720
0
721
0
    default:
722
0
        PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
723
0
        goto err;
724
0
    }
725
0
726
0
    if (si_sk != NULL) {
727
0
        for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
728
0
            si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
729
0
            if (si->pkey == NULL)
730
0
                continue;
731
0
732
0
            j = OBJ_obj2nid(si->digest_alg->algorithm);
733
0
734
0
            btmp = bio;
735
0
736
0
            btmp = PKCS7_find_digest(&mdc, btmp, j);
737
0
738
0
            if (btmp == NULL)
739
0
                goto err;
740
0
741
0
            /*
742
0
             * We now have the EVP_MD_CTX, lets do the signing.
743
0
             */
744
0
            if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
745
0
                goto err;
746
0
747
0
            sk = si->auth_attr;
748
0
749
0
            /*
750
0
             * If there are attributes, we add the digest attribute and only
751
0
             * sign the attributes
752
0
             */
753
0
            if (sk_X509_ATTRIBUTE_num(sk) > 0) {
754
0
                if (!do_pkcs7_signed_attrib(si, ctx_tmp))
755
0
                    goto err;
756
0
            } else {
757
0
                unsigned char *abuf = NULL;
758
0
                unsigned int abuflen;
759
0
                abuflen = EVP_PKEY_size(si->pkey);
760
0
                abuf = OPENSSL_malloc(abuflen);
761
0
                if (abuf == NULL)
762
0
                    goto err;
763
0
764
0
                if (!EVP_SignFinal(ctx_tmp, abuf, &abuflen, si->pkey)) {
765
0
                    OPENSSL_free(abuf);
766
0
                    PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_EVP_LIB);
767
0
                    goto err;
768
0
                }
769
0
                ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
770
0
            }
771
0
        }
772
0
    } else if (i == NID_pkcs7_digest) {
773
0
        unsigned char md_data[EVP_MAX_MD_SIZE];
774
0
        unsigned int md_len;
775
0
        if (!PKCS7_find_digest(&mdc, bio,
776
0
                               OBJ_obj2nid(p7->d.digest->md->algorithm)))
777
0
            goto err;
778
0
        if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
779
0
            goto err;
780
0
        if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
781
0
            goto err;
782
0
    }
783
0
784
0
    if (!PKCS7_is_detached(p7)) {
785
0
        /*
786
0
         * NOTE(emilia): I think we only reach os == NULL here because detached
787
0
         * digested data support is broken.
788
0
         */
789
0
        if (os == NULL)
790
0
            goto err;
791
0
        if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
792
0
            char *cont;
793
0
            long contlen;
794
0
            btmp = BIO_find_type(bio, BIO_TYPE_MEM);
795
0
            if (btmp == NULL) {
796
0
                PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
797
0
                goto err;
798
0
            }
799
0
            contlen = BIO_get_mem_data(btmp, &cont);
800
0
            /*
801
0
             * Mark the BIO read only then we can use its copy of the data
802
0
             * instead of making an extra copy.
803
0
             */
804
0
            BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
805
0
            BIO_set_mem_eof_return(btmp, 0);
806
0
            ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
807
0
        }
808
0
    }
809
0
    ret = 1;
810
0
 err:
811
0
    EVP_MD_CTX_free(ctx_tmp);
812
0
    return ret;
813
0
}
814
815
int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
816
0
{
817
0
    EVP_MD_CTX *mctx;
818
0
    EVP_PKEY_CTX *pctx = NULL;
819
0
    unsigned char *abuf = NULL;
820
0
    int alen;
821
0
    size_t siglen;
822
0
    const EVP_MD *md = NULL;
823
0
824
0
    md = EVP_get_digestbyobj(si->digest_alg->algorithm);
825
0
    if (md == NULL)
826
0
        return 0;
827
0
828
0
    mctx = EVP_MD_CTX_new();
829
0
    if (mctx == NULL) {
830
0
        PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, ERR_R_MALLOC_FAILURE);
831
0
        goto err;
832
0
    }
833
0
834
0
    if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
835
0
        goto err;
836
0
837
0
    if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
838
0
                          EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) {
839
0
        PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
840
0
        goto err;
841
0
    }
842
0
843
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
844
0
                         ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
845
0
    if (!abuf)
846
0
        goto err;
847
0
    if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
848
0
        goto err;
849
0
    OPENSSL_free(abuf);
850
0
    abuf = NULL;
851
0
    if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
852
0
        goto err;
853
0
    abuf = OPENSSL_malloc(siglen);
854
0
    if (abuf == NULL)
855
0
        goto err;
856
0
    if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
857
0
        goto err;
858
0
859
0
    if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
860
0
                          EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) {
861
0
        PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
862
0
        goto err;
863
0
    }
864
0
865
0
    EVP_MD_CTX_free(mctx);
866
0
867
0
    ASN1_STRING_set0(si->enc_digest, abuf, siglen);
868
0
869
0
    return 1;
870
0
871
0
 err:
872
0
    OPENSSL_free(abuf);
873
0
    EVP_MD_CTX_free(mctx);
874
0
    return 0;
875
0
876
0
}
877
878
int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
879
                     PKCS7 *p7, PKCS7_SIGNER_INFO *si)
880
0
{
881
0
    PKCS7_ISSUER_AND_SERIAL *ias;
882
0
    int ret = 0, i;
883
0
    STACK_OF(X509) *cert;
884
0
    X509 *x509;
885
0
886
0
    if (p7 == NULL) {
887
0
        PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_INVALID_NULL_POINTER);
888
0
        return 0;
889
0
    }
890
0
891
0
    if (p7->d.ptr == NULL) {
892
0
        PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
893
0
        return 0;
894
0
    }
895
0
896
0
    if (PKCS7_type_is_signed(p7)) {
897
0
        cert = p7->d.sign->cert;
898
0
    } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
899
0
        cert = p7->d.signed_and_enveloped->cert;
900
0
    } else {
901
0
        PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
902
0
        goto err;
903
0
    }
904
0
    /* XXXXXXXXXXXXXXXXXXXXXXX */
905
0
    ias = si->issuer_and_serial;
906
0
907
0
    x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
908
0
909
0
    /* were we able to find the cert in passed to us */
910
0
    if (x509 == NULL) {
911
0
        PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
912
0
                 PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
913
0
        goto err;
914
0
    }
915
0
916
0
    /* Lets verify */
917
0
    if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
918
0
        PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
919
0
        goto err;
920
0
    }
921
0
    X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
922
0
    i = X509_verify_cert(ctx);
923
0
    if (i <= 0) {
924
0
        PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
925
0
        X509_STORE_CTX_cleanup(ctx);
926
0
        goto err;
927
0
    }
928
0
    X509_STORE_CTX_cleanup(ctx);
929
0
930
0
    return PKCS7_signatureVerify(bio, p7, si, x509);
931
0
 err:
932
0
    return ret;
933
0
}
934
935
int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
936
                          X509 *x509)
937
0
{
938
0
    ASN1_OCTET_STRING *os;
939
0
    EVP_MD_CTX *mdc_tmp, *mdc;
940
0
    int ret = 0, i;
941
0
    int md_type;
942
0
    STACK_OF(X509_ATTRIBUTE) *sk;
943
0
    BIO *btmp;
944
0
    EVP_PKEY *pkey;
945
0
946
0
    mdc_tmp = EVP_MD_CTX_new();
947
0
    if (mdc_tmp == NULL) {
948
0
        PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_MALLOC_FAILURE);
949
0
        goto err;
950
0
    }
951
0
952
0
    if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
953
0
        PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
954
0
        goto err;
955
0
    }
956
0
957
0
    md_type = OBJ_obj2nid(si->digest_alg->algorithm);
958
0
959
0
    btmp = bio;
960
0
    for (;;) {
961
0
        if ((btmp == NULL) ||
962
0
            ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
963
0
            PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
964
0
                     PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
965
0
            goto err;
966
0
        }
967
0
        BIO_get_md_ctx(btmp, &mdc);
968
0
        if (mdc == NULL) {
969
0
            PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR);
970
0
            goto err;
971
0
        }
972
0
        if (EVP_MD_CTX_type(mdc) == md_type)
973
0
            break;
974
0
        /*
975
0
         * Workaround for some broken clients that put the signature OID
976
0
         * instead of the digest OID in digest_alg->algorithm
977
0
         */
978
0
        if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
979
0
            break;
980
0
        btmp = BIO_next(btmp);
981
0
    }
982
0
983
0
    /*
984
0
     * mdc is the digest ctx that we want, unless there are attributes, in
985
0
     * which case the digest is the signed attributes
986
0
     */
987
0
    if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
988
0
        goto err;
989
0
990
0
    sk = si->auth_attr;
991
0
    if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
992
0
        unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
993
0
        unsigned int md_len;
994
0
        int alen;
995
0
        ASN1_OCTET_STRING *message_digest;
996
0
997
0
        if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
998
0
            goto err;
999
0
        message_digest = PKCS7_digest_from_attributes(sk);
1000
0
        if (!message_digest) {
1001
0
            PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
1002
0
                     PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1003
0
            goto err;
1004
0
        }
1005
0
        if ((message_digest->length != (int)md_len) ||
1006
0
            (memcmp(message_digest->data, md_dat, md_len))) {
1007
0
            PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE);
1008
0
            ret = -1;
1009
0
            goto err;
1010
0
        }
1011
0
1012
0
        if (!EVP_VerifyInit_ex(mdc_tmp, EVP_get_digestbynid(md_type), NULL))
1013
0
            goto err;
1014
0
1015
0
        alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1016
0
                             ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1017
0
        if (alen <= 0) {
1018
0
            PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB);
1019
0
            ret = -1;
1020
0
            goto err;
1021
0
        }
1022
0
        if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1023
0
            goto err;
1024
0
1025
0
        OPENSSL_free(abuf);
1026
0
    }
1027
0
1028
0
    os = si->enc_digest;
1029
0
    pkey = X509_get0_pubkey(x509);
1030
0
    if (!pkey) {
1031
0
        ret = -1;
1032
0
        goto err;
1033
0
    }
1034
0
1035
0
    i = EVP_VerifyFinal(mdc_tmp, os->data, os->length, pkey);
1036
0
    if (i <= 0) {
1037
0
        PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE);
1038
0
        ret = -1;
1039
0
        goto err;
1040
0
    }
1041
0
    ret = 1;
1042
0
 err:
1043
0
    EVP_MD_CTX_free(mdc_tmp);
1044
0
    return ret;
1045
0
}
1046
1047
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1048
0
{
1049
0
    STACK_OF(PKCS7_RECIP_INFO) *rsk;
1050
0
    PKCS7_RECIP_INFO *ri;
1051
0
    int i;
1052
0
1053
0
    i = OBJ_obj2nid(p7->type);
1054
0
    if (i != NID_pkcs7_signedAndEnveloped)
1055
0
        return NULL;
1056
0
    if (p7->d.signed_and_enveloped == NULL)
1057
0
        return NULL;
1058
0
    rsk = p7->d.signed_and_enveloped->recipientinfo;
1059
0
    if (rsk == NULL)
1060
0
        return NULL;
1061
0
    if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1062
0
        return NULL;
1063
0
    ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1064
0
    return ri->issuer_and_serial;
1065
0
}
1066
1067
ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
1068
0
{
1069
0
    return get_attribute(si->auth_attr, nid);
1070
0
}
1071
1072
ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
1073
0
{
1074
0
    return get_attribute(si->unauth_attr, nid);
1075
0
}
1076
1077
static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1078
0
{
1079
0
    int idx;
1080
0
    X509_ATTRIBUTE *xa;
1081
0
    idx = X509at_get_attr_by_NID(sk, nid, -1);
1082
0
    xa = X509at_get_attr(sk, idx);
1083
0
    return X509_ATTRIBUTE_get0_type(xa, 0);
1084
0
}
1085
1086
ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1087
0
{
1088
0
    ASN1_TYPE *astype;
1089
0
    if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1090
0
        return NULL;
1091
0
    return astype->value.octet_string;
1092
0
}
1093
1094
int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1095
                                STACK_OF(X509_ATTRIBUTE) *sk)
1096
0
{
1097
0
    int i;
1098
0
1099
0
    sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1100
0
    p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1101
0
    if (p7si->auth_attr == NULL)
1102
0
        return 0;
1103
0
    for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1104
0
        if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1105
0
                                   X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1106
0
                                                      (sk, i))))
1107
0
            == NULL)
1108
0
            return 0;
1109
0
    }
1110
0
    return 1;
1111
0
}
1112
1113
int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1114
                         STACK_OF(X509_ATTRIBUTE) *sk)
1115
0
{
1116
0
    int i;
1117
0
1118
0
    sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1119
0
    p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1120
0
    if (p7si->unauth_attr == NULL)
1121
0
        return 0;
1122
0
    for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1123
0
        if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1124
0
                                   X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1125
0
                                                      (sk, i))))
1126
0
            == NULL)
1127
0
            return 0;
1128
0
    }
1129
0
    return 1;
1130
0
}
1131
1132
int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1133
                               void *value)
1134
0
{
1135
0
    return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1136
0
}
1137
1138
int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1139
                        void *value)
1140
0
{
1141
0
    return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1142
0
}
1143
1144
static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1145
                         void *value)
1146
0
{
1147
0
    X509_ATTRIBUTE *attr = NULL;
1148
0
1149
0
    if (*sk == NULL) {
1150
0
        if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1151
0
            return 0;
1152
0
 new_attrib:
1153
0
        if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
1154
0
            return 0;
1155
0
        if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
1156
0
            X509_ATTRIBUTE_free(attr);
1157
0
            return 0;
1158
0
        }
1159
0
    } else {
1160
0
        int i;
1161
0
1162
0
        for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
1163
0
            attr = sk_X509_ATTRIBUTE_value(*sk, i);
1164
0
            if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
1165
0
                X509_ATTRIBUTE_free(attr);
1166
0
                attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1167
0
                if (attr == NULL)
1168
0
                    return 0;
1169
0
                if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
1170
0
                    X509_ATTRIBUTE_free(attr);
1171
0
                    return 0;
1172
0
                }
1173
0
                goto end;
1174
0
            }
1175
0
        }
1176
0
        goto new_attrib;
1177
0
    }
1178
0
 end:
1179
0
    return 1;
1180
0
}