Debian Codegraph @

crt/s2n/tests/unit/s2n_build_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #ifndef _S2N_PRELUDE_INCLUDED
17 /* make sure s2n_prelude.h is includes as part of the compiler flags, if not then fail the build */
18 #error "Expected s2n_prelude.h to be included as part of the compiler flags"
19 #endif
20
21 #define _GNU_SOURCE
22 #include <ctype.h>
23 #include <openssl/crypto.h>
24 #include <openssl/opensslv.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "crypto/s2n_libcrypto.h"
30 #include "s2n_test.h"
31
32 #define MAX_LIBCRYPTO_NAME_LEN 100
33
34 int tokenize_s2n_libcrypto(char *s2n_libcrypto, char **name, char **version)
35 {
36 if (name == NULL || version == NULL || s2n_libcrypto == NULL) {
37 return S2N_FAILURE;
38 }
39
40 *name = strtok(s2n_libcrypto, "-");
41 if (*name == NULL) {
42 return S2N_FAILURE;
43 }
44
45 char *remaining = strtok(NULL, "");
46
47 *version = NULL;
48 if (remaining != NULL && isdigit(remaining[0])) {
49 *version = strtok(remaining, "_-");
50 }
51
52 return S2N_SUCCESS;
53 }
54
55 S2N_RESULT s2n_test_lowercase_copy(const char *input, char *destination, size_t max_len)
56 {
57 RESULT_ENSURE_REF(input);
58 RESULT_ENSURE_REF(destination);
59
60 for (size_t i = 0; i < strlen(input); i++) {
61 RESULT_ENSURE_LT(i, max_len);
62 destination[i] = tolower(input[i]);
63 }
64
65 return S2N_RESULT_OK;
66 }
67
68 S2N_RESULT s2n_check_supported_libcrypto(const char *s2n_libcrypto)
69 {
70 RESULT_ENSURE_REF(s2n_libcrypto);
71
72 /* List of supported libcrypto variants we test with */
73 const struct {
74 const char *libcrypto;
75 bool is_openssl;
76 bool is_opensslfips;
77 } supported_libcrypto[] = {
78 { .libcrypto = "awslc", .is_openssl = false },
79 { .libcrypto = "awslc-fips", .is_openssl = false },
80 { .libcrypto = "awslc-fips-2022", .is_openssl = false },
81 { .libcrypto = "awslc-fips-2024", .is_openssl = false },
82 { .libcrypto = "awslc-fips-next", .is_openssl = false },
83 { .libcrypto = "boringssl", .is_openssl = false },
84 { .libcrypto = "libressl", .is_openssl = false },
85 { .libcrypto = "openssl-1.0.2", .is_openssl = true },
86 { .libcrypto = "openssl-1.0.2-fips", .is_openssl = true },
87 { .libcrypto = "openssl-1.1.1", .is_openssl = true },
88 { .libcrypto = "openssl-3.0", .is_openssl = true },
89 { .libcrypto = "openssl-3.0-fips", .is_openssl = true, .is_opensslfips = true },
90 };
91
92 for (size_t i = 0; i < s2n_array_len(supported_libcrypto); i++) {
93 if (strcmp(s2n_libcrypto, supported_libcrypto[i].libcrypto) != 0) {
94 continue;
95 }
96 EXPECT_EQUAL(s2n_libcrypto_is_openssl(), supported_libcrypto[i].is_openssl);
97 EXPECT_EQUAL(s2n_libcrypto_is_openssl_fips(), supported_libcrypto[i].is_opensslfips);
98 return S2N_RESULT_OK;
99 }
100
101 /* Fail if no matching libcrypto was found.
102 * Since we check for openssl via process of elimination, we must know
103 * and test the full list of non-openssl libcryptos.
104 */
105 FAIL_MSG("Unknown libcrypto - update supported_libcrypto list");
106 }
107
108 int main()
109 {
110 BEGIN_TEST();
111
112 const char *s2n_libcrypto = getenv("S2N_LIBCRYPTO");
113
114 /* S2N_LIBCRYPTO and S2N_BUILD_PRESET should be consistent, but only
115 if S2N_BUILD_PRESET is set. */
116 {
117 const char *s2n_build_preset = getenv("S2N_BUILD_PRESET");
118 if (s2n_build_preset != NULL) {
119 EXPECT_NOT_NULL(s2n_libcrypto);
120 EXPECT_NOT_NULL(strstr(s2n_build_preset, s2n_libcrypto));
121 }
122 };
123
124 if (s2n_libcrypto == NULL || s2n_libcrypto[0] == '\0') {
125 END_TEST();
126 }
127
128 if (strcmp(s2n_libcrypto, "default") == 0) {
129 END_TEST();
130 }
131
132 /* Ensure that FIPS mode is enabled only when linked to a fips library */
133 s2n_fips_mode fips_mode = S2N_FIPS_MODE_DISABLED;
134 EXPECT_SUCCESS(s2n_get_fips_mode(&fips_mode));
135 if (strstr(s2n_libcrypto, "fips") && !strstr(s2n_libcrypto, "1.0.2")) {
136 EXPECT_EQUAL(fips_mode, S2N_FIPS_MODE_ENABLED);
137 } else {
138 EXPECT_EQUAL(fips_mode, S2N_FIPS_MODE_DISABLED);
139 }
140
141 char s2n_libcrypto_copy[MAX_LIBCRYPTO_NAME_LEN] = { 0 };
142 EXPECT_TRUE(strlen(s2n_libcrypto) < MAX_LIBCRYPTO_NAME_LEN);
143 EXPECT_OK(s2n_test_lowercase_copy(s2n_libcrypto, &s2n_libcrypto_copy[0], s2n_array_len(s2n_libcrypto_copy)));
144 char *name = NULL;
145 char *version = NULL;
146 EXPECT_SUCCESS(tokenize_s2n_libcrypto(s2n_libcrypto_copy, &name, &version));
147
148 /* Check libcrypto name matches the intent of the CI. */
149 {
150 if (strstr(name, "awslc") != NULL) {
151 /* Early versions of awslc's SSLeay_version return an inaccurate value left over
152 * after its fork from BoringSSL.
153 */
154 EXPECT_TRUE(s2n_libcrypto_is_awslc());
155 } else {
156 /* Any other library should have the name of the library (modulo case) in its version string. */
157 char ssleay_version_text[MAX_LIBCRYPTO_NAME_LEN] = { 0 };
158 EXPECT_OK(s2n_test_lowercase_copy(SSLeay_version(SSLEAY_VERSION), &ssleay_version_text[0], MAX_LIBCRYPTO_NAME_LEN));
159 EXPECT_NOT_NULL(strstr(ssleay_version_text, name));
160 }
161 };
162
163 /* Check libcrypto version matches the intent of the CI. */
164 {
165 if (version != NULL) {
166 const char *ssleay_version_text = SSLeay_version(SSLEAY_VERSION);
167 if (strstr(ssleay_version_text, version) == NULL) {
168 char fail_msg[256] = { 0 };
169 snprintf(
170 fail_msg, sizeof(fail_msg),
171 "Libcrypto version mismatch: expected version '%s' "
172 "not found in actual libcrypto version string '%s'",
173 version, ssleay_version_text);
174 FAIL_MSG(fail_msg);
175 }
176 }
177 };
178
179 /* Ensure we are testing with supported libcryto variants.
180 *
181 * We need to update s2n_libcrypto_is_openssl() when adding support
182 * for a new libcrypto.
183 */
184 {
185 EXPECT_OK(s2n_check_supported_libcrypto(s2n_libcrypto));
186 };
187
188 END_TEST();
189 }