Coverage Report

Created: 2026-07-12 07:21

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