Coverage Report

Created: 2025-08-28 07:07

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