Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/crmf/crmf_lib.c
Line
Count
Source
1
/*-
2
 * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright Nokia 2007-2018
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
 * CRMF implementation by Martin Peylo, Miikka Viljanen, and David von Oheimb.
12
 */
13
14
/*
15
 * This file contains the functions that handle the individual items inside
16
 * the CRMF structures
17
 */
18
19
/*
20
 * NAMING
21
 *
22
 * The 0 functions use the supplied structure pointer directly in the parent and
23
 * it will be freed up when the parent is freed.
24
 *
25
 * The 1 functions use a copy of the supplied structure pointer (or in some
26
 * cases increases its link count) in the parent and so both should be freed up.
27
 */
28
29
#include "crmf_local.h"
30
#include <openssl/asn1t.h>
31
#include "internal/constant_time.h"
32
#include "internal/sizes.h" /* for OSSL_MAX_NAME_SIZE */
33
#include "crypto/x509.h" /* for ossl_x509_check_private_key() */
34
35
/*-
36
 * atyp = Attribute Type
37
 * valt = Value Type
38
 * ctrlinf = "regCtrl" or "regInfo"
39
 */
40
#define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf)                             \
41
    valt *OSSL_CRMF_MSG_get0_##ctrlinf##_##atyp(const OSSL_CRMF_MSG *msg)         \
42
0
    {                                                                             \
43
0
        int i;                                                                    \
44
0
        STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *controls;                      \
45
0
        OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL;                             \
46
0
                                                                                  \
47
0
        if (msg == NULL || msg->certReq == NULL)                                  \
48
0
            return NULL;                                                          \
49
0
        controls = msg->certReq->controls;                                        \
50
0
        for (i = 0; i < sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_num(controls); i++) {  \
51
0
            atav = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_value(controls, i);         \
52
0
            if (OBJ_obj2nid(atav->type) == NID_id_##ctrlinf##_##atyp)             \
53
0
                return atav->value.atyp;                                          \
54
0
        }                                                                         \
55
0
        return NULL;                                                              \
56
0
    }                                                                             \
57
                                                                                  \
58
    int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, const valt *in) \
59
90
    {                                                                             \
60
90
        OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL;                             \
61
90
                                                                                  \
62
90
        if (msg == NULL || in == NULL)                                            \
63
90
            goto err;                                                             \
64
90
        if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL)               \
65
90
            goto err;                                                             \
66
90
        if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL)        \
67
90
            goto err;                                                             \
68
90
        if ((atav->value.atyp = valt##_dup(in)) == NULL)                          \
69
90
            goto err;                                                             \
70
90
        if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav))                            \
71
90
            goto err;                                                             \
72
90
        return 1;                                                                 \
73
90
    err:                                                                          \
74
0
        OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav);                               \
75
0
        return 0;                                                                 \
76
90
    }
77
78
/*-
79
 * Pushes the given control attribute into the controls stack of a CertRequest
80
 * (section 6)
81
 * returns 1 on success, 0 on error
82
 */
83
static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm,
84
    OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl)
85
90
{
86
90
    int new = 0;
87
88
90
    if (crm == NULL || crm->certReq == NULL || ctrl == NULL) {
89
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
90
0
        return 0;
91
0
    }
92
93
90
    if (crm->certReq->controls == NULL) {
94
90
        crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
95
90
        if (crm->certReq->controls == NULL)
96
0
            goto err;
97
90
        new = 1;
98
90
    }
99
90
    if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl))
100
0
        goto err;
101
102
90
    return 1;
103
0
err:
104
0
    if (new != 0) {
105
0
        sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls);
106
0
        crm->certReq->controls = NULL;
107
0
    }
108
0
    return 0;
109
90
}
110
111
/* id-regCtrl-regToken Control (section 6.1) */
112
0
IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl)
Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_regToken
Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_regToken
113
114
/* id-regCtrl-authenticator Control (section 6.2) */
115
0
#define ASN1_UTF8STRING_dup ASN1_STRING_dup
116
0
IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl)
Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_authenticator
Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_authenticator
117
118
int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi,
119
    int method, GENERAL_NAME *nm)
120
0
{
121
0
    if (spi == NULL
122
0
        || method < OSSL_CRMF_PUB_METHOD_DONTCARE
123
0
        || method > OSSL_CRMF_PUB_METHOD_LDAP) {
124
0
        ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT);
125
0
        return 0;
126
0
    }
127
128
0
    if (!ASN1_INTEGER_set(spi->pubMethod, method))
