cmp_util.c 11 KB

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