Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/crmf/crmf_lib.c
Line
Count
Source
1
/*-
2
 * Copyright 2007-2026 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
132
{
173
132
    OSSL_CRMF_CERTID *cid = NULL;
174
175
132
    if (issuer == NULL || serial == NULL) {
176
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
177
0
        return NULL;
178
0
    }
179
180
132
    if ((cid = OSSL_CRMF_CERTID_new()) == NULL)
181
0
        goto err;
182
183
132
    if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
184
0
        goto err;
185
132
    cid->issuer->type = GEN_DIRNAME;
186
187
132
    ASN1_INTEGER_free(cid->serialNumber);
188
132
    if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
189
0
        goto err;
190
191
132
    return cid;
192
193
0
err:
194
0
    OSSL_CRMF_CERTID_free(cid);
195
0
    return NULL;
196
132
}
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.66k
{
242
4.66k
    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.66k
    return crm->certReq->certTemplate;
247
4.66k
}
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.31k
{
270
1.31k
    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.31k
    return ASN1_INTEGER_set(crm->certReq->certReqId, rid);
276
1.31k
}
277
278
/* get ASN.1 encoded integer, return -1 on error */
279
static int crmf_asn1_get_int(const ASN1_INTEGER *a)
280
3.10k
{
281
3.10k
    int64_t res;
282
283
3.10k
    if (!ASN1_INTEGER_get_int64(&res, a)) {
284
6
        ERR_raise(ERR_LIB_CRMF, ASN1_R_INVALID_NUMBER);
285
6
        return -1;
286
6
    }
287
3.09k
    if (res < INT_MIN) {
288
46
        ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_SMALL);
289
46
        return -1;
290
46
    }
291
3.05k
    if (res > INT_MAX) {
292
13
        ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_LARGE);
293
13
        return -1;
294
13
    }
295
3.03k
    return (int)res;
296
3.05k
}
297
298
int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm)
299
3.10k
{
300
3.10k
    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.10k
    return crmf_asn1_get_int(crm->certReq->certReqId);
305
3.10k
}
306
307
int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm,
308
    X509_EXTENSIONS *exts)
309
1.31k
{
310
1.31k
    OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
311
312
1.31k
    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.31k
    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.31k
    sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free);
323
1.31k
    tmpl->extensions = exts;
324
1.31k
    return 1;
325
1.31k
}
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
    if (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
        != 0)
386
0
        return 1;
387
0
    ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SIGNING_POPO);
388
0
    return 0;
389
0
}
390
391
int OSSL_CRMF_MSG_create_popo(int meth, OSSL_CRMF_MSG *crm,
392
    EVP_PKEY *pkey, const EVP_MD *digest,
393
    OSSL_LIB_CTX *libctx, const char *propq)
394
1.31k
{
395
1.31k
    OSSL_CRMF_POPO *pp = NULL;
396
1.31k
    ASN1_INTEGER *tag = NULL;
397
398
1.31k
    if (crm == NULL || (meth == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
399
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
400
0
        return 0;
401
0
    }
402
403
1.31k
    if (meth == OSSL_CRMF_POPO_NONE)
404
1.31k
        goto end;
405
0
    if ((pp = OSSL_CRMF_POPO_new()) == NULL)
406
0
        goto err;
407
0
    pp->type = meth;
408
409
0
    switch (meth) {
410
0
    case OSSL_CRMF_POPO_RAVERIFIED:
411
0
        if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
412
0
            goto err;
413
0
        break;
414
415
0
    case OSSL_CRMF_POPO_SIGNATURE: {
416
0
        OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
417
418
0
        if (ps == NULL)
419
0
            goto err;
420
0
        if (!create_popo_signature(ps, crm->certReq, pkey, digest,
421
0
                libctx, propq)) {
422
0
            OSSL_CRMF_POPOSIGNINGKEY_free(ps);
423
0
            goto err;
424
0
        }
425
0
        pp->value.signature = ps;
426
0
    } break;
427
428
0
    case OSSL_CRMF_POPO_KEYENC:
429
0
        if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
430
0
            goto err;
431
0
        tag = ASN1_INTEGER_new();
432
0
        pp->value.keyEncipherment->type = OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
433
0
        pp->value.keyEncipherment->value.subsequentMessage = tag;
434
0
        if (tag == NULL
435
0
            || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
436
0
            goto err;
437
0
        break;
438
439
0
    default:
440
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
441
0
        goto err;
442
0
    }
443
444
1.31k
end:
445
1.31k
    OSSL_CRMF_POPO_free(crm->popo);
446
1.31k
    crm->popo = pp;
447
448
1.31k
    return 1;
449
0
err:
450
0
    OSSL_CRMF_POPO_free(pp);
451
0
    return 0;
452
0
}
453
454
/* verifies the Proof-of-Possession of the request with the given rid in reqs */
455
int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
456
    int rid, int acceptRAVerified,
457
    OSSL_LIB_CTX *libctx, const char *propq)
458
2.64k
{
459
2.64k
    OSSL_CRMF_MSG *req = NULL;
460
2.64k
    X509_PUBKEY *pubkey = NULL;
461
2.64k
    OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
462
2.64k
    const ASN1_ITEM *it;
463
2.64k
    void *asn;
464
465
2.64k
    if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) {
466
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
467
0
        return 0;
468
0
    }
469
470
2.64k
    if (req->popo == NULL) {
471
3
        ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING);
472
3
        return 0;
473
3
    }