129
0
        return 0;
130
0
    GENERAL_NAME_free(spi->pubLocation);
131
0
    spi->pubLocation = nm;
132
0
    return 1;
133
0
}
134
135
int OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
136
    OSSL_CRMF_SINGLEPUBINFO *spi)
137
0
{
138
0
    if (pi == NULL || spi == NULL) {
139
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
140
0
        return 0;
141
0
    }
142
0
    if (pi->pubInfos == NULL)
143
0
        pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
144
0
    if (pi->pubInfos == NULL)
145
0
        return 0;
146
147
0
    return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi);
148
0
}
149
150
int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
151
    int action)
152
0
{
153
0
    if (pi == NULL
154
0
        || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH
155
0
        || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) {
156
0
        ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT);
157
0
        return 0;
158
0
    }
159
160
0
    return ASN1_INTEGER_set(pi->action, action);
161
0
}
162
163
/* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
164
0
IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO,
Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_pkiPublicationInfo
Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_pkiPublicationInfo
165
    regCtrl)
166
167
/* id-regCtrl-oldCertID Control (section 6.5) from the given */
168
90
IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_oldCertID
OSSL_CRMF_MSG_set1_regCtrl_oldCertID
Line
Count
Source
168
IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
169
170
OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer,
171
    const ASN1_INTEGER *serial)
172
140
{
173
140
    OSSL_CRMF_CERTID *cid = NULL;
174
175
140
    if (issuer == NULL || serial == NULL) {
176
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
177
0
        return NULL;
178
0
    }
179
180
140
    if ((cid = OSSL_CRMF_CERTID_new()) == NULL)
181
0
        goto err;
182
183
140
    if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
184
0
        goto err;
185
140
    cid->issuer->type = GEN_DIRNAME;
186
187
140
    ASN1_INTEGER_free(cid->serialNumber);
188
140
    if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
189
0
        goto err;
190
191
140
    return cid;
192
193
0
err:
194
0
    OSSL_CRMF_CERTID_free(cid);
195
0
    return NULL;
196
140
}
197
198
/*
199
 * id-regCtrl-protocolEncrKey Control (section 6.6)
200
 */
201
0
IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl)
Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_protocolEncrKey
Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_protocolEncrKey
202
203
/*-
204
 * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
205
 * (section 7)
206
 * returns 1 on success, 0 on error
207
 */
208
static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm,
209
    OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri)
210
0
{
211
0
    STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL;
212
213
0
    if (crm == NULL || ri == NULL) {
214
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
215
0
        return 0;
216
0
    }
217
218
0
    if (crm->regInfo == NULL)
219
0
        crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
220
0
    if (crm->regInfo == NULL)
221
0
        goto err;
222
0
    if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri))
223
0
        goto err;
224
0
    return 1;
225
226
0
err:
227
0
    if (info != NULL)
228
0
        crm->regInfo = NULL;
229
0
    sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info);
230
0
    return 0;
231
0
}
232
233
/* id-regInfo-utf8Pairs to regInfo (section 7.1) */
234
0
IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo)
Unexecuted instantiation: OSSL_CRMF_MSG_get0_regInfo_utf8Pairs
Unexecuted instantiation: OSSL_CRMF_MSG_set1_regInfo_utf8Pairs
235
236
/* id-regInfo-certReq to regInfo (section 7.2) */
237
0
IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo)
Unexecuted instantiation: OSSL_CRMF_MSG_get0_regInfo_certReq
Unexecuted instantiation: OSSL_CRMF_MSG_set1_regInfo_certReq
238
239
/* retrieves the certificate template of crm */
240
OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm)
241
4.42k
{
242
4.42k
    if (crm == NULL || crm->certReq == NULL) {
243
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
244
0
        return NULL;
245
0
    }
246
4.42k
    return crm->certReq->certTemplate;
247
4.42k
}
248
249
int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG *crm,
250
    ASN1_TIME *notBefore, ASN1_TIME *notAfter)
251
0
{
252
0
    OSSL_CRMF_OPTIONALVALIDITY *vld;
253
0
    OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
254
255
0
    if (tmpl == NULL) { /* also crm == NULL implies this */
256
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
257
0
        return 0;
258
0
    }
259
260
0
    if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL)
261
0
        return 0;
262
0
    vld->notBefore = notBefore;
263
0
    vld->notAfter = notAfter;
264
0
    tmpl->validity = vld;
265
0
    return 1;
