Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/cmp/cmp_ctx.c
Line
Count
Source
1
/*
2
 * Copyright 2007-2026 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
#include "cmp_local.h"
13
#include <openssl/ocsp.h> /* for OCSP_REVOKED_STATUS_* */
14
15
#define DEFINE_OSSL_CMP_CTX_get0(FIELD, TYPE) \
16
    DEFINE_OSSL_CMP_CTX_get0_NAME(FIELD, FIELD, TYPE)
17
#define DEFINE_OSSL_CMP_CTX_get0_NAME(NAME, FIELD, TYPE)    \
18
    TYPE *OSSL_CMP_CTX_get0_##NAME(const OSSL_CMP_CTX *ctx) \
19
0
    {                                                       \
20
0
        if (ctx == NULL) {                                  \
21
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);    \
22
0
            return NULL;                                    \
23
0
        }                                                   \
24
0
        return ctx->FIELD;                                  \
25
0
    }
26
27
/*
28
 * Get current certificate store containing trusted root CA certs
29
 */
30
0
DEFINE_OSSL_CMP_CTX_get0_NAME(trusted, trusted, X509_STORE)
31
32
#define DEFINE_OSSL_set0(PREFIX, FIELD, TYPE) \
33
    DEFINE_OSSL_set0_NAME(PREFIX, FIELD, FIELD, TYPE)
34
#define DEFINE_OSSL_set0_NAME(PREFIX, NAME, FIELD, TYPE)     \
35
    int PREFIX##_set0##_##NAME(OSSL_CMP_CTX *ctx, TYPE *val) \
36
1.19k
    {                                                        \
37
1.19k
        if (ctx == NULL) {                                   \
38
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);     \
39
0
            return 0;                                        \
40
0
        }                                                    \
41
1.19k
        TYPE##_free(ctx->FIELD);                             \
42
1.19k
        ctx->FIELD = val;                                    \
43
1.19k
        return 1;                                            \
44
1.19k
    }
45
46
    /*
47
     * Set certificate store containing trusted (root) CA certs and possibly CRLs
48
     * and a cert verification callback function used for CMP server authentication.
49
     * Any already existing store entry is freed. Given NULL, the entry is reset.
50
     */
51
0
    DEFINE_OSSL_set0_NAME(OSSL_CMP_CTX, trusted, trusted, X509_STORE)
52
53
0
        DEFINE_OSSL_CMP_CTX_get0(libctx, OSSL_LIB_CTX)
54
0
            DEFINE_OSSL_CMP_CTX_get0(propq, const char)
55
0
56
0
    /* Get current list of non-trusted intermediate certs */
57
0
    DEFINE_OSSL_CMP_CTX_get0(untrusted, STACK_OF(X509))
58
0
59
0
    /*
60
0
     * Set untrusted certificates for path construction in authentication of
61
0
     * the CMP server and potentially others (TLS server, newly enrolled cert).
62
0
     */
63
0
    int OSSL_CMP_CTX_set1_untrusted(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
64
0
{
65
0
    STACK_OF(X509) *untrusted = NULL;
66
67
0
    if (ctx == NULL) {
68
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
69
0
        return 0;
70
0
    }
71
0
    if (!ossl_x509_add_certs_new(&untrusted, certs,
72
0
            X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
73
0
        goto err;
74
0
    OSSL_STACK_OF_X509_free(ctx->untrusted);
75
0
    ctx->untrusted = untrusted;
76
0
    return 1;
77
0
err:
78
0
    OSSL_STACK_OF_X509_free(untrusted);
79
0
    return 0;
80
0
}
81
82
static int cmp_ctx_set_md(OSSL_CMP_CTX *ctx, EVP_MD **pmd, int nid)
83
26.4k
{
84
26.4k
    const char *name = OBJ_nid2sn(nid);
85
26.4k
    EVP_MD *md;
86
87
26.4k
    if (name == NULL) {
88
0
        ERR_raise_data(ERR_LIB_CMP, CMP_R_UNKNOWN_ALGORITHM_ID, "nid=%d", nid);
89
0
        return 0;
90
0
    }
91
26.4k
    md = EVP_MD_fetch(ctx->libctx, name, ctx->propq);
92
    /* fetching in advance to be able to throw error early if unsupported */
93
94
26.4k
    if (md == NULL) {
95
0
        ERR_raise_data(ERR_LIB_CMP, CMP_R_UNSUPPORTED_ALGORITHM, "name=%s,nid=%d", name, nid);
96
0
        return 0;
97
0
    }
98
26.4k
    EVP_MD_free(*pmd);
99
26.4k
    *pmd = md;
100
26.4k
    return 1;
101
26.4k
}
102
103
/*
104
 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
105
 * Returns new context on success, NULL on error
106
 */
107
OSSL_CMP_CTX *OSSL_CMP_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
108
66.1k
{
109
66.1k
    OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
110
111
66.1k
    if (ctx == NULL)
112
0
        goto err;
113
114
66.1k
    ctx->libctx = libctx;
115
66.1k
    if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)
116
0
        goto err;
117
118
66.1k
    ctx->log_verbosity = OSSL_CMP_LOG_INFO;
119
120
66.1k
    ctx->status = OSSL_CMP_PKISTATUS_unspecified;
121
66.1k
    ctx->failInfoCode = -1;
122
123
66.1k
    ctx->keep_alive = 1;
124
66.1k
    ctx->msg_timeout = -1;
125
66.1k
    ctx->tls_used = -1; /* default for backward compatibility */
126
127
66.1k
    if ((ctx->untrusted = sk_X509_new_null()) == NULL) {
128
0
        ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
129
0
        goto err;
130
0
    }
131
132
    /*
133
     * https://www.rfc-editor.org/rfc/rfc9045.html#name-password-based-message-auth says:
134
     * The salt SHOULD be at least 8 octets (64 bits) long.
135
     */
136
66.1k
    ctx->pbm_slen = 16;
137
66.1k
    if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, NID_sha256))
138
0
        goto err;
139
66.1k
    ctx->pbm_itercnt = 1024;
140
66.1k
    ctx->pbm_mac = NID_hmac_sha1;
141
    /*
142
     * For maximal interoperability with existing deployments, by default using HMAC-SHA1
143
     * as required in https://www.rfc-editor.org/rfc/rfc4211.html#section-4.4:
144
     * All implementations MUST support SHA-1.
145
     */
146
147
66.1k
    if (!cmp_ctx_set_md(ctx, &ctx->digest, NID_sha256))
148
0
        goto err;
149
66.1k
    ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
150
66.1k
    ctx->revocationReason = CRL_REASON_NONE;
151
152
    /* all other elements are initialized to 0 or NULL, respectively */
153
66.1k
    return ctx;
154
155
0
err:
156
0
    OSSL_CMP_CTX_free(ctx);
157
0
    return NULL;
158
66.1k
}
159
160
#define OSSL_CMP_ITAVs_free(itavs) \
161
159k
    sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