474
475
2.63k
    switch (req->popo->type) {
476
0
    case OSSL_CRMF_POPO_RAVERIFIED:
477
0
        if (!acceptRAVerified) {
478
0
            ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED);
479
0
            return 0;
480
0
        }
481
0
        break;
482
2.63k
    case OSSL_CRMF_POPO_SIGNATURE:
483
2.63k
        pubkey = req->certReq->certTemplate->publicKey;
484
2.63k
        if (pubkey == NULL) {
485
3
            ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY);
486
3
            return 0;
487
3
        }
488
2.63k
        sig = req->popo->value.signature;
489
2.63k
        if (sig->poposkInput != NULL) {
490
            /*
491
             * According to RFC 4211: publicKey contains a copy of
492
             * the public key from the certificate template. This MUST be
493
             * exactly the same value as contained in the certificate template.
494
             */
495
0
            if (sig->poposkInput->publicKey == NULL) {
496
0
                ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY);
497
0
                return 0;
498
0
            }
499
0
            if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) {
500
0
                ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY);
501
0
                return 0;
502
0
            }
503
504
            /*
505
             * Should check at this point the contents of the authInfo sub-field
506
             * as requested in FR #19807 according to RFC 4211 section 4.1.
507
             */
508
509
0
            it = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT);
510
0
            asn = sig->poposkInput;
511
2.63k
        } else {
512
2.63k
            if (req->certReq->certTemplate->subject == NULL) {
513
11
                ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_SUBJECT);
514
11
                return 0;
515
11
            }
516
2.62k
            it = ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST);
517
2.62k
            asn = req->certReq;
518
2.62k
        }
519
2.62k
        if (ASN1_item_verify_ex(it, sig->algorithmIdentifier, sig->signature,
520
2.62k
                asn, NULL, X509_PUBKEY_get0(pubkey), libctx,
521
2.62k
                propq)
522
2.62k
            < 1)
523
2.54k
            return 0;
524
80
        break;
525
80
    case OSSL_CRMF_POPO_KEYENC:
526
        /*
527
         * When OSSL_CMP_certrep_new() supports encrypted certs,
528
         * should return 1 if the type of req->popo->value.keyEncipherment
529
         * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
530
         * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
531
         */
532
0
    case OSSL_CRMF_POPO_KEYAGREE:
533
0
    default:
534
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_POPO_METHOD);
535
0
        return 0;
536
2.63k
    }
537
80
    return 1;
538
2.63k
}
539
540
int OSSL_CRMF_MSG_centralkeygen_requested(const OSSL_CRMF_MSG *crm, const X509_REQ *p10)
541
3.27k
{
542
3.27k
    X509_PUBKEY *pubkey = NULL;
543
3.27k
    const unsigned char *pk = NULL;
544
3.27k
    int pklen, ret = 0;
545
546
3.27k
    if (crm == NULL && p10 == NULL) {
547
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
548
0
        return -1;
549
0
    }
550
551
3.27k
    if (crm != NULL)
552
1.35k
        pubkey = OSSL_CRMF_CERTTEMPLATE_get0_publicKey(OSSL_CRMF_MSG_get0_tmpl(crm));
553
1.92k
    else
554
1.92k
        pubkey = p10->req_info.pubkey;
555
556
3.27k
    if (pubkey == NULL
557
3.27k
        || (X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey)
558
3.27k
            && pklen == 0))
559
5
        ret = 1;
560
561
    /*
562
     * In case of CRMF, POPO MUST be absent if central key generation
563
     * is requested, otherwise MUST be present
564
     */
