1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
* Copyright Siemens AG 2022
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "cmp_local.h"
#include <openssl/cmp_util.h>
static const X509_VERIFY_PARAM *get0_trustedStore_vpm(const OSSL_CMP_CTX *ctx)
{
const X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore(ctx);
return ts == NULL ? NULL : X509_STORE_get0_param(ts);
}
static void cert_msg(const char *func, const char *file, int lineno,
OSSL_CMP_severity level, OSSL_CMP_CTX *ctx,
const char *source, X509 *cert, const char *msg)
{
char *subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
ossl_cmp_print_log(level, ctx, func, file, lineno,
level == OSSL_CMP_LOG_WARNING ? "WARN" : "ERR",
"certificate from '%s' with subject '%s' %s",
source, subj, msg);
OPENSSL_free(subj);
}
/* use |type_CA| -1 (no CA type check) or 0 (must be EE) or 1 (must be CA) */
static int ossl_X509_check(OSSL_CMP_CTX *ctx, const char *source, X509 *cert,
int type_CA, const X509_VERIFY_PARAM *vpm)
{
uint32_t ex_flags = X509_get_extension_flags(cert);
int res = X509_cmp_timeframe(vpm, X509_get0_notBefore(cert),
X509_get0_notAfter(cert));
int ret = res == 0;
OSSL_CMP_severity level =
vpm == NULL ? OSSL_CMP_LOG_WARNING : OSSL_CMP_LOG_ERR;
if (!ret)
cert_msg(OPENSSL_FUNC, OPENSSL_FILE, OPENSSL_LINE, level, ctx,
source, cert, res > 0 ? "has expired" : "not yet valid");
if (type_CA >= 0 && (ex_flags & EXFLAG_V1) == 0) {
int is_CA = (ex_flags & EXFLAG_CA) != 0;
if ((type_CA != 0) != is_CA) {
cert_msg(OPENSSL_FUNC, OPENSSL_FILE, OPENSSL_LINE, level, ctx,
source, cert,
is_CA ? "is not an EE cert" : "is not a CA cert");
ret = 0;
}
}
return ret;
}
static int ossl_X509_check_all(OSSL_CMP_CTX *ctx, const char *source,
STACK_OF(X509) *certs,
int type_CA, const X509_VERIFY_PARAM *vpm)
{
int i;
int ret = 1;
for (i = 0; i < sk_X509_num(certs /* may be NULL */); i++)
ret = ossl_X509_check(ctx, source,
sk_X509_value(certs, i), type_CA, vpm)
&& ret; /* Having 'ret' after the '&&', all certs are checked. */
return ret;
}
static OSSL_CMP_ITAV *get_genm_itav(OSSL_CMP_CTX *ctx,
OSSL_CMP_ITAV *req, /* gets consumed */
int expected, const char *desc)
{
STACK_OF(OSSL_CMP_ITAV) *itavs = NULL;
int i, n;
if (ctx == NULL) {
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
goto err;
}
if (OSSL_CMP_CTX_get_status(ctx) != OSSL_CMP_PKISTATUS_unspecified) {
ERR_raise_data(ERR_LIB_CMP, CMP_R_UNCLEAN_CTX,
"client context in unsuitable state; should call CMPclient_reinit() before");
goto err;
}
if (!OSSL_CMP_CTX_push0_genm_ITAV(ctx, req))
goto err;
req = NULL;
itavs = OSSL_CMP_exec_GENM_ses(ctx);
if (itavs == NULL) {
if (OSSL_CMP_CTX_get_status(ctx) != OSSL_CMP_PKISTATUS_request)
ERR_raise_data(ERR_LIB_CMP, CMP_R_GETTING_GENP,
"with infoType %s", desc);
return NULL;
}
if ((n = sk_OSSL_CMP_ITAV_num(itavs)) <= 0) {
ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_GENP,
"response on genm requesting infoType %s does not include suitable value", desc);
sk_OSSL_CMP_ITAV_free(itavs);
return NULL;
}
if (n > 1)
ossl_cmp_log2(WARN, ctx,
"response on genm contains %d ITAVs; will use the first ITAV with infoType id-it-%s",
n, desc);
for (i = 0; i < n; i++) {
OSSL_CMP_ITAV *itav = sk_OSSL_CMP_ITAV_shift(itavs);
ASN1_OBJECT *obj = OSSL_CMP_ITAV_get0_type(itav);
char name[128] = "genp contains InfoType '";
size_t offset = strlen(name);
if (OBJ_obj2nid(obj) == expected) {
for (i++; i < n; i++)
OSSL_CMP_ITAV_free(sk_OSSL_CMP_ITAV_shift(itavs));
sk_OSSL_CMP_ITAV_free(itavs);
return itav;
}
if (OBJ_obj2txt(name + offset, sizeof(name) - offset, obj, 0) < 0)
strcat(name, "<unknown>");
ossl_cmp_log2(WARN, ctx, "%s' while expecting 'id-it-%s'", name, desc);
OSSL_CMP_ITAV_free(itav);
}
ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_GENP,
"could not find any ITAV for %s", desc);
err:
sk_OSSL_CMP_ITAV_free(itavs);
OSSL_CMP_ITAV_free(req);
return NULL;
}
int OSSL_CMP_get1_caCerts(OSSL_CMP_CTX *ctx, STACK_OF(X509) **out)
{
OSSL_CMP_ITAV *req, *itav;
STACK_OF(X509) *certs = NULL;
int ret = 0;
if (out == NULL) {
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
return 0;
}
*out = NULL;
if ((req = OSSL_CMP_ITAV_new_caCerts(NULL)) == NULL)
return 0;
if ((itav = get_genm_itav(ctx, req, NID_id_it_caCerts, "caCerts")) == NULL)
return 0;
if (!OSSL_CMP_ITAV_get0_caCerts(itav, &certs))
goto end;
ret = 1;
if (certs == NULL) /* no CA certificate available */
goto end;
if (!ossl_X509_check_all(ctx, "genp", certs, 1 /* CA */,
get0_trustedStore_vpm(ctx))) {
ret = 0;
goto end;
}
*out = sk_X509_new_reserve(NULL, sk_X509_num(certs));
if (!X509_add_certs(*out, certs,
X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP)) {
sk_X509_pop_free(*out, X509_free);
*out = NULL;
ret = 0;
}
end:
OSSL_CMP_ITAV_free(itav);
return ret;
}
static int selfsigned_verify_cb(int ok, X509_STORE_CTX *store_ctx)
{
if (ok == 0
&& X509_STORE_CTX_get_error_depth(store_ctx) == 0
&& X509_STORE_CTX_get_error(store_ctx)
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) {
/* in this case, custom chain building */
int i;
STACK_OF(X509) *trust;
STACK_OF(X509) *chain = X509_STORE_CTX_get0_chain(store_ctx);
STACK_OF(X509) *untrusted = X509_STORE_CTX_get0_untrusted(store_ctx);
X509_STORE_CTX_check_issued_fn check_issued =
X509_STORE_CTX_get_check_issued(store_ctx);
X509 *cert = sk_X509_value(chain, 0); /* target cert */
X509 *issuer;
for (i = 0; i < sk_X509_num(untrusted); i++) {
cert = sk_X509_value(untrusted, i);
if (!X509_add_cert(chain, cert, X509_ADD_FLAG_UP_REF))
return 0;
|