162
#define X509_EXTENSIONS_free(exts) \
163
79.6k
    sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free)
164
#define OSSL_CMP_PKIFREETEXT_free(text) \
165
79.6k
    sk_ASN1_UTF8STRING_pop_free(text, ASN1_UTF8STRING_free)
166
167
/* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
168
int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
169
0
{
170
0
    if (ctx == NULL) {
171
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
172
0
        return 0;
173
0
    }
174
175
0
#ifndef OPENSSL_NO_HTTP
176
0
    if (ctx->http_ctx != NULL) {
177
0
        (void)OSSL_HTTP_close(ctx->http_ctx, 1);
178
0
        ossl_cmp_debug(ctx, "disconnected from CMP server");
179
0
        ctx->http_ctx = NULL;
180
0
    }
181
0
#endif
182
0
    ctx->status = OSSL_CMP_PKISTATUS_unspecified;
183
0
    ctx->failInfoCode = -1;
184
185
0
    OSSL_CMP_ITAVs_free(ctx->genm_ITAVs);
186
0
    ctx->genm_ITAVs = NULL;
187
188
0
    return ossl_cmp_ctx_set0_statusString(ctx, NULL)
189
0
        && ossl_cmp_ctx_set0_newCert(ctx, NULL)
190
0
        && ossl_cmp_ctx_set1_newChain(ctx, NULL)
191
0
        && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
192
0
        && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
193
0
        && ossl_cmp_ctx_set1_validatedSrvCert(ctx, NULL)
194
0
        && ossl_cmp_ctx_set1_first_senderNonce(ctx, NULL)
195
0
        && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
196
0
        && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
197
0
        && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
198
0
}
199
200
/* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
201
void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
202
79.6k
{
203
79.6k
    if (ctx == NULL)
204
0
        return;
205
206
79.6k
#ifndef OPENSSL_NO_HTTP
207
79.6k
    if (ctx->http_ctx != NULL) {
208
0
        (void)OSSL_HTTP_close(ctx->http_ctx, 1);
209
0
        ossl_cmp_debug(ctx, "disconnected from CMP server");
210
0
    }
211
79.6k
#endif
212
79.6k
    OPENSSL_free(ctx->propq);
213
79.6k
    OPENSSL_free(ctx->serverPath);
214
79.6k
    OPENSSL_free(ctx->server);
215
79.6k
    OPENSSL_free(ctx->proxy);
216
79.6k
    OPENSSL_free(ctx->no_proxy);
217
218
79.6k
    X509_free(ctx->srvCert);
219
79.6k
    X509_free(ctx->validatedSrvCert);
220
79.6k
    X509_NAME_free(ctx->expected_sender);
221
79.6k
    X509_STORE_free(ctx->trusted);
222
79.6k
    OSSL_STACK_OF_X509_free(ctx->untrusted);
223
224
79.6k
    X509_free(ctx->cert);
225
79.6k
    OSSL_STACK_OF_X509_free(ctx->chain);
226
79.6k
    EVP_PKEY_free(ctx->pkey);
227
79.6k
    ASN1_OCTET_STRING_free(ctx->referenceValue);
228
79.6k
    if (ctx->secretValue != NULL)
229
39.8k
        OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
230
79.6k
    ASN1_OCTET_STRING_free(ctx->secretValue);
231
79.6k
    EVP_MD_free(ctx->pbm_owf);
232
233
79.6k
    X509_NAME_free(ctx->recipient);
234
79.6k
    EVP_MD_free(ctx->digest);
235
79.6k
    ASN1_OCTET_STRING_free(ctx->transactionID);
236
79.6k
    ASN1_OCTET_STRING_free(ctx->senderNonce);
237
79.6k
    ASN1_OCTET_STRING_free(ctx->recipNonce);
238
79.6k
    ASN1_OCTET_STRING_free(ctx->first_senderNonce);
239
79.6k
    OSSL_CMP_ITAVs_free(ctx->geninfo_ITAVs);
240
79.6k
    OSSL_STACK_OF_X509_free(ctx->extraCertsOut);
241
242
79.6k
    EVP_PKEY_free(ctx->newPkey);
243
79.6k
    X509_NAME_free(ctx->issuer);
244
79.6k
    ASN1_INTEGER_free(ctx->serialNumber);
245
79.6k
    X509_NAME_free(ctx->subjectName);
246
79.6k
    sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
247
79.6k
    X509_EXTENSIONS_free(ctx->reqExtensions);
248
79.6k
    sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
249
79.6k
    X509_free(ctx->oldCert);
250
79.6k
    X509_REQ_free(ctx->p10CSR);
251
252
79.6k
    OSSL_CMP_ITAVs_free(ctx->genm_ITAVs);
253
254
79.6k
    OSSL_CMP_PKIFREETEXT_free(ctx->statusString);
255
79.6k
    X509_free(ctx->newCert);
256
79.6k
    OSSL_STACK_OF_X509_free(ctx->newChain);
257
79.6k
    OSSL_STACK_OF_X509_free(ctx->caPubs);
258
79.6k
    OSSL_STACK_OF_X509_free(ctx->extraCertsIn);
259
260
79.6k
    OPENSSL_free(ctx);
261
79.6k
}
262
263
#define DEFINE_OSSL_set(PREFIX, FIELD, TYPE)              \
264
    int PREFIX##_set_##FIELD(OSSL_CMP_CTX *ctx, TYPE val) \
265
79.6k
    {                                                     \
266
79.6k
        if (ctx == NULL) {                                \
267
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);  \
268
0
            return 0;                                     \
269
0
        }                                                 \
270
79.6k
        ctx->FIELD = val;                                 \
271
79.6k
        return 1;                                         \
272
79.6k
    }
273
274
0
DEFINE_OSSL_set(ossl_cmp_ctx, status, int)
275
276
#define DEFINE_OSSL_get(PREFIX, FIELD, TYPE, ERR_RET)    \
277
    TYPE PREFIX##_get_##FIELD(const OSSL_CMP_CTX *ctx)   \
278
0
    {                                                    \
279
0
        if (ctx == NULL) {                               \
280
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
281
0
            return ERR_RET;                              \
282
0
        }                                                \
283
0
        return ctx->FIELD;                               \
284
0
    }
285
286
    /*
287
     * Returns the PKIStatus from the last CertRepMessage
288
     * or Revocation Response or error message, -1 on error
289
     */
