Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/cms/cms_att.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2024 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 <openssl/asn1t.h>
11
#include <openssl/pem.h>
12
#include <openssl/x509v3.h>
13
#include <openssl/err.h>
14
#include <openssl/cms.h>
15
#include "internal/nelem.h"
16
#include "crypto/x509.h"
17
#include "cms_local.h"
18
19
/*-
20
 * Attribute flags.
21
 * CMS attribute restrictions are discussed in
22
 *  - RFC 5652 Section 11.
23
 * ESS attribute restrictions are discussed in
24
 *  - RFC 2634 Section 1.3.4  AND
25
 *  - RFC 5035 Section 5.4
26
 */
27
/* This is a signed attribute */
28
0
#define CMS_ATTR_F_SIGNED 0x01
29
/* This is an unsigned attribute */
30
0
#define CMS_ATTR_F_UNSIGNED 0x02
31
/* Must be present if there are any other attributes of the same type */
32
0
#define CMS_ATTR_F_REQUIRED_COND 0x10
33
/* There can only be one instance of this attribute */
34
0
#define CMS_ATTR_F_ONLY_ONE 0x20
35
/* The Attribute's value must have exactly one entry */
36
0
#define CMS_ATTR_F_ONE_ATTR_VALUE 0x40
37
38
/* Attributes rules for different attributes */
39
static const struct {
40
    int nid; /* The attribute id */
41
    int flags;
42
} cms_attribute_properties[] = {
43
    /* See RFC Section 11 */
44
    { NID_pkcs9_contentType, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE | CMS_ATTR_F_REQUIRED_COND },
45
    { NID_pkcs9_messageDigest, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE | CMS_ATTR_F_REQUIRED_COND },
46
    { NID_pkcs9_signingTime, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE },
47
    { NID_pkcs9_countersignature, CMS_ATTR_F_UNSIGNED },
48
    /* ESS */
49
    { NID_id_smime_aa_signingCertificate, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE },
50
    { NID_id_smime_aa_signingCertificateV2, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE },
51
    { NID_id_smime_aa_receiptRequest, CMS_ATTR_F_SIGNED | CMS_ATTR_F_ONLY_ONE | CMS_ATTR_F_ONE_ATTR_VALUE }
52
};
53
54
/* CMS SignedData Attribute utilities */
55
56
int CMS_signed_get_attr_count(const CMS_SignerInfo *si)
57
0
{
58
0
    return X509at_get_attr_count(si->signedAttrs);
59
0
}
60
61
int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
62
0
{
63
0
    return X509at_get_attr_by_NID(si->signedAttrs, nid, lastpos);
64
0
}
65
66
int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
67
    int lastpos)
68
0
{
69
0
    return X509at_get_attr_by_OBJ(si->signedAttrs, obj, lastpos);
70
0
}
71
72
X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc)
73
0
{
74
0
    return X509at_get_attr(si->signedAttrs, loc);
75
0
}
76
77
X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc)
78
0
{
79
0
    return X509at_delete_attr(si->signedAttrs, loc);
80
0
}
81
82
int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
83
0
{
84
0
    if (ossl_x509at_add1_attr(&si->signedAttrs, attr))
85
0
        return 1;
86
0
    return 0;
87
0
}
88
89
int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si,
90
    const ASN1_OBJECT *obj, int type,
91
    const void *bytes, int len)
92
0
{
93
0
    if (ossl_x509at_add1_attr_by_OBJ(&si->signedAttrs, obj, type, bytes, len))
94
0
        return 1;
95
0
    return 0;
96
0
}
97
98
int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si,
99
    int nid, int type, const void *bytes, int len)
100
0
{
101
0
    if (ossl_x509at_add1_attr_by_NID(&si->signedAttrs, nid, type, bytes, len))
102
0
        return 1;
103
0
    return 0;
104
0
}
105
106
int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si,
107
    const char *attrname, int type,
108
    const void *bytes, int len)
109
0
{
110
0
    if (ossl_x509at_add1_attr_by_txt(&si->signedAttrs, attrname, type, bytes,
111
0
            len))
112
0
        return 1;
113
0
    return 0;
114
0
}
115
116
void *CMS_signed_get0_data_by_OBJ(const CMS_SignerInfo *si,
117
    const ASN1_OBJECT *oid,
118
    int lastpos, int type)
119
0
{
120
0
    return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type);
121
0
}
122
123
int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si)
124
0
{
125
0
    return X509at_get_attr_count(si->unsignedAttrs);
126
0
}
127
128
int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
129
    int lastpos)
130
0
{
131
0
    return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos);
132
0
}
133
134
int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si,
135
    const ASN1_OBJECT *obj, int lastpos)
136
0
{
137
0
    return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos);
