Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/cmp/cmp_msg.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2007-2023 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
30.9k
{
25
30.9k
    OSSL_CMP_MSG *msg = NULL;
26
27
30.9k
    msg = (OSSL_CMP_MSG *)ASN1_item_new_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG),
28
30.9k
                                           libctx, propq);
29
30.9k
    if (!ossl_cmp_msg_set0_libctx(msg, libctx, propq)) {
30
0
        OSSL_CMP_MSG_free(msg);
31
0
        msg = NULL;
32
0
    }
33
30.9k
    return msg;
34
30.9k
}
35
36
void OSSL_CMP_MSG_free(OSSL_CMP_MSG *msg)
37
91.4k
{
38
91.4k
    ASN1_item_free((ASN1_VALUE *)msg, ASN1_ITEM_rptr(OSSL_CMP_MSG));
39
91.4k
}
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
30.9k
{
49
30.9k
    if (msg != NULL) {
50
30.9k
        msg->libctx = libctx;
51
30.9k
        OPENSSL_free(msg->propq);
52
30.9k
        msg->propq = NULL;
53
30.9k
        if (propq != NULL) {
54
0
            msg->propq = OPENSSL_strdup(propq);
55
0
            if (msg->propq == NULL)
56
0
                return 0;
57
0
        }
58
30.9k
    }
59
30.9k
    return 1;
60
30.9k
}
61
62
OSSL_CMP_PKIHEADER *OSSL_CMP_MSG_get0_header(const OSSL_CMP_MSG *msg)
63
68.4k
{
64
68.4k
    if (msg == NULL) {
65
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
66
0
        return NULL;
67
0
    }
68
68.4k
    return msg->header;
69
68.4k
}
70
71
const char *ossl_cmp_bodytype_to_string(int type)
72
28.3k
{
73
28.3k
    static const char *type_names[] = {
74
28.3k
        "IR", "IP", "CR", "CP", "P10CR",
75
28.3k
        "POPDECC", "POPDECR", "KUR", "KUP",
76
28.3k
        "KRR", "KRP", "RR", "RP", "CCR", "CCP",
77
28.3k
        "CKUANN", "CANN", "RANN", "CRLANN", "PKICONF", "NESTED",
78
28.3k
        "GENM", "GENP", "ERROR", "CERTCONF", "POLLREQ", "POLLREP",
79
28.3k
    };
80
81
28.3k
    if (type < 0 || type > OSSL_CMP_PKIBODY_TYPE_MAX)
82
0
        return "illegal body type";
83
28.3k
    return type_names[type];
84
28.3k
}
85
86
int ossl_cmp_msg_set_bodytype(OSSL_CMP_MSG *msg, int type)
87
30.9k
{
88
30.9k
    if (!ossl_assert(msg != NULL && msg->body != NULL))
89
0
        return 0;
90
91
30.9k
    msg->body->type = type;
92
30.9k
    return 1;
93
30.9k
}
94
95
int OSSL_CMP_MSG_get_bodytype(const OSSL_CMP_MSG *msg)
96
75.0k
{
97
75.0k
    if (!ossl_assert(msg != NULL && msg->body != NULL))
98
0
        return -1;
99
100
75.0k
    return msg->body->type;
101
75.0k
}
102
103
/* Add an extension to the referenced extension stack, which may be NULL */
104
static int add1_extension(X509_EXTENSIONS **pexts, int nid, int crit, void *ex)
105
0
{
106
0
    X509_EXTENSION *ext;
107
0
    int res;
108
109
0
    if (!ossl_assert(pexts != NULL)) /* pointer to var must not be NULL */
110
0
        return 0;
111
112
0
    if ((ext = X509V3_EXT_i2d(nid, crit, ex)) == NULL)
113
0
        return 0;
114
115
0
    res = X509v3_add_ext(pexts, ext, 0) != NULL;
116
0
    X509_EXTENSION_free(ext);
117
0
    return res;
118
0
}
119
120
/* Add extension list to the referenced extension stack, which may be NULL */
121
static int add_extensions(STACK_OF(X509_EXTENSION) **target,
122
                          const STACK_OF(X509_EXTENSION) *exts)
123
0
{
124
0
    int i;
125
126
0
    if (target == NULL)
127
0
        return 0;
128
129
0
    for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
130
0
        X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
131
0
        ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext);
132
0
        int idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
133
134
        /* Does extension exist in target? */
135
0
        if (idx != -1) {
136
            /* Delete all extensions of same type */
137
0
            do {
138
0
                X509_EXTENSION_free(sk_X509_EXTENSION_delete(*target, idx));
139
0
                idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
140
0
            } while (idx != -1);
141
0
        }
142
0
        if (!X509v3_add_ext(target, ext, -1))
143
0
            return 0;
144
0
    }