290
0
    DEFINE_OSSL_get(OSSL_CMP_CTX, status, int, -1)
291
292
    /*
293
     * Returns the statusString from the last CertRepMessage
294
     * or Revocation Response or error message, NULL on error
295
     */
296
0
    DEFINE_OSSL_CMP_CTX_get0(statusString, OSSL_CMP_PKIFREETEXT)
297
0
298
0
        DEFINE_OSSL_set0(ossl_cmp_ctx, statusString, OSSL_CMP_PKIFREETEXT)
299
0
300
0
    /* Set callback function for checking if the cert is ok or should be rejected */
301
0
    DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb, OSSL_CMP_certConf_cb_t)
302
303
    /*
304
     * Set argument, respectively a pointer to a structure containing arguments,
305
     * optionally to be used by the certConf callback.
306
     */
307
0
    DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb_arg, void *)
308
309
    /*
310
     * Get argument, respectively the pointer to a structure containing arguments,
311
     * optionally to be used by certConf callback.
312
     * Returns callback argument set previously (NULL if not set or on error)
313
     */
314
0
    DEFINE_OSSL_get(OSSL_CMP_CTX, certConf_cb_arg, void *, NULL)
315
316
#ifndef OPENSSL_NO_TRACE
317
        static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
318
            int category, int cmd, void *vdata)
319
{
320
    OSSL_CMP_CTX *ctx = vdata;
321
    const char *msg;
322
    OSSL_CMP_severity level = -1;
323
    char *func = NULL;
324
    char *file = NULL;
325
    int line = 0;
326
327
    if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
328
        return 0;
329
    if (ctx->log_cb == NULL)
330
        return 1; /* silently drop message */
331
332
    msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
333
334
    if (level > ctx->log_verbosity) /* excludes the case level is unknown */
335
        goto end; /* suppress output since severity is not sufficient */
336
337
    if (!ctx->log_cb(func != NULL ? func : "(no func)",
338
            file != NULL ? file : "(no file)",
339
            line, level, msg))
340
        cnt = 0;
341
342
end:
343
    OPENSSL_free(func);
344
    OPENSSL_free(file);
345
    return cnt;
346
}
347
#endif
348
349
/* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
350
int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
351
    const char *func, const char *file, int line,
352
    const char *level_str, const char *format, ...)
353
323k
{
354
323k
    va_list args;
355
323k
    char hugebuf[1024 * 2];
356
323k
    int res = 0;
357
358
323k
    if (ctx == NULL || ctx->log_cb == NULL)
359
80.8k
        return 1; /* silently drop message */