266
0
}
267
268
int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid)
269
1.17k
{
270
1.17k
    if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) {
271
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
272
0
        return 0;
273
0
    }
274
275
1.17k
    return ASN1_INTEGER_set(crm->certReq->certReqId, rid);
276
1.17k
}
277
278
/* get ASN.1 encoded integer, return -1 on error */
279
static int crmf_asn1_get_int(const ASN1_INTEGER *a)
280
3.32k
{
281
3.32k
    int64_t res;
282
283
3.32k
    if (!ASN1_INTEGER_get_int64(&res, a)) {
284
7
        ERR_raise(ERR_LIB_CRMF, ASN1_R_INVALID_NUMBER);
285
7
        return -1;
286
7
    }
287
3.32k
    if (res < INT_MIN) {
288
65
        ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_SMALL);
289
65
        return -1;
290
65
    }
291
3.25k
    if (res > INT_MAX) {
292
8
        ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_LARGE);
293
8
        return -1;
294
8
    }
295
3.24k
    return (int)res;
296
3.25k
}
297
298
int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm)
299
3.32k
{
300
3.32k
    if (crm == NULL || /* not really needed: */ crm->certReq == NULL) {
301
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
302
0
        return -1;
303
0
    }
304
3.32k
    return crmf_asn1_get_int(crm->certReq->certReqId);
305
3.32k
}
306
307
int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm,
308
    X509_EXTENSIONS *exts)
309
1.17k
{
310
1.17k
    OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
311
312
1.17k
    if (tmpl == NULL) { /* also crm == NULL implies this */
313
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
314
0
        return 0;
315
0
    }
316
317
1.17k
    if (sk_X509_EXTENSION_num(exts) == 0) {
318
0
        sk_X509_EXTENSION_free(exts);
319
0
        exts = NULL; /* do not include empty extensions list */
320
0
    }
321
322
1.17k
    sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free);
323
1.17k
    tmpl->extensions = exts;
324
1.17k
    return 1;
325
1.17k
}
326
327
int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm,
328
    X509_EXTENSION *ext)
329
0
{
330
0
    int new = 0;
331
0
    OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
332
333
0
    if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */
334
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
335
0
        return 0;
336
0
    }
337
338
0
    if (tmpl->extensions == NULL) {
339
0
        if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL)
340
0
            goto err;
341
0
        new = 1;
342
0
    }
343
344
0
    if (!sk_X509_EXTENSION_push(tmpl->extensions, ext))
345
0
        goto err;
346
0
    return 1;
347
0
err:
348
0
    if (new != 0) {
349
0
        sk_X509_EXTENSION_free(tmpl->extensions);
350
0
        tmpl->extensions = NULL;
351
0
    }
352
0
    return 0;
353
0
}
354
355
static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY *ps,
356
    const OSSL_CRMF_CERTREQUEST *cr,
357
    EVP_PKEY *pkey, const EVP_MD *digest,
358
    OSSL_LIB_CTX *libctx, const char *propq)
359
0
{
360
0
    char name[80] = "";
361
0
    EVP_PKEY *pub;
362
363
0
    if (ps == NULL || cr == NULL || pkey == NULL) {
364
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
365
0
        return 0;
366
0
    }
367
0
    pub = X509_PUBKEY_get0(cr->certTemplate->publicKey);
368
0
    if (!ossl_x509_check_private_key(pub, pkey))
369
0
        return 0;
370
371
0
    if (ps->poposkInput != NULL) {
372
        /* We do not support cases 1+2 defined in RFC 4211, section 4.1 */
373
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_POPOSKINPUT_NOT_SUPPORTED);
374
0
        return 0;
375
0
    }
376
377
0
    if (EVP_PKEY_get_default_digest_name(pkey, name, sizeof(name)) > 0
378
0
        && strcmp(name, "UNDEF") == 0) /* at least for Ed25519, Ed448 */
379
0
        digest = NULL;
380
381
0
    return ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
382
0
        ps->algorithmIdentifier, /* sets this X509_ALGOR */
383
0
        NULL, ps->signature, /* sets the ASN1_BIT_STRING */
384
0
        cr, NULL, pkey, digest, libctx, propq);
385
0
}
386
387
int OSSL_CRMF_MSG_create_popo(int meth, OSSL_CRMF_MSG *crm,
388
    EVP_PKEY *pkey, const EVP_MD *digest,
389
    OSSL_LIB_CTX *libctx, const char *propq)
