Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/cmp/cmp_msg.c
Line
Count
Source
1
/*
2
 * Copyright 2007-2024 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 PKIMessage construction */
13
14
#include "cmp_local.h"
15
16
/* explicit #includes not strictly needed since implied by the above: */
17
#include <openssl/asn1t.h>
18
#include <openssl/cmp.h>
19
#include <openssl/crmf.h>
20
#include <openssl/err.h>
21
#include <openssl/x509.h>
22
23
OSSL_CMP_MSG *OSSL_CMP_MSG_new(OSSL_LIB_CTX *libctx, const char *propq)
24
43.1k
{
25
43.1k
    OSSL_CMP_MSG *msg = NULL;
26
27
43.1k
    msg = (OSSL_CMP_MSG *)ASN1_item_new_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG),
28
43.1k
        libctx, propq);
29
43.1k
    if (!ossl_cmp_msg_set0_libctx(msg, libctx, propq)) {
30
0
        OSSL_CMP_MSG_free(msg);
31
0
        msg = NULL;
32
0
    }
33
43.1k
    return msg;
34
43.1k
}
35
36
void OSSL_CMP_MSG_free(OSSL_CMP_MSG *msg)
37
126k
{
38
126k
    ASN1_item_free((ASN1_VALUE *)msg, ASN1_ITEM_rptr(OSSL_CMP_MSG));
39
126k
}
40
41
/*
42
 * This should only be used if the X509 object was embedded inside another
43
 * asn1 object and it needs a libctx to operate.
44
 * Use OSSL_CMP_MSG_new() instead if possible.
45
 */
46
int ossl_cmp_msg_set0_libctx(OSSL_CMP_MSG *msg, OSSL_LIB_CTX *libctx,
47
    const char *propq)
48
43.1k
{
49
43.1k
    if (msg != NULL) {
50
43.1k
        msg->libctx = libctx;
51
43.1k
        OPENSSL_free(msg->propq);
52
43.1k
        msg->propq = NULL;
53
43.1k
        if (propq != NULL) {
54
0
            msg->propq = OPENSSL_strdup(propq);
55
0
            if (msg->propq == NULL)
56
0
                return 0;
57
0
        }
58
43.1k
    }
59
43.1k
    return 1;
60
43.1k
}
61
62
OSSL_CMP_PKIHEADER *OSSL_CMP_MSG_get0_header(const OSSL_CMP_MSG *msg)
63
95.8k
{
64
95.8k
    if (msg == NULL) {
65
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
66
0
        return NULL;
67
0
    }
68
95.8k
    return msg->header;
69
95.8k
}
70
71
const char *ossl_cmp_bodytype_to_string(int type)
72
39.4k
{
73
39.4k
    static const char *type_names[] = {
74
39.4k
        "IR",
75
39.4k
        "IP",
76
39.4k
        "CR",
77
39.4k
        "CP",
78
39.4k
        "P10CR",
79
39.4k
        "POPDECC",
80
39.4k
        "POPDECR",
81
39.4k
        "KUR",
82
39.4k
        "KUP",
83
39.4k
        "KRR",
84
39.4k
        "KRP",
85
39.4k
        "RR",
86
39.4k
        "RP",
87
39.4k
        "CCR",
88
39.4k
        "CCP",
89
39.4k
        "CKUANN",
90
39.4k
        "CANN",
91
39.4k
        "RANN",
92
39.4k
        "CRLANN",
93
39.4k
        "PKICONF",
94
39.4k
        "NESTED",
95
39.4k
        "GENM",
96
39.4k
        "GENP",
97
39.4k
        "ERROR",
98
39.4k
        "CERTCONF",
99
39.4k
        "POLLREQ",
100
39.4k
        "POLLREP",
101
39.4k
    };
102
103
39.4k
    if (type < 0 || type > OSSL_CMP_PKIBODY_TYPE_MAX)
104
0
        return "illegal body type";
105
39.4k
    return type_names[type];
106
39.4k
}
107
108
int ossl_cmp_msg_set_bodytype(OSSL_CMP_MSG *msg, int type)
109
43.1k
{
110
43.1k
    if (!ossl_assert(msg != NULL && msg->body != NULL))
111
0
        return 0;
112
113
43.1k
    msg->body->type = type;
114
43.1k
    return 1;
115
43.1k
}
116
117
int OSSL_CMP_MSG_get_bodytype(const OSSL_CMP_MSG *msg)
118
127k
{
119
127k
    if (!ossl_assert(msg != NULL && msg->body != NULL))
120
0
        return -1;
121
122
127k
    return msg->body->type;
123
127k
}
124
125
X509_PUBKEY *OSSL_CMP_MSG_get0_certreq_publickey(const OSSL_CMP_MSG *msg)
126
0
{
127
0
    const OSSL_CRMF_MSGS *reqs;
128
0
    const OSSL_CRMF_MSG *crm;
129
0
    const OSSL_CRMF_CERTTEMPLATE *tmpl;
130
0
    X509_PUBKEY *pubkey;
131
132
0
    switch (OSSL_CMP_MSG_get_bodytype(msg)) {
133
0
    case OSSL_CMP_PKIBODY_IR:
134
0
    case OSSL_CMP_PKIBODY_CR:
135
0
    case OSSL_CMP_PKIBODY_KUR:
136
0
        reqs = msg->body->value.ir; /* value.ir is same for cr and kur */
137
0
        if ((crm = sk_OSSL_CRMF_MSG_value(reqs, 0)) == NULL) {
138
0
            ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND);
139
0
            return NULL;
140
0
        }
141
0
        if ((tmpl = OSSL_CRMF_MSG_get0_tmpl(crm)) == NULL
142
0
            || (pubkey = OSSL_CRMF_CERTTEMPLATE_get0_publicKey(tmpl)) == NULL) {
143
0
            ERR_raise(ERR_LIB_CMP, CRMF_R_POPO_MISSING_PUBLIC_KEY);
144
0
            return NULL;
145
0
        }
146
0
        return pubkey;
147
0
    default:
148
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
149
0
        return NULL;
150
0
    }
