Coverage Report

Created: 2026-02-14 07:20

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