2
0

cmp_util.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright 2007-2019 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, char **func, char **file, int *line)
  64. {
  65. const char *p_func = buf;
  66. const char *p_file = buf == NULL ? NULL : strchr(buf, ':');
  67. const char *p_level = buf;
  68. const char *msg = buf;
  69. *level = -1;
  70. *func = NULL;
  71. *file = NULL;
  72. *line = 0;
  73. if (p_file != NULL) {
  74. const char *p_line = strchr(++p_file, ':');
  75. if ((*level = parse_level(buf)) < 0 && p_line != NULL) {
  76. /* check if buf contains location info and logging level */
  77. char *p_level_tmp = (char *)p_level;
  78. const long line_number = strtol(++p_line, &p_level_tmp, 10);
  79. p_level = p_level_tmp;
  80. if (p_level > p_line && *(p_level++) == ':') {
  81. if ((*level = parse_level(p_level)) >= 0) {
  82. *func = OPENSSL_strndup(p_func, p_file - 1 - p_func);
  83. *file = OPENSSL_strndup(p_file, p_line - 1 - p_file);
  84. /* no real problem if OPENSSL_strndup() returns NULL */
  85. *line = (int)line_number;
  86. msg = strchr(p_level, ':') + 1;
  87. if (*msg == ' ')
  88. msg++;
  89. }
  90. }
  91. }
  92. }
  93. return msg;
  94. }
  95. /*
  96. * auxiliary function for incrementally reporting texts via the error queue
  97. */
  98. static void put_error(int lib, const char *func, int reason,
  99. const char *file, int line)
  100. {
  101. ERR_new();
  102. ERR_set_debug(file, line, func);
  103. ERR_set_error(lib, reason, NULL /* no data here, so fmt is NULL */);
  104. }
  105. #define ERR_print_errors_cb_LIMIT 4096 /* size of char buf[] variable there */
  106. #define TYPICAL_MAX_OUTPUT_BEFORE_DATA 100
  107. #define MAX_DATA_LEN (ERR_print_errors_cb_LIMIT-TYPICAL_MAX_OUTPUT_BEFORE_DATA)
  108. void ossl_cmp_add_error_txt(const char *separator, const char *txt)
  109. {
  110. const char *file = NULL;
  111. int line;
  112. const char *func = NULL;
  113. const char *data = NULL;
  114. int flags;
  115. unsigned long err = ERR_peek_last_error();
  116. if (separator == NULL)
  117. separator = "";
  118. if (err == 0)
  119. put_error(ERR_LIB_CMP, NULL, 0, "", 0);
  120. do {
  121. size_t available_len, data_len;
  122. const char *curr = txt, *next = txt;
  123. char *tmp;
  124. ERR_peek_last_error_all(&file, &line, &func, &data, &flags);
  125. if ((flags & ERR_TXT_STRING) == 0) {
  126. data = "";
  127. separator = "";
  128. }
  129. data_len = strlen(data);
  130. /* workaround for limit of ERR_print_errors_cb() */
  131. if (data_len >= MAX_DATA_LEN
  132. || strlen(separator) >= (size_t)(MAX_DATA_LEN - data_len))
  133. available_len = 0;
  134. else
  135. available_len = MAX_DATA_LEN - data_len - strlen(separator) - 1;
  136. /* MAX_DATA_LEN > available_len >= 0 */
  137. if (separator[0] == '\0') {
  138. const size_t len_next = strlen(next);
  139. if (len_next <= available_len) {
  140. next += len_next;
  141. curr = NULL; /* no need to split */
  142. }
  143. else {
  144. next += available_len;
  145. curr = next; /* will split at this point */
  146. }
  147. } else {
  148. while (*next != '\0' && (size_t)(next - txt) <= available_len) {
  149. curr = next;
  150. next = strstr(curr, separator);
  151. if (next != NULL)
  152. next += strlen(separator);
  153. else
  154. next = curr + strlen(curr);
  155. }
  156. if ((size_t)(next - txt) <= available_len)
  157. curr = NULL; /* the above loop implies *next == '\0' */
  158. }
  159. if (curr != NULL) {
  160. /* split error msg at curr since error data would get too long */
  161. if (curr != txt) {
  162. tmp = OPENSSL_strndup(txt, curr - txt);
  163. if (tmp == NULL)
  164. return;
  165. ERR_add_error_data(2, separator, tmp);
  166. OPENSSL_free(tmp);
  167. }
  168. put_error(ERR_LIB_CMP, func, err, file, line);
  169. txt = curr;
  170. } else {
  171. ERR_add_error_data(2, separator, txt);
  172. txt = next; /* finished */
  173. }
  174. } while (*txt != '\0');
  175. }
  176. /* this is similar to ERR_print_errors_cb, but uses the CMP-specific cb type */
  177. void OSSL_CMP_print_errors_cb(OSSL_cmp_log_cb_t log_fn)
  178. {
  179. unsigned long err;
  180. char msg[ERR_print_errors_cb_LIMIT];
  181. const char *file = NULL, *func = NULL, *data = NULL;
  182. int line, flags;
  183. if (log_fn == NULL) {
  184. #ifndef OPENSSL_NO_STDIO
  185. ERR_print_errors_fp(stderr);
  186. #else
  187. /* CMPerr(0, CMP_R_NO_STDIO) makes no sense during error printing */
  188. #endif
  189. return;
  190. }
  191. while ((err = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
  192. char component[128];
  193. const char *func_ = func != NULL && *func != '\0' ? func : "<unknown>";
  194. if (!(flags & ERR_TXT_STRING))
  195. data = NULL;
  196. #ifdef OSSL_CMP_PRINT_LIBINFO
  197. BIO_snprintf(component, sizeof(component), "OpenSSL:%s:%s",
  198. ERR_lib_error_string(err), func_);
  199. #else
  200. BIO_snprintf(component, sizeof(component), "%s",func_);
  201. #endif
  202. BIO_snprintf(msg, sizeof(msg), "%s%s%s", ERR_reason_error_string(err),
  203. data == NULL ? "" : " : ", data == NULL ? "" : data);
  204. if (log_fn(component, file, line, OSSL_CMP_LOG_ERR, msg) <= 0)
  205. break; /* abort outputting the error report */
  206. }
  207. }
  208. /*
  209. * functions manipulating lists of certificates etc.
  210. * these functions could be generally useful.
  211. */
  212. int ossl_cmp_sk_X509_add1_cert(STACK_OF(X509) *sk, X509 *cert,
  213. int no_dup, int prepend)
  214. {
  215. if (sk == NULL) {
  216. CMPerr(0, CMP_R_NULL_ARGUMENT);
  217. return 0;
  218. }
  219. if (no_dup) {
  220. /*
  221. * not using sk_X509_set_cmp_func() and sk_X509_find()
  222. * because this re-orders the certs on the stack
  223. */
  224. int i;
  225. for (i = 0; i < sk_X509_num(sk); i++) {
  226. if (X509_cmp(sk_X509_value(sk, i), cert) == 0)
  227. return 1;
  228. }
  229. }
  230. if (!X509_up_ref(cert))
  231. return 0;
  232. if (!sk_X509_insert(sk, cert, prepend ? 0 : -1)) {
  233. X509_free(cert);
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. int ossl_cmp_sk_X509_add1_certs(STACK_OF(X509) *sk, STACK_OF(X509) *certs,
  239. int no_self_signed, int no_dups, int prepend)
  240. /* compiler would allow 'const' for the list of certs, yet they are up-ref'ed */
  241. {
  242. int i;
  243. if (sk == NULL) {
  244. CMPerr(0, CMP_R_NULL_ARGUMENT);
  245. return 0;
  246. }
  247. for (i = 0; i < sk_X509_num(certs); i++) { /* certs may be NULL */
  248. X509 *cert = sk_X509_value(certs, i);
  249. if (!no_self_signed || X509_check_issued(cert, cert) != X509_V_OK) {
  250. if (!ossl_cmp_sk_X509_add1_cert(sk, cert, no_dups, prepend))
  251. return 0;
  252. }
  253. }
  254. return 1;
  255. }
  256. int ossl_cmp_X509_STORE_add1_certs(X509_STORE *store, STACK_OF(X509) *certs,
  257. int only_self_signed)
  258. {
  259. int i;
  260. if (store == NULL) {
  261. CMPerr(0, CMP_R_NULL_ARGUMENT);
  262. return 0;
  263. }
  264. if (certs == NULL)
  265. return 1;
  266. for (i = 0; i < sk_X509_num(certs); i++) {
  267. X509 *cert = sk_X509_value(certs, i);
  268. if (!only_self_signed || X509_check_issued(cert, cert) == X509_V_OK)
  269. if (!X509_STORE_add_cert(store, cert)) /* ups cert ref counter */
  270. return 0;
  271. }
  272. return 1;
  273. }
  274. STACK_OF(X509) *ossl_cmp_X509_STORE_get1_certs(X509_STORE *store)
  275. {
  276. int i;
  277. STACK_OF(X509) *sk;
  278. STACK_OF(X509_OBJECT) *objs;
  279. if (store == NULL) {
  280. CMPerr(0, CMP_R_NULL_ARGUMENT);
  281. return 0;
  282. }
  283. if ((sk = sk_X509_new_null()) == NULL)
  284. return NULL;
  285. objs = X509_STORE_get0_objects(store);
  286. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  287. X509 *cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
  288. if (cert != NULL) {
  289. if (!sk_X509_push(sk, cert))
  290. goto err;
  291. if (!X509_up_ref(cert)) {
  292. (void)sk_X509_pop(sk);
  293. goto err;
  294. }
  295. }
  296. }
  297. return sk;
  298. err:
  299. sk_X509_pop_free(sk, X509_free);
  300. return NULL;
  301. }
  302. /*-
  303. * Builds up the certificate chain of certs as high up as possible using
  304. * the given list of certs containing all possible intermediate certificates and
  305. * optionally the (possible) trust anchor(s). See also ssl_add_cert_chain().
  306. *
  307. * Intended use of this function is to find all the certificates above the trust
  308. * anchor needed to verify an EE's own certificate. Those are supposed to be
  309. * included in the ExtraCerts field of every first sent message of a transaction
  310. * when MSG_SIG_ALG is utilized.
  311. *
  312. * NOTE: This allocates a stack and increments the reference count of each cert,
  313. * so when not needed any more the stack and all its elements should be freed.
  314. * NOTE: in case there is more than one possibility for the chain,
  315. * OpenSSL seems to take the first one, check X509_verify_cert() for details.
  316. *
  317. * returns a pointer to a stack of (up_ref'ed) X509 certificates containing:
  318. * - the EE certificate given in the function arguments (cert)
  319. * - all intermediate certificates up the chain toward the trust anchor
  320. * whereas the (self-signed) trust anchor is not included
  321. * returns NULL on error
  322. */
  323. STACK_OF(X509) *ossl_cmp_build_cert_chain(STACK_OF(X509) *certs, X509 *cert)
  324. {
  325. STACK_OF(X509) *chain = NULL, *result = NULL;
  326. X509_STORE *store = X509_STORE_new();
  327. X509_STORE_CTX *csc = NULL;
  328. if (certs == NULL || cert == NULL || store == NULL) {
  329. CMPerr(0, CMP_R_NULL_ARGUMENT);
  330. goto err;
  331. }
  332. csc = X509_STORE_CTX_new();
  333. if (csc == NULL)
  334. goto err;
  335. if (!ossl_cmp_X509_STORE_add1_certs(store, certs, 0)
  336. || !X509_STORE_CTX_init(csc, store, cert, NULL))
  337. goto err;
  338. (void)ERR_set_mark();
  339. /*
  340. * ignore return value as it would fail without trust anchor given in store
  341. */
  342. (void)X509_verify_cert(csc);
  343. /* don't leave any new errors in the queue */
  344. (void)ERR_pop_to_mark();
  345. chain = X509_STORE_CTX_get0_chain(csc);
  346. /* result list to store the up_ref'ed not self-signed certificates */
  347. if ((result = sk_X509_new_null()) == NULL)
  348. goto err;
  349. if (!ossl_cmp_sk_X509_add1_certs(result, chain, 1 /* no self-signed */,
  350. 1 /* no duplicates */, 0)) {
  351. sk_X509_free(result);
  352. result = NULL;
  353. }
  354. err:
  355. X509_STORE_free(store);
  356. X509_STORE_CTX_free(csc);
  357. return result;
  358. }
  359. int ossl_cmp_asn1_octet_string_set1(ASN1_OCTET_STRING **tgt,
  360. const ASN1_OCTET_STRING *src)
  361. {
  362. ASN1_OCTET_STRING *new;
  363. if (tgt == NULL) {
  364. CMPerr(0, CMP_R_NULL_ARGUMENT);
  365. return 0;
  366. }
  367. if (*tgt == src) /* self-assignment */
  368. return 1;
  369. if (src != NULL) {
  370. if ((new = ASN1_OCTET_STRING_dup(src)) == NULL)
  371. return 0;
  372. } else {
  373. new = NULL;
  374. }
  375. ASN1_OCTET_STRING_free(*tgt);
  376. *tgt = new;
  377. return 1;
  378. }
  379. int ossl_cmp_asn1_octet_string_set1_bytes(ASN1_OCTET_STRING **tgt,
  380. const unsigned char *bytes, int len)
  381. {
  382. ASN1_OCTET_STRING *new = NULL;
  383. if (tgt == NULL) {
  384. CMPerr(0, CMP_R_NULL_ARGUMENT);
  385. return 0;
  386. }
  387. if (bytes != NULL) {
  388. if ((new = ASN1_OCTET_STRING_new()) == NULL
  389. || !(ASN1_OCTET_STRING_set(new, bytes, len))) {
  390. ASN1_OCTET_STRING_free(new);
  391. return 0;
  392. }
  393. }
  394. ASN1_OCTET_STRING_free(*tgt);
  395. *tgt = new;
  396. return 1;
  397. }