cmp_util.c 9.4 KB

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