565
3.27k
    if (crm != NULL && ret != (crm->popo == NULL)) {
566
3
        ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_CENTRAL_KEYGEN);
567
3
        return -2;
568
3
    }
569
3.27k
    return ret;
570
3.27k
}
571
572
X509_PUBKEY
573
*OSSL_CRMF_CERTTEMPLATE_get0_publicKey(const OSSL_CRMF_CERTTEMPLATE *tmpl)
574
1.35k
{
575
1.35k
    return tmpl != NULL ? tmpl->publicKey : NULL;
576
1.35k
}
577
578
const ASN1_INTEGER *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl)
579
63
{
580
63
    return tmpl != NULL ? tmpl->serialNumber : NULL;
581
63
}
582
583
const X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_subject(const OSSL_CRMF_CERTTEMPLATE *tmpl)
584
0
{
585
0
    return tmpl != NULL ? tmpl->subject : NULL;
586
0
}
587
588
const X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl)
589
63
{
590
63
    return tmpl != NULL ? tmpl->issuer : NULL;
591
63
}
592
593
X509_EXTENSIONS
594
*OSSL_CRMF_CERTTEMPLATE_get0_extensions(const OSSL_CRMF_CERTTEMPLATE *tmpl)
595
0
{
596
0
    return tmpl != NULL ? tmpl->extensions : NULL;
597
0
}
598
599
const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid)
600
0
{
601
0
    return cid != NULL && cid->issuer->type == GEN_DIRNAME ? cid->issuer->d.directoryName : NULL;
602
0
}
603
604
const ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID
605
        *cid)
606
0
{
607
0
    return cid != NULL ? cid->serialNumber : NULL;
608
0
}
609
610
/*-
611
 * Fill in the certificate template |tmpl|.
612
 * Any other NULL argument will leave the respective field unchanged.
613
 */
614
int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
615
    EVP_PKEY *pubkey,
616
    const X509_NAME *subject,
617
    const X509_NAME *issuer,
618
    const ASN1_INTEGER *serial)
619
1.51k
{
620
1.51k
    if (tmpl == NULL) {
621
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
622
0
        return 0;
623
0
    }
624
1.51k
    if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject))
625
0
        return 0;
626
1.51k
    if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer))
627
0
        return 0;
628
1.51k
    if (serial != NULL) {
629
193
        ASN1_INTEGER_free(tmpl->serialNumber);
630
193
        if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
631
0
            return 0;
632
193
    }
633
1.51k
    if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
634
0
        return 0;
635
1.51k
    return 1;
636
1.51k
}
637
638
#ifndef OPENSSL_NO_CMS
639
DECLARE_ASN1_ITEM(CMS_SignedData) /* copied from cms_local.h */
640
641
/* check for KGA authorization implied by CA flag or by explicit EKU cmKGA */
642
static int check_cmKGA(ossl_unused const X509_PURPOSE *purpose, const X509 *x, int ca)
643
0
{
644
0
    STACK_OF(ASN1_OBJECT) *ekus;
645
0
    int i, ret = 1;
646
647
0
    if (ca)
648
0
        return ret;
649
0
    ekus = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL);
650
0
    for (i = 0; i < sk_ASN1_OBJECT_num(ekus); i++) {
651
0
        if (OBJ_obj2nid(sk_ASN1_OBJECT_value(ekus, i)) == NID_cmKGA)
652
0
            goto end;
653
0
    }
654
0
    ret = 0;
655
656
0
end:
657
0
    sk_ASN1_OBJECT_pop_free(ekus, ASN1_OBJECT_free);
658
0
    return ret;
659
0
}
660
#endif /* OPENSSL_NO_CMS */
661
662
EVP_PKEY *OSSL_CRMF_ENCRYPTEDKEY_get1_pkey(const OSSL_CRMF_ENCRYPTEDKEY *encryptedKey,
663
    X509_STORE *ts, STACK_OF(X509) *extra, EVP_PKEY *pkey,
664
    X509 *cert, ASN1_OCTET_STRING *secret,
665
    OSSL_LIB_CTX *libctx, const char *propq)