390
1.17k
{
391
1.17k
    OSSL_CRMF_POPO *pp = NULL;
392
1.17k
    ASN1_INTEGER *tag = NULL;
393
394
1.17k
    if (crm == NULL || (meth == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
395
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
396
0
        return 0;
397
0
    }
398
399
1.17k
    if (meth == OSSL_CRMF_POPO_NONE)
400
1.17k
        goto end;
401
0
    if ((pp = OSSL_CRMF_POPO_new()) == NULL)
402
0
        goto err;
403
0
    pp->type = meth;
404
405
0
    switch (meth) {
406
0
    case OSSL_CRMF_POPO_RAVERIFIED:
407
0
        if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
408
0
            goto err;
409
0
        break;
410
411
0
    case OSSL_CRMF_POPO_SIGNATURE: {
412
0
        OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
413
414
0
        if (ps == NULL)
415
0
            goto err;
416
0
        if (!create_popo_signature(ps, crm->certReq, pkey, digest,
417
0
                libctx, propq)) {
418
0
            OSSL_CRMF_POPOSIGNINGKEY_free(ps);
419
0
            goto err;
420
0
        }
421
0
        pp->value.signature = ps;
422
0
    } break;
423
424
0
    case OSSL_CRMF_POPO_KEYENC:
425
0
        if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
426
0
            goto err;
427
0
        tag = ASN1_INTEGER_new();
428
0
        pp->value.keyEncipherment->type = OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
429
0
        pp->value.keyEncipherment->value.subsequentMessage = tag;
430
0
        if (tag == NULL
431
0
            || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
432
0
            goto err;
433
0
        break;
434
435
0
    default:
436
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
437
0
        goto err;
438
0
    }
439
440
1.17k
end:
441
1.17k
    OSSL_CRMF_POPO_free(crm->popo);
442
1.17k
    crm->popo = pp;
443
444
1.17k
    return 1;
445
0
err:
446
0
    OSSL_CRMF_POPO_free(pp);
447
0
    return 0;
448
0
}
449
450
/* verifies the Proof-of-Possession of the request with the given rid in reqs */
451
int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
452
    int rid, int acceptRAVerified,
453
    OSSL_LIB_CTX *libctx, const char *propq)
454
2.85k
{
455
2.85k
    OSSL_CRMF_MSG *req = NULL;
456
2.85k
    X509_PUBKEY *pubkey = NULL;
457
2.85k
    OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
458
2.85k
    const ASN1_ITEM *it;
459
2.85k
    void *asn;
460
461
2.85k
    if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) {
462
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
463
0
        return 0;
464
0
    }
465
466
2.85k
    if (req->popo == NULL) {
467
3
        ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING);
468
3
        return 0;
469
3
    }
470
471
2.85k
    switch (req->popo->type) {
472
0
    case OSSL_CRMF_POPO_RAVERIFIED:
473
0
        if (!acceptRAVerified) {
474
0
            ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED);
475
0
            return 0;
476
0
        }
477
0
        break;
478
2.85k
    case OSSL_CRMF_POPO_SIGNATURE:
479
2.85k
        pubkey = req->certReq->certTemplate->publicKey;
480
2.85k
        if (pubkey == NULL) {
481
3
            ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY);
482
3
            return 0;
483
3
        }
484
2.85k
        sig = req->popo->value.signature;
485
2.85k
        if (sig->poposkInput != NULL) {
486
            /*
487
             * According to RFC 4211: publicKey contains a copy of
488
             * the public key from the certificate template. This MUST be
489
             * exactly the same value as contained in the certificate template.
490
             */
491
0
            if (sig->poposkInput->publicKey == NULL) {
492
0
                ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY);
493
0
                return 0;
494
0
            }
495
0
            if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) {
496
0
                ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY);
497
0
                return 0;
498
0
            }
499
500
            /*
501
             * Should check at this point the contents of the authInfo sub-field
502
             * as requested in FR #19807 according to RFC 4211 section 4.1.
503
             */
504
505
0
            it = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT);
506
0
            asn = sig->poposkInput;
507
2.85k
        } else {
508
2.85k
            if (req->certReq->certTemplate->subject == NULL) {
509
13
                ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_SUBJECT);
510
13
                return 0;
511
13
            }
512
2.84k
            it = ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST);
513
2.84k
            asn = req->certReq;
514
2.84k
        }
515
2.84k
        if (ASN1_item_verify_ex(it, sig->algorithmIdentifier, sig->signature,
516
2.84k
                asn, NULL, X509_PUBKEY_get0(pubkey), libctx,
517
2.84k
                propq)
518
2.84k
            < 1)
