Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/cmp/cmp_hdr.c
Line
Count
Source
1
/*
2
 * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright Nokia 2007-2019
4
 * Copyright Siemens AG 2015-2019
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
/* CMP functions for PKIHeader handling */
13
14
#include "cmp_local.h"
15
#include <openssl/rand.h> /* for RAND_bytes_ex() */
16
17
int ossl_cmp_hdr_set_pvno(OSSL_CMP_PKIHEADER *hdr, int pvno)
18
46.9k
{
19
46.9k
    if (!ossl_assert(hdr != NULL))
20
0
        return 0;
21
46.9k
    return ASN1_INTEGER_set(hdr->pvno, pvno);
22
46.9k
}
23
24
int ossl_cmp_hdr_get_pvno(const OSSL_CMP_PKIHEADER *hdr)
25
74.2k
{
26
74.2k
    int64_t pvno;
27
28
74.2k
    if (!ossl_assert(hdr != NULL))
29
0
        return -1;
30
74.2k
    if (!ASN1_INTEGER_get_int64(&pvno, hdr->pvno) || pvno < 0 || pvno > INT_MAX)
31
25.1k
        return -1;
32
49.1k
    return (int)pvno;
33
74.2k
}
34
35
int ossl_cmp_hdr_get_protection_nid(const OSSL_CMP_PKIHEADER *hdr)
36
59.8k
{
37
59.8k
    if (!ossl_assert(hdr != NULL)
38
59.8k
            || hdr->protectionAlg == NULL)
39
29.5k
        return NID_undef;
40
30.3k
    return OBJ_obj2nid(hdr->protectionAlg->algorithm);
41
59.8k
}
42
43
ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_transactionID(const
44
                                                   OSSL_CMP_PKIHEADER *hdr)
45
0
{
46
0
    if (hdr == NULL) {
47
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
48
0
        return NULL;
49
0
    }
50
0
    return hdr->transactionID;
51
0
}
52
53
ASN1_OCTET_STRING *ossl_cmp_hdr_get0_senderNonce(const OSSL_CMP_PKIHEADER *hdr)
54
0
{
55
0
    if (!ossl_assert(hdr != NULL))
56
0
        return NULL;
57
0
    return hdr->senderNonce;
58
0
}
59
60
ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_recipNonce(const OSSL_CMP_PKIHEADER *hdr)
61
0
{
62
0
    if (hdr == NULL) {
63
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
64
0
        return NULL;
65
0
    }
66
0
    return hdr->recipNonce;
67
0
}
68
69
STACK_OF(OSSL_CMP_ITAV)
70
    *OSSL_CMP_HDR_get0_geninfo_ITAVs(const OSSL_CMP_PKIHEADER *hdr)
71
0
{
72
0
    if (hdr == NULL) {
73
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
74
0
        return NULL;
75
0
    }
76
0
    return hdr->generalInfo;
77
0
}
78
79
/* a NULL-DN as an empty sequence of RDNs */
80
int ossl_cmp_general_name_is_NULL_DN(GENERAL_NAME *name)
81
3.08k
{
82
3.08k
    return name == NULL
83
3.08k
        || (name->type == GEN_DIRNAME && IS_NULL_DN(name->d.directoryName));
84
3.08k
}
85
86
/*
87
 * Set the sender name in PKIHeader.
88
 * when nm is NULL, sender is set to an empty string
89
 * returns 1 on success, 0 on error
90
 */
91
int ossl_cmp_hdr_set1_sender(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm)
92
46.9k
{
93
46.9k
    if (!ossl_assert(hdr != NULL))
94
0
        return 0;
95
46.9k
    return GENERAL_NAME_set1_X509_NAME(&hdr->sender, nm);
96
46.9k
}
97
98
int ossl_cmp_hdr_set1_recipient(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm)
99
46.9k
{
100
46.9k
    if (!ossl_assert(hdr != NULL))
101
0
        return 0;
102
46.9k
    return GENERAL_NAME_set1_X509_NAME(&hdr->recipient, nm);
103
46.9k
}
104
105
int ossl_cmp_hdr_update_messageTime(OSSL_CMP_PKIHEADER *hdr)
106
46.9k
{
107
46.9k
    if (!ossl_assert(hdr != NULL))
108
0
        return 0;
109
46.9k
    if (hdr->messageTime == NULL
110
46.9k
            && (hdr->messageTime = ASN1_GENERALIZEDTIME_new()) == NULL)
111
0
        return 0;
112
46.9k
    return ASN1_GENERALIZEDTIME_set(hdr->messageTime, time(NULL)) != NULL;
113
46.9k
}
114
115
/* assign to *tgt a random byte array of given length */
116
static int set_random(ASN1_OCTET_STRING **tgt, OSSL_CMP_CTX *ctx, int len)
117
83.2k
{
118
83.2k
    unsigned char *bytes = OPENSSL_malloc(len);
119
83.2k
    int res = 0;
120
121
83.2k
    if (bytes == NULL || RAND_bytes_ex(ctx->libctx, bytes, len, 0) <= 0)
122
83.2k
        ERR_raise(ERR_LIB_CMP, CMP_R_FAILURE_OBTAINING_RANDOM);
123
83.2k
    else
124
83.2k
        res = ossl_cmp_asn1_octet_string_set1_bytes(tgt, bytes, len);
125
83.2k
    OPENSSL_free(bytes);
126
83.2k
    return res;
127
83.2k
}
128
129
int ossl_cmp_hdr_set1_senderKID(OSSL_CMP_PKIHEADER *hdr,
130
                                const ASN1_OCTET_STRING *senderKID)