145
0
    return 1;
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
30.9k
{
162
30.9k
    OSSL_CMP_MSG *msg = NULL;
163
164
30.9k
    if (!ossl_assert(ctx != NULL))
165
0
        return NULL;
166
167
30.9k
    if ((msg = OSSL_CMP_MSG_new(ctx->libctx, ctx->propq)) == NULL)
168
0
        return NULL;
169
30.9k
    if (!ossl_cmp_hdr_init(ctx, msg->header)
170
30.9k
            || !ossl_cmp_msg_set_bodytype(msg, bodytype))
171
0
        goto err;
172
30.9k
    if (ctx->geninfo_ITAVs != NULL
173
30.9k
            && !ossl_cmp_hdr_generalInfo_push1_items(msg->header,
174
0
                                                     ctx->geninfo_ITAVs))
175
0
        goto err;
176
177
30.9k
    switch (bodytype) {
178
546
    case OSSL_CMP_PKIBODY_IR:
179
706
    case OSSL_CMP_PKIBODY_CR:
180
762
    case OSSL_CMP_PKIBODY_KUR:
181
762
        if ((msg->body->value.ir = OSSL_CRMF_MSGS_new()) == NULL)
182
0
            goto err;
183
762
        return msg;
184
185
160
    case OSSL_CMP_PKIBODY_P10CR:
186
160
        if (ctx->p10CSR == NULL) {
187
160
            ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_P10CSR);
188
160
            goto err;
189
160
        }
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
204
    case OSSL_CMP_PKIBODY_RR:
202
204
        if ((msg->body->value.rr = sk_OSSL_CMP_REVDETAILS_new_null()) == NULL)
203
0
            goto err;
204
204
        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
278
    case OSSL_CMP_PKIBODY_PKICONF:
216
278
        if ((msg->body->value.pkiconf = ASN1_TYPE_new()) == NULL)
217
0
            goto err;
218
278
        ASN1_TYPE_set(msg->body->value.pkiconf, V_ASN1_NULL, NULL);
219
278
        return msg;
220
221
390
    case OSSL_CMP_PKIBODY_POLLREQ:
222
390
        if ((msg->body->value.pollReq = sk_OSSL_CMP_POLLREQ_new_null()) == NULL)
223
0
            goto err;
224
390
        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
746
    case OSSL_CMP_PKIBODY_GENM:
231
746
    case OSSL_CMP_PKIBODY_GENP:
232
746
        if ((msg->body->value.genm = sk_OSSL_CMP_ITAV_new_null()) == NULL)
233
0
            goto err;
234
746
        return msg;
235
236
28.3k
    case OSSL_CMP_PKIBODY_ERROR:
237
28.3k
        if ((msg->body->value.error = OSSL_CMP_ERRORMSGCONTENT_new()) == NULL)
238
0
            goto err;
239
28.3k
        return msg;
240
241
0
    default:
242
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
243
0
        goto err;
244
30.9k
    }
245
246
160
 err:
247
160
    OSSL_CMP_MSG_free(msg);
248
160
    return NULL;
249
30.9k
}
250
251
#define HAS_SAN(ctx) \
252
1.62k
    (sk_GENERAL_NAME_num((ctx)->subjectAltNames) > 0 \
253
1.62k
         || 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
762
{
258
762
    if (ctx->subjectName != NULL)
259
0
        return IS_NULL_DN(ctx->subjectName) ? NULL : ctx->subjectName;
260
762
    if (ctx->p10CSR != NULL) /* first default is from any given CSR */
261
0
        return X509_REQ_get_subject_name(ctx->p10CSR);
262
762
    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
762
        return ref_subj;
268
0
    return NULL;
269
762
}
270
271
OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid)
272
459
{
273
459
    OSSL_CRMF_MSG *crm = NULL;
274
459
    X509 *refcert = ctx->oldCert != NULL ? ctx->oldCert : ctx->cert;
275
    /* refcert defaults to current client cert */
276
459
    EVP_PKEY *rkey = ossl_cmp_ctx_get0_newPubkey(ctx);
277
459
    STACK_OF(GENERAL_NAME) *default_sans = NULL;
278
459
    const X509_NAME *ref_subj =
279
459
        refcert != NULL ? X509_get_subject_name(refcert) : NULL;
280
459
    const X509_NAME *subject = determine_subj(ctx, for_KUR, ref_subj);
281
459
    const X509_NAME *issuer = ctx->issuer != NULL || refcert == NULL
282
459
        ? (IS_NULL_DN(ctx->issuer) ? NULL : ctx->issuer)
283
459
        : X509_get_issuer_name(refcert);
284
459
    int crit = ctx->setSubjectAltNameCritical || subject == NULL;
285
    /* RFC5280: subjectAltName MUST be critical if subject is null */
286
459
    X509_EXTENSIONS *exts = NULL;
287
288
459
    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
459
    }
