cmp_util.c 8.8 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
  55. strcmp(level_copy, "EMERG") == 0 ? OSSL_CMP_LOG_EMERG :
  56. strcmp(level_copy, "ALERT") == 0 ? OSSL_CMP_LOG_ALERT :
  57. strcmp(level_copy, "CRIT") == 0 ? OSSL_CMP_LOG_CRIT :
  58. strcmp(level_copy, "ERROR") == 0 ? OSSL_CMP_LOG_ERR :
  59. strcmp(level_copy, "WARN") == 0 ? OSSL_CMP_LOG_WARNING :
  60. strcmp(level_copy, "NOTE") == 0 ? OSSL_CMP_LOG_NOTICE :
  61. strcmp(level_copy, "INFO") == 0 ? OSSL_CMP_LOG_INFO :
  62. strcmp(level_copy, "DEBUG") == 0 ? OSSL_CMP_LOG_DEBUG :
  63. -1;
  64. }
  65. const char *ossl_cmp_log_parse_metadata(const char *buf,
  66. OSSL_CMP_severity *level,
  67. char **func, char **file, int *line)
  68. {
  69. const char *p_func = buf;
  70. const char *p_file = buf == NULL ? NULL : strchr(buf, ':');
  71. const char *p_level = buf;
  72. const char *msg = buf;
  73. *level = -1;
  74. *func = NULL;
  75. *file = NULL;
  76. *line = 0;
  77. if (p_file != NULL) {
  78. const char *p_line = strchr(++p_file, ':');
  79. if ((*level = parse_level(buf)) < 0 && p_line != NULL) {
  80. /* check if buf contains location info and logging level */
  81. char *p_level_tmp = (char *)p_level;
  82. const long line_number = strtol(++p_line, &p_level_tmp, 10);
  83. p_level = p_level_tmp;
  84. if (p_level > p_line && *(p_level++) == ':') {
  85. if ((*level = parse_level(p_level)) >= 0) {
  86. *func = OPENSSL_strndup(p_func, p_file - 1 - p_func);
  87. *file = OPENSSL_strndup(p_file, p_line - 1 - p_file);
  88. /* no real problem if OPENSSL_strndup() returns NULL */
  89. *line = (int)line_number;
  90. msg = strchr(p_level, ':');
  91. if (msg != NULL && *++msg == ' ')
  92. msg++;
  93. }
  94. }
  95. }
  96. }
  97. return msg;
  98. }
  99. #define UNKNOWN_FUNC "(unknown function)" /* the default for OPENSSL_FUNC */
  100. /*
  101. * substitute fallback if component/function name is NULL or empty or contains
  102. * just pseudo-information "(unknown function)" due to -pedantic and macros.h
  103. */
  104. static const char *improve_location_name(const char *func, const char *fallback)
  105. {
  106. if (fallback == NULL)
  107. return func == NULL ? UNKNOWN_FUNC : func;
  108. return func == NULL || *func == '\0' || strcmp(func, UNKNOWN_FUNC) == 0
  109. ? fallback : func;
  110. }
  111. int OSSL_CMP_print_to_bio(BIO *bio, const char *component, const char *file,
  112. int line, OSSL_CMP_severity level, const char *msg)
  113. {
  114. const char *level_string =
  115. level == OSSL_CMP_LOG_EMERG ? "EMERG" :
  116. level == OSSL_CMP_LOG_ALERT ? "ALERT" :
  117. level == OSSL_CMP_LOG_CRIT ? "CRIT" :
  118. level == OSSL_CMP_LOG_ERR ? "error" :
  119. level == OSSL_CMP_LOG_WARNING ? "warning" :
  120. level == OSSL_CMP_LOG_NOTICE ? "NOTE" :
  121. level == OSSL_CMP_LOG_INFO ? "info" :
  122. level == OSSL_CMP_LOG_DEBUG ? "DEBUG" : "(unknown level)";
  123. #ifndef NDEBUG
  124. if (BIO_printf(bio, "%s:%s:%d:", improve_location_name(component, "CMP"),
  125. file, line) < 0)
  126. return 0;
  127. #endif
  128. return BIO_printf(bio, OSSL_CMP_LOG_PREFIX"%s: %s\n",
  129. level_string, msg) >= 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 =
  141. improve_location_name(func, ERR_lib_error_string(err));
  142. unsigned long reason = ERR_GET_REASON(err);
  143. const char *rs = NULL;
  144. char rsbuf[256];
  145. #ifndef OPENSSL_NO_ERR
  146. if (ERR_SYSTEM_ERROR(err)) {
  147. if (openssl_strerror_r(reason, rsbuf, sizeof(rsbuf)))
  148. rs = rsbuf;
  149. } else {
  150. rs = ERR_reason_error_string(err);
  151. }
  152. #endif
  153. if (rs == NULL) {
  154. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", reason);
  155. rs = rsbuf;
  156. }
  157. if (data != NULL && (flags & ERR_TXT_STRING) != 0)
  158. BIO_snprintf(msg, sizeof(msg), "%s:%s", rs, data);
  159. else
  160. BIO_snprintf(msg, sizeof(msg), "%s", rs);
  161. if (log_fn == NULL) {
  162. #ifndef OPENSSL_NO_STDIO
  163. BIO *bio = BIO_new_fp(stderr, BIO_NOCLOSE);
  164. if (bio != NULL) {
  165. OSSL_CMP_print_to_bio(bio, component, file, line,
  166. OSSL_CMP_LOG_ERR, msg);
  167. BIO_free(bio);
  168. }
  169. #else
  170. /* ERR_raise(ERR_LIB_CMP, CMP_R_NO_STDIO) makes no sense during error printing */
  171. #endif
  172. } else {
  173. if (log_fn(component, file, line, OSSL_CMP_LOG_ERR, msg) <= 0)
  174. break; /* abort outputting the error report */
  175. }
  176. }
  177. }
  178. int ossl_cmp_X509_STORE_add1_certs(X509_STORE *store, STACK_OF(X509) *certs,
  179. int only_self_signed)
  180. {
  181. int i;
  182. if (store == NULL) {
  183. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  184. return 0;
  185. }
  186. if (certs == NULL)
  187. return 1;
  188. for (i = 0; i < sk_X509_num(certs); i++) {
  189. X509 *cert = sk_X509_value(certs, i);
  190. if (!only_self_signed || X509_self_signed(cert, 0) == 1)
  191. if (!X509_STORE_add_cert(store, cert)) /* ups cert ref counter */
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. int ossl_cmp_sk_ASN1_UTF8STRING_push_str(STACK_OF(ASN1_UTF8STRING) *sk,
  197. const char *text, int len)
  198. {
  199. ASN1_UTF8STRING *utf8string;
  200. if (!ossl_assert(sk != NULL && text != NULL))
  201. return 0;
  202. if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
  203. return 0;
  204. if (!ASN1_STRING_set(utf8string, text, len))
  205. goto err;
  206. if (!sk_ASN1_UTF8STRING_push(sk, utf8string))
  207. goto err;
  208. return 1;
  209. err:
  210. ASN1_UTF8STRING_free(utf8string);
  211. return 0;
  212. }
  213. int ossl_cmp_asn1_octet_string_set1(ASN1_OCTET_STRING **tgt,
  214. const ASN1_OCTET_STRING *src)
  215. {
  216. ASN1_OCTET_STRING *new;
  217. if (tgt == NULL) {
  218. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  219. return 0;
  220. }
  221. if (*tgt == src) /* self-assignment */
  222. return 1;
  223. if (src != NULL) {
  224. if ((new = ASN1_OCTET_STRING_dup(src)) == NULL)
  225. return 0;
  226. } else {
  227. new = NULL;
  228. }
  229. ASN1_OCTET_STRING_free(*tgt);
  230. *tgt = new;
  231. return 1;
  232. }
  233. int ossl_cmp_asn1_octet_string_set1_bytes(ASN1_OCTET_STRING **tgt,
  234. const unsigned char *bytes, int len)
  235. {
  236. ASN1_OCTET_STRING *new = NULL;
  237. if (tgt == NULL) {
  238. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  239. return 0;
  240. }
  241. if (bytes != NULL) {
  242. if ((new = ASN1_OCTET_STRING_new()) == NULL
  243. || !(ASN1_OCTET_STRING_set(new, bytes, len))) {
  244. ASN1_OCTET_STRING_free(new);
  245. return 0;
  246. }
  247. }
  248. ASN1_OCTET_STRING_free(*tgt);
  249. *tgt = new;
  250. return 1;
  251. }