Coverage Report

Created: 2026-02-14 07:20

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