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