Coverage Report

Created: 2025-12-31 06:58

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