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