Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/pkcs7/pk7_attr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <openssl/bio.h>
13
#include <openssl/asn1.h>
14
#include <openssl/asn1t.h>
15
#include <openssl/pem.h>
16
#include <openssl/pkcs7.h>
17
#include <openssl/x509.h>
18
#include <openssl/err.h>
19
20
int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
21
                              STACK_OF(X509_ALGOR) *cap)
22
0
{
23
0
    ASN1_STRING *seq;
24
25
0
    if ((seq = ASN1_STRING_new()) == NULL) {
26
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
27
0
        return 0;
28
0
    }
29
0
    seq->length = ASN1_item_i2d((ASN1_VALUE *)cap, &seq->data,
30
0
                                ASN1_ITEM_rptr(X509_ALGORS));
31
0
    if (!PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
32
0
                                    V_ASN1_SEQUENCE, seq)) {
33
0
        ASN1_STRING_free(seq);
34
0
        return 0;
35
0
    }
36
0
    return 1;
37
0
}
38
39
STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
40
95
{
41
95
    ASN1_TYPE *cap;
42
95
    const unsigned char *p;
43
44
95
    cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
45
95
    if (cap == NULL || (cap->type != V_ASN1_SEQUENCE))
46
94
        return NULL;
47
1
    p = cap->value.sequence->data;
48
1
    return (STACK_OF(X509_ALGOR) *)
49
1
        ASN1_item_d2i(NULL, &p, cap->value.sequence->length,
50
1
                      ASN1_ITEM_rptr(X509_ALGORS));
51
95
}
52
53
/* Basic smime-capabilities OID and optional integer arg */
54
int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
55
0
{
56
0
    ASN1_INTEGER *nbit = NULL;
57
0
    X509_ALGOR *alg;
58
59
0
    if ((alg = X509_ALGOR_new()) == NULL) {
60
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
61
0
        return 0;
62
0
    }
63
0
    ASN1_OBJECT_free(alg->algorithm);
64
0
    alg->algorithm = OBJ_nid2obj(nid);
65
0
    if (arg > 0) {
66
0
        if ((alg->parameter = ASN1_TYPE_new()) == NULL) {
67
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
68
0
            goto err;
69
0
        }
70
0
        if ((nbit = ASN1_INTEGER_new()) == NULL) {
71
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
72
0
            goto err;
73
0
        }
74
0
        if (!ASN1_INTEGER_set(nbit, arg)) {
75
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
76
0
            goto err;
77
0
        }
78
0
        alg->parameter->value.integer = nbit;
79
0
        alg->parameter->type = V_ASN1_INTEGER;
80
0
        nbit = NULL;
81
0
    }
82
0
    if (!sk_X509_ALGOR_push(sk, alg)) {
83
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
84
0
        goto err;
85
0
    }
86
0
    return 1;
87
0
err:
88
0
    ASN1_INTEGER_free(nbit);
89
0
    X509_ALGOR_free(alg);
90
0
    return 0;
91
0
}
92
93
int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
94
0
{
95
0
    if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
96
0
        return 0;
97
0
    if (!coid)
98
0
        coid = OBJ_nid2obj(NID_pkcs7_data);
99
0
    return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
100
0
                                      V_ASN1_OBJECT, coid);
101
0
}
102
103
int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
104
0
{
105
0
    ASN1_TIME *tmp = NULL;
106
107
0
    if (t == NULL && (tmp = t = X509_gmtime_adj(NULL, 0)) == NULL) {
108
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
109
0
        return 0;
110
0
    }
111
0
    if (!PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
112
0
                                    V_ASN1_UTCTIME, t)) {
113
0
        ASN1_TIME_free(tmp);
114
0
        return 0;
115
0
    }
116
0
    return 1;
117
0
}
118
119
int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
120
                             const unsigned char *md, int mdlen)
121
0
{
122
0
    ASN1_OCTET_STRING *os;
123
0
    os = ASN1_OCTET_STRING_new();
124
0
    if (os == NULL)
125
0
        return 0;
126
0
    if (!ASN1_STRING_set(os, md, mdlen)
127
0
        || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
128
0
                                       V_ASN1_OCTET_STRING, os)) {
129
0
        ASN1_OCTET_STRING_free(os);
130
0
        return 0;
131
0
    }
132
0
    return 1;
133
0
}