Coverage Report

Created: 2026-02-14 07:20

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