519
2.73k
            return 0;
520
106
        break;
521
106
    case OSSL_CRMF_POPO_KEYENC:
522
        /*
523
         * When OSSL_CMP_certrep_new() supports encrypted certs,
524
         * should return 1 if the type of req->popo->value.keyEncipherment
525
         * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
526
         * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
527
         */
528
0
    case OSSL_CRMF_POPO_KEYAGREE:
529
0
    default:
530
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_POPO_METHOD);
531
0
        return 0;
532
2.85k
    }
533
106
    return 1;
534
2.85k
}
535
536
int OSSL_CRMF_MSG_centralkeygen_requested(const OSSL_CRMF_MSG *crm, const X509_REQ *p10cr)
537
3.44k
{
538
3.44k
    X509_PUBKEY *pubkey = NULL;
539
3.44k
    const unsigned char *pk = NULL;
540
3.44k
    int pklen, ret = 0;
541
542
3.44k
    if (crm == NULL && p10cr == NULL) {
543
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
544
0
        return -1;
545
0
    }
546
547
3.44k
    if (crm != NULL)
548
1.54k
        pubkey = OSSL_CRMF_CERTTEMPLATE_get0_publicKey(OSSL_CRMF_MSG_get0_tmpl(crm));
549
1.89k
    else
550
1.89k
        pubkey = p10cr->req_info.pubkey;
551
552
3.44k
    if (pubkey == NULL
553
3.44k
        || (X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey)
554
3.44k
            && pklen == 0))
555
6
        ret = 1;
556
557
    /*
558
     * In case of CRMF, POPO MUST be absent if central key generation
559
     * is requested, otherwise MUST be present
560
     */
561
3.44k
    if (crm != NULL && ret != (crm->popo == NULL)) {
562
3
        ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_CENTRAL_KEYGEN);
563
3
        return -2;
564
3
    }
565
3.44k
    return ret;
566
3.44k
}
567
568
X509_PUBKEY
569
*OSSL_CRMF_CERTTEMPLATE_get0_publicKey(const OSSL_CRMF_CERTTEMPLATE *tmpl)
570
1.54k
{
571
1.54k
    return tmpl != NULL ? tmpl->publicKey : NULL;
572
1.54k
}
573
574
const ASN1_INTEGER *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl)
575
73
{
576
73
    return tmpl != NULL ? tmpl->serialNumber : NULL;
577
73
}
578
579
const X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_subject(const OSSL_CRMF_CERTTEMPLATE *tmpl)
580
0
{
581
0
    return tmpl != NULL ? tmpl->subject : NULL;
582
0
}
583
584
const X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl)
585
73
{
586
73
    return tmpl != NULL ? tmpl->issuer : NULL;
587
73
}
588
589
X509_EXTENSIONS
590
*OSSL_CRMF_CERTTEMPLATE_get0_extensions(const OSSL_CRMF_CERTTEMPLATE *tmpl)
591
0
{
592
0
    return tmpl != NULL ? tmpl->extensions : NULL;
593
0
}
594
595
const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid)
596
0
{
597
0
    return cid != NULL && cid->issuer->type == GEN_DIRNAME ? cid->issuer->d.directoryName : NULL;
598
0
}
599
600
const ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID
601
        *cid)
602
0
{
603
0
    return cid != NULL ? cid->serialNumber : NULL;
604
0
}
605
606
/*-
607
 * Fill in the certificate template |tmpl|.
608
 * Any other NULL argument will leave the respective field unchanged.
609
 */
610
int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
611
    EVP_PKEY *pubkey,
612
    const X509_NAME *subject,
613
    const X509_NAME *issuer,
614
    const ASN1_INTEGER *serial)
615
1.35k
{
616
1.35k
    if (tmpl == NULL) {
617
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
618
0
        return 0;
619
0
    }
620
1.35k
    if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject))
621
0
        return 0;
622
1.35k
    if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer))
623
0
        return 0;
624
1.35k
    if (serial != NULL) {
625
182
        ASN1_INTEGER_free(tmpl->serialNumber);
626
182
        if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
627
0
            return 0;
628
182
    }
629
1.35k
    if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
630
0
        return 0;
631
1.35k
    return 1;