131
0
{
132
0
    if (!ossl_assert(hdr != NULL))
133
0
        return 0;
134
0
    return ossl_cmp_asn1_octet_string_set1(&hdr->senderKID, senderKID);
135
0
}
136
137
/* push the given text string to the given PKIFREETEXT ft */
138
int ossl_cmp_hdr_push0_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text)
139
0
{
140
0
    if (!ossl_assert(hdr != NULL && text != NULL))
141
0
        return 0;
142
143
0
    if (hdr->freeText == NULL
144
0
            && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL)
145
0
        return 0;
146
147
0
    return sk_ASN1_UTF8STRING_push(hdr->freeText, text);
148
0
}
149
150
int ossl_cmp_hdr_push1_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text)
151
0
{
152
0
    if (!ossl_assert(hdr != NULL && text != NULL))
153
0
        return 0;
154
155
0
    if (hdr->freeText == NULL
156
0
            && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL)
157
0
        return 0;
158
159
0
    return
160
0
        ossl_cmp_sk_ASN1_UTF8STRING_push_str(hdr->freeText, (char *)text->data,
161
0
                                             text->length);
162
0
}
163
164
int ossl_cmp_hdr_generalInfo_push0_item(OSSL_CMP_PKIHEADER *hdr,
165
                                        OSSL_CMP_ITAV *itav)
166
0
{
167
0
    if (!ossl_assert(hdr != NULL && itav != NULL))
168
0
        return 0;
169
0
    return OSSL_CMP_ITAV_push0_stack_item(&hdr->generalInfo, itav);
170
0
}
171
172
int ossl_cmp_hdr_generalInfo_push1_items(OSSL_CMP_PKIHEADER *hdr,
173
                                         const STACK_OF(OSSL_CMP_ITAV) *itavs)
174
0
{
175
0
    int i;
176
0
    OSSL_CMP_ITAV *itav;
177
178
0
    if (!ossl_assert(hdr != NULL))
179
0
        return 0;
180
181
0
    for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
182
0
        itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
183
0
        if (itav == NULL)
184
0
            return 0;
185
186
0
        if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav)) {
187
0
            OSSL_CMP_ITAV_free(itav);
188
0
            return 0;
189
0
        }
190
0
    }
191
0
    return 1;
192
0
}
193
194
int ossl_cmp_hdr_set_implicitConfirm(OSSL_CMP_PKIHEADER *hdr)
195
0
{
196
0
    OSSL_CMP_ITAV *itav;
197
0
    ASN1_TYPE *asn1null;
198
199
0
    if (!ossl_assert(hdr != NULL))
200
0
        return 0;
201
0
    asn1null = (ASN1_TYPE *)ASN1_NULL_new();
202
0
    if (asn1null == NULL)
203
0
        return 0;
204
0
    if ((itav = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm),
205
0
                                     asn1null)) == NULL)
206
0
        goto err;
207
0
    if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav))
208
0
        goto err;
209
0
    return 1;
210
211
0
 err:
212
0
    ASN1_TYPE_free(asn1null);
213
0
    OSSL_CMP_ITAV_free(itav);
214
0
    return 0;
215
0
}
216
217
/* return 1 if implicitConfirm in the generalInfo field of the header is set */
218
int ossl_cmp_hdr_has_implicitConfirm(const OSSL_CMP_PKIHEADER *hdr)
219
0
{
220
0
    int itavCount;
221
0
    int i;
222
0
    OSSL_CMP_ITAV *itav;
223
224
0
    if (!ossl_assert(hdr != NULL))
225
0
        return 0;
226
227
0
    itavCount = sk_OSSL_CMP_ITAV_num(hdr->generalInfo);
228
0
    for (i = 0; i < itavCount; i++) {
229
0
        itav = sk_OSSL_CMP_ITAV_value(hdr->generalInfo, i);
230
0
        if (itav != NULL
231
0
                && OBJ_obj2nid(itav->infoType) == NID_id_it_implicitConfirm)
232
0
            return 1;
233
0
    }
234
235
0
    return 0;
236
0
}
237
238
/*
239
 * set ctx->transactionID in CMP header
240
 * if ctx->transactionID is NULL, a random one is created with 128 bit
241
 * according to section 5.1.1:
242
 *
243
 * It is RECOMMENDED that the clients fill the transactionID field with
244
 * 128 bits of (pseudo-) random data for the start of a transaction to
245
 * reduce the probability of having the transactionID in use at the server.
246
 */
