xref: /openssl/crypto/cmp/cmp_util.c (revision 357bfe73)
1 /*
2  * Copyright 2007-2021 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 
OSSL_CMP_log_open(void)23 int OSSL_CMP_log_open(void) /* is designed to be idempotent */
24 {
25 #ifdef OPENSSL_NO_TRACE
26     return 1;
27 #else
28 # ifndef OPENSSL_NO_STDIO
29     BIO *bio = BIO_new_fp(stdout, BIO_NOCLOSE);
30 
31     if (bio != NULL && OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_CMP, bio))
32         return 1;
33     BIO_free(bio);
34 # endif
35     ERR_raise(ERR_LIB_CMP, CMP_R_NO_STDIO);
36     return 0;
37 #endif
38 }
39 
OSSL_CMP_log_close(void)40 void OSSL_CMP_log_close(void) /* is designed to be idempotent */
41 {
42     (void)OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_CMP, NULL);
43 }
44 
45 /* return >= 0 if level contains logging level, possibly preceded by "CMP " */
46 #define max_level_len 5 /* = max length of the below strings, e.g., "EMERG" */
parse_level(const char * level)47 static OSSL_CMP_severity parse_level(const char *level)
48 {
49     const char *end_level = strchr(level, ':');
50     int len;
51     char level_copy[max_level_len + 1];
52 
53     if (end_level == NULL)
54         return -1;
55 
56     if (HAS_PREFIX(level, OSSL_CMP_LOG_PREFIX))
57         level += strlen(OSSL_CMP_LOG_PREFIX);
58     len = end_level - level;
59     if (len > max_level_len)
60         return -1;
61     OPENSSL_strlcpy(level_copy, level, len + 1);
62     return
63         strcmp(level_copy, "EMERG") == 0 ? OSSL_CMP_LOG_EMERG :
64         strcmp(level_copy, "ALERT") == 0 ? OSSL_CMP_LOG_ALERT :
65         strcmp(level_copy, "CRIT") == 0 ? OSSL_CMP_LOG_CRIT :
66         strcmp(level_copy, "ERROR") == 0 ? OSSL_CMP_LOG_ERR :
67         strcmp(level_copy, "WARN") == 0 ? OSSL_CMP_LOG_WARNING :
68         strcmp(level_copy, "NOTE") == 0 ? OSSL_CMP_LOG_NOTICE :
69         strcmp(level_copy, "INFO") == 0 ? OSSL_CMP_LOG_INFO :
70         strcmp(level_copy, "DEBUG") == 0 ? OSSL_CMP_LOG_DEBUG :
71         -1;
72 }
73 
ossl_cmp_log_parse_metadata(const char * buf,OSSL_CMP_severity * level,char ** func,char ** file,int * line)74 const char *ossl_cmp_log_parse_metadata(const char *buf,
75                                         OSSL_CMP_severity *level,
76                                         char **func, char **file, int *line)
77 {
78     const char *p_func = buf;
79     const char *p_file = buf == NULL ? NULL : strchr(buf, ':');
80     const char *p_level = buf;
81     const char *msg = buf;
82 
83     *level = -1;
84     *func = NULL;
85     *file = NULL;
86     *line = 0;
87 
88     if (p_file != NULL) {
89         const char *p_line = strchr(++p_file, ':');
90 
91         if ((*level = parse_level(buf)) < 0 && p_line != NULL) {
92             /* check if buf contains location info and logging level */
93             char *p_level_tmp = (char *)p_level;
94             const long line_number = strtol(++p_line, &p_level_tmp, 10);
95 
96             p_level = p_level_tmp;
97             if (p_level > p_line && *(p_level++) == ':') {
98                 if ((*level = parse_level(p_level)) >= 0) {
99                     *func = OPENSSL_strndup(p_func, p_file - 1 - p_func);
100                     *file = OPENSSL_strndup(p_file, p_line - 1 - p_file);
101                     /* no real problem if OPENSSL_strndup() returns NULL */
102                     *line = (int)line_number;
103                     msg = strchr(p_level, ':');
104                     if (msg != NULL && *++msg == ' ')
105                         msg++;
106                 }
107             }
108         }
109     }
110     return msg;
111 }
112 
113 #define UNKNOWN_FUNC "(unknown function)" /* the default for OPENSSL_FUNC */
114 /*
115  * substitute fallback if component/function name is NULL or empty or contains
116  * just pseudo-information "(unknown function)" due to -pedantic and macros.h
117  */
improve_location_name(const char * func,const char * fallback)118 static const char *improve_location_name(const char *func, const char *fallback)
119 {
120     if (fallback == NULL)
121         return func == NULL ? UNKNOWN_FUNC : func;
122 
123     return func == NULL || *func == '\0' || strcmp(func, UNKNOWN_FUNC) == 0
124         ? fallback : func;
125 }
126 
OSSL_CMP_print_to_bio(BIO * bio,const char * component,const char * file,int line,OSSL_CMP_severity level,const char * msg)127 int OSSL_CMP_print_to_bio(BIO *bio, const char *component, const char *file,
128                           int line, OSSL_CMP_severity level, const char *msg)
129 {
130     const char *level_string =
131         level == OSSL_CMP_LOG_EMERG ? "EMERG" :
132         level == OSSL_CMP_LOG_ALERT ? "ALERT" :
133         level == OSSL_CMP_LOG_CRIT ? "CRIT" :
134         level == OSSL_CMP_LOG_ERR ? "error" :
135         level == OSSL_CMP_LOG_WARNING ? "warning" :
136         level == OSSL_CMP_LOG_NOTICE ? "NOTE" :
137         level == OSSL_CMP_LOG_INFO ? "info" :
138         level == OSSL_CMP_LOG_DEBUG ? "DEBUG" : "(unknown level)";
139 
140 #ifndef NDEBUG
141     if (BIO_printf(bio, "%s:%s:%d:", improve_location_name(component, "CMP"),
142                    file, line) < 0)
143         return 0;
144 #endif
145     return BIO_printf(bio, OSSL_CMP_LOG_PREFIX"%s: %s\n",
146                       level_string, msg) >= 0;
147 }
148 
149 #define ERR_PRINT_BUF_SIZE 4096
150 /* this is similar to ERR_print_errors_cb, but uses the CMP-specific cb type */
OSSL_CMP_print_errors_cb(OSSL_CMP_log_cb_t log_fn)151 void OSSL_CMP_print_errors_cb(OSSL_CMP_log_cb_t log_fn)
152 {
153     unsigned long err;
154     char msg[ERR_PRINT_BUF_SIZE];
155     const char *file = NULL, *func = NULL, *data = NULL;
156     int line, flags;
157 
158     while ((err = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
159         const char *component =
160             improve_location_name(func, ERR_lib_error_string(err));
161         unsigned long reason = ERR_GET_REASON(err);
162         const char *rs = NULL;
163         char rsbuf[256];
164 
165 #ifndef OPENSSL_NO_ERR
166         if (ERR_SYSTEM_ERROR(err)) {
167             if (openssl_strerror_r(reason, rsbuf, sizeof(rsbuf)))
168                 rs = rsbuf;
169         } else {
170             rs = ERR_reason_error_string(err);
171         }
172 #endif
173         if (rs == NULL) {
174             BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", reason);
175             rs = rsbuf;
176         }
177         if (data != NULL && (flags & ERR_TXT_STRING) != 0)
178             BIO_snprintf(msg, sizeof(msg), "%s:%s", rs, data);
179         else
180             BIO_snprintf(msg, sizeof(msg), "%s", rs);
181 
182         if (log_fn == NULL) {
183 #ifndef OPENSSL_NO_STDIO
184             BIO *bio = BIO_new_fp(stderr, BIO_NOCLOSE);
185 
186             if (bio != NULL) {
187                 OSSL_CMP_print_to_bio(bio, component, file, line,
188                                       OSSL_CMP_LOG_ERR, msg);
189                 BIO_free(bio);
190             }
191 #else
192             /* ERR_raise(..., CMP_R_NO_STDIO) would make no sense here */
193 #endif
194         } else {
195             if (log_fn(component, file, line, OSSL_CMP_LOG_ERR, msg) <= 0)
196                 break; /* abort outputting the error report */
197         }
198     }
199 }
200 
ossl_cmp_X509_STORE_add1_certs(X509_STORE * store,STACK_OF (X509)* certs,int only_self_signed)201 int ossl_cmp_X509_STORE_add1_certs(X509_STORE *store, STACK_OF(X509) *certs,
202                                    int only_self_signed)
203 {
204     int i;
205 
206     if (store == NULL) {
207         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
208         return 0;
209     }
210     if (certs == NULL)
211         return 1;
212     for (i = 0; i < sk_X509_num(certs); i++) {
213         X509 *cert = sk_X509_value(certs, i);
214 
215         if (!only_self_signed || X509_self_signed(cert, 0) == 1)
216             if (!X509_STORE_add_cert(store, cert)) /* ups cert ref counter */
217                 return 0;
218     }
219     return 1;
220 }
221 
ossl_cmp_sk_ASN1_UTF8STRING_push_str(STACK_OF (ASN1_UTF8STRING)* sk,const char * text,int len)222 int ossl_cmp_sk_ASN1_UTF8STRING_push_str(STACK_OF(ASN1_UTF8STRING) *sk,
223                                          const char *text, int len)
224 {
225     ASN1_UTF8STRING *utf8string;
226 
227     if (!ossl_assert(sk != NULL && text != NULL))
228         return 0;
229     if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
230         return 0;
231     if (!ASN1_STRING_set(utf8string, text, len))
232         goto err;
233     if (!sk_ASN1_UTF8STRING_push(sk, utf8string))
234         goto err;
235     return 1;
236 
237  err:
238     ASN1_UTF8STRING_free(utf8string);
239     return 0;
240 }
241 
ossl_cmp_asn1_octet_string_set1(ASN1_OCTET_STRING ** tgt,const ASN1_OCTET_STRING * src)242 int ossl_cmp_asn1_octet_string_set1(ASN1_OCTET_STRING **tgt,
243                                     const ASN1_OCTET_STRING *src)
244 {
245     ASN1_OCTET_STRING *new;
246 
247     if (tgt == NULL) {
248         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
249         return 0;
250     }
251     if (*tgt == src) /* self-assignment */
252         return 1;
253 
254     if (src != NULL) {
255         if ((new = ASN1_OCTET_STRING_dup(src)) == NULL)
256             return 0;
257     } else {
258         new = NULL;
259     }
260 
261     ASN1_OCTET_STRING_free(*tgt);
262     *tgt = new;
263     return 1;
264 }
265 
ossl_cmp_asn1_octet_string_set1_bytes(ASN1_OCTET_STRING ** tgt,const unsigned char * bytes,int len)266 int ossl_cmp_asn1_octet_string_set1_bytes(ASN1_OCTET_STRING **tgt,
267                                           const unsigned char *bytes, int len)
268 {
269     ASN1_OCTET_STRING *new = NULL;
270 
271     if (tgt == NULL) {
272         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
273         return 0;
274     }
275     if (bytes != NULL) {
276         if ((new = ASN1_OCTET_STRING_new()) == NULL
277                 || !(ASN1_OCTET_STRING_set(new, bytes, len))) {
278             ASN1_OCTET_STRING_free(new);
279             return 0;
280         }
281     }
282 
283     ASN1_OCTET_STRING_free(*tgt);
284     *tgt = new;
285     return 1;
286 }
287