294
459
    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
459
    if ((crm = OSSL_CRMF_MSG_new()) == NULL)
299
0
        return NULL;
300
459
    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
459
            || !OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_MSG_get0_tmpl(crm), rkey,
307
459
                                            subject, issuer, NULL /* serial */))
308
0
        goto err;
309
459
    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
459
    if (ctx->p10CSR != NULL
325
459
            && (exts = X509_REQ_get_extensions(ctx->p10CSR)) == NULL)
326
0
        goto err;
327
459
    if (!ctx->SubjectAltName_nodefault && !HAS_SAN(ctx) && refcert != NULL
328
459
        && (default_sans = X509V3_get_d2i(X509_get0_extensions(refcert),
329
459
                                          NID_subject_alt_name, NULL, NULL))
330
459
        != NULL
331
459
            && !add1_extension(&exts, NID_subject_alt_name, crit, default_sans))
332
0
        goto err;
333
459
    if (ctx->reqExtensions != NULL /* augment/override existing ones */
334
459
            && !add_extensions(&exts, ctx->reqExtensions))
335
0
        goto err;
336
459
    if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0
337
459
            && !add1_extension(&exts, NID_subject_alt_name,
338
0
                               crit, ctx->subjectAltNames))
339
0
        goto err;
340
459
    if (ctx->policies != NULL
341
459
            && !add1_extension(&exts, NID_certificate_policies,
342
0
                               ctx->setPoliciesCritical, ctx->policies))
343
0
        goto err;
344
459
    if (!OSSL_CRMF_MSG_set0_extensions(crm, exts))
345
0
        goto err;
346
459
    exts = NULL;
347
    /* end fill certTemplate, now set any controls */
348
349
    /* for KUR, set OldCertId according to D.6 */
350
459
    if (for_KUR && refcert != NULL) {
351
33
        OSSL_CRMF_CERTID *cid =
352
33
            OSSL_CRMF_CERTID_gen(X509_get_issuer_name(refcert),
353
33
                                 X509_get0_serialNumber(refcert));
354
33
        int ret;
355
356
33
        if (cid == NULL)
357
0
            goto err;
358
33
        ret = OSSL_CRMF_MSG_set1_regCtrl_oldCertID(crm, cid);
359
33
        OSSL_CRMF_CERTID_free(cid);
360
33
        if (ret == 0)
361
0
            goto err;
362
33
    }
363
364
459
    goto end;
365
366
459
 err:
367
0
    OSSL_CRMF_MSG_free(crm);
368
0
    crm = NULL;
369
370
459
 end:
371
459
    sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
372
459
    sk_GENERAL_NAME_pop_free(default_sans, GENERAL_NAME_free);
373
459
    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
922
{
379
922
    OSSL_CMP_MSG *msg;
380
922
    OSSL_CRMF_MSG *local_crm = NULL;
381
382
922
    if (!ossl_assert(ctx != NULL))
383
0
        return NULL;
384
385
922
    if (type != OSSL_CMP_PKIBODY_IR && type != OSSL_CMP_PKIBODY_CR
386
922
            && 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
922
    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
922
    if ((msg = ossl_cmp_msg_create(ctx, type)) == NULL)
396
160
        goto err;
397
398
    /* header */
399
762
    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
762
    if (type != OSSL_CMP_PKIBODY_P10CR) {
405
762
        EVP_PKEY *privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
406
407
        /* privkey is ctx->newPkey (if private, else NULL) or ctx->pkey */
408
762
        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
762
        if (crm == NULL) {
413
762
            local_crm = OSSL_CMP_CTX_setup_CRM(ctx,
414
762
                                               type == OSSL_CMP_PKIBODY_KUR,
415
762
                                               OSSL_CMP_CERTREQID);
416
762
            if (local_crm == NULL
417
762
                || !OSSL_CRMF_MSG_create_popo(ctx->popoMethod, local_crm,
418
762
                                              privkey, ctx->digest,
419
762
                                              ctx->libctx, ctx->propq))
420
0
                goto err;
421
762
        } 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
762
        if (!sk_OSSL_CRMF_MSG_push(msg->body->value.ir, local_crm))
428
0
            goto err;
429
762
        local_crm = NULL;
430
762
    }
431
432
762
    if (!ossl_cmp_msg_protect(ctx, msg))
433
762
        goto err;
434
435
0
    return msg;
436
437
922
 err:
438
922
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREQ);
439
922
    OSSL_CRMF_MSG_free(local_crm);
440
922
    OSSL_CMP_MSG_free(msg);
441
922
    return NULL;
442
762
}
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
139
{
520
139
    OSSL_CMP_MSG *msg = NULL;
521
139
    const X509_NAME *issuer = NULL;
522
139
    const X509_NAME *subject = NULL;
523
139
    const ASN1_INTEGER *serialNumber = NULL;
524
139
    EVP_PKEY *pubkey = NULL;
525
139
    OSSL_CMP_REVDETAILS *rd;
526
139
    int ret;
527
528
139
    if (!ossl_assert(ctx != NULL
529
139
                     && (ctx->oldCert != NULL || ctx->p10CSR != NULL
530
139
                         || (ctx->serialNumber != NULL && ctx->issuer != NULL))))
531
0
        return NULL;
532
533
139
    if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL)
