1 /**
2  * Module to deal with the library version being used
3  *
4  * This library provide bindings for a wide range of OpenSSL versions,
5  * ranging from v0.9.x to v3.0.x. Some versions are not compatible with
6  * one another, either due to different ABI or different behavior,
7  * for example OpenSSL 1.0 requires initialization but later versions do not.
8  *
9  * While things tend to mostly work or error out while linking when the version
10  * the bindings assume and the actually C library version are too different,
11  * we prefer to try detecting the currently used version, and allow users
12  * to specify the version explicitly, before falling back to the latest bindings
13  */
14 module deimos.openssl.opensslv;
15 
16 import deimos.openssl._d_util;
17 
18 version (DeimosOpenSSL_1_0_0)
19 {
20     // https://www.openssl.org/news/changelog.html#openssl-100
21     // OpenSSL 1.0.0t was released 2015-12-03
22     public alias OpenSSLVersion = OpenSSLVersionTemplate!"1.0.0t";
23 }
24 else version (DeimosOpenSSL_1_0_1)
25 {
26     // https://www.openssl.org/news/changelog.html#openssl-101
27     // OpenSSL 1.0.1u was released 2016-09-22
28     public alias OpenSSLVersion = OpenSSLVersionTemplate!"1.0.1u";
29 }
30 else version (DeimosOpenSSL_1_0_2)
31 {
32     // https://www.openssl.org/news/changelog.html#openssl-102
33     // OpenSSL 1.0.2t was released 2019-09-10
34     public alias OpenSSLVersion = OpenSSLVersionTemplate!"1.0.2t";
35 }
36 else version (DeimosOpenSSL_1_1_0)
37 {
38     // https://www.openssl.org/news/changelog.html#openssl-110
39     // OpenSSL 1.1.0l was released 2019-09-10
40     public alias OpenSSLVersion = OpenSSLVersionTemplate!"1.1.0l";
41 }
42 else version (DeimosOpenSSL_1_1_1)
43 {
44     // https://www.openssl.org/news/changelog.html#openssl-111
45     // OpenSSL 1.1.1m was released 2021-12-14
46     public alias OpenSSLVersion = OpenSSLVersionTemplate!"1.1.1m";
47 }
48 else version (DeimosOpenSSL_3_0)
49 {
50     // https://www.openssl.org/news/changelog.html#openssl-30
51     // OpenSSL 3.0.3 was released 2022-05-03
52     public alias OpenSSLVersion = OpenSSLVersionTemplate!"3.0.3";
53 }
54 else version (DeimosOpenSSLAutoDetect)
55 {
56     import deimos.openssl.version_;
57 
58     public alias OpenSSLVersion = OpenSSLVersionTemplate!OpenSSLTextVersion;
59 }
60 else
61 {
62     // It was decided in https://github.com/D-Programming-Deimos/openssl/pull/66
63     // that we should fall back to the latest supported version of the bindings,
64     // should the user provide neither explicit version nor `DeimosOpenSSLAutoDetect`
65     public alias OpenSSLVersion = OpenSSLVersionTemplate!"1.1.0h";
66 }
67 
68 // Publicly aliased above
69 private struct OpenSSLVersionTemplate (string textVersion)
70 {
71     enum text = textVersion;
72 
73     enum int major = (text[0] - '0');
74     static assert (major >= 0);
75 
76     enum int minor = (text[2] - '0');
77     static assert (minor >= 0);
78 
79     enum int patch = (text[4] - '0');
80     static assert (patch >= 0);
81 
82     static if (text.length == "1.1.0h".length)
83     {
84         enum int build = (text[5] - '`');
85         static assert (build >= 0);
86     }
87     else
88         enum int build = 0;
89 }
90 
91 /* Numeric release version identifier:
92  * MNNFFPPS: major minor fix patch status
93  * The status nibble has one of the values 0 for development, 1 to e for betas
94  * 1 to 14, and f for release.  The patch level is exactly that.
95  * For example:
96  * 0.9.3-dev	  0x00903000
97  * 0.9.3-beta1	  0x00903001
98  * 0.9.3-beta2-dev 0x00903002
99  * 0.9.3-beta2    0x00903002 (same as ...beta2-dev)
100  * 0.9.3	  0x0090300f
101  * 0.9.3a	  0x0090301f
102  * 0.9.4 	  0x0090400f
103  * 1.2.3z	  0x102031af
104  *
105  * For continuity reasons (because 0.9.5 is already out, and is coded
106  * 0x00905100), between 0.9.5 and 0.9.6 the coding of the patch level
107  * part is slightly different, by setting the highest bit.  This means
108  * that 0.9.5a looks like this: 0x0090581f.  At 0.9.6, we can start
109  * with 0x0090600S...
110  *
111  * (Prior to 0.9.3-dev a different scheme was used: 0.9.2b is 0x0922.)
112  * (Prior to 0.9.5a beta1, a different scheme was used: MMNNFFRBB for
113  * major minor fix final patch/beta)
114  */
115 
116 /* Version macros for compile-time API version detection */
117 enum OPENSSL_VERSION_MAJOR   = OpenSSLVersion.major;
118 
119 enum OPENSSL_VERSION_MINOR   = OpenSSLVersion.minor;
120 
121 enum OPENSSL_VERSION_PATCH   = OpenSSLVersion.patch;
122 
123 enum OPENSSL_VERSION_BUILD   = OpenSSLVersion.build;
124 
125 int OPENSSL_MAKE_VERSION(int major, int minor, int patch, int build)
126 {
127     return (major << 28) | (minor << 20) | (patch << 12) | (build << 4) | 0xf;
128 }
129 
130 enum OPENSSL_VERSION_NUMBER =
131     OPENSSL_MAKE_VERSION(OpenSSLVersion.major, OpenSSLVersion.minor,
132                          OpenSSLVersion.patch, OpenSSLVersion.build);
133 
134 bool OPENSSL_VERSION_AT_LEAST(int major, int minor, int patch = 0, int build = 0)
135 {
136     return OPENSSL_VERSION_NUMBER >= OPENSSL_MAKE_VERSION(major, minor, patch, build);
137 }
138 
139 bool OPENSSL_VERSION_BEFORE(int major, int minor, int patch = 0, int build = 0)
140 {
141     return OPENSSL_VERSION_NUMBER < OPENSSL_MAKE_VERSION(major, minor, patch, build);
142 }
143 
144 /* The macros below are to be used for shared library (.so, .dll, ...)
145  * versioning.  That kind of versioning works a bit differently between
146  * operating systems.  The most usual scheme is to set a major and a minor
147  * number, and have the runtime loader check that the major number is equal
148  * to what it was at application link time, while the minor number has to
149  * be greater or equal to what it was at application link time.  With this
150  * scheme, the version number is usually part of the file name, like this:
151  *
152  *	libcrypto.so.0.9
153  *
154  * Some unixen also make a softlink with the major version number only:
155  *
156  *	libcrypto.so.0
157  *
158  * On Tru64 and IRIX 6.x it works a little bit differently.  There, the
159  * shared library version is stored in the file, and is actually a series
160  * of versions, separated by colons.  The rightmost version present in the
161  * library when linking an application is stored in the application to be
162  * matched at run time.  When the application is run, a check is done to
163  * see if the library version stored in the application matches any of the
164  * versions in the version string of the library itself.
165  * This version string can be constructed in any way, depending on what
166  * kind of matching is desired.  However, to implement the same scheme as
167  * the one used in the other unixen, all compatible versions, from lowest
168  * to highest, should be part of the string.  Consecutive builds would
169  * give the following versions strings:
170  *
171  *	3.0
172  *	3.0:3.1
173  *	3.0:3.1:3.2
174  *	4.0
175  *	4.0:4.1
176  *
177  * Notice how version 4 is completely incompatible with version, and
178  * therefore give the breach you can see.
179  *
180  * There may be other schemes as well that I haven't yet discovered.
181  *
182  * So, here's the way it works here: first of all, the library version
183  * number doesn't need at all to match the overall OpenSSL version.
184  * However, it's nice and more understandable if it actually does.
185  * The current library version is stored in the macro SHLIB_VERSION_NUMBER,
186  * which is just a piece of text in the format "M.m.e" (Major, minor, edit).
187  * For the sake of Tru64, IRIX, and any other OS that behaves in similar ways,
188  * we need to keep a history of version numbers, which is done in the
189  * macro SHLIB_VERSION_HISTORY.  The numbers are separated by colons and
190  * should only keep the versions that are binary compatible with the current.
191  */
192 enum SHLIB_VERSION_HISTORY = "";
193 enum SHLIB_VERSION_NUMBER = "1.0.0";