666
0
{
667
0
#ifndef OPENSSL_NO_CMS
668
0
    BIO *bio = NULL;
669
0
    CMS_SignedData *sd = NULL;
670
0
    BIO *pkey_bio = NULL;
671
0
    int purpose_id, bak_purpose_id;
672
0
    X509_VERIFY_PARAM *vpm;
673
0
#endif
674
0
    EVP_PKEY *ret = NULL;
675
676
0
    if (encryptedKey == NULL) {
677
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
678
0
        return NULL;
679
0
    }
680
0
    if (encryptedKey->type != OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA) {
681
0
        unsigned char *p;
682
0
        const unsigned char *p_copy;
683
0
        int len;
684
685
0
        p = OSSL_CRMF_ENCRYPTEDVALUE_decrypt(encryptedKey->value.encryptedValue,
686
0
            libctx, propq, pkey, &len);
687
0
        if ((p_copy = p) != NULL)
688
0
            ret = d2i_AutoPrivateKey_ex(NULL, &p_copy, len, libctx, propq);
689
0
        OPENSSL_free(p);
690
0
        return ret;
691
0
    }
692
693
0
#ifndef OPENSSL_NO_CMS
694
0
    if (ts == NULL) {
695
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
696
0
        return NULL;
697
0
    }
698
0
    if ((bio = CMS_EnvelopedData_decrypt(encryptedKey->value.envelopedData,
699
0
             NULL, pkey, cert, secret, 0,
700
0
             libctx, propq))
701
0
        == NULL) {
702
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_ENCRYPTEDKEY);
703
0
        goto end;
704
0
    }
705
0
    sd = ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_SignedData), bio, NULL);
706
0
    if (sd == NULL)
707
0
        goto end;
708
709
0
    if ((purpose_id = X509_PURPOSE_get_by_sname(SN_cmKGA)) < 0) {
710
0
        purpose_id = X509_PURPOSE_get_unused_id(libctx);
711
0
        if (!X509_PURPOSE_add(purpose_id, X509_TRUST_COMPAT, 0, check_cmKGA,
712
0
                LN_cmKGA, SN_cmKGA, NULL))
713
0
            goto end;
714
0
    }
715
0
    if ((vpm = X509_STORE_get0_param(ts)) == NULL)
716
0
        goto end;
717
718
    /* temporarily override X509_PURPOSE_SMIME_SIGN: */
719
0
    bak_purpose_id = X509_VERIFY_PARAM_get_purpose(vpm);
720
0
    if (!X509_STORE_set_purpose(ts, purpose_id)) {
721
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SETTING_PURPOSE);
722
0
        goto end;
723
0
    }
724
725
0
    pkey_bio = CMS_SignedData_verify(sd, NULL, NULL /* scerts */, ts,
726
0
        extra, NULL, 0, libctx, propq);
727
728
0
    if (!X509_STORE_set_purpose(ts, bak_purpose_id)) {
729
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SETTING_PURPOSE);
730
0
        goto end;
731
0
    }
732
733
0
    if (pkey_bio == NULL) {
734
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_VERIFYING_ENCRYPTEDKEY);
735
0
        goto end;
736
0
    }
737
738
    /* unpack AsymmetricKeyPackage */
739
0
    if ((ret = d2i_PrivateKey_ex_bio(pkey_bio, NULL, libctx, propq)) == NULL)
740
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_ENCRYPTEDKEY);
741
742
0
end:
743
0
    CMS_SignedData_free(sd);
744
0
    BIO_free(bio);
745
0
    BIO_free(pkey_bio);
746
0
    return ret;
747
#else
748
    /* prevent warning on unused parameters: */
749
    ((void)ts, (void)extra, (void)cert, (void)secret);
750
    ERR_raise(ERR_LIB_CRMF, CRMF_R_CMS_NOT_SUPPORTED);
751
    return NULL;
752
#endif /* OPENSSL_NO_CMS */
753
0
}
754
755
unsigned char *OSSL_CRMF_ENCRYPTEDVALUE_decrypt(const OSSL_CRMF_ENCRYPTEDVALUE *enc,
756
    OSSL_LIB_CTX *libctx, const char *propq,
757
    EVP_PKEY *pkey, int *outlen)
758
0
{
759
0
    EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
760
0
    unsigned char *ek = NULL; /* decrypted symmetric encryption key */
761
0
    size_t eksize = 0; /* size of decrypted symmetric encryption key */
762
0
    EVP_CIPHER *cipher = NULL; /* used cipher */
763
0
    int cikeysize = 0; /* key size from cipher */
764
0
    unsigned char *iv = NULL; /* initial vector for symmetric encryption */
765
0
    unsigned char *out = NULL; /* decryption output buffer */
766
0
    int n, ret = 0;
767
0
    EVP_PKEY_CTX *pkctx = NULL; /* private key context */
768
0
    char name[OSSL_MAX_NAME_SIZE];
769
770
0
    if (outlen == NULL) {
771
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
772
0
        return NULL;
773
0
    }
774
0
    *outlen = 0;
775
0
    if (enc == NULL || enc->symmAlg == NULL || enc->encSymmKey == NULL
776
0
        || enc->encValue == NULL || pkey == NULL) {
777
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
778
0
        return NULL;
779
0
    }
780
781
    /* select symmetric cipher based on algorithm given in message */
782
0
    OBJ_obj2txt(name, sizeof(name), enc->symmAlg->algorithm, 0);
783
0
    cipher = EVP_CIPHER_fetch(libctx, name, propq);
784
0
    if (cipher == NULL) {
785
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_CIPHER);
786
0
        goto end;
787
0
    }
