Coverage Report

Created: 2025-12-04 06:33

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