247
int ossl_cmp_hdr_set_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr)
248
46.9k
{
249
46.9k
    if (ctx->transactionID == NULL) {
250
36.2k
        char *tid;
251
252
36.2k
        if (!set_random(&ctx->transactionID, ctx,
253
36.2k
                        OSSL_CMP_TRANSACTIONID_LENGTH))
254
0
            return 0;
255
36.2k
        tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
256
36.2k
        if (tid != NULL)
257
36.2k
            ossl_cmp_log1(DEBUG, ctx,
258
36.2k
                          "Starting new transaction with ID=%s", tid);
259
36.2k
        OPENSSL_free(tid);
260
36.2k
    }
261
262
46.9k
    return ossl_cmp_asn1_octet_string_set1(&hdr->transactionID,
263
46.9k
                                           ctx->transactionID);
264
46.9k
}
265
266
/* fill in all fields of the hdr according to the info given in ctx */
267
int ossl_cmp_hdr_init(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr)
268
39.5k
{
269
39.5k
    const X509_NAME *sender;
270
39.5k
    const X509_NAME *rcp = NULL;
271
272
39.5k
    if (!ossl_assert(ctx != NULL && hdr != NULL))
273
0
        return 0;
274
275
    /* set the CMP version */
276
39.5k
    if (!ossl_cmp_hdr_set_pvno(hdr, OSSL_CMP_PVNO))
277
0
        return 0;
278
279
    /*
280
     * If no protection cert nor oldCert nor CSR nor subject is given,
281
     * sender name is not known to the client and thus set to NULL-DN
282
     */
283
39.5k
    sender = ctx->cert != NULL ? X509_get_subject_name(ctx->cert) :
284
39.5k
        ctx->oldCert != NULL ? X509_get_subject_name(ctx->oldCert) :
285
39.5k
        ctx->p10CSR != NULL ? X509_REQ_get_subject_name(ctx->p10CSR) :
286
36.9k
        ctx->subjectName;
287
39.5k
    if (!ossl_cmp_hdr_set1_sender(hdr, sender))
288
0
        return 0;
289
290
    /* determine recipient entry in PKIHeader */
291
39.5k
    if (ctx->recipient != NULL)
292
13.5k
        rcp = ctx->recipient;
293
26.0k
    else if (ctx->srvCert != NULL)
294
0
        rcp = X509_get_subject_name(ctx->srvCert);
295
26.0k
    else if (ctx->issuer != NULL)
296
0
        rcp = ctx->issuer;
297
26.0k
    else if (ctx->oldCert != NULL)
298
2.61k
        rcp = X509_get_issuer_name(ctx->oldCert);
299
23.3k
    else if (ctx->cert != NULL)
300
0
        rcp = X509_get_issuer_name(ctx->cert);
301
39.5k
    if (!ossl_cmp_hdr_set1_recipient(hdr, rcp))
302
0
        return 0;
303
304
    /* set current time as message time */
305
39.5k
    if (!ossl_cmp_hdr_update_messageTime(hdr))
306
0
        return 0;
307
308
39.5k
    if (ctx->recipNonce != NULL
309
9.69k
            && !ossl_cmp_asn1_octet_string_set1(&hdr->recipNonce,
310
9.69k
                                                ctx->recipNonce))
311
0
        return 0;
312
313
39.5k
    if (!ossl_cmp_hdr_set_transactionID(ctx, hdr))
314
0
        return 0;
315
316
    /*-
317
     * set random senderNonce
318
     * according to section 5.1.1:
319
     *
320
     * senderNonce                  present
321
     *         -- 128 (pseudo-)random bits
322
     * The senderNonce and recipNonce fields protect the PKIMessage against
323
     * replay attacks. The senderNonce will typically be 128 bits of
324
     * (pseudo-) random data generated by the sender, whereas the recipNonce
325
     * is copied from the senderNonce of the previous message in the
326
     * transaction.
327
     */
328
39.5k
    if (!set_random(&hdr->senderNonce, ctx, OSSL_CMP_SENDERNONCE_LENGTH))
329
0
        return 0;
330
331
    /* store senderNonce - for cmp with recipNonce in next outgoing msg */
332
39.5k
    if (!OSSL_CMP_CTX_set1_senderNonce(ctx, hdr->senderNonce))
333
0
        return 0;
334
335
    /*-
336
     * freeText                [7] PKIFreeText OPTIONAL,
337
     * -- this may be used to indicate context-specific instructions
338
     * -- (this field is intended for human consumption)
339
     */
340
39.5k
    if (ctx->freeText != NULL
341
0
            && !ossl_cmp_hdr_push1_freeText(hdr, ctx->freeText))
342
0
        return 0;
343
344
39.5k
    return 1;
345
39.5k
}