632
1.35k
}
633
634
#ifndef OPENSSL_NO_CMS
635
DECLARE_ASN1_ITEM(CMS_SignedData) /* copied from cms_local.h */
636
637
/* check for KGA authorization implied by CA flag or by explicit EKU cmKGA */
638
static int check_cmKGA(ossl_unused const X509_PURPOSE *purpose, const X509 *x, int ca)
639
0
{
640
0
    STACK_OF(ASN1_OBJECT) *ekus;
641
0
    int i, ret = 1;
642
643
0
    if (ca)
644
0
        return ret;
645
0
    ekus = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL);
646
0
    for (i = 0; i < sk_ASN1_OBJECT_num(ekus); i++) {
647
0
        if (OBJ_obj2nid(sk_ASN1_OBJECT_value(ekus, i)) == NID_cmKGA)
648
0
            goto end;
649
0
    }
650
0
    ret = 0;
651
652
0
end:
653
0
    sk_ASN1_OBJECT_pop_free(ekus, ASN1_OBJECT_free);
654
0
    return ret;
655
0
}
656
#endif /* OPENSSL_NO_CMS */
657
658
EVP_PKEY *OSSL_CRMF_ENCRYPTEDKEY_get1_pkey(const OSSL_CRMF_ENCRYPTEDKEY *encryptedKey,
659
    X509_STORE *ts, STACK_OF(X509) *extra, EVP_PKEY *pkey,
660
    X509 *cert, ASN1_OCTET_STRING *secret,
661
    OSSL_LIB_CTX *libctx, const char *propq)
662
0
{
663
0
#ifndef OPENSSL_NO_CMS
664
0
    BIO *bio = NULL;
665
0
    CMS_SignedData *sd = NULL;
666
0
    BIO *pkey_bio = NULL;
667
0
    int purpose_id, bak_purpose_id;
668
0
    X509_VERIFY_PARAM *vpm;
669
0
#endif
670
0
    EVP_PKEY *ret = NULL;
671
672
0
    if (encryptedKey == NULL) {
673
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
674
0
        return NULL;
675
0
    }
676
0
    if (encryptedKey->type != OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA) {
677
0
        unsigned char *p;
678
0
        const unsigned char *p_copy;
679
0
        int len;
680
681
0
        p = OSSL_CRMF_ENCRYPTEDVALUE_decrypt(encryptedKey->value.encryptedValue,
682
0
            libctx, propq, pkey, &len);
683
0
        if ((p_copy = p) != NULL)
684
0
            ret = d2i_AutoPrivateKey_ex(NULL, &p_copy, len, libctx, propq);
685
0
        OPENSSL_free(p);
686
0
        return ret;
687
0
    }
688
689
0
#ifndef OPENSSL_NO_CMS
690
0
    if (ts == NULL) {
691
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
692
0
        return NULL;
693
0
    }
694
0
    if ((bio = CMS_EnvelopedData_decrypt(encryptedKey->value.envelopedData,
695
0
             NULL, pkey, cert, secret, 0,
696
0
             libctx, propq))
697
0
        == NULL) {
698
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_ENCRYPTEDKEY);
699
0
        goto end;
700
0
    }
701
0
    sd = ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_SignedData), bio, NULL);
702
0
    if (sd == NULL)
703
0
        goto end;
704
705
0
    if ((purpose_id = X509_PURPOSE_get_by_sname(SN_cmKGA)) < 0) {
706
0
        purpose_id = X509_PURPOSE_get_unused_id(libctx);
707
0
        if (!X509_PURPOSE_add(purpose_id, X509_TRUST_COMPAT, 0, check_cmKGA,
708
0
                LN_cmKGA, SN_cmKGA, NULL))
709
0
            goto end;
710
0
    }
711
0
    if ((vpm = X509_STORE_get0_param(ts)) == NULL)
712
0
        goto end;
713
714
    /* temporarily override X509_PURPOSE_SMIME_SIGN: */
715
0
    bak_purpose_id = X509_VERIFY_PARAM_get_purpose(vpm);
716
0
    if (!X509_STORE_set_purpose(ts, purpose_id)) {
717
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SETTING_PURPOSE);
718
0
        goto end;
719
0
    }
720
721
0
    pkey_bio = CMS_SignedData_verify(sd, NULL, NULL /* scerts */, ts,
722
0
        extra, NULL, 0, libctx, propq);
723
724
0
    if (!X509_STORE_set_purpose(ts, bak_purpose_id)) {
725
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SETTING_PURPOSE);
726
0
        goto end;
727
0
    }
728
729
0
    if (pkey_bio == NULL) {
730
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_VERIFYING_ENCRYPTEDKEY);
731
0
        goto end;
732
0
    }
733
734
    /* unpack AsymmetricKeyPackage */