534
0
        goto err;
535
536
139
    if (ctx->serialNumber != NULL && ctx->issuer != NULL) {
537
0
        issuer = ctx->issuer;
538
0
        serialNumber = ctx->serialNumber;
539
139
    } else if (ctx->oldCert != NULL) {
540
139
        issuer = X509_get_issuer_name(ctx->oldCert);
541
139
        serialNumber = X509_get0_serialNumber(ctx->oldCert);
542
139
    } 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
    }
546
0
    else {
547
0
        goto err;
548
0
    }
549
550
    /* Fill the template from the contents of the certificate to be revoked */
551
139
    ret = OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails, pubkey, subject,
552
139
                                      issuer, serialNumber);
553
139
    if (!ret)
554
0
        goto err;
555
556
    /* revocation reason code is optional */
557
139
    if (ctx->revocationReason != CRL_REASON_NONE
558
139
            && !add_crl_reason_extension(&rd->crlEntryDetails,
559
0
                                         ctx->revocationReason))
560
0
        goto err;
561
562
139
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL)
563
0
        goto err;
564
565
139
    if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd))
566
0
        goto err;
567
139
    rd = NULL;
568
    /* Revocation Passphrase according to section 5.3.19.9 could be set here */
569
570
139
    if (!ossl_cmp_msg_protect(ctx, msg))
571
139
        goto err;
572
573
0
    return msg;
574
575
139
 err:
576
139
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
577
139
    OSSL_CMP_MSG_free(msg);
578
139
    OSSL_CMP_REVDETAILS_free(rd);
579
139
    return NULL;
580
139
}
581
582
OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
583
                              const OSSL_CRMF_CERTID *cid, int unprotectedErrors)
584
0
{
585
0
    OSSL_CMP_REVREPCONTENT *rep = NULL;
586
0
    OSSL_CMP_PKISI *si1 = NULL;
587
0
    OSSL_CRMF_CERTID *cid_copy = NULL;
588
0
    OSSL_CMP_MSG *msg = NULL;
589
590
0
    if (!ossl_assert(ctx != NULL && si != NULL))
591
0
        return NULL;
592
593
0
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL)
594
0
        goto err;
595
0
    rep = msg->body->value.rp;
596
597
0
    if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL)
598
0
        goto err;
599
600
0
    if (!sk_OSSL_CMP_PKISI_push(rep->status, si1)) {
601
0
        OSSL_CMP_PKISI_free(si1);
602
0
        goto err;
603
0
    }
604
605
0
    if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL)
606
0
        goto err;
607
0
    if (cid != NULL) {
608
0
        if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL)
609
0
            goto err;
610
0
        if (!sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) {
611
0
            OSSL_CRMF_CERTID_free(cid_copy);
612
0
            goto err;
613
0
        }
614
0
    }
615
616
0
    if (!unprotectedErrors
617
0
            || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection)
618
0
        if (!ossl_cmp_msg_protect(ctx, msg))
619
0
            goto err;
620
621
0
    return msg;
622
623
0
 err:
624
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RP);
625
0
    OSSL_CMP_MSG_free(msg);
626
0
    return NULL;
627
0
}
628
629
OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx)
630
278
{
631
278
    OSSL_CMP_MSG *msg;
632
633
278
    if (!ossl_assert(ctx != NULL))
634
0
        return NULL;
635
636
278
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL)
637
0
        goto err;
638
278
    if (ossl_cmp_msg_protect(ctx, msg))
639
0
        return msg;
640
641
278
 err:
642
278
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
643
278
    OSSL_CMP_MSG_free(msg);
644
278
    return NULL;
645
278
}
646
647
int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav)
648
0
{
649
0
    int bodytype;
650
651
0
    if (!ossl_assert(msg != NULL && itav != NULL))
652
0
        return 0;
653
654
0
    bodytype = OSSL_CMP_MSG_get_bodytype(msg);
655
0
    if (bodytype != OSSL_CMP_PKIBODY_GENM
656
0
            && bodytype != OSSL_CMP_PKIBODY_GENP) {
657
0
        ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
658
0
        return 0;
659
0
    }
660
661
    /* value.genp has the same structure, so this works for genp as well */
662
0
    return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav);
663
0
}
664
665
int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg,
666
                                 const STACK_OF(OSSL_CMP_ITAV) *itavs)