360
361
242k
    if (level > ctx->log_verbosity) /* excludes the case level is unknown */
362
89.7k
        return 1; /* suppress output since severity is not sufficient */
363
364
153k
    if (format == NULL)
365
0
        return 0;
366
367
153k
    va_start(args, format);
368
369
153k
    if (func == NULL)
370
0
        func = "(unset function name)";
371
153k
    if (file == NULL)
372
0
        file = "(unset file name)";
373
153k
    if (level_str == NULL)
374
0
        level_str = "(unset level string)";
375
376
#ifndef OPENSSL_NO_TRACE
377
    if (OSSL_TRACE_ENABLED(CMP)) {
378
        OSSL_TRACE_BEGIN(CMP)
379
        {
380
            int printed = BIO_snprintf(hugebuf, sizeof(hugebuf),
381
                "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
382
                func, file, line, level_str);
383
            if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
384
                if (BIO_vsnprintf(hugebuf + printed,
385
                        sizeof(hugebuf) - printed, format, args)
386
                    > 0)
387
                    res = BIO_puts(trc_out, hugebuf) > 0;
388
            }
389
        }
390
        OSSL_TRACE_END(CMP);
391
    }
392
#else /* compensate for disabled trace API */
393
153k
    {
394
153k
        if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
395
153k
            res = ctx->log_cb(func, file, line, level, hugebuf);
396
153k
    }
397
153k
#endif
398
153k
    va_end(args);
399
153k
    return res;
400
153k
}
401
402
/* Set a callback function for error reporting and logging messages */
403
int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
404
79.6k
{
405
79.6k
    if (ctx == NULL) {
406
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
407
0
        return 0;
408
0
    }
409
79.6k
    ctx->log_cb = cb;
410
411
#ifndef OPENSSL_NO_TRACE
412
    /* do also in case cb == NULL, to switch off logging output: */
413
    if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
414
            ossl_cmp_log_trace_cb, ctx))
415
        return 0;
416
#endif
417
418
79.6k
    return 1;
419
79.6k
}
420
421
/* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
422
void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX *ctx)
423
44.3k
{
424
44.3k
    if (ctx != NULL && OSSL_CMP_LOG_ERR > ctx->log_verbosity)
425
0
        return; /* suppress output since severity is not sufficient */
426
44.3k
    OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
427
44.3k
}
428
429
/*
430
 * Set or clear the reference value to be used for identification
431
 * (i.e., the user name) when using PBMAC.
432
 */
433
int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
434
    const unsigned char *ref, int len)
435
0
{
436
0
    if (ctx == NULL) {
437
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
438
0
        return 0;
439
0
    }
440
0
    return ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref, len);
441
0
}
442
443
/* Set or clear the password to be used for protecting messages with PBMAC */
444
int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx,
445
    const unsigned char *sec, int len)
446
39.8k
{
447
39.8k
    ASN1_OCTET_STRING *secretValue = NULL;
448
449
39.8k
    if (ctx == NULL) {
450
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
451
0
        return 0;
452
0
    }
453
39.8k
    if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
454
0
        return 0;
455
39.8k
    if (ctx->secretValue != NULL) {
456
0
        OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
457
0
        ASN1_OCTET_STRING_free(ctx->secretValue);
458
0
    }
459
39.8k
    ctx->secretValue = secretValue;
460
39.8k
    return 1;
461
39.8k
}
462
463
#define DEFINE_OSSL_CMP_CTX_get1_certs(FIELD)                          \
464
    STACK_OF(X509) *OSSL_CMP_CTX_get1_##FIELD(const OSSL_CMP_CTX *ctx) \
465
0
    {                                                                  \
466
0
        if (ctx == NULL) {                                             \
467
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);               \
468
0
            return NULL;                                               \
469
0
        }                                                              \
470
0
        return X509_chain_up_ref(ctx->FIELD);                          \
471
0
    }
472
473
/* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
474
0
DEFINE_OSSL_CMP_CTX_get1_certs(newChain)
475
476
#define DEFINE_OSSL_set1_certs(PREFIX, FIELD)                                    \
477
    int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)          \
478
0
    {                                                                            \
479
0
        if (ctx == NULL) {                                                       \
480
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);                         \
481
0
            return 0;                                                            \
482
0
        }                                                                        \
483
0
        OSSL_STACK_OF_X509_free(ctx->FIELD);                                     \
484
0
        ctx->FIELD = NULL;                                                       \
485
0
        return certs == NULL || (ctx->FIELD = X509_chain_up_ref(certs)) != NULL; \
486
0
    }
487
488
    /*
489
     * Copies any given stack of inbound X509 certificates to newChain
490
     * of the OSSL_CMP_CTX structure so that they may be retrieved later.
491
     */
492
0
    DEFINE_OSSL_set1_certs(ossl_cmp_ctx, newChain)
493
494
    /* Returns the stack of extraCerts received in CertRepMessage, NULL on error */
495
0
    DEFINE_OSSL_CMP_CTX_get1_certs(extraCertsIn)
496
497
    /*
498
     * Copies any given stack of inbound X509 certificates to extraCertsIn
499
     * of the OSSL_CMP_CTX structure so that they may be retrieved later.
500
     */
501
0
    DEFINE_OSSL_set1_certs(ossl_cmp_ctx, extraCertsIn)