151
0
}
152
153
/* Add an extension to the referenced extension stack, which may be NULL */
154
static int add1_extension(X509_EXTENSIONS **pexts, int nid, int crit, void *ex)
155
0
{
156
0
    X509_EXTENSION *ext;
157
0
    int res;
158
159
0
    if (!ossl_assert(pexts != NULL)) /* pointer to var must not be NULL */
160
0
        return 0;
161
162
0
    if ((ext = X509V3_EXT_i2d(nid, crit, ex)) == NULL)
163
0
        return 0;
164
165
0
    res = X509v3_add_ext(pexts, ext, 0) != NULL;
166
0
    X509_EXTENSION_free(ext);
167
0
    return res;
168
0
}
169
170
/* Add a CRL revocation reason code to extension stack, which may be NULL */
171
static int add_crl_reason_extension(X509_EXTENSIONS **pexts, int reason_code)
172
0
{
173
0
    ASN1_ENUMERATED *val = ASN1_ENUMERATED_new();
174
0
    int res = 0;
175
176
0
    if (val != NULL && ASN1_ENUMERATED_set(val, reason_code))
177
0
        res = add1_extension(pexts, NID_crl_reason, 0 /* non-critical */, val);
178
0
    ASN1_ENUMERATED_free(val);
179
0
    return res;
180
0
}
181
182
OSSL_CMP_MSG *ossl_cmp_msg_create(OSSL_CMP_CTX *ctx, int bodytype)
183
43.1k
{
184
43.1k
    OSSL_CMP_MSG *msg = NULL;
185
186
43.1k
    if (!ossl_assert(ctx != NULL))
187
0
        return NULL;
188
189
43.1k
    if ((msg = OSSL_CMP_MSG_new(ctx->libctx, ctx->propq)) == NULL)
190
0
        return NULL;
191
43.1k
    if (!ossl_cmp_hdr_init(ctx, msg->header)
192
43.1k
        || !ossl_cmp_msg_set_bodytype(msg, bodytype))
193
0
        goto err;
194
43.1k
    if (ctx->geninfo_ITAVs != NULL
195
0
        && !ossl_cmp_hdr_generalInfo_push1_items(msg->header,
196
0
            ctx->geninfo_ITAVs))
197
0
        goto err;
198
199
43.1k
    switch (bodytype) {
200
1.05k
    case OSSL_CMP_PKIBODY_IR:
201
1.13k
    case OSSL_CMP_PKIBODY_CR:
202
1.21k
    case OSSL_CMP_PKIBODY_KUR:
203
1.21k
        if ((msg->body->value.ir = OSSL_CRMF_MSGS_new()) == NULL)
204
0
            goto err;
205
1.21k
        return msg;
206
207
84
    case OSSL_CMP_PKIBODY_P10CR:
208
84
        if (ctx->p10CSR == NULL) {
209
84
            ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_P10CSR);
210
84
            goto err;
211
84
        }
212
0
        if ((msg->body->value.p10cr = X509_REQ_dup(ctx->p10CSR)) == NULL)
213
0
            goto err;
214
0
        return msg;
215
216
0
    case OSSL_CMP_PKIBODY_IP:
217
0
    case OSSL_CMP_PKIBODY_CP:
218
0
    case OSSL_CMP_PKIBODY_KUP:
219
0
        if ((msg->body->value.ip = OSSL_CMP_CERTREPMESSAGE_new()) == NULL)
220
0
            goto err;
221
0
        return msg;
222
223
193
    case OSSL_CMP_PKIBODY_RR:
224
193
        if ((msg->body->value.rr = sk_OSSL_CMP_REVDETAILS_new_null()) == NULL)
225
0
            goto err;
226
193
        return msg;
227
0
    case OSSL_CMP_PKIBODY_RP:
228
0
        if ((msg->body->value.rp = OSSL_CMP_REVREPCONTENT_new()) == NULL)
229
0
            goto err;
230
0
        return msg;
231
232
0
    case OSSL_CMP_PKIBODY_CERTCONF:
233
0
        if ((msg->body->value.certConf = sk_OSSL_CMP_CERTSTATUS_new_null()) == NULL)
234
0
            goto err;
235
0
        return msg;
236
800
    case OSSL_CMP_PKIBODY_PKICONF:
237
800
        if ((msg->body->value.pkiconf = ASN1_TYPE_new()) == NULL)
238
0
            goto err;
239
800
        ASN1_TYPE_set(msg->body->value.pkiconf, V_ASN1_NULL, NULL);
240
800
        return msg;
241
242
373
    case OSSL_CMP_PKIBODY_POLLREQ:
243
373
        if ((msg->body->value.pollReq = sk_OSSL_CMP_POLLREQ_new_null()) == NULL)
244
0
            goto err;
245
373
        return msg;
246
0
    case OSSL_CMP_PKIBODY_POLLREP:
247
0
        if ((msg->body->value.pollRep = sk_OSSL_CMP_POLLREP_new_null()) == NULL)
248
0
            goto err;
249
0
        return msg;
250
251
1.00k
    case OSSL_CMP_PKIBODY_GENM:
252
1.00k
    case OSSL_CMP_PKIBODY_GENP:
253
1.00k
        if ((msg->body->value.genm = sk_OSSL_CMP_ITAV_new_null()) == NULL)
254
0
            goto err;
255
1.00k
        return msg;
256
257
39.4k
    case OSSL_CMP_PKIBODY_ERROR:
258
39.4k
        if ((msg->body->value.error = OSSL_CMP_ERRORMSGCONTENT_new()) == NULL)
259
0
            goto err;
260
39.4k
        return msg;
261
262
0
    default:
263
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
264
0
        goto err;
265
43.1k
    }
266
267
84
err:
268
84
    OSSL_CMP_MSG_free(msg);
269
84
    return NULL;
270
43.1k
}
271
272
#define HAS_SAN(ctx)                                 \
273
1.58k
    (sk_GENERAL_NAME_num((ctx)->subjectAltNames) > 0 \
274
1.58k
        || OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1)
275
276
static const X509_NAME *determine_subj(OSSL_CMP_CTX *ctx, int for_KUR,
277
    const X509_NAME *ref_subj)