667
0
{
668
0
    int i;
669
0
    OSSL_CMP_ITAV *itav = NULL;
670
671
0
    if (!ossl_assert(msg != NULL))
672
0
        return 0;
673
674
0
    for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
675
0
        itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
676
0
        if (itav == NULL
677
0
                || !ossl_cmp_msg_gen_push0_ITAV(msg, itav)) {
678
0
            OSSL_CMP_ITAV_free(itav);
679
0
            return 0;
680
0
        }
681
0
    }
682
0
    return 1;
683
0
}
684
685
/*
686
 * Creates a new General Message/Response with a copy of the given itav stack
687
 * returns a pointer to the PKIMessage on success, NULL on error
688
 */
689
static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx,
690
                             const STACK_OF(OSSL_CMP_ITAV) *itavs,
691
                             int body_type, int err_code)
692
746
{
693
746
    OSSL_CMP_MSG *msg = NULL;
694
695
746
    if (!ossl_assert(ctx != NULL))
696
0
        return NULL;
697
698
746
    if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL)
699
0
        return NULL;
700
701
746
    if (itavs != NULL && !ossl_cmp_msg_gen_push1_ITAVs(msg, itavs))
702
0
        goto err;
703
704
746
    if (!ossl_cmp_msg_protect(ctx, msg))
705
746
        goto err;
706
707
0
    return msg;
708
709
746
 err:
710
746
    ERR_raise(ERR_LIB_CMP, err_code);
711
746
    OSSL_CMP_MSG_free(msg);
712
746
    return NULL;
713
746
}
714
715
OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx)
716
746
{
717
746
    return gen_new(ctx, ctx->genm_ITAVs,
718
746
                   OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM);
719
746
}
720
721
OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx,
722
                                const STACK_OF(OSSL_CMP_ITAV) *itavs)
723
0
{
724
0
    return gen_new(ctx, itavs,
725
0
                   OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP);
726
0
}
727
728
OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si,
729
                                 int64_t errorCode, const char *details,
730
                                 int unprotected)
731
28.3k
{
732
28.3k
    OSSL_CMP_MSG *msg = NULL;
733
28.3k
    const char *lib = NULL, *reason = NULL;
734
28.3k
    OSSL_CMP_PKIFREETEXT *ft;
735
736
28.3k
    if (!ossl_assert(ctx != NULL && si != NULL))
737
0
        return NULL;
738
739
28.3k
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL)
740
0
        goto err;
741
742
28.3k
    OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo);
743
28.3k
    if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si))
744
28.3k
        == NULL)
745
0
        goto err;
746
28.3k
    if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL)
747
0
        goto err;
748
28.3k
    if (!ASN1_INTEGER_set_int64(msg->body->value.error->errorCode, errorCode))
749
0
        goto err;
750
28.3k
    if (errorCode > 0
751
28.3k
            && (uint64_t)errorCode < ((uint64_t)ERR_SYSTEM_FLAG << 1)) {
752
28.3k
        lib = ERR_lib_error_string((unsigned long)errorCode);
753
28.3k
        reason = ERR_reason_error_string((unsigned long)errorCode);
754
28.3k
    }
755
28.3k
    if (lib != NULL || reason != NULL || details != NULL) {
756
28.3k
        if ((ft = sk_ASN1_UTF8STRING_new_null()) == NULL)
757
0
            goto err;
758
28.3k
        msg->body->value.error->errorDetails = ft;
759
28.3k
        if (lib != NULL && *lib != '\0'
760
28.3k
                && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, lib, -1))
761
0
            goto err;
762
28.3k
        if (reason != NULL && *reason != '\0'
763
28.3k
                && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, reason, -1))
764
0
            goto err;
765
28.3k
        if (details != NULL
766
28.3k
                && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, details, -1))
767
0
            goto err;
768
28.3k
    }
769
770
28.3k
    if (!unprotected && !ossl_cmp_msg_protect(ctx, msg))
771
28.3k
        goto err;
772
0
    return msg;
773
774
28.3k
 err:
775
28.3k
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_ERROR);
776
28.3k
    OSSL_CMP_MSG_free(msg);
777
28.3k
    return NULL;
778
28.3k
}
779
780
/*
781
 * Set the certHash field of a OSSL_CMP_CERTSTATUS structure.
782
 * This is used in the certConf message, for example,
783
 * to confirm that the certificate was received successfully.
784
 */
785
int ossl_cmp_certstatus_set0_certHash(OSSL_CMP_CERTSTATUS *certStatus,
786
                                      ASN1_OCTET_STRING *hash)
787
0
{
788
0
    if (!ossl_assert(certStatus != NULL))
789
0
        return 0;
790
0
    ASN1_OCTET_STRING_free(certStatus->certHash);
791
0
    certStatus->certHash = hash;
792
0
    return 1;
793
0
}
794
795
OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int certReqId,
796
                                    int fail_info, const char *text)