502
503
    /*
504
     * Copies any given stack as the new stack of X509
505
     * certificates to send out in the extraCerts field.
506
     */
507
0
    DEFINE_OSSL_set1_certs(OSSL_CMP_CTX, extraCertsOut)
508
509
    /*
510
     * Add the given policy info object
511
     * to the X509_EXTENSIONS of the requested certificate template.
512
     */
513
    int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
514
0
{
515
0
    if (ctx == NULL || pinfo == NULL) {
516
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
517
0
        return 0;
518
0
    }
519
520
0
    if (ctx->policies == NULL
521
0
        && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
522
0
        return 0;
523
524
0
    return sk_POLICYINFO_push(ctx->policies, pinfo);
525
0
}
526
527
/* Add an ITAV for geninfo of the PKI message header */
528
int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
529
0
{
530
0
    if (ctx == NULL) {
531
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
532
0
        return 0;
533
0
    }
534
0
    return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
535
0
}
536
537
int OSSL_CMP_CTX_reset_geninfo_ITAVs(OSSL_CMP_CTX *ctx)
538
0
{
539
0
    if (ctx == NULL) {
540
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
541
0
        return 0;
542
0
    }
543
0
    OSSL_CMP_ITAVs_free(ctx->geninfo_ITAVs);
544
0
    ctx->geninfo_ITAVs = NULL;
545
0
    return 1;
546
0
}
547
548
0
DEFINE_OSSL_CMP_CTX_get0(geninfo_ITAVs, STACK_OF(OSSL_CMP_ITAV))
549
0
550
0
    /* Add an itav for the body of outgoing general messages */
551
0
    int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
552
0
{
553
0
    if (ctx == NULL) {
554
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
555
0
        return 0;
556
0
    }
557
0
    return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
558
0
}
559
560
/*
561
 * Returns a duplicate of the stack of X509 certificates that
562
 * were received in the caPubs field of the last CertRepMessage.
563
 * Returns NULL on error
564
 */
565
0
DEFINE_OSSL_CMP_CTX_get1_certs(caPubs)
566
567
    /*
568
     * Copies any given stack of certificates to the given
569
     * OSSL_CMP_CTX structure so that they may be retrieved later.
570
     */
571
0
    DEFINE_OSSL_set1_certs(ossl_cmp_ctx, caPubs)
572
573
0
#define char_dup OPENSSL_strdup
574
0
#define char_free OPENSSL_free
575
#define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */    \
576
    int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
577
13.5k
    {                                                                 \
578
13.5k
        TYPE *val_dup = NULL;                                         \
579
13.5k
                                                                      \
580
13.5k
        if (ctx == NULL) {                                            \
581
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);              \
582
0
            return 0;                                                 \
583
0
        }                                                             \
584
13.5k
                                                                      \
585
13.5k
        if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL)       \
586
13.5k
            return 0;                                                 \
587
13.5k
        TYPE##_free(ctx->FIELD);                                      \
588
13.5k
        ctx->FIELD = val_dup;                                         \
589
13.5k
        return 1;                                                     \
590
13.5k
    }
591
592
0
#define X509_invalid(cert) (!ossl_x509v3_cache_extensions(cert))
593
0
#define EVP_PKEY_invalid(key) 0
594
595
#define DEFINE_OSSL_set1_up_ref(PREFIX, FIELD, TYPE)                             \
596
    int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val)                      \
597
0
    {                                                                            \
598
0
        if (ctx == NULL) {                                                       \
599
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);                         \
600
0
            return 0;                                                            \
601
0
        }                                                                        \
602
0
                                                                                 \
603
0
        /* prevent misleading error later on malformed cert or provider issue */ \
604
0
        if (val != NULL && TYPE##_invalid(val)) {                                \
605
0
            ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE);       \
606
0
            return 0;                                                            \
607
0
        }                                                                        \
608
0
        if (val != NULL && !TYPE##_up_ref(val))                                  \
609
0
            return 0;                                                            \
610
0
        TYPE##_free(ctx->FIELD);                                                 \
611
0
        ctx->FIELD = val;                                                        \
612
0
        return 1;                                                                \
613
0
    }
614
615
0
        DEFINE_OSSL_set1_up_ref(ossl_cmp_ctx, validatedSrvCert, X509)
616
617
    /*
618
     * Pins the server certificate to be directly trusted (even if it is expired)
619
     * for verifying response messages.
620
     * Cert pointer is not consumed. It may be NULL to clear the entry.
621
     */
622
0
    DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, srvCert, X509)
623
624
    /* Set the X509 name of the recipient to be placed in the PKIHeader */
625
13.5k
    DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
626
627
    /* Store the X509 name of the expected sender in the PKIHeader of responses */
628
0
    DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
629
630
    /* Set the X509 name of the issuer to be placed in the certTemplate */
631
0
    DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
632
633
    /* Set the ASN1_INTEGER serial to be placed in the certTemplate for rr */
634
0
    DEFINE_OSSL_CMP_CTX_set1(serialNumber, ASN1_INTEGER)
635
    /*
636
     * Set the subject name that will be placed in the certificate
637
     * request. This will be the subject name on the received certificate.
638
     */
639
0
    DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
640
641
    /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
642
    int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
