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