xref: /openssl/crypto/cversion.c (revision f4c46745)
1 /*
2  * Copyright 1995-2024 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 #include <stdio.h>
11 #include <openssl/bio.h>
12 #include "internal/e_os.h"
13 #include "internal/cryptlib.h"
14 #include "internal/common.h"
15 #include "internal/thread_once.h"
16 
17 #include "buildinf.h"
18 
OpenSSL_version_num(void)19 unsigned long OpenSSL_version_num(void)
20 {
21     return OPENSSL_VERSION_NUMBER;
22 }
23 
OPENSSL_version_major(void)24 unsigned int OPENSSL_version_major(void)
25 {
26     return OPENSSL_VERSION_MAJOR;
27 }
28 
OPENSSL_version_minor(void)29 unsigned int OPENSSL_version_minor(void)
30 {
31     return OPENSSL_VERSION_MINOR;
32 }
33 
OPENSSL_version_patch(void)34 unsigned int OPENSSL_version_patch(void)
35 {
36     return OPENSSL_VERSION_PATCH;
37 }
38 
OPENSSL_version_pre_release(void)39 const char *OPENSSL_version_pre_release(void)
40 {
41     return OPENSSL_VERSION_PRE_RELEASE;
42 }
43 
OPENSSL_version_build_metadata(void)44 const char *OPENSSL_version_build_metadata(void)
45 {
46     return OPENSSL_VERSION_BUILD_METADATA;
47 }
48 
49 extern char ossl_cpu_info_str[];
50 
51 #if defined(_WIN32) && defined(OSSL_WINCTX)
52 /* size: MAX_PATH + sizeof("OPENSSLDIR: \"\"") */
53 static char openssldir[MAX_PATH + 15];
54 
55 /* size: MAX_PATH + sizeof("ENGINESDIR: \"\"") */
56 static char enginesdir[MAX_PATH + 15];
57 
58 /* size: MAX_PATH + sizeof("MODULESDIR: \"\"") */
59 static char modulesdir[MAX_PATH + 15];
60 
61 static CRYPTO_ONCE version_strings_once = CRYPTO_ONCE_STATIC_INIT;
62 
DEFINE_RUN_ONCE_STATIC(version_strings_setup)63 DEFINE_RUN_ONCE_STATIC(version_strings_setup)
64 {
65     BIO_snprintf(openssldir, sizeof(openssldir), "OPENSSLDIR: \"%s\"",
66                  ossl_get_openssldir());
67     BIO_snprintf(enginesdir, sizeof(enginesdir), "ENGINESDIR: \"%s\"",
68                  ossl_get_enginesdir());
69     BIO_snprintf(modulesdir, sizeof(modulesdir), "MODULESDIR: \"%s\"",
70                  ossl_get_modulesdir());
71     return 1;
72 }
73 
74 # define TOSTR(x) #x
75 # define OSSL_WINCTX_STRING "OSSL_WINCTX: \"" ## TOSTR(OSSL_WINCTX) ## "\""
76 
77 #endif
78 
OpenSSL_version(int t)79 const char *OpenSSL_version(int t)
80 {
81 #if defined(_WIN32) && defined(OSSL_WINCTX)
82     /* Cannot really fail but we would return empty strings anyway */
83     (void)RUN_ONCE(&version_strings_once, version_strings_setup);
84 #endif
85 
86     switch (t) {
87     case OPENSSL_VERSION:
88         return OPENSSL_VERSION_TEXT;
89     case OPENSSL_VERSION_STRING:
90         return OPENSSL_VERSION_STR;
91     case OPENSSL_FULL_VERSION_STRING:
92         return OPENSSL_FULL_VERSION_STR;
93     case OPENSSL_BUILT_ON:
94         return DATE;
95     case OPENSSL_CFLAGS:
96         return compiler_flags;
97     case OPENSSL_PLATFORM:
98         return PLATFORM;
99 #if defined(_WIN32) && defined(OSSL_WINCTX)
100     case OPENSSL_DIR:
101         return openssldir;
102     case OPENSSL_ENGINES_DIR:
103         return enginesdir;
104     case OPENSSL_MODULES_DIR:
105         return modulesdir;
106 #else
107     case OPENSSL_DIR:
108 # ifdef OPENSSLDIR
109         return "OPENSSLDIR: \"" OPENSSLDIR "\"";
110 # else
111         return "OPENSSLDIR: N/A";
112 # endif
113     case OPENSSL_ENGINES_DIR:
114 # ifdef ENGINESDIR
115         return "ENGINESDIR: \"" ENGINESDIR "\"";
116 # else
117         return "ENGINESDIR: N/A";
118 # endif
119     case OPENSSL_MODULES_DIR:
120 # ifdef MODULESDIR
121         return "MODULESDIR: \"" MODULESDIR "\"";
122 # else
123         return "MODULESDIR: N/A";
124 # endif
125 #endif
126     case OPENSSL_CPU_INFO:
127         if (OPENSSL_info(OPENSSL_INFO_CPU_SETTINGS) != NULL)
128             return ossl_cpu_info_str;
129         else
130             return "CPUINFO: N/A";
131     case OPENSSL_WINCTX:
132 #if defined(_WIN32) && defined(OSSL_WINCTX)
133         return OSSL_WINCTX_STRING;
134 #else
135         return "OSSL_WINCTX: Undefined";
136 #endif
137     }
138     return "not available";
139 }
140