common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_COMMON_H
  10. # define OSSL_INTERNAL_COMMON_H
  11. # pragma once
  12. # include <stdlib.h>
  13. # include <string.h>
  14. # include "openssl/configuration.h"
  15. # include "internal/e_os.h" /* ossl_inline in many files */
  16. # include "internal/nelem.h"
  17. #ifdef NDEBUG
  18. # define ossl_assert(x) ((x) != 0)
  19. #else
  20. __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
  21. const char *file, int line)
  22. {
  23. if (!expr)
  24. OPENSSL_die(exprstr, file, line);
  25. return expr;
  26. }
  27. # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
  28. __FILE__, __LINE__)
  29. #endif
  30. /* Check if |pre|, which must be a string literal, is a prefix of |str| */
  31. #define HAS_PREFIX(str, pre) (strncmp(str, pre "", sizeof(pre) - 1) == 0)
  32. /* As before, and if check succeeds, advance |str| past the prefix |pre| */
  33. #define CHECK_AND_SKIP_PREFIX(str, pre) \
  34. (HAS_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
  35. /* Check if the string literal |p| is a case-insensitive prefix of |s| */
  36. #define HAS_CASE_PREFIX(s, p) (OPENSSL_strncasecmp(s, p "", sizeof(p) - 1) == 0)
  37. /* As before, and if check succeeds, advance |str| past the prefix |pre| */
  38. #define CHECK_AND_SKIP_CASE_PREFIX(str, pre) \
  39. (HAS_CASE_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
  40. /* Check if the string literal |suffix| is a case-insensitive suffix of |str| */
  41. #define HAS_CASE_SUFFIX(str, suffix) (strlen(str) < sizeof(suffix) - 1 ? 0 : \
  42. OPENSSL_strcasecmp(str + strlen(str) - sizeof(suffix) + 1, suffix "") == 0)
  43. /*
  44. * Use this inside a union with the field that needs to be aligned to a
  45. * reasonable boundary for the platform. The most pessimistic alignment
  46. * of the listed types will be used by the compiler.
  47. */
  48. # define OSSL_UNION_ALIGN \
  49. double align; \
  50. ossl_uintmax_t align_int; \
  51. void *align_ptr
  52. # define OPENSSL_CONF "openssl.cnf"
  53. # ifndef OPENSSL_SYS_VMS
  54. # define X509_CERT_AREA OPENSSLDIR
  55. # define X509_CERT_DIR OPENSSLDIR "/certs"
  56. # define X509_CERT_FILE OPENSSLDIR "/cert.pem"
  57. # define X509_PRIVATE_DIR OPENSSLDIR "/private"
  58. # define CTLOG_FILE OPENSSLDIR "/ct_log_list.cnf"
  59. # else
  60. # define X509_CERT_AREA "OSSL$DATAROOT:[000000]"
  61. # define X509_CERT_DIR "OSSL$DATAROOT:[CERTS]"
  62. # define X509_CERT_FILE "OSSL$DATAROOT:[000000]cert.pem"
  63. # define X509_PRIVATE_DIR "OSSL$DATAROOT:[PRIVATE]"
  64. # define CTLOG_FILE "OSSL$DATAROOT:[000000]ct_log_list.cnf"
  65. # endif
  66. #ifndef OPENSSL_NO_WINSTORE
  67. # define X509_CERT_URI "org.openssl.winstore://"
  68. #else
  69. # define X509_CERT_URI ""
  70. #endif
  71. # define X509_CERT_URI_EVP "SSL_CERT_URI"
  72. # define X509_CERT_PATH_EVP "SSL_CERT_PATH"
  73. # define X509_CERT_DIR_EVP "SSL_CERT_DIR"
  74. # define X509_CERT_FILE_EVP "SSL_CERT_FILE"
  75. # define CTLOG_FILE_EVP "CTLOG_FILE"
  76. /* size of string representations */
  77. # define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
  78. # define HEX_SIZE(type) (sizeof(type)*2)
  79. static ossl_inline int ossl_ends_with_dirsep(const char *path)
  80. {
  81. if (*path != '\0')
  82. path += strlen(path) - 1;
  83. # if defined __VMS
  84. if (*path == ']' || *path == '>' || *path == ':')
  85. return 1;
  86. # elif defined _WIN32
  87. if (*path == '\\')
  88. return 1;
  89. # endif
  90. return *path == '/';
  91. }
  92. static ossl_inline int ossl_is_absolute_path(const char *path)
  93. {
  94. # if defined __VMS
  95. if (strchr(path, ':') != NULL
  96. || ((path[0] == '[' || path[0] == '<')
  97. && path[1] != '.' && path[1] != '-'
  98. && path[1] != ']' && path[1] != '>'))
  99. return 1;
  100. # elif defined _WIN32
  101. if (path[0] == '\\'
  102. || (path[0] != '\0' && path[1] == ':'))
  103. return 1;
  104. # endif
  105. return path[0] == '/';
  106. }
  107. #endif