278
1.21k
{
279
1.21k
    if (ctx->subjectName != NULL)
280
0
        return IS_NULL_DN(ctx->subjectName) ? NULL : ctx->subjectName;
281
1.21k
    if (ctx->p10CSR != NULL) /* first default is from any given CSR */
282
0
        return X509_REQ_get_subject_name(ctx->p10CSR);
283
1.21k
    if (for_KUR || !HAS_SAN(ctx))
284
        /*
285
         * For KUR, copy subject from any reference cert as fallback.
286
         * For IR or CR, do the same only if there is no subjectAltName.
287
         */
288
1.21k
        return ref_subj;
289
0
    return NULL;
290
1.21k
}
291
292
OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid)
293
226
{
294
226
    OSSL_CRMF_MSG *crm = NULL;
295
226
    X509 *refcert = ctx->oldCert != NULL ? ctx->oldCert : ctx->cert;
296
    /* refcert defaults to current client cert */
297
226
    EVP_PKEY *rkey = ossl_cmp_ctx_get0_newPubkey(ctx);
298
226
    STACK_OF(GENERAL_NAME) *default_sans = NULL;
299
226
    const X509_NAME *ref_subj = refcert != NULL ? X509_get_subject_name(refcert) : NULL;
300
226
    const X509_NAME *subject = determine_subj(ctx, for_KUR, ref_subj);
301
226
    const X509_NAME *issuer = ctx->issuer != NULL || refcert == NULL
302
226
        ? (IS_NULL_DN(ctx->issuer) ? NULL : ctx->issuer)
303
226
        : X509_get_issuer_name(refcert);
304
226
    int crit = ctx->setSubjectAltNameCritical || subject == NULL;
305
    /* RFC5280: subjectAltName MUST be critical if subject is null */
306
226
    X509_EXTENSIONS *exts = NULL;
307
308
226
    if (rkey == NULL) {
309
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
310
        ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PUBLIC_KEY);
311
        return NULL;
312
#endif
313
226
    }
314
226
    if (for_KUR && refcert == NULL && ctx->p10CSR == NULL) {
315
0
        ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT);
316
0
        return NULL;
317
0
    }
318
226
    if ((crm = OSSL_CRMF_MSG_new()) == NULL)
319
0
        return NULL;
320
226
    if (!OSSL_CRMF_MSG_set_certReqId(crm, rid)
321
        /*
322
         * fill certTemplate, corresponding to CertificationRequestInfo
323
         * of PKCS#10. The rkey param cannot be NULL so far -
324
         * it could be NULL if centralized key creation was supported
325
         */
326
226
        || !OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_MSG_get0_tmpl(crm), rkey,
327
226
            subject, issuer, NULL /* serial */))
328
0
        goto err;
329
226
    if (ctx->days != 0) {
330
0
        time_t now = time(NULL);
331
0
        ASN1_TIME *notBefore = ASN1_TIME_adj(NULL, now, 0, 0);
332
0
        ASN1_TIME *notAfter = ASN1_TIME_adj(NULL, now, ctx->days, 0);
333
334
0
        if (notBefore == NULL
335
0
            || notAfter == NULL
336
0
            || !OSSL_CRMF_MSG_set0_validity(crm, notBefore, notAfter)) {
337
0
            ASN1_TIME_free(notBefore);
338
0
            ASN1_TIME_free(notAfter);
339
0
            goto err;
340
0
        }
341
0
    }
342
343
    /* extensions */
344
226
    if (ctx->p10CSR != NULL
345
0
        && (exts = X509_REQ_get_extensions(ctx->p10CSR)) == NULL)
346
0
        goto err;
347
226
    if (!ctx->SubjectAltName_nodefault && !HAS_SAN(ctx) && refcert != NULL
348
226
        && (default_sans = X509V3_get_d2i(X509_get0_extensions(refcert),
349
226
                NID_subject_alt_name, NULL, NULL))
350
226
            != NULL
351
0
        && !add1_extension(&exts, NID_subject_alt_name, crit, default_sans))
352
0
        goto err;
353
226
    if (sk_X509_EXTENSION_num(ctx->reqExtensions) > 0 /* augment/override existing ones */
354
0
        && X509v3_add_extensions(&exts, ctx->reqExtensions) == NULL)
355
0
        goto err;
356
226
    if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0
357
0
        && !add1_extension(&exts, NID_subject_alt_name,
358
0
            crit, ctx->subjectAltNames))
359
0
        goto err;
360
226
    if (ctx->policies != NULL
361
0
        && !add1_extension(&exts, NID_certificate_policies,
362
0
            ctx->setPoliciesCritical, ctx->policies))
363
0
        goto err;
364
226
    if (!OSSL_CRMF_MSG_set0_extensions(crm, exts))
365
0
        goto err;
366
226
    exts = NULL;
367
    /* end fill certTemplate, now set any controls */
368
369
    /* for KUR, set OldCertId according to D.6 */
370
226
    if (for_KUR && refcert != NULL) {
371
5
        OSSL_CRMF_CERTID *cid = OSSL_CRMF_CERTID_gen(X509_get_issuer_name(refcert),
372
5
            X509_get0_serialNumber(refcert));
373
5
        int ret;
374
375
5
        if (cid == NULL)
376
0
            goto err;
377
5
        ret = OSSL_CRMF_MSG_set1_regCtrl_oldCertID(crm, cid);
378
5
        OSSL_CRMF_CERTID_free(cid);
379
5
        if (ret == 0)
380
0
            goto err;
381
5
    }
382
383
226
    goto end;
384
385
226
err:
386
0
    OSSL_CRMF_MSG_free(crm);
387
0
    crm = NULL;
388
389
226
end:
390
226
    sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
391
226
    sk_GENERAL_NAME_pop_free(default_sans, GENERAL_NAME_free);
392
226
    return crm;
393
0
}
394
395
OSSL_CMP_MSG *ossl_cmp_certreq_new(OSSL_CMP_CTX *ctx, int type,
396
    const OSSL_CRMF_MSG *crm)