797
0
{
798
0
    OSSL_CMP_MSG *msg = NULL;
799
0
    OSSL_CMP_CERTSTATUS *certStatus = NULL;
800
0
    EVP_MD *md;
801
0
    int is_fallback;
802
0
    ASN1_OCTET_STRING *certHash = NULL;
803
0
    OSSL_CMP_PKISI *sinfo;
804
805
0
    if (!ossl_assert(ctx != NULL && ctx->newCert != NULL
806
0
                     && (certReqId == OSSL_CMP_CERTREQID
807
0
                         || certReqId == OSSL_CMP_CERTREQID_NONE)))
808
0
        return NULL;
809
810
0
    if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
811
0
        ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE);
812
0
        return NULL;
813
0
    }
814
815
0
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL)
816
0
        goto err;
817
818
0
    if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL)
819
0
        goto err;
820
    /* consume certStatus into msg right away so it gets deallocated with msg */
821
0
    if (sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus) < 1) {
822
0
        OSSL_CMP_CERTSTATUS_free(certStatus);
823
0
        goto err;
824
0
    }
825
826
    /* set the ID of the certReq */
827
0
    if (!ASN1_INTEGER_set(certStatus->certReqId, certReqId))
828
0
        goto err;
829
830
0
    certStatus->hashAlg = NULL;
831
    /*
832
     * The hash of the certificate, using the same hash algorithm
833
     * as is used to create and verify the certificate signature.
834
     * If not available, a fallback hash algorithm is used.
835
     */
836
0
    if ((certHash = X509_digest_sig(ctx->newCert, &md, &is_fallback)) == NULL)
837
0
        goto err;
838
0
    if (is_fallback) {
839
0
        if (!ossl_cmp_hdr_set_pvno(msg->header, OSSL_CMP_PVNO_3))
840
0
            goto err;
841
0
        if ((certStatus->hashAlg = X509_ALGOR_new()) == NULL)
842
0
            goto err;
843
0
        X509_ALGOR_set_md(certStatus->hashAlg, md);
844
0
    }
845
0
    EVP_MD_free(md);
846
847
0
    if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash))
848
0
        goto err;
849
0
    certHash = NULL;
850
    /*
851
     * For any particular CertStatus, omission of the statusInfo field
852
     * indicates ACCEPTANCE of the specified certificate.  Alternatively,
853
     * explicit status details (with respect to acceptance or rejection) MAY
854
     * be provided in the statusInfo field, perhaps for auditing purposes at
855
     * the CA/RA.
856
     */
857
0
    sinfo = fail_info != 0 ?
858
0
        OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) :
859
0
        OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text);
860
0
    if (sinfo == NULL)
861
0
        goto err;
862
0
    certStatus->statusInfo = sinfo;
863
864
0
    if (!ossl_cmp_msg_protect(ctx, msg))
865
0
        goto err;
866
867
0
    return msg;
868
869
0
 err:
870
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF);
871
0
    OSSL_CMP_MSG_free(msg);
872
0
    ASN1_OCTET_STRING_free(certHash);
873
0
    return NULL;
874
0
}
875
876
OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid)
877
390
{
878
390
    OSSL_CMP_MSG *msg = NULL;
879
390
    OSSL_CMP_POLLREQ *preq = NULL;
880
881
390
    if (!ossl_assert(ctx != NULL))
882
0
        return NULL;
883
884
390
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL)
885
0
        goto err;
886
887
390
    if ((preq = OSSL_CMP_POLLREQ_new()) == NULL
888
390
            || !ASN1_INTEGER_set(preq->certReqId, crid)
889
390
            || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq))
890
0
        goto err;
891
892
390
    preq = NULL;
893
390
    if (!ossl_cmp_msg_protect(ctx, msg))
894
390
        goto err;
895
896
0
    return msg;
897
898
390
 err:
899
390
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ);
900
390
    OSSL_CMP_POLLREQ_free(preq);
901
390
    OSSL_CMP_MSG_free(msg);
902
390
    return NULL;
903
390
}
904
905
OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid,
906
                                   int64_t poll_after)
907
0
{
908
0
    OSSL_CMP_MSG *msg;
909
0
    OSSL_CMP_POLLREP *prep;
910
911
0
    if (!ossl_assert(ctx != NULL))
912
0
        return NULL;
913
914
0
    if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL)
915
0
        goto err;
916
0
    if ((prep = OSSL_CMP_POLLREP_new()) == NULL)
917
0
        goto err;
918
0
    if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep))
919
0
        goto err;
920
0
    if (!ASN1_INTEGER_set(prep->certReqId, crid))
921
0
        goto err;
922
0
    if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after))