788
789
0
    cikeysize = EVP_CIPHER_get_key_length(cipher);
790
    /* first the symmetric key needs to be decrypted */
791
0
    pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
792
0
    if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx) > 0) {
793
0
        ASN1_BIT_STRING *encKey = enc->encSymmKey;
794
0
        size_t failure;
795
0
        int retval;
796
797
0
        if (EVP_PKEY_decrypt(pkctx, NULL, &eksize,
798
0
                encKey->data, encKey->length)
799
0
                <= 0
800
0
            || (ek = OPENSSL_malloc(eksize)) == NULL)
801
0
            goto end;
802
0
        retval = EVP_PKEY_decrypt(pkctx, ek, &eksize, encKey->data, encKey->length);
803
0
        failure = ~constant_time_is_zero_s(constant_time_msb(retval)
804
0
            | constant_time_is_zero(retval));
805
0
        failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize);
806
0
        if (failure) {
807
0
            ERR_clear_error(); /* error state may have sensitive information */
808
0
            ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
809
0
            goto end;
810
0
        }
811
0
    } else {
812
0
        goto end;
813
0
    }
814
0
    if ((iv = OPENSSL_malloc(EVP_CIPHER_get_iv_length(cipher))) == NULL)
815
0
        goto end;
816
0
    if (ASN1_TYPE_get_octetstring(enc->symmAlg->parameter, iv,
817
0
            EVP_CIPHER_get_iv_length(cipher))
818
0
        != EVP_CIPHER_get_iv_length(cipher)) {
819
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_MALFORMED_IV);
820
0
        goto end;
821
0
    }
822
823
0
    if ((out = OPENSSL_malloc(enc->encValue->length + EVP_CIPHER_get_block_size(cipher))) == NULL
824
0
        || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
825
0
        goto end;
826
0
    EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
827
828
0
    if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
829
0
        || !EVP_DecryptUpdate(evp_ctx, out, outlen,
830
0
            enc->encValue->data,
831
0
            enc->encValue->length)
832
0
        || !EVP_DecryptFinal(evp_ctx, out + *outlen, &n)) {
833
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_ENCRYPTEDVALUE);
834
0
        goto end;
835
0
    }
836
0
    *outlen += n;
837
0
    ret = 1;
838
839
0
end:
840
0
    EVP_PKEY_CTX_free(pkctx);
841
0
    EVP_CIPHER_CTX_free(evp_ctx);
842
0
    EVP_CIPHER_free(cipher);
843
0
    OPENSSL_clear_free(ek, eksize);
844
0
    OPENSSL_free(iv);
845
0
    if (ret)
846
0
        return out;
847
0
    OPENSSL_free(out);
848
0
    return NULL;
849
0
}
850
851
/*-
852
 * Decrypts the certificate in the given encryptedValue using private key pkey.
853
 * This is needed for the indirect PoP method as in RFC 9810 section 5.2.8.3.2.
854
 *
855
 * returns a pointer to the decrypted certificate
856
 * returns NULL on error or if no certificate available
857
 */
858
X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert,
859
    OSSL_LIB_CTX *libctx, const char *propq,
860
    EVP_PKEY *pkey)
861
0
{
862
0
    unsigned char *buf = NULL;
863
0
    const unsigned char *p;
864
0
    int len;
865
0
    X509 *cert = NULL;
866
867
0
    buf = OSSL_CRMF_ENCRYPTEDVALUE_decrypt(ecert, libctx, propq, pkey, &len);
868
0
    if ((p = buf) == NULL || (cert = X509_new_ex(libctx, propq)) == NULL)
869
0
        goto end;
870
871
0
    if (d2i_X509(&cert, &p, len) == NULL) {
872
0
        ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE);
873
0
        X509_free(cert);
874
0
        cert = NULL;
875
0
    }
876
877
0
end:
878
0
    OPENSSL_free(buf);
879
0
    return cert;
880
0
}
881
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