397
1.29k
{
398
1.29k
    OSSL_CMP_MSG *msg;
399
1.29k
    OSSL_CRMF_MSG *local_crm = NULL;
400
401
1.29k
    if (!ossl_assert(ctx != NULL))
402
0
        return NULL;
403
404
1.29k
    if (type != OSSL_CMP_PKIBODY_IR && type != OSSL_CMP_PKIBODY_CR
405
160
        && type != OSSL_CMP_PKIBODY_KUR && type != OSSL_CMP_PKIBODY_P10CR) {
406
0
        ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
407
0
        return NULL;
408
0
    }
409
1.29k
    if (type == OSSL_CMP_PKIBODY_P10CR && crm != NULL) {
410
0
        ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
411
0
        return NULL;
412
0
    }
413
414
1.29k
    if ((msg = ossl_cmp_msg_create(ctx, type)) == NULL)
415
84
        goto err;
416
417
    /* header */
418
1.21k
    if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
419
0
        goto err;
420
421
    /* body */
422
    /* For P10CR the content has already been set in OSSL_CMP_MSG_create */
423
1.21k
    if (type != OSSL_CMP_PKIBODY_P10CR) {
424
1.21k
        EVP_PKEY *privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
425
426
        /* privkey is ctx->newPkey (if private, else NULL) or ctx->pkey */
427
1.21k
        if (ctx->popoMethod >= OSSL_CRMF_POPO_SIGNATURE && privkey == NULL) {
428
0
            ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY_FOR_POPO);
429
0
            goto err;
430
0
        }
431
1.21k
        if (crm == NULL) {
432
1.21k
            local_crm = OSSL_CMP_CTX_setup_CRM(ctx,
433
1.21k
                type == OSSL_CMP_PKIBODY_KUR,
434
1.21k
                OSSL_CMP_CERTREQID);
435
1.21k
            if (local_crm == NULL
436
1.21k
                || !OSSL_CRMF_MSG_create_popo(ctx->popoMethod, local_crm,
437
1.21k
                    privkey, ctx->digest,
438
1.21k
                    ctx->libctx, ctx->propq))
439
0
                goto err;
440
1.21k
        } else {
441
0
            if ((local_crm = OSSL_CRMF_MSG_dup(crm)) == NULL)
442
0
                goto err;
443
0
        }
444
445
        /* value.ir is same for cr and kur */
446
1.21k
        if (!sk_OSSL_CRMF_MSG_push(msg->body->value.ir, local_crm))
447
0
            goto err;
448
1.21k
        local_crm = NULL;
449
1.21k
    }
450
451
1.21k
    if (!ossl_cmp_msg_protect(ctx, msg))
452
1.21k
        goto err;
453
454
0
    return msg;
455
456
1.29k
err:
457
1.29k
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREQ);
458
1.29k
    OSSL_CRMF_MSG_free(local_crm);
459
1.29k
    OSSL_CMP_MSG_free(msg);
460
1.29k
    return NULL;
461
1.21k
}
462
463
OSSL_CMP_MSG *ossl_cmp_certrep_new(OSSL_CMP_CTX *ctx, int bodytype,
464
    int certReqId, const OSSL_CMP_PKISI *si,
465
    X509 *cert, const X509 *encryption_recip,
466
    STACK_OF(X509) *chain, STACK_OF(X509) *caPubs,
467
    int unprotectedErrors)
468
0
{
469
0
    OSSL_CMP_MSG *msg = NULL;
470
0
    OSSL_CMP_CERTREPMESSAGE *repMsg = NULL;
471
0
    OSSL_CMP_CERTRESPONSE *resp = NULL;
472
0
    int status = OSSL_CMP_PKISTATUS_unspecified;
473
474
0
    if (!ossl_assert(ctx != NULL && si != NULL))
475
0
        return NULL;
476
477
0
    if ((msg = ossl_cmp_msg_create(ctx, bodytype)) == NULL)
478
0
        goto err;
479
0
    repMsg = msg->body->value.ip; /* value.ip is same for cp and kup */
480
481
    /* header */
482
0
    if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header))
483
0
        goto err;
484
485
    /* body */
486
0
    if ((resp = OSSL_CMP_CERTRESPONSE_new()) == NULL)
487
0
        goto err;
488
0
    OSSL_CMP_PKISI_free(resp->status);
489
0
    if ((resp->status = OSSL_CMP_PKISI_dup(si)) == NULL
490
0
        || !ASN1_INTEGER_set(resp->certReqId, certReqId))
491
0
        goto err;
492
493
0
    status = ossl_cmp_pkisi_get_status(resp->status);
494
0
    if (status != OSSL_CMP_PKISTATUS_rejection
495
0
        && status != OSSL_CMP_PKISTATUS_waiting && cert != NULL) {
496
0
        if (encryption_recip != NULL) {
497
0
            ERR_raise(ERR_LIB_CMP, ERR_R_UNSUPPORTED);
498
0
            goto err;
499
0
        }
500
501
0
        if ((resp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new())
502
0
            == NULL)
503
0
            goto err;
504
0
        resp->certifiedKeyPair->certOrEncCert->type = OSSL_CMP_CERTORENCCERT_CERTIFICATE;
505
0
        if (!X509_up_ref(cert))
506
0
            goto err;
507
0
        resp->certifiedKeyPair->certOrEncCert->value.certificate = cert;
508
0
    }
509
510
0
    if (!sk_OSSL_CMP_CERTRESPONSE_push(repMsg->response, resp))
511
0
        goto err;
512
0
    resp = NULL;
513
514
0
    if (bodytype == OSSL_CMP_PKIBODY_IP && caPubs != NULL
515
0
        && (repMsg->caPubs = X509_chain_up_ref(caPubs)) == NULL)
516
0
        goto err;