923
0
        goto err;
924
925
0
    if (!ossl_cmp_msg_protect(ctx, msg))
926
0
        goto err;
927
0
    return msg;
928
929
0
 err:
930
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
931
0
    OSSL_CMP_MSG_free(msg);
932
0
    return NULL;
933
0
}
934
935
/*-
936
 * returns the status field of the RevRepContent with the given
937
 * request/sequence id inside a revocation response.
938
 * RevRepContent has the revocation statuses in same order as they were sent in
939
 * RevReqContent.
940
 * returns NULL on error
941
 */
942
OSSL_CMP_PKISI *
943
ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
944
0
{
945
0
    OSSL_CMP_PKISI *status;
946
947
0
    if (!ossl_assert(rrep != NULL))
948
0
        return NULL;
949
950
0
    if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL)
951
0
        return status;
952
953
0
    ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND);
954
0
    return NULL;
955
0
}
956
957
/*
958
 * returns the CertId field in the revCerts part of the RevRepContent
959
 * with the given request/sequence id inside a revocation response.
960
 * RevRepContent has the CertIds in same order as they were sent in
961
 * RevReqContent.
962
 * returns NULL on error
963
 */
964
OSSL_CRMF_CERTID *
965
ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid)
966
0
{
967
0
    OSSL_CRMF_CERTID *cid = NULL;
968
969
0
    if (!ossl_assert(rrep != NULL))
970
0
        return NULL;
971
972
0
    if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL)
973
0
        return cid;
974
975
0
    ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND);
976
0
    return NULL;
977
0
}
978
979
static int suitable_rid(const ASN1_INTEGER *certReqId, int rid)
980
0
{
981
0
    int trid;
982
983
0
    if (rid == OSSL_CMP_CERTREQID_NONE)
984
0
        return 1;
985
986
0
    trid = ossl_cmp_asn1_get_int(certReqId);
987
988
0
    if (trid == OSSL_CMP_CERTREQID_NONE) {
989
0
        ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
990
0
        return 0;
991
0
    }
992
0
    return rid == trid;
993
0
}
994
995
/*
996
 * returns a pointer to the PollResponse with the given CertReqId
997
 * (or the first one in case -1) inside a PollRepContent
998
 * returns NULL on error or if no suitable PollResponse available
999
 */
1000
OSSL_CMP_POLLREP *
1001
ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc,
1002
                                     int rid)
1003
0
{
1004
0
    OSSL_CMP_POLLREP *pollRep = NULL;
1005
0
    int i;
1006
1007
0
    if (!ossl_assert(prc != NULL))
1008
0
        return NULL;
1009
1010
0
    for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) {
1011
0
        pollRep = sk_OSSL_CMP_POLLREP_value(prc, i);
1012
0
        if (suitable_rid(pollRep->certReqId, rid))
1013
0
            return pollRep;
1014
0
    }
1015
1016
0
    ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
1017
0
                   "expected certReqId = %d", rid);
1018
0
    return NULL;
1019
0
}
1020
1021
/*
1022
 * returns a pointer to the CertResponse with the given CertReqId
1023
 * (or the first one in case -1) inside a CertRepMessage
1024
 * returns NULL on error or if no suitable CertResponse available
1025
 */
1026
OSSL_CMP_CERTRESPONSE *
1027
ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm,
1028
                                          int rid)
1029
0
{
1030
0
    OSSL_CMP_CERTRESPONSE *crep = NULL;
1031
0
    int i;
1032
1033
0
    if (!ossl_assert(crm != NULL && crm->response != NULL))
1034
0
        return NULL;
1035
1036
0
    for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) {
1037
0
        crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i);
1038
0
        if (suitable_rid(crep->certReqId, rid))
1039
0
            return crep;
1040
0
    }
1041
1042
0
    ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND,
1043
0
                   "expected certReqId = %d", rid);
1044
0
    return NULL;
1045
0
}
1046
1047
/*-
1048
 * Retrieve the newly enrolled certificate from the given certResponse crep.
1049
 * Uses libctx and propq from ctx, in case of indirect POPO also private key.
1050
 * Returns a pointer to a copy of the found certificate, or NULL if not found.
1051
 */
1052
X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CTX *ctx,
1053
                                      const OSSL_CMP_CERTRESPONSE *crep)