643
0
{
644
0
    if (ctx == NULL) {
645
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
646
0
        return 0;
647
0
    }
648
649
0
    if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
650
0
        && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
651
0
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
652
0
        return 0;
653
0
    }
654
0
    X509_EXTENSIONS_free(ctx->reqExtensions);
655
0
    ctx->reqExtensions = exts;
656
0
    return 1;
657
0
}
658
659
/* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
660
int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
661
2.49k
{
662
2.49k
    if (ctx == NULL) {
663
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
664
0
        return -1;
665
0
    }
666
    /* if one of the following conditions 'fail' this is not an error */
667
2.49k
    return ctx->reqExtensions != NULL
668
0
        && X509v3_get_ext_by_NID(ctx->reqExtensions,
669
0
               NID_subject_alt_name, -1)
670
0
        >= 0;
671
2.49k
}
672
673
/*
674
 * Add a GENERAL_NAME structure that will be added to the CRMF
675
 * request's extensions field to request subject alternative names.
676
 */
677
int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
678
    const GENERAL_NAME *name)
679
0
{
680
0
    GENERAL_NAME *name_dup;
681
682
0
    if (ctx == NULL || name == NULL) {
683
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
684
0
        return 0;
685
0
    }
686
687
0
    if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
688
0
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
689
0
        return 0;
690
0
    }
691
692
0
    if (ctx->subjectAltNames == NULL
693
0
        && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
694
0
        return 0;
695
0
    if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
696
0
        return 0;
697
0
    if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
698
0
        GENERAL_NAME_free(name_dup);
699
0
        return 0;
700
0
    }
701
0
    return 1;
702
0
}
703
704
/*
705
 * Set our own client certificate, used for example in KUR and when
706
 * doing the IR with existing certificate.
707
 */
708
0
DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, cert, X509)
709
710
    int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX *ctx, X509_STORE *own_trusted,
711
        STACK_OF(X509) *candidates)
712
0
{
713
0
    STACK_OF(X509) *chain;
714
715
0
    if (ctx == NULL) {
716
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
717
0
        return 0;
718
0
    }
719
720
0
    if (!ossl_x509_add_certs_new(&ctx->untrusted, candidates,
721
0
            X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
722
0
        return 0;
723
724
0
    ossl_cmp_debug(ctx, "trying to build chain for own CMP signer cert");
725
0
    chain = X509_build_chain(ctx->cert, ctx->untrusted, own_trusted, 0,
726
0
        ctx->libctx, ctx->propq);
727
0
    if (chain == NULL) {
728
0
        ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_BUILDING_OWN_CHAIN);
729
0
        return 0;
730
0
    }
731
0
    ossl_cmp_debug(ctx, "success building chain for own CMP signer cert");
732
0
    ctx->chain = chain;
733
0
    return 1;
734
0
}
735
736
/*
737
 * Set the old certificate that we are updating in KUR
738
 * or the certificate to be revoked in RR, respectively.
739
 * Also used as reference cert (defaulting to cert) for deriving subject DN
740
 * and SANs. Its issuer is used as default recipient in the CMP message header.
741
 */
742
0
DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, oldCert, X509)
743
744
    /* Set the PKCS#10 CSR to be sent in P10CR */
745
0
    DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
746
747
    /*
748
     * Set the (newly received in IP/KUP/CP) certificate in the context.
749
     * This only permits for one cert to be enrolled at a time.
750
     */
751
1.19k
    DEFINE_OSSL_set0(ossl_cmp_ctx, newCert, X509)
752
1.19k
753
1.19k
    /* Get successfully validated sender cert, if any, of current transaction */
754
1.19k
    DEFINE_OSSL_CMP_CTX_get0(validatedSrvCert, X509)
755
0
756
0
    /*
757
0
     * Get the (newly received in IP/KUP/CP) client certificate from the context
758
0
     * This only permits for one client cert to be received...
759
0
     */
760
0
    DEFINE_OSSL_CMP_CTX_get0(newCert, X509)
761
0
762
0
    /* Set the client's current private key */
763
0
    DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, pkey, EVP_PKEY)
764
765
    /* Set new key pair. Used e.g. when doing Key Update */
766
    int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
767
3.25k
{
768
3.25k
    if (ctx == NULL) {
769
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
770
0
        return 0;
771
0
    }
772
773
3.25k
    EVP_PKEY_free(ctx->newPkey);
774
3.25k
    ctx->newPkey = pkey;
775
3.25k
    ctx->newPkey_priv = priv;
776
3.25k
    return 1;
777
3.25k
}
778
779
/* Get the private/public key to use for cert enrollment, or NULL on error */
780
/* In case |priv| == 0, better use ossl_cmp_ctx_get0_newPubkey() below */
781
EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
782
1.28k
{
783
1.28k
    if (ctx == NULL) {
784
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
785
0
        return NULL;
786
0
    }
787
788
1.28k
    if (ctx->newPkey != NULL)
789
0
        return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
790
1.28k
    if (ctx->p10CSR != NULL)
791
0
        return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
792
1.28k
    return ctx->pkey; /* may be NULL */
793
1.28k
}
794
795
EVP_PKEY *ossl_cmp_ctx_get0_newPubkey(const OSSL_CMP_CTX *ctx)
796
1.28k
{
797
1.28k
    if (!ossl_assert(ctx != NULL))
798
0
        return NULL;
799
1.28k
    if (ctx->newPkey != NULL)
800
0
        return ctx->newPkey;
801
1.28k
    if (ctx->p10CSR != NULL)
802
0
        return X509_REQ_get0_pubkey(ctx->p10CSR);
803
1.28k
    if (ctx->oldCert != NULL)
804
1.28k
        return X509_get0_pubkey(ctx->oldCert);
805
0
    if (ctx->cert != NULL)
806
0
        return X509_get0_pubkey(ctx->cert);
807
0
    return ctx->pkey;
808
0
}
809
810
#define DEFINE_set1_ASN1_OCTET_STRING(PREFIX, FIELD)                          \
811
    int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, const ASN1_OCTET_STRING *id) \