517
0
    if (sk_X509_num(chain) > 0
518
0
        && !ossl_x509_add_certs_new(&msg->extraCerts, chain,
519
0
            X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
520
0
        goto err;
521
522
0
    if (!unprotectedErrors
523
0
        || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
524
0
        if (!ossl_cmp_msg_protect(ctx, msg))
525
0
            goto err;
526
527
0
    return msg;
528
529
0
err:
530
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
531
0
    OSSL_CMP_CERTRESPONSE_free(resp);
532
0
    OSSL_CMP_MSG_free(msg);
533
0
    return NULL;
534
0
}
535
536
OSSL_CMP_MSG *ossl_cmp_rr_new(OSSL_CMP_CTX *ctx)
537
156
{
538
156
    OSSL_CMP_MSG *msg = NULL;
539
156
    const X509_NAME *issuer = NULL;
540
156
    const X509_NAME *subject = NULL;
541
156
    const ASN1_INTEGER *serialNumber = NULL;
542
156
    EVP_PKEY *pubkey = NULL;
543
156
    OSSL_CMP_REVDETAILS *rd;
544
156
    int ret;
545
546
156
    if (!ossl_assert(ctx != NULL
547
156
            && (ctx->oldCert != NULL || ctx->p10CSR != NULL
548
156
                || (ctx->serialNumber != NULL && ctx->issuer != NULL))))
549
0
        return NULL;
550
551
156
    if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL)
552
0
        goto err;
553
554
156
    if (ctx->serialNumber != NULL && ctx->issuer != NULL) {
555
0
        issuer = ctx->issuer;
556
0
        serialNumber = ctx->serialNumber;
557
156
    } else if (ctx->oldCert != NULL) {
558
156
        issuer = X509_get_issuer_name(ctx->oldCert);
559
156
        serialNumber = X509_get0_serialNumber(ctx->oldCert);
560
156
    } else if (ctx->p10CSR != NULL) {
561
0
        pubkey = X509_REQ_get0_pubkey(ctx->p10CSR);
562
0
        subject = X509_REQ_get_subject_name(ctx->p10CSR);
563
0
    } else {
564
0
        goto err;
565
0
    }
566
567
    /* Fill the template from the contents of the certificate to be revoked */
568
156
    ret = OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails, pubkey, subject,
569
156
        issuer, serialNumber);
570
156
    if (!ret)
571
0
        goto err;
572
573
    /* revocation reason code is optional */
574
156
    if (ctx->revocationReason != CRL_REASON_NONE
575
0
        && !add_crl_reason_extension(&rd->crlEntryDetails,
576
0
            ctx->revocationReason))
577
0
        goto err;
578
579
156
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL)
580
0
        goto err;
581
582
156
    if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd))
583
0
        goto err;
584
156
    rd = NULL;
585
    /* Revocation Passphrase according to section 5.3.19.9 could be set here */
586
587
156
    if (!ossl_cmp_msg_protect(ctx, msg))
588
156
        goto err;
589
590
0
    return msg;
591
592
156
err:
593
156
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
594
156
    OSSL_CMP_MSG_free(msg);
595
156
    OSSL_CMP_REVDETAILS_free(rd);
596
156
    return NULL;
597
156
}
598
599
OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
600
    const OSSL_CRMF_CERTID *cid, int unprotectedErrors)
601
0
{
602
0
    OSSL_CMP_REVREPCONTENT *rep = NULL;
603
0
    OSSL_CMP_PKISI *si1 = NULL;
604
0
    OSSL_CRMF_CERTID *cid_copy = NULL;
605
0
    OSSL_CMP_MSG *msg = NULL;
606
607
0
    if (!ossl_assert(ctx != NULL && si != NULL))
608
0
        return NULL;
609
610
0
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL)
611
0
        goto err;
612
0
    rep = msg->body->value.rp;
613
614
0
    if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL)
615
0
        goto err;
616
617
0
    if (!sk_OSSL_CMP_PKISI_push(rep->status, si1)) {
618
0
        OSSL_CMP_PKISI_free(si1);
619
0
        goto err;
620
0
    }
621
622
0
    if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL)
623
0
        goto err;
624
0
    if (cid != NULL) {
625
0
        if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL)
626
0
            goto err;
627
0
        if (!sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) {
628
0
            OSSL_CRMF_CERTID_free(cid_copy);
629
0
            goto err;
630
0
        }
631
0
    }
632
633
0
    if (!unprotectedErrors
634
0
        || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
635
0
        if (!ossl_cmp_msg_protect(ctx, msg))
636
0
            goto err;
637
638
0
    return msg;
639
640
0
err:
641
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RP);
642
0
    OSSL_CMP_MSG_free(msg);
643
0
    return NULL;
644
0
}
645
646
OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx)
647
800
{
648
800
    OSSL_CMP_MSG *msg;
649
650
800
    if (!ossl_assert(ctx != NULL))
651
0
        return NULL;
652
653
800
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL)
654
0
        goto err;
655
800
    if (ossl_cmp_msg_protect(ctx, msg))
656
0
        return msg;
657
658
800
err:
659
800
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
660
800
    OSSL_CMP_MSG_free(msg);
661
800
    return NULL;
662
800
}
663
664
int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav)
665
0
{
666
0
    int bodytype;
667
668
0
    if (!ossl_assert(msg != NULL && itav != NULL))
669
0
        return 0;
670
671
0
    bodytype = OSSL_CMP_MSG_get_bodytype(msg);
672
0
    if (bodytype != OSSL_CMP_PKIBODY_GENM
673
0
        && bodytype != OSSL_CMP_PKIBODY_GENP) {
674
0
        ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
675
0
        return 0;
676
0
    }
677
678
    /* value.genp has the same structure, so this works for genp as well */
679
0
    return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav);
680
0
}
681
682
int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg,
683
    const STACK_OF(OSSL_CMP_ITAV) *itavs)
684
0
{
685
0
    int i;
686
0
    OSSL_CMP_ITAV *itav = NULL;
687
688
0
    if (!ossl_assert(msg != NULL))
689
0
        return 0;
690
691
0
    for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
692
0
        itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
693
0
        if (itav == NULL
694
0
            || !ossl_cmp_msg_gen_push0_ITAV(msg, itav)) {
695
0
            OSSL_CMP_ITAV_free(itav);
696
0
            return 0;
697
0
        }
698
0
    }
699
0
    return 1;
700
0
}
701
702
/*
703
 * Creates a new General Message/Response with a copy of the given itav stack
704
 * returns a pointer to the PKIMessage on success, NULL on error
705
 */
706
static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx,
707
    const STACK_OF(OSSL_CMP_ITAV) *itavs,
708
    int body_type, int err_code)
709
1.00k
{
710
1.00k
    OSSL_CMP_MSG *msg = NULL;
711
712
1.00k
    if (!ossl_assert(ctx != NULL))
713
0
        return NULL;
714
715
1.00k
    if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL)