735
0
    if ((ret = d2i_PrivateKey_ex_bio(pkey_bio, NULL, libctx, propq)) == NULL)
736
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_ENCRYPTEDKEY);
737
738
0
end:
739
0
    CMS_SignedData_free(sd);
740
0
    BIO_free(bio);
741
0
    BIO_free(pkey_bio);
742
0
    return ret;
743
#else
744
    /* prevent warning on unused parameters: */
745
    ((void)ts, (void)extra, (void)cert, (void)secret);
746
    ERR_raise(ERR_LIB_CRMF, CRMF_R_CMS_NOT_SUPPORTED);
747
    return NULL;
748
#endif /* OPENSSL_NO_CMS */
749
0
}
750
751
unsigned char *OSSL_CRMF_ENCRYPTEDVALUE_decrypt(const OSSL_CRMF_ENCRYPTEDVALUE *enc,
752
    OSSL_LIB_CTX *libctx, const char *propq,
753
    EVP_PKEY *pkey, int *outlen)
754
0
{
755
0
    EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
756
0
    unsigned char *ek = NULL; /* decrypted symmetric encryption key */
757
0
    size_t eksize = 0; /* size of decrypted symmetric encryption key */
758
0
    EVP_CIPHER *cipher = NULL; /* used cipher */
759
0
    int cikeysize = 0; /* key size from cipher */
760
0
    unsigned char *iv = NULL; /* initial vector for symmetric encryption */
761
0
    unsigned char *out = NULL; /* decryption output buffer */
762
0
    int n, ret = 0;
763
0
    EVP_PKEY_CTX *pkctx = NULL; /* private key context */
764
0
    char name[OSSL_MAX_NAME_SIZE];
765
766
0
    if (outlen == NULL) {
767
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
768
0
        return NULL;
769
0
    }
770
0
    *outlen = 0;
771
0
    if (enc == NULL || enc->symmAlg == NULL || enc->encSymmKey == NULL
772
0
        || enc->encValue == NULL || pkey == NULL) {
773
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
774
0
        return NULL;
775
0
    }
776
777
    /* select symmetric cipher based on algorithm given in message */
778
0
    OBJ_obj2txt(name, sizeof(name), enc->symmAlg->algorithm, 0);
779
0
    (void)ERR_set_mark();
780
0
    cipher = EVP_CIPHER_fetch(libctx, name, propq);
781
0
    if (cipher == NULL)
782
0
        cipher = (EVP_CIPHER *)EVP_get_cipherbyobj(enc->symmAlg->algorithm);
783
0
    if (cipher == NULL) {
784
0
        (void)ERR_clear_last_mark();
785
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_CIPHER);
786
0
        goto end;
787
0
    }
788
0
    (void)ERR_pop_to_mark();
789
790
0
    cikeysize = EVP_CIPHER_get_key_length(cipher);
791
    /* first the symmetric key needs to be decrypted */
792
0
    pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
793
0
    if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx) > 0) {
794
0
        ASN1_BIT_STRING *encKey = enc->encSymmKey;
795
0
        size_t failure;
796
0
        int retval;
797
798
0
        if (EVP_PKEY_decrypt(pkctx, NULL, &eksize,
799
0
                encKey->data, encKey->length)
800
0
                <= 0
801
0
            || (ek = OPENSSL_malloc(eksize)) == NULL)
802
0
            goto end;
803
0
        retval = EVP_PKEY_decrypt(pkctx, ek, &eksize, encKey->data, encKey->length);
804
0
        failure = ~constant_time_is_zero_s(constant_time_msb(retval)
805
0
            | constant_time_is_zero(retval));
806
0
        failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize);
807
0
        if (failure) {
808
0
            ERR_clear_error(); /* error state may have sensitive information */
809
0
            ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
810
0
            goto end;
811
0
        }
812
0
    } else {
813
0
        goto end;
814
0
    }
815
0
    if ((iv = OPENSSL_malloc(EVP_CIPHER_get_iv_length(cipher))) == NULL)
816
0
        goto end;
817
0
    if (ASN1_TYPE_get_octetstring(enc->symmAlg->parameter, iv,
818
0
            EVP_CIPHER_get_iv_length(cipher))
819
0
        != EVP_CIPHER_get_iv_length(cipher)) {
820
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_MALFORMED_IV);
821
0
        goto end;
822
0
    }