138
0
}
139
140
X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc)
141
0
{
142
0
    return X509at_get_attr(si->unsignedAttrs, loc);
143
0
}
144
145
X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc)
146
0
{
147
0
    return X509at_delete_attr(si->unsignedAttrs, loc);
148
0
}
149
150
int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
151
0
{
152
0
    if (ossl_x509at_add1_attr(&si->unsignedAttrs, attr))
153
0
        return 1;
154
0
    return 0;
155
0
}
156
157
int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
158
    const ASN1_OBJECT *obj, int type,
159
    const void *bytes, int len)
160
0
{
161
0
    if (ossl_x509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len))
162
0
        return 1;
163
0
    return 0;
164
0
}
165
166
int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
167
    int nid, int type,
168
    const void *bytes, int len)
169
0
{
170
0
    if (ossl_x509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len))
171
0
        return 1;
172
0
    return 0;
173
0
}
174
175
int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
176
    const char *attrname, int type,
177
    const void *bytes, int len)
178
0
{
179
0
    if (ossl_x509at_add1_attr_by_txt(&si->unsignedAttrs, attrname,
180
0
            type, bytes, len))
181
0
        return 1;
182
0
    return 0;
183
0
}
184
185
void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
186
    int lastpos, int type)
187
0
{
188
0
    return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type);
189
0
}
190
191
/*
192
 * Retrieve an attribute by nid from a stack of attributes starting at index
193
 * *lastpos + 1.
194
 * Returns the attribute or NULL if there is no attribute.
195
 * If an attribute was found *lastpos returns the index of the found attribute.
196
 */
197
static X509_ATTRIBUTE *cms_attrib_get(int nid,
198
    const STACK_OF(X509_ATTRIBUTE) *attrs,
199
    int *lastpos)
200
0
{
201
0
    X509_ATTRIBUTE *at;
202
0
    int loc;
203
204
0
    loc = X509at_get_attr_by_NID(attrs, nid, *lastpos);
205
0
    if (loc < 0)
206
0
        return NULL;
207
208
0
    at = X509at_get_attr(attrs, loc);
209
0
    *lastpos = loc;
210
0
    return at;
211
0
}
212
213
static int cms_check_attribute(int nid, int flags, int type,
214
    const STACK_OF(X509_ATTRIBUTE) *attrs,
215
    int have_attrs)
216
0
{
217
0
    int lastpos = -1;
218
0
    X509_ATTRIBUTE *at = cms_attrib_get(nid, attrs, &lastpos);
219
220
0
    if (at != NULL) {
221
0
        int count = X509_ATTRIBUTE_count(at);
222
223
        /* Is this attribute allowed? */
224
0
        if (((flags & type) == 0)
225
            /* check if multiple attributes of the same type are allowed */
226
0
            || (((flags & CMS_ATTR_F_ONLY_ONE) != 0)
227
0
                && cms_attrib_get(nid, attrs, &lastpos) != NULL)
228
            /* Check if attribute should have exactly one value in its set */
229
0
            || (((flags & CMS_ATTR_F_ONE_ATTR_VALUE) != 0)
230
0
                && count != 1)
231
            /* There should be at least one value */
232
0
            || count == 0)
233
0
            return 0;
234
0
    } else {
235
        /* fail if a required attribute is missing */
236
0
        if (have_attrs
237
0
            && ((flags & CMS_ATTR_F_REQUIRED_COND) != 0)
238
0
            && (flags & type) != 0)
239
0
            return 0;
240
0
    }
241
0
    return 1;
242
0
}
243
244
/*
245
 * Check that the signerinfo attributes obey the attribute rules which includes
246
 * the following checks
247
 * - If any signed attributes exist then there must be a Content Type
248
 * and Message Digest attribute in the signed attributes.
249
 * - The countersignature attribute is an optional unsigned attribute only.
250
 * - Content Type, Message Digest, and Signing time attributes are signed
251
 *     attributes. Only one instance of each is allowed, with each of these
252
 *     attributes containing a single attribute value in its set.
253
 */
254
int ossl_cms_si_check_attributes(const CMS_SignerInfo *si)
255
0
{
256
0
    int i;
257
0
    int have_signed_attrs = (CMS_signed_get_attr_count(si) > 0);
258
0
    int have_unsigned_attrs = (CMS_unsigned_get_attr_count(si) > 0);
259
260
0
    for (i = 0; i < (int)OSSL_NELEM(cms_attribute_properties); ++i) {
261
0
        int nid = cms_attribute_properties[i].nid;
262
0
        int flags = cms_attribute_properties[i].flags;
263
264
0
        if (!cms_check_attribute(nid, flags, CMS_ATTR_F_SIGNED,
265
0
                si->signedAttrs, have_signed_attrs)
266
0
            || !cms_check_attribute(nid, flags, CMS_ATTR_F_UNSIGNED,
267
0
                si->unsignedAttrs, have_unsigned_attrs)) {
268
0
            ERR_raise(ERR_LIB_CMS, CMS_R_ATTRIBUTE_ERROR);
269
0
            return 0;
270
0
        }
271
0
    }
272
0
    return 1;
273
0
}