812
277k
    {                                                                         \
813
277k
        if (ctx == NULL) {                                                    \
814
0
            ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);                      \
815
0
            return 0;                                                         \
816
0
        }                                                                     \
817
277k
        return ossl_cmp_asn1_octet_string_set1(&ctx->FIELD, id);              \
818
277k
    }
819
820
/* Set the given transactionID to the context */
821
121k
DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, transactionID)
822
823
    /* Set the nonce to be used for the recipNonce in the message created next */
824
63.8k
    DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx, recipNonce)
825
826
    /* Stores the given nonce as the last senderNonce sent out */
827
92.1k
    DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, senderNonce)
828
829
    /* store the first req sender nonce for verifying delayed delivery */
830
186
    DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx, first_senderNonce)
831
832
    /* Set the proxy server to use for HTTP(S) connections */
833
0
    DEFINE_OSSL_CMP_CTX_set1(proxy, char)
834
835
    /* Set the (HTTP) hostname of the CMP server */
836
0
    DEFINE_OSSL_CMP_CTX_set1(server, char)
837
838
    /* Set the server exclusion list of the HTTP proxy server */
839
0
    DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
840
841
#ifndef OPENSSL_NO_HTTP
842
    /* Set the http connect/disconnect callback function to be used for HTTP(S) */
843
0
    DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb, OSSL_HTTP_bio_cb_t)
844
845
    /* Set argument optionally to be used by the http connect/disconnect callback */
846
0
    DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb_arg, void *)
847
848
    /*
849
     * Get argument optionally to be used by the http connect/disconnect callback
850
     * Returns callback argument set previously (NULL if not set or on error)
851
     */
852
0
    DEFINE_OSSL_get(OSSL_CMP_CTX, http_cb_arg, void *, NULL)
853
#endif
854
855
    /* Set callback function for sending CMP request and receiving response */
856
39.8k
    DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb, OSSL_CMP_transfer_cb_t)
857
858
    /* Set argument optionally to be used by the transfer callback */
859
39.8k
    DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb_arg, void *)
860
861
    /*
862
     * Get argument optionally to be used by the transfer callback.
863
     * Returns callback argument set previously (NULL if not set or on error)
864
     */
865
0
    DEFINE_OSSL_get(OSSL_CMP_CTX, transfer_cb_arg, void *, NULL)
866
867
    /** Set the HTTP server port to be used */
868
0
    DEFINE_OSSL_set(OSSL_CMP_CTX, serverPort, int)
869
870
    /* Set the HTTP path to be used on the server (e.g "pkix/") */
871
0
    DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
872
873
    /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
874
0
    DEFINE_OSSL_set(ossl_cmp_ctx, failInfoCode, int)
875
876
    /*
877
     * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
878
     * Returns bit string as integer on success, -1 on error
879
     */
880
0
    DEFINE_OSSL_get(OSSL_CMP_CTX, failInfoCode, int, -1)
881
882
    /* Set a Boolean or integer option of the context to the "val" arg */
883
    int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
884
0
{
885
0
    int min_val;
886
887
0
    if (ctx == NULL) {
888
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
889
0
        return 0;
890
0
    }
891
892
0
    switch (opt) {
893
0
    case OSSL_CMP_OPT_REVOCATION_REASON:
894
0
        min_val = OCSP_REVOKED_STATUS_NOSTATUS;
895
0
        break;
896
0
    case OSSL_CMP_OPT_POPO_METHOD:
897
0
        min_val = OSSL_CRMF_POPO_NONE;
898
0
        break;
899
0
    default:
900
0
        min_val = 0;
901
0
        break;
902
0
    }
903
0
    if (val < min_val) {
904
0
        ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_SMALL);
905
0
        return 0;
906
0
    }