716
0
        return NULL;
717
718
1.00k
    if (itavs != NULL && !ossl_cmp_msg_gen_push1_ITAVs(msg, itavs))
719
0
        goto err;
720
721
1.00k
    if (!ossl_cmp_msg_protect(ctx, msg))
722
1.00k
        goto err;
723
724
0
    return msg;
725
726
1.00k
err:
727
1.00k
    ERR_raise(ERR_LIB_CMP, err_code);
728
1.00k
    OSSL_CMP_MSG_free(msg);
729
1.00k
    return NULL;
730
1.00k
}
731
732
OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx)
733
1.00k
{
734
1.00k
    return gen_new(ctx, ctx->genm_ITAVs,
735
1.00k
        OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM);
736
1.00k
}
737
738
OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx,
739
    const STACK_OF(OSSL_CMP_ITAV) *itavs)
740
0
{
741
0
    return gen_new(ctx, itavs,
742
0
        OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP);
743
0
}
744
745
OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
746
    int64_t errorCode, const char *details,
747
    int unprotected)
748
39.4k
{
749
39.4k
    OSSL_CMP_MSG *msg = NULL;
750
39.4k
    const char *lib = NULL, *reason = NULL;
751
39.4k
    OSSL_CMP_PKIFREETEXT *ft;
752
753
39.4k
    if (!ossl_assert(ctx != NULL && si != NULL))
754
0
        return NULL;
755
756
39.4k
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL)
757
0
        goto err;
758
759
39.4k
    OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo);
760
39.4k
    if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si))
761
39.4k
        == NULL)
762
0
        goto err;
763
39.4k
    if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL)
764
0
        goto err;
765
39.4k
    if (!ASN1_INTEGER_set_int64(msg->body->value.error->errorCode, errorCode))
766
0
        goto err;
767
39.4k
    if (errorCode > 0
768
39.4k
        && (uint64_t)errorCode < ((uint64_t)ERR_SYSTEM_FLAG << 1)) {
769
39.4k
        lib = ERR_lib_error_string((unsigned long)errorCode);
770
39.4k
        reason = ERR_reason_error_string((unsigned long)errorCode);
771
39.4k
    }
772
39.4k
    if (lib != NULL || reason != NULL || details != NULL) {
773
39.4k
        if ((ft = sk_ASN1_UTF8STRING_new_null()) == NULL)
774
0
            goto err;
775
39.4k
        msg->body->value.error->errorDetails = ft;
776
39.4k
        if (lib != NULL && *lib != '\0'
777
39.4k
            && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, lib, -1))
778
0
            goto err;
779
39.4k
        if (reason != NULL && *reason != '\0'
780
39.3k
            && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, reason, -1))
781
0
            goto err;
782
39.4k
        if (details != NULL
783
2.47k
            && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, details, -1))
784
0
            goto err;
785
39.4k
    }
786
787
39.4k
    if (!unprotected && !ossl_cmp_msg_protect(ctx, msg))
788
39.4k
        goto err;
789
0
    return msg;
790
791
39.4k
err:
792
39.4k
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_ERROR);
793
39.4k
    OSSL_CMP_MSG_free(msg);
794
39.4k
    return NULL;
795
39.4k
}
796
797
/*
798
 * Set the certHash field of a OSSL_CMP_CERTSTATUS structure.
799
 * This is used in the certConf message, for example,
800
 * to confirm that the certificate was received successfully.
801
 */
802
int ossl_cmp_certstatus_set0_certHash(OSSL_CMP_CERTSTATUS *certStatus,
803
    ASN1_OCTET_STRING *hash)
804
0
{
805
0
    if (!ossl_assert(certStatus != NULL))
806
0
        return 0;
807
0
    ASN1_OCTET_STRING_free(certStatus->certHash);
808
0
    certStatus->certHash = hash;
809
0
    return 1;
810
0
}
811
812
OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int certReqId,
813
    int fail_info, const char *text)
814
0
{
815
0
    OSSL_CMP_MSG *msg = NULL;
816
0
    OSSL_CMP_CERTSTATUS *certStatus = NULL;
817
0
    EVP_MD *md;
818
0
    int is_fallback;
819
0
    ASN1_OCTET_STRING *certHash = NULL;
820
0
    OSSL_CMP_PKISI *sinfo;
821
822
0
    if (!ossl_assert(ctx != NULL && ctx->newCert != NULL
823
0
            && (certReqId == OSSL_CMP_CERTREQID
824
0
                || certReqId == OSSL_CMP_CERTREQID_NONE)))
825
0
        return NULL;
826
827
0
    if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
828
0
        ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE);
829
0
        return NULL;
830
0
    }
831
832
0
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL)
833
0
        goto err;
834
835
0
    if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL)
836
0
        goto err;
837
    /* consume certStatus into msg right away so it gets deallocated with msg */
838
0
    if (sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus) < 1) {
839
0
        OSSL_CMP_CERTSTATUS_free(certStatus);
840
0
        goto err;
841
0
    }
842
843
    /* set the ID of the certReq */
844
0
    if (!ASN1_INTEGER_set(certStatus->certReqId, certReqId))
845
0
        goto err;
846
847
0
    certStatus->hashAlg = NULL;
848
    /*
849
     * The hash of the certificate, using the same hash algorithm
850
     * as is used to create and verify the certificate signature.
851
     * If not available, a fallback hash algorithm is used.
852
     */
853
0
    if ((certHash = X509_digest_sig(ctx->newCert, &md, &is_fallback)) == NULL)
854
0
        goto err;
855
0
    if (is_fallback) {
856
0
        if (!ossl_cmp_hdr_set_pvno(msg->header, OSSL_CMP_PVNO_3))
857
0
            goto err;
858
0
        if ((certStatus->hashAlg = X509_ALGOR_new()) == NULL)
859
0
            goto err;
860
0
        X509_ALGOR_set_md(certStatus->hashAlg, md);
861
0
    }
862
0
    EVP_MD_free(md);
863
864
0
    if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash))
865
0
        goto err;
866
0
    certHash = NULL;
