Coverage Report

Created: 2026-09-12 06:55

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