2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
13 #include <openssl/cmp_util.h>
14 #include "cmp_local.h" /* just for decls of internal functions defined here */
15 #include <openssl/cmperr.h>
16 #include <openssl/err.h> /* should be implied by cmperr.h */
17 #include <openssl/x509v3.h>
19 DEFINE_STACK_OF(X509_OBJECT)
22 * use trace API for CMP-specific logging, prefixed by "CMP " and severity
25 int OSSL_CMP_log_open(void) /* is designed to be idempotent */
27 #ifndef OPENSSL_NO_STDIO
28 BIO *bio = BIO_new_fp(stdout, BIO_NOCLOSE);
30 if (bio != NULL && OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_CMP, bio))
34 CMPerr(0, CMP_R_NO_STDIO);
38 void OSSL_CMP_log_close(void) /* is designed to be idempotent */
40 (void)OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_CMP, NULL);
43 /* return >= 0 if level contains logging level, possibly preceded by "CMP " */
44 #define max_level_len 5 /* = max length of the below strings, e.g., "EMERG" */
45 static OSSL_CMP_severity parse_level(const char *level)
47 const char *end_level = strchr(level, ':');
49 char level_copy[max_level_len + 1];
51 if (end_level == NULL)
54 if (strncmp(level, OSSL_CMP_LOG_PREFIX,
55 strlen(OSSL_CMP_LOG_PREFIX)) == 0)
56 level += strlen(OSSL_CMP_LOG_PREFIX);
57 len = end_level - level;
58 if (len > max_level_len)
60 OPENSSL_strlcpy(level_copy, level, len + 1);
62 strcmp(level_copy, "EMERG") == 0 ? OSSL_CMP_LOG_EMERG :
63 strcmp(level_copy, "ALERT") == 0 ? OSSL_CMP_LOG_ALERT :
64 strcmp(level_copy, "CRIT") == 0 ? OSSL_CMP_LOG_CRIT :
65 strcmp(level_copy, "ERROR") == 0 ? OSSL_CMP_LOG_ERR :
66 strcmp(level_copy, "WARN") == 0 ? OSSL_CMP_LOG_WARNING :
67 strcmp(level_copy, "NOTE") == 0 ? OSSL_CMP_LOG_NOTICE :
68 strcmp(level_copy, "INFO") == 0 ? OSSL_CMP_LOG_INFO :
69 strcmp(level_copy, "DEBUG") == 0 ? OSSL_CMP_LOG_DEBUG :
73 const char *ossl_cmp_log_parse_metadata(const char *buf,
74 OSSL_CMP_severity *level,
75 char **func, char **file, int *line)
77 const char *p_func = buf;
78 const char *p_file = buf == NULL ? NULL : strchr(buf, ':');
79 const char *p_level = buf;
80 const char *msg = buf;
88 const char *p_line = strchr(++p_file, ':');
90 if ((*level = parse_level(buf)) < 0 && p_line != NULL) {
91 /* check if buf contains location info and logging level */
92 char *p_level_tmp = (char *)p_level;
93 const long line_number = strtol(++p_line, &p_level_tmp, 10);
95 p_level = p_level_tmp;
96 if (p_level > p_line && *(p_level++) == ':') {
97 if ((*level = parse_level(p_level)) >= 0) {
98 *func = OPENSSL_strndup(p_func, p_file - 1 - p_func);
99 *file = OPENSSL_strndup(p_file, p_line - 1 - p_file);
100 /* no real problem if OPENSSL_strndup() returns NULL */
101 *line = (int)line_number;
102 msg = strchr(p_level, ':') + 1;
112 #define UNKNOWN_FUNC "(unknown function)" /* the default for OPENSSL_FUNC */
114 * substitute fallback if component/function name is NULL or empty or contains
115 * just pseudo-information "(unknown function)" due to -pedantic and macros.h
117 static const char *improve_location_name(const char *func, const char *fallback)
119 if (fallback == NULL)
120 return func == NULL ? UNKNOWN_FUNC : func;
122 return func == NULL || *func == '\0' || strcmp(func, UNKNOWN_FUNC) == 0
126 int OSSL_CMP_print_to_bio(BIO *bio, const char *component, const char *file,
127 int line, OSSL_CMP_severity level, const char *msg)
129 const char *level_string =
130 level == OSSL_CMP_LOG_EMERG ? "EMERG" :
131 level == OSSL_CMP_LOG_ALERT ? "ALERT" :
132 level == OSSL_CMP_LOG_CRIT ? "CRIT" :
133 level == OSSL_CMP_LOG_ERR ? "error" :
134 level == OSSL_CMP_LOG_WARNING ? "warning" :
135 level == OSSL_CMP_LOG_NOTICE ? "NOTE" :
136 level == OSSL_CMP_LOG_INFO ? "info" :
137 level == OSSL_CMP_LOG_DEBUG ? "DEBUG" : "(unknown level)";
140 if (BIO_printf(bio, "%s:%s:%d:", improve_location_name(component, "CMP"),
144 return BIO_printf(bio, OSSL_CMP_LOG_PREFIX"%s: %s\n",
145 level_string, msg) >= 0;
148 #define ERR_PRINT_BUF_SIZE 4096
149 /* this is similar to ERR_print_errors_cb, but uses the CMP-specific cb type */
150 void OSSL_CMP_print_errors_cb(OSSL_CMP_log_cb_t log_fn)
153 char msg[ERR_PRINT_BUF_SIZE];
154 const char *file = NULL, *func = NULL, *data = NULL;
157 while ((err = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
158 const char *component =
159 improve_location_name(func, ERR_lib_error_string(err));
161 if (!(flags & ERR_TXT_STRING))
163 BIO_snprintf(msg, sizeof(msg), "%s%s%s", ERR_reason_error_string(err),
164 data == NULL || *data == '\0' ? "" : " : ",
165 data == NULL ? "" : data);
166 if (log_fn == NULL) {
167 #ifndef OPENSSL_NO_STDIO
168 BIO *bio = BIO_new_fp(stderr, BIO_NOCLOSE);
171 OSSL_CMP_print_to_bio(bio, component, file, line,
172 OSSL_CMP_LOG_ERR, msg);
176 /* CMPerr(0, CMP_R_NO_STDIO) makes no sense during error printing */
179 if (log_fn(component, file, line, OSSL_CMP_LOG_ERR, msg) <= 0)
180 break; /* abort outputting the error report */
185 int ossl_cmp_X509_STORE_add1_certs(X509_STORE *store, STACK_OF(X509) *certs,
186 int only_self_signed)
191 CMPerr(0, CMP_R_NULL_ARGUMENT);
196 for (i = 0; i < sk_X509_num(certs); i++) {
197 X509 *cert = sk_X509_value(certs, i);
199 if (!only_self_signed || X509_self_signed(cert, 0) == 1)
200 if (!X509_STORE_add_cert(store, cert)) /* ups cert ref counter */
207 * Builds a certificate chain starting from <cert>
208 * using the optional list of intermediate CA certificates <certs>.
209 * If <store> is NULL builds the chain as far down as possible, ignoring errors.
210 * Else the chain must reach a trust anchor contained in <store>.
212 * Returns NULL on error, else a pointer to a stack of (up_ref'ed) certificates
213 * starting with given EE certificate and followed by all available intermediate
214 * certificates down towards any trust anchor but without including the latter.
216 * NOTE: If a non-NULL stack is returned the caller is responsible for freeing.
217 * NOTE: In case there is more than one possibility for the chain,
218 * OpenSSL seems to take the first one; check X509_verify_cert() for details.
220 /* TODO this should be of more general interest and thus be exported. */
222 *ossl_cmp_build_cert_chain(OPENSSL_CTX *libctx, const char *propq,
224 STACK_OF(X509) *certs, X509 *cert)
226 STACK_OF(X509) *chain = NULL, *result = NULL;
227 X509_STORE *ts = store == NULL ? X509_STORE_new() : store;
228 X509_STORE_CTX *csc = NULL;
230 if (ts == NULL || cert == NULL) {
231 CMPerr(0, CMP_R_NULL_ARGUMENT);
235 if ((csc = X509_STORE_CTX_new_with_libctx(libctx, propq)) == NULL)
237 if (store == NULL && certs != NULL
238 && !ossl_cmp_X509_STORE_add1_certs(ts, certs, 0))
240 if (!X509_STORE_CTX_init(csc, ts, cert,
241 store == NULL ? NULL : certs))
243 /* disable any cert status/revocation checking etc. */
244 X509_VERIFY_PARAM_clear_flags(X509_STORE_CTX_get0_param(csc),
245 ~(X509_V_FLAG_USE_CHECK_TIME
246 | X509_V_FLAG_NO_CHECK_TIME));
248 if (X509_verify_cert(csc) <= 0 && store != NULL)
250 chain = X509_STORE_CTX_get0_chain(csc);
252 /* result list to store the up_ref'ed not self-signed certificates */
253 if ((result = sk_X509_new_null()) == NULL)
255 if (!X509_add_certs(result, chain,
256 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP
257 | X509_ADD_FLAG_NO_SS)) {
258 sk_X509_free(result);
265 X509_STORE_CTX_free(csc);
269 int ossl_cmp_sk_ASN1_UTF8STRING_push_str(STACK_OF(ASN1_UTF8STRING) *sk,
272 ASN1_UTF8STRING *utf8string;
274 if (!ossl_assert(sk != NULL && text != NULL))
276 if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
278 if (!ASN1_STRING_set(utf8string, text, -1))
280 if (!sk_ASN1_UTF8STRING_push(sk, utf8string))
285 ASN1_UTF8STRING_free(utf8string);
289 int ossl_cmp_asn1_octet_string_set1(ASN1_OCTET_STRING **tgt,
290 const ASN1_OCTET_STRING *src)
292 ASN1_OCTET_STRING *new;
294 CMPerr(0, CMP_R_NULL_ARGUMENT);
297 if (*tgt == src) /* self-assignment */
301 if ((new = ASN1_OCTET_STRING_dup(src)) == NULL)
307 ASN1_OCTET_STRING_free(*tgt);
312 int ossl_cmp_asn1_octet_string_set1_bytes(ASN1_OCTET_STRING **tgt,
313 const unsigned char *bytes, int len)
315 ASN1_OCTET_STRING *new = NULL;
318 CMPerr(0, CMP_R_NULL_ARGUMENT);
322 if ((new = ASN1_OCTET_STRING_new()) == NULL
323 || !(ASN1_OCTET_STRING_set(new, bytes, len))) {
324 ASN1_OCTET_STRING_free(new);
329 ASN1_OCTET_STRING_free(*tgt);