867
    /*
868
     * For any particular CertStatus, omission of the statusInfo field
869
     * indicates ACCEPTANCE of the specified certificate.  Alternatively,
870
     * explicit status details (with respect to acceptance or rejection) MAY
871
     * be provided in the statusInfo field, perhaps for auditing purposes at
872
     * the CA/RA.
873
     */
874
0
    sinfo = fail_info != 0 ? OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) : OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text);
875
0
    if (sinfo == NULL)
876
0
        goto err;
877
0
    certStatus->statusInfo = sinfo;
878
879
0
    if (!ossl_cmp_msg_protect(ctx, msg))
880
0
        goto err;
881
882
0
    return msg;
883
884
0
err:
885
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF);
886
0
    OSSL_CMP_MSG_free(msg);
887
0
    ASN1_OCTET_STRING_free(certHash);
888
0
    return NULL;
889
0
}
890
891
OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid)
892
373
{
893
373
    OSSL_CMP_MSG *msg = NULL;
894
373
    OSSL_CMP_POLLREQ *preq = NULL;
895
896
373
    if (!ossl_assert(ctx != NULL))
897
0
        return NULL;
898
899
373
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL)
900
0
        goto err;
901
902
373
    if ((preq = OSSL_CMP_POLLREQ_new()) == NULL
903
373
        || !ASN1_INTEGER_set(preq->certReqId, crid)
904
373
        || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq))
905
0
        goto err;
906
907
373
    preq = NULL;
908
373
    if (!ossl_cmp_msg_protect(ctx, msg))
909
373
        goto err;
910
911
0
    return msg;
912
913
373
err:
914
373
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ);
915
373
    OSSL_CMP_POLLREQ_free(preq);
916
373
    OSSL_CMP_MSG_free(msg);
917
373
    return NULL;
918
373
}
919
920
OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid,
921
    int64_t poll_after)
922
0
{
923
0
    OSSL_CMP_MSG *msg;
924
0
    OSSL_CMP_POLLREP *prep;
925
926
0
    if (!ossl_assert(ctx != NULL))
927
0
        return NULL;
928
929
0
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL)
930
0
        goto err;
931
0
    if ((prep = OSSL_CMP_POLLREP_new()) == NULL)
932
0
        goto err;
933
0
    if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep))
934
0
        goto err;
935
0
    if (!ASN1_INTEGER_set(prep->certReqId, crid))
936
0
        goto err;
937
0
    if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after))
938
0
        goto err;
939
940
0
    if (!ossl_cmp_msg_protect(ctx, msg))
941
0
        goto err;
942
0
    return msg;
943
944
0
err:
945
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
946
0
    OSSL_CMP_MSG_free(msg);
947
0
    return NULL;
948
0
}
949
950
/*-
951
 * returns the status field of the RevRepContent with the given
952
 * request/sequence id inside a revocation response.
953
 * RevRepContent has the revocation statuses in same order as they were sent in
954
 * RevReqContent.
955
 * returns NULL on error
956
 */
957
OSSL_CMP_PKISI *
958
ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
959
0
{
960
0
    OSSL_CMP_PKISI *status;
961
962
0
    if (!ossl_assert(rrep != NULL))
963
0
        return NULL;
964
965
0
    if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL)
966
0
        return status;
967
968
0
    ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND);
969
0
    return NULL;
970
0
}
971
972
/*
973
 * returns the CertId field in the revCerts part of the RevRepContent
974
 * with the given request/sequence id inside a revocation response.
975
 * RevRepContent has the CertIds in same order as they were sent in
976
 * RevReqContent.
977
 * returns NULL on error
978
 */
979
OSSL_CRMF_CERTID *
980
ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
981
0
{
982
0
    OSSL_CRMF_CERTID *cid = NULL;
983
984
0
    if (!ossl_assert(rrep != NULL))
985
0
        return NULL;
986
987
0
    if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL)
988
0
        return cid;
989
990
0
    ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND);
991
0
    return NULL;
992
0
}
993
994
static int suitable_rid(const ASN1_INTEGER *certReqId, int rid)
995
0
{
996
0
    int trid;
997
998
0
    if (rid == OSSL_CMP_CERTREQID_NONE)
999
0
        return 1;
1000
1001
0
    trid = ossl_cmp_asn1_get_int(certReqId);
1002
0
    if (trid <= OSSL_CMP_CERTREQID_INVALID) {
1003
0
        ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
1004
0
        return 0;
1005
0
    }
1006
0
    return rid == trid;
1007
0
}
1008
1009
/*
1010
 * returns a pointer to the PollResponse with the given CertReqId
1011
 * (or the first one in case -1) inside a PollRepContent
1012
 * returns NULL on error or if no suitable PollResponse available
1013
 */
1014
OSSL_CMP_POLLREP *
1015
ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc,
1016
    int rid)
1017
0
{
1018
0
    OSSL_CMP_POLLREP *pollRep = NULL;
1019
0
    int i;
1020
1021
0
    if (!ossl_assert(prc != NULL))
1022
0
        return NULL;
1023
1024
0
    for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) {
1025
0
        pollRep = sk_OSSL_CMP_POLLREP_value(prc, i);
1026
0
        if (suitable_rid(pollRep->certReqId, rid))
1027
0
            return pollRep;
1028
0
    }
1029
1030
0
    ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
1031
0
        "expected certReqId = %d", rid);
1032
0
    return NULL;
1033
0
}
1034
1035
/*
1036
 * returns a pointer to the CertResponse with the given CertReqId
1037
 * (or the first one in case -1) inside a CertRepMessage
1038
 * returns NULL on error or if no suitable CertResponse available
1039
 */
1040
OSSL_CMP_CERTRESPONSE *
1041
ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm,
1042
    int rid)
1043
0
{
1044
0
    OSSL_CMP_CERTRESPONSE *crep = NULL;
1045
0
    int i;
1046
1047
0
    if (!ossl_assert(crm != NULL && crm->response != NULL))
1048
0
        return NULL;
1049
1050
0
    for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) {
1051
0
        crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i);
1052
0
        if (suitable_rid(crep->certReqId, rid))
1053
0
            return crep;
1054
0
    }
1055
1056
0
    ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
1057
0
        "expected certReqId = %d", rid);
1058
0
    return NULL;