823
824
0
    if ((out = OPENSSL_malloc(enc->encValue->length + EVP_CIPHER_get_block_size(cipher))) == NULL
825
0
        || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
826
0
        goto end;
827
0
    EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
828
829
0
    if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
830
0
        || !EVP_DecryptUpdate(evp_ctx, out, outlen,
831
0
            enc->encValue->data,
832
0
            enc->encValue->length)
833
0
        || !EVP_DecryptFinal(evp_ctx, out + *outlen, &n)) {
834
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_ENCRYPTEDVALUE);
835
0
        goto end;
836
0
    }
837
0
    *outlen += n;
838
0
    ret = 1;
839
840
0
end:
841
0
    EVP_PKEY_CTX_free(pkctx);
842
0
    EVP_CIPHER_CTX_free(evp_ctx);
843
0
    EVP_CIPHER_free(cipher);
844
0
    OPENSSL_clear_free(ek, eksize);
845
0
    OPENSSL_free(iv);
846
0
    if (ret)
847
0
        return out;
848
0
    OPENSSL_free(out);
849
0
    return NULL;
850
0
}
851
852
/*
853
 * Decrypts the certificate in the given encryptedValue using private key pkey.
854
 * This is needed for the indirect PoP method as in RFC 9810 section 5.2.8.3.2.
855
 *
856
 * returns a pointer to the decrypted certificate
857
 * returns NULL on error or if no certificate available
858
 */
859
X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert,
860
    OSSL_LIB_CTX *libctx, const char *propq,
861
    EVP_PKEY *pkey)
862
0
{
863
0
    unsigned char *buf = NULL;
864
0
    const unsigned char *p;
865
0
    int len;
866
0
    X509 *cert = NULL;
867
868
0
    buf = OSSL_CRMF_ENCRYPTEDVALUE_decrypt(ecert, libctx, propq, pkey, &len);
869
0
    if ((p = buf) == NULL || (cert = X509_new_ex(libctx, propq)) == NULL)
870
0
        goto end;
871
872
0
    if (d2i_X509(&cert, &p, len) == NULL) {
873
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE);
874
0
        X509_free(cert);
875
0
        cert = NULL;
876
0
    }
877
878
0
end:
879
0
    OPENSSL_free(buf);
880
0
    return cert;
881
0
}
882
/*-
883
 * Decrypts the certificate in the given encryptedKey using private key pkey.
884
 * This is needed for the indirect PoP method as in RFC 9810 section 5.2.8.3.2.
885
 *
886
 * returns a pointer to the decrypted certificate
887
 * returns NULL on error or if no certificate available
888
 */
889
X509 *OSSL_CRMF_ENCRYPTEDKEY_get1_encCert(const OSSL_CRMF_ENCRYPTEDKEY *ecert,
890
    OSSL_LIB_CTX *libctx, const char *propq,
891
    EVP_PKEY *pkey, unsigned int flags)
892
0
{
893
0
#ifndef OPENSSL_NO_CMS
894
0
    BIO *bio;
895
0
    X509 *cert = NULL;
896
0
#endif
897
898
0
    if (ecert->type != OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA)
899
0
        return OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(ecert->value.encryptedValue,
900
0
            libctx, propq, pkey);
901
0
#ifndef OPENSSL_NO_CMS
902
0
    bio = CMS_EnvelopedData_decrypt(ecert->value.envelopedData, NULL,
903
0
        pkey, NULL /* cert */, NULL, flags,
904
0
        libctx, propq);
905
0
    if (bio == NULL)
906
0
        return NULL;
907
0
    cert = d2i_X509_bio(bio, NULL);
908
0
    if (cert == NULL)
909
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE);
910
0
    BIO_free(bio);
911
0
    return cert;
912
#else
913
    (void)flags; /* prevent warning on unused parameter */
914
    ERR_raise(ERR_LIB_CRMF, CRMF_R_CMS_NOT_SUPPORTED);
915
    return NULL;
916
#endif /* OPENSSL_NO_CMS */
917
0
}
918
919
#ifndef OPENSSL_NO_CMS
920
OSSL_CRMF_ENCRYPTEDKEY *OSSL_CRMF_ENCRYPTEDKEY_init_envdata(CMS_EnvelopedData *envdata)
921
0
{
922
0
    OSSL_CRMF_ENCRYPTEDKEY *ek = OSSL_CRMF_ENCRYPTEDKEY_new();
923
924
0
    if (ek == NULL)
925
0
        return NULL;
926
0
    ek->type = OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA;
927
0
    ek->value.envelopedData = envdata;
928
0
    return ek;
929
0
}
930
#endif