Use read/write locking on Windows
[openssl.git] / crypto / cmp / cmp_util.c
1 /*
2  * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2019
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
12 #include <string.h>
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>
18
19 /*
20  * use trace API for CMP-specific logging, prefixed by "CMP " and severity
21  */
22
23 int OSSL_CMP_log_open(void) /* is designed to be idempotent */
24 {
25 #ifndef OPENSSL_NO_STDIO
26     BIO *bio = BIO_new_fp(stdout, BIO_NOCLOSE);
27
28     if (bio != NULL && OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_CMP, bio))
29         return 1;
30     BIO_free(bio);
31 #endif
32     ERR_raise(ERR_LIB_CMP, CMP_R_NO_STDIO);
33     return 0;
34 }
35
36 void OSSL_CMP_log_close(void) /* is designed to be idempotent */
37 {
38     (void)OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_CMP, NULL);
39 }
40
41 /* return >= 0 if level contains logging level, possibly preceded by "CMP " */
42 #define max_level_len 5 /* = max length of the below strings, e.g., "EMERG" */
43 static OSSL_CMP_severity parse_level(const char *level)
44 {
45     const char *end_level = strchr(level, ':');
46     int len;
47     char level_copy[max_level_len + 1];
48
49     if (end_level == NULL)
50         return -1;
51
52     if (strncmp(level, OSSL_CMP_LOG_PREFIX,
53                 strlen(OSSL_CMP_LOG_PREFIX)) == 0)
54         level += strlen(OSSL_CMP_LOG_PREFIX);
55     len = end_level - level;
56     if (len > max_level_len)
57         return -1;
58     OPENSSL_strlcpy(level_copy, level, len + 1);
59     return
60         strcmp(level_copy, "EMERG") == 0 ? OSSL_CMP_LOG_EMERG :
61         strcmp(level_copy, "ALERT") == 0 ? OSSL_CMP_LOG_ALERT :
62         strcmp(level_copy, "CRIT") == 0 ? OSSL_CMP_LOG_CRIT :
63         strcmp(level_copy, "ERROR") == 0 ? OSSL_CMP_LOG_ERR :
64         strcmp(level_copy, "WARN") == 0 ? OSSL_CMP_LOG_WARNING :
65         strcmp(level_copy, "NOTE") == 0 ? OSSL_CMP_LOG_NOTICE :
66         strcmp(level_copy, "INFO") == 0 ? OSSL_CMP_LOG_INFO :
67         strcmp(level_copy, "DEBUG") == 0 ? OSSL_CMP_LOG_DEBUG :
68         -1;
69 }
70
71 const char *ossl_cmp_log_parse_metadata(const char *buf,
72                                         OSSL_CMP_severity *level,
73                                         char **func, char **file, int *line)
74 {
75     const char *p_func = buf;
76     const char *p_file = buf == NULL ? NULL : strchr(buf, ':');
77     const char *p_level = buf;
78     const char *msg = buf;
79
80     *level = -1;
81     *func = NULL;
82     *file = NULL;
83     *line = 0;
84
85     if (p_file != NULL) {
86         const char *p_line = strchr(++p_file, ':');
87
88         if ((*level = parse_level(buf)) < 0 && p_line != NULL) {
89             /* check if buf contains location info and logging level */
90             char *p_level_tmp = (char *)p_level;
91             const long line_number = strtol(++p_line, &p_level_tmp, 10);
92
93             p_level = p_level_tmp;
94             if (p_level > p_line && *(p_level++) == ':') {
95                 if ((*level = parse_level(p_level)) >= 0) {
96                     *func = OPENSSL_strndup(p_func, p_file - 1 - p_func);
97                     *file = OPENSSL_strndup(p_file, p_line - 1 - p_file);
98                     /* no real problem if OPENSSL_strndup() returns NULL */
99                     *line = (int)line_number;
100                     msg = strchr(p_level, ':') + 1;
101                     if (*msg == ' ')
102                         msg++;
103                 }
104             }
105         }
106     }
107     return msg;
108 }
109
110 #define UNKNOWN_FUNC "(unknown function)" /* the default for OPENSSL_FUNC */
111 /*
112  * substitute fallback if component/function name is NULL or empty or contains
113  * just pseudo-information "(unknown function)" due to -pedantic and macros.h
114  */
115 static const char *improve_location_name(const char *func, const char *fallback)
116 {
117     if (fallback == NULL)
118         return func == NULL ? UNKNOWN_FUNC : func;
119
120     return func == NULL || *func == '\0' || strcmp(func, UNKNOWN_FUNC) == 0
121         ? fallback : func;
122 }
123
124 int OSSL_CMP_print_to_bio(BIO *bio, const char *component, const char *file,
125                           int line, OSSL_CMP_severity level, const char *msg)
126 {
127     const char *level_string =
128         level == OSSL_CMP_LOG_EMERG ? "EMERG" :
129         level == OSSL_CMP_LOG_ALERT ? "ALERT" :
130         level == OSSL_CMP_LOG_CRIT ? "CRIT" :
131         level == OSSL_CMP_LOG_ERR ? "error" :
132         level == OSSL_CMP_LOG_WARNING ? "warning" :
133         level == OSSL_CMP_LOG_NOTICE ? "NOTE" :
134         level == OSSL_CMP_LOG_INFO ? "info" :
135         level == OSSL_CMP_LOG_DEBUG ? "DEBUG" : "(unknown level)";
136
137 #ifndef NDEBUG
138     if (BIO_printf(bio, "%s:%s:%d:", improve_location_name(component, "CMP"),
139                    file, line) < 0)
140         return 0;
141 #endif
142     return BIO_printf(bio, OSSL_CMP_LOG_PREFIX"%s: %s\n",
143                       level_string, msg) >= 0;
144 }
145
146 #define ERR_PRINT_BUF_SIZE 4096
147 /* this is similar to ERR_print_errors_cb, but uses the CMP-specific cb type */
148 void OSSL_CMP_print_errors_cb(OSSL_CMP_log_cb_t log_fn)
149 {
150     unsigned long err;
151     char msg[ERR_PRINT_BUF_SIZE];
152     const char *file = NULL, *func = NULL, *data = NULL;
153     int line, flags;
154
155     while ((err = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
156         const char *component =
157             improve_location_name(func, ERR_lib_error_string(err));
158         unsigned long reason = ERR_GET_REASON(err);
159         const char *rs = NULL;
160         char rsbuf[256];
161
162 #ifndef OPENSSL_NO_ERR
163         if (ERR_SYSTEM_ERROR(err)) {
164             if (openssl_strerror_r(reason, rsbuf, sizeof(rsbuf)))
165                 rs = rsbuf;
166         } else {
167             rs = ERR_reason_error_string(err);
168         }
169 #endif
170         if (rs == NULL) {
171             BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", reason);
172             rs = rsbuf;
173         }
174         if (data != NULL && (flags & ERR_TXT_STRING) != 0)
175             BIO_snprintf(msg, sizeof(msg), "%s:%s", rs, data);
176         else
177             BIO_snprintf(msg, sizeof(msg), "%s", rs);
178
179         if (log_fn == NULL) {
180 #ifndef OPENSSL_NO_STDIO
181             BIO *bio = BIO_new_fp(stderr, BIO_NOCLOSE);
182
183             if (bio != NULL) {
184                 OSSL_CMP_print_to_bio(bio, component, file, line,
185                                       OSSL_CMP_LOG_ERR, msg);
186                 BIO_free(bio);
187             }
188 #else
189             /* ERR_raise(ERR_LIB_CMP, CMP_R_NO_STDIO) makes no sense during error printing */
190 #endif
191         } else {
192             if (log_fn(component, file, line, OSSL_CMP_LOG_ERR, msg) <= 0)
193                 break; /* abort outputting the error report */
194         }
195     }
196 }
197
198 int ossl_cmp_X509_STORE_add1_certs(X509_STORE *store, STACK_OF(X509) *certs,
199                                    int only_self_signed)
200 {
201     int i;
202
203     if (store == NULL) {
204         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
205         return 0;
206     }
207     if (certs == NULL)
208         return 1;
209     for (i = 0; i < sk_X509_num(certs); i++) {
210         X509 *cert = sk_X509_value(certs, i);
211
212         if (!only_self_signed || X509_self_signed(cert, 0) == 1)
213             if (!X509_STORE_add_cert(store, cert)) /* ups cert ref counter */
214                 return 0;
215     }
216     return 1;
217 }
218
219 /*-
220  * Builds a certificate chain starting from <cert>
221  * using the optional list of intermediate CA certificates <certs>.
222  * If <store> is NULL builds the chain as far down as possible, ignoring errors.
223  * Else the chain must reach a trust anchor contained in <store>.
224  *
225  * Returns NULL on error, else a pointer to a stack of (up_ref'ed) certificates
226  * starting with given EE certificate and followed by all available intermediate
227  * certificates down towards any trust anchor but without including the latter.
228  *
229  * NOTE: If a non-NULL stack is returned the caller is responsible for freeing.
230  * NOTE: In case there is more than one possibility for the chain,
231  * OpenSSL seems to take the first one; check X509_verify_cert() for details.
232  */
233 /* TODO this should be of more general interest and thus be exported. */
234 STACK_OF(X509)
235     *ossl_cmp_build_cert_chain(OSSL_LIB_CTX *libctx, const char *propq,
236                                X509_STORE *store,
237                                STACK_OF(X509) *certs, X509 *cert)
238 {
239     STACK_OF(X509) *chain = NULL, *result = NULL;
240     X509_STORE *ts = store == NULL ? X509_STORE_new() : store;
241     X509_STORE_CTX *csc = NULL;
242
243     if (ts == NULL || cert == NULL) {
244         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
245         goto err;
246     }
247
248     if ((csc = X509_STORE_CTX_new_ex(libctx, propq)) == NULL)
249         goto err;
250     if (store == NULL && certs != NULL
251             && !ossl_cmp_X509_STORE_add1_certs(ts, certs, 0))
252         goto err;
253     if (!X509_STORE_CTX_init(csc, ts, cert,
254                              store == NULL ? NULL : certs))
255         goto err;
256     /* disable any cert status/revocation checking etc. */
257     X509_VERIFY_PARAM_clear_flags(X509_STORE_CTX_get0_param(csc),
258                                   ~(X509_V_FLAG_USE_CHECK_TIME
259                                     | X509_V_FLAG_NO_CHECK_TIME));
260
261     if (X509_verify_cert(csc) <= 0 && store != NULL)
262         goto err;
263     chain = X509_STORE_CTX_get0_chain(csc);
264
265     /* result list to store the up_ref'ed not self-signed certificates */
266     if (!ossl_x509_add_certs_new(&result, chain,
267                                  X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP
268                                  | X509_ADD_FLAG_NO_SS)) {
269         sk_X509_free(result);
270         result = NULL;
271     }
272
273  err:
274     if (store == NULL)
275         X509_STORE_free(ts);
276     X509_STORE_CTX_free(csc);
277     return result;
278 }
279
280 int ossl_cmp_sk_ASN1_UTF8STRING_push_str(STACK_OF(ASN1_UTF8STRING) *sk,
281                                          const char *text)
282 {
283     ASN1_UTF8STRING *utf8string;
284
285     if (!ossl_assert(sk != NULL && text != NULL))
286         return 0;
287     if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
288         return 0;
289     if (!ASN1_STRING_set(utf8string, text, -1))
290         goto err;
291     if (!sk_ASN1_UTF8STRING_push(sk, utf8string))
292         goto err;
293     return 1;
294
295  err:
296     ASN1_UTF8STRING_free(utf8string);
297     return 0;
298 }
299
300 int ossl_cmp_asn1_octet_string_set1(ASN1_OCTET_STRING **tgt,
301                                     const ASN1_OCTET_STRING *src)
302 {
303     ASN1_OCTET_STRING *new;
304     if (tgt == NULL) {
305         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
306         return 0;
307     }
308     if (*tgt == src) /* self-assignment */
309         return 1;
310
311     if (src != NULL) {
312         if ((new = ASN1_OCTET_STRING_dup(src)) == NULL)
313             return 0;
314     } else {
315         new = NULL;
316     }
317
318     ASN1_OCTET_STRING_free(*tgt);
319     *tgt = new;
320     return 1;
321 }
322
323 int ossl_cmp_asn1_octet_string_set1_bytes(ASN1_OCTET_STRING **tgt,
324                                           const unsigned char *bytes, int len)
325 {
326     ASN1_OCTET_STRING *new = NULL;
327
328     if (tgt == NULL) {
329         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
330         return 0;
331     }
332     if (bytes != NULL) {
333         if ((new = ASN1_OCTET_STRING_new()) == NULL
334                 || !(ASN1_OCTET_STRING_set(new, bytes, len))) {
335             ASN1_OCTET_STRING_free(new);
336             return 0;
337         }
338     }
339
340     ASN1_OCTET_STRING_free(*tgt);
341     *tgt = new;
342     return 1;
343 }