1059
0
}
1060
1061
/*-
1062
 * Retrieve the newly enrolled certificate from the given certResponse crep.
1063
 * Uses libctx and propq from ctx, in case of indirect POPO also private key.
1064
 * Returns a pointer to a copy of the found certificate, or NULL if not found.
1065
 */
1066
X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CTX *ctx,
1067
    const OSSL_CMP_CERTRESPONSE *crep)
1068
0
{
1069
0
    OSSL_CMP_CERTORENCCERT *coec;
1070
0
    X509 *crt = NULL;
1071
0
    EVP_PKEY *pkey;
1072
1073
0
    if (!ossl_assert(crep != NULL && ctx != NULL))
1074
0
        return NULL;
1075
1076
0
    if (crep->certifiedKeyPair
1077
0
        && (coec = crep->certifiedKeyPair->certOrEncCert) != NULL) {
1078
0
        switch (coec->type) {
1079
0
        case OSSL_CMP_CERTORENCCERT_CERTIFICATE:
1080
0
            crt = X509_dup(coec->value.certificate);
1081
0
            break;
1082
0
        case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT:
1083
            /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */
1084
0
            pkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
1085
            /* pkey is ctx->newPkey (if private, else NULL) or ctx->pkey */
1086
0
            if (pkey == NULL) {
1087
0
                ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY);
1088
0
                return NULL;
1089
0
            }
1090
0
            crt = OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(coec->value.encryptedCert,
1091
0
                ctx->libctx, ctx->propq,
1092
0
                pkey);
1093
0
            break;
1094
0
        default:
1095
0
            ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE);
1096
0
            return NULL;
1097
0
        }
1098
0
    }
1099
0
    if (crt == NULL)
1100
0
        ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
1101
0
    else
1102
0
        (void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq);
1103
0
    return crt;
1104
0
}
1105
1106
int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
1107
0
{
1108
0
    if (ctx == NULL || msg == NULL) {
1109
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1110
0
        return 0;
1111
0
    }
1112
0
    if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header))
1113
0
        return 0;
1114
0
    return msg->header->protectionAlg == NULL
1115
0
        || ossl_cmp_msg_protect(ctx, msg);
1116
0
}
1117
1118
int OSSL_CMP_MSG_update_recipNonce(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
1119
0
{
1120
0
    if (ctx == NULL || msg == NULL || msg->header == NULL) {
1121
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1122
0
        return 0;
1123
0
    }
1124
0
    if (ctx->recipNonce == NULL) /* nothing to do for 1st msg in transaction */
1125
0
        return 1;
1126
0
    if (!ossl_cmp_asn1_octet_string_set1(&msg->header->recipNonce,
1127
0
            ctx->recipNonce))
1128
0
        return 0;
1129
0
    return msg->header->protectionAlg == NULL || ossl_cmp_msg_protect(ctx, msg);
1130
0
}
1131
1132
OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx,
1133
    const char *propq)
1134
0
{
1135
0
    OSSL_CMP_MSG *msg;
1136
0
    BIO *bio = NULL;
1137
1138
0
    if (file == NULL) {
1139
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1140
0
        return NULL;
1141
0
    }
1142
1143
0
    msg = OSSL_CMP_MSG_new(libctx, propq);
1144
0
    if (msg == NULL) {
1145
0
        ERR_raise(ERR_LIB_CMP, ERR_R_CMP_LIB);
1146
0
        return NULL;
1147
0
    }
1148
1149
0
    if ((bio = BIO_new_file(file, "rb")) == NULL
1150
0
        || d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) {
1151
0
        OSSL_CMP_MSG_free(msg);
1152
0
        msg = NULL;
1153
0
    }
1154
0
    BIO_free(bio);
1155
0
    return msg;
1156
0
}
1157
1158
int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg)
1159
0
{
1160
0
    BIO *bio;
1161
0
    int res;
1162
1163
0
    if (file == NULL || msg == NULL) {
1164
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1165
0
        return -1;
1166
0
    }
1167
1168
0
    bio = BIO_new_file(file, "wb");
1169
0
    if (bio == NULL)
1170
0
        return -2;
1171
0
    res = i2d_OSSL_CMP_MSG_bio(bio, msg);
1172
0
    BIO_free(bio);
1173
0
    return res;
1174
0
}
1175
1176
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in,
1177
    long len)
1178
0
{
1179
0
    OSSL_LIB_CTX *libctx = NULL;
1180
0
    const char *propq = NULL;
1181
1182
0
    if (msg != NULL && *msg != NULL) {
1183
0
        libctx = (*msg)->libctx;
1184
0
        propq = (*msg)->propq;
1185
0
    }
1186
1187
0
    return (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len,
1188
0
        ASN1_ITEM_rptr(OSSL_CMP_MSG),
1189
0
        libctx, propq);
1190
0
}
1191
1192
int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out)
1193
78.9k
{
1194
78.9k
    return ASN1_item_i2d((const ASN1_VALUE *)msg, out,
1195
78.9k
        ASN1_ITEM_rptr(OSSL_CMP_MSG));
1196
78.9k
}
1197
1198
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg)
1199
57.5k
{
1200
57.5k
    OSSL_LIB_CTX *libctx = NULL;
1201
57.5k
    const char *propq = NULL;
1202
1203
57.5k
    if (msg != NULL && *msg != NULL) {
1204
0
        libctx = (*msg)->libctx;
1205
0
        propq = (*msg)->propq;
1206
0
    }
1207
1208
57.5k
    return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx,
1209
57.5k
        propq);
1210
57.5k
}
1211
1212
int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg)
1213
39.4k
{
1214
39.4k
    return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg);
1215
39.4k
}
1216
1217
int ossl_cmp_is_error_with_waiting(const OSSL_CMP_MSG *msg)
1218
0
{
1219
0
    if (!ossl_assert(msg != NULL))
1220
0
        return 0;
1221
1222
0
    return (OSSL_CMP_MSG_get_bodytype(msg) == OSSL_CMP_PKIBODY_ERROR
1223
0
        && ossl_cmp_pkisi_get_status(msg->body->value.error->pKIStatusInfo)
1224
0
            == OSSL_CMP_PKISTATUS_waiting);
1225
0
}