907
908
0
    switch (opt) {
909
0
    case OSSL_CMP_OPT_LOG_VERBOSITY:
910
0
        if (val > OSSL_CMP_LOG_MAX) {
911
0
            ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
912
0
            return 0;
913
0
        }
914
0
        ctx->log_verbosity = val;
915
0
        break;
916
0
    case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
917
0
        ctx->implicitConfirm = val;
918
0
        break;
919
0
    case OSSL_CMP_OPT_DISABLE_CONFIRM:
920
0
        ctx->disableConfirm = val;
921
0
        break;
922
0
    case OSSL_CMP_OPT_UNPROTECTED_SEND:
923
0
        ctx->unprotectedSend = val;
924
0
        break;
925
0
    case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
926
0
        ctx->unprotectedErrors = val;
927
0
        break;
928
0
    case OSSL_CMP_OPT_NO_CACHE_EXTRACERTS:
929
0
        ctx->noCacheExtraCerts = val;
930
0
        break;
931
0
    case OSSL_CMP_OPT_VALIDITY_DAYS:
932
0
        ctx->days = val;
933
0
        break;
934
0
    case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
935
0
        ctx->SubjectAltName_nodefault = val;
936
0
        break;
937
0
    case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
938
0
        ctx->setSubjectAltNameCritical = val;
939
0
        break;
940
0
    case OSSL_CMP_OPT_POLICIES_CRITICAL:
941
0
        ctx->setPoliciesCritical = val;
942
0
        break;
943
0
    case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
944
0
        ctx->ignore_keyusage = val;
945
0
        break;
946
0
    case OSSL_CMP_OPT_POPO_METHOD:
947
0
        if (val > OSSL_CRMF_POPO_KEYAGREE) {
948
0
            ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
949
0
            return 0;
950
0
        }
951
0
        ctx->popoMethod = val;
952
0
        break;
953
0
    case OSSL_CMP_OPT_DIGEST_ALGNID:
954
0
        if (!cmp_ctx_set_md(ctx, &ctx->digest, val))
955
0
            return 0;
956
0
        break;
957
0
    case OSSL_CMP_OPT_OWF_ALGNID:
958
0
        if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, val))
959
0
            return 0;
960
0
        break;
961
0
    case OSSL_CMP_OPT_MAC_ALGNID:
962
0
        ctx->pbm_mac = val;
963
0
        break;
964
0
    case OSSL_CMP_OPT_KEEP_ALIVE:
965
0
        ctx->keep_alive = val;
966
0
        break;
967
0
    case OSSL_CMP_OPT_MSG_TIMEOUT:
968
0
        ctx->msg_timeout = val;
969
0
        break;
970
0
    case OSSL_CMP_OPT_TOTAL_TIMEOUT:
971
0
        ctx->total_timeout = val;
972
0
        break;
973
0
    case OSSL_CMP_OPT_USE_TLS:
974
0
        ctx->tls_used = val;
975
0
        break;
976
0
    case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
977
0
        ctx->permitTAInExtraCertsForIR = val;
978
0
        break;
979
0
    case OSSL_CMP_OPT_REVOCATION_REASON:
980
0
        if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
981
0
            ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
982
0
            return 0;
983
0
        }
984
0
        ctx->revocationReason = val;
985
0
        break;
986
0
    default:
987
0
        ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_OPTION, "%d", opt);
988
0
        return 0;
989
0
    }
990
991
0
    return 1;
992
0
}
993
994
/*
995
 * Reads a Boolean or integer option value from the context.
996
 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
997
 */
998
int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
999
1.55k
{
1000
1.55k
    if (ctx == NULL) {
1001
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1002
0
        return -1;
1003
0
    }
1004
1005
1.55k
    switch (opt) {
1006
0
    case OSSL_CMP_OPT_LOG_VERBOSITY:
1007
0
        return ctx->log_verbosity;
1008
204
    case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
1009
204
        return ctx->implicitConfirm;
1010
0
    case OSSL_CMP_OPT_DISABLE_CONFIRM:
1011
0
        return ctx->disableConfirm;
1012
0
    case OSSL_CMP_OPT_UNPROTECTED_SEND:
1013
0
        return ctx->unprotectedSend;
1014
713
    case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1015
713
        return ctx->unprotectedErrors;
1016
0
    case OSSL_CMP_OPT_NO_CACHE_EXTRACERTS:
1017
0
        return ctx->noCacheExtraCerts;
1018
0
    case OSSL_CMP_OPT_VALIDITY_DAYS:
1019
0
        return ctx->days;
1020
0
    case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1021
0
        return ctx->SubjectAltName_nodefault;
1022
0
    case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1023
0
        return ctx->setSubjectAltNameCritical;
1024
0
    case OSSL_CMP_OPT_POLICIES_CRITICAL:
1025
0
        return ctx->setPoliciesCritical;
1026
0
    case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1027
0
        return ctx->ignore_keyusage;
1028
635
    case OSSL_CMP_OPT_POPO_METHOD:
1029
635
        return ctx->popoMethod;
1030
0
    case OSSL_CMP_OPT_DIGEST_ALGNID:
1031
0
        return EVP_MD_get_type(ctx->digest);
1032
0
    case OSSL_CMP_OPT_OWF_ALGNID:
1033
0
        return EVP_MD_get_type(ctx->pbm_owf);
1034
0
    case OSSL_CMP_OPT_MAC_ALGNID:
1035
0
        return ctx->pbm_mac;
1036
0
    case OSSL_CMP_OPT_KEEP_ALIVE:
1037
0
        return ctx->keep_alive;
1038
0
    case OSSL_CMP_OPT_MSG_TIMEOUT:
1039
0
        return ctx->msg_timeout;
1040
0
    case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1041
0
        return ctx->total_timeout;
1042
0
    case OSSL_CMP_OPT_USE_TLS:
1043
0
        return ctx->tls_used;
1044
0
    case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1045
0
        return ctx->permitTAInExtraCertsForIR;
1046
0
    case OSSL_CMP_OPT_REVOCATION_REASON:
1047
0
        return ctx->revocationReason;
1048
0
    default:
1049
0
        ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_OPTION, "%d", opt);
1050
0
        return -1;
1051
1.55k
    }
1052
1.55k
}