1054
0
{
1055
0
    OSSL_CMP_CERTORENCCERT *coec;
1056
0
    X509 *crt = NULL;
1057
0
    EVP_PKEY *pkey;
1058
1059
0
    if (!ossl_assert(crep != NULL && ctx != NULL))
1060
0
        return NULL;
1061
1062
0
    if (crep->certifiedKeyPair
1063
0
            && (coec = crep->certifiedKeyPair->certOrEncCert) != NULL) {
1064
0
        switch (coec->type) {
1065
0
        case OSSL_CMP_CERTORENCCERT_CERTIFICATE:
1066
0
            crt = X509_dup(coec->value.certificate);
1067
0
            break;
1068
0
        case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT:
1069
            /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */
1070
0
            pkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
1071
            /* pkey is ctx->newPkey (if private, else NULL) or ctx->pkey */
1072
0
            if (pkey == NULL) {
1073
0
                ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY);
1074
0
                return NULL;
1075
0
            }
1076
0
            crt =
1077
0
                OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(coec->value.encryptedCert,
1078
0
                                                      ctx->libctx, ctx->propq,
1079
0
                                                      pkey);
1080
0
            break;
1081
0
        default:
1082
0
            ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE);
1083
0
            return NULL;
1084
0
        }
1085
0
    }
1086
0
    if (crt == NULL)
1087
0
        ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
1088
0
    else
1089
0
        (void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq);
1090
0
    return crt;
1091
0
}
1092
1093
int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
1094
0
{
1095
0
    if (ctx == NULL || msg == NULL) {
1096
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1097
0
        return 0;
1098
0
    }
1099
0
    if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header))
1100
0
        return 0;
1101
0
    return msg->header->protectionAlg == NULL
1102
0
            || ossl_cmp_msg_protect(ctx, msg);
1103
0
}
1104
1105
int OSSL_CMP_MSG_update_recipNonce(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
1106
0
{
1107
0
    if (ctx == NULL || msg == NULL || msg->header == NULL) {
1108
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1109
0
        return 0;
1110
0
    }
1111
0
    if (ctx->recipNonce == NULL) /* nothing to do for 1st msg in transaction */
1112
0
        return 1;
1113
0
    if (!ossl_cmp_asn1_octet_string_set1(&msg->header->recipNonce,
1114
0
                                         ctx->recipNonce))
1115
0
        return 0;
1116
0
    return msg->header->protectionAlg == NULL || ossl_cmp_msg_protect(ctx, msg);
1117
0
}
1118
1119
OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx,
1120
                                const char *propq)
1121
0
{
1122
0
    OSSL_CMP_MSG *msg;
1123
0
    BIO *bio = NULL;
1124
1125
0
    if (file == NULL) {
1126
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1127
0
        return NULL;
1128
0
    }
1129
1130
0
    msg = OSSL_CMP_MSG_new(libctx, propq);
1131
0
    if (msg == NULL) {
1132
0
        ERR_raise(ERR_LIB_CMP, ERR_R_CMP_LIB);
1133
0
        return NULL;
1134
0
    }
1135
1136
0
    if ((bio = BIO_new_file(file, "rb")) == NULL
1137
0
            || d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) {
1138
0
        OSSL_CMP_MSG_free(msg);
1139
0
        msg = NULL;
1140
0
    }
1141
0
    BIO_free(bio);
1142
0
    return msg;
1143
0
}
1144
1145
int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg)
1146
0
{
1147
0
    BIO *bio;
1148
0
    int res;
1149
1150
0
    if (file == NULL || msg == NULL) {
1151
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1152
0
        return -1;
1153
0
    }
1154
1155
0
    bio = BIO_new_file(file, "wb");
1156
0
    if (bio == NULL)
1157
0
        return -2;
1158
0
    res = i2d_OSSL_CMP_MSG_bio(bio, msg);
1159
0
    BIO_free(bio);
1160
0
    return res;
1161
0
}
1162
1163
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in,
1164
                               long len)
1165
0
{
1166
0
    OSSL_LIB_CTX *libctx = NULL;
1167
0
    const char *propq = NULL;
1168
1169
0
    if (msg != NULL && *msg != NULL) {
1170
0
        libctx  = (*msg)->libctx;
1171
0
        propq = (*msg)->propq;
1172
0
    }
1173
1174
0
    return (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len,
1175
0
                                            ASN1_ITEM_rptr(OSSL_CMP_MSG),
1176
0
                                            libctx, propq);
1177
0
}
1178
1179
int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out)
1180
56.7k
{
1181
56.7k
    return ASN1_item_i2d((const ASN1_VALUE *)msg, out,
1182
56.7k
                         ASN1_ITEM_rptr(OSSL_CMP_MSG));
1183
56.7k
}
1184
1185
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg)
1186
42.0k
{
1187
42.0k
    OSSL_LIB_CTX *libctx = NULL;
1188
42.0k
    const char *propq = NULL;
1189
1190
42.0k
    if (msg != NULL && *msg != NULL) {
1191
0
        libctx  = (*msg)->libctx;
1192
0
        propq = (*msg)->propq;
1193
0
    }
1194
1195
42.0k
    return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx,
1196
42.0k
                                propq);
1197
42.0k
}
1198
1199
int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg)
1200
28.3k
{
1201
28.3k
    return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg);
1202
28.3k
}