]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/provider.c
2 * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "../testutil.h"
12 #include <openssl/provider.h>
13 #include <openssl/core_names.h>
16 int test_get_libctx(OSSL_LIB_CTX
**libctx
, OSSL_PROVIDER
**default_null_prov
,
17 const char *config_file
,
18 OSSL_PROVIDER
**provider
, const char *module_name
)
20 OSSL_LIB_CTX
*new_libctx
= NULL
;
23 if ((new_libctx
= *libctx
= OSSL_LIB_CTX_new()) == NULL
) {
24 opt_printf_stderr("Failed to create libctx\n");
29 if (default_null_prov
!= NULL
30 && (*default_null_prov
= OSSL_PROVIDER_load(NULL
, "null")) == NULL
) {
31 opt_printf_stderr("Failed to load null provider into default libctx\n");
35 if (config_file
!= NULL
36 && !OSSL_LIB_CTX_load_config(new_libctx
, config_file
)) {
37 opt_printf_stderr("Error loading config from file %s\n", config_file
);
41 if (module_name
!= NULL
42 && (*provider
= OSSL_PROVIDER_load(new_libctx
, module_name
)) == NULL
) {
43 opt_printf_stderr("Failed to load provider %s\n", module_name
);
49 ERR_print_errors_fp(stderr
);
53 int test_arg_libctx(OSSL_LIB_CTX
**libctx
, OSSL_PROVIDER
**default_null_prov
,
54 OSSL_PROVIDER
**provider
, int argn
, const char *usage
)
56 const char *module_name
;
58 if (!TEST_ptr(module_name
= test_get_argument(argn
))) {
59 TEST_error("usage: <prog> %s", usage
);
62 if (strcmp(module_name
, "none") == 0)
64 return test_get_libctx(libctx
, default_null_prov
,
65 test_get_argument(argn
+ 1), provider
, module_name
);
69 int major
, minor
, patch
;
73 * Query the FIPS provider to determine it's version number.
74 * Returns 1 if the version is retrieved correctly, 0 if the FIPS provider isn't
75 * loaded and -1 on error.
77 static int fips_provider_version(OSSL_LIB_CTX
*libctx
, FIPS_VERSION
*vers
)
79 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
80 OSSL_PROVIDER
*fips_prov
;
83 if (!OSSL_PROVIDER_available(libctx
, "fips"))
85 *params
= OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION
, &vs
, 0);
86 if ((fips_prov
= OSSL_PROVIDER_load(libctx
, "fips")) == NULL
)
88 if (!OSSL_PROVIDER_get_params(fips_prov
, params
)
89 || sscanf(vs
, "%d.%d.%d", &vers
->major
, &vers
->minor
, &vers
->patch
) != 3)
91 if (!OSSL_PROVIDER_unload(fips_prov
))
95 OSSL_PROVIDER_unload(fips_prov
);
99 int fips_provider_version_eq(OSSL_LIB_CTX
*libctx
, int major
, int minor
, int patch
)
104 if ((res
= fips_provider_version(libctx
, &prov
)) <= 0)
106 return major
== prov
.major
&& minor
== prov
.minor
&& patch
== prov
.patch
;
109 int fips_provider_version_ne(OSSL_LIB_CTX
*libctx
, int major
, int minor
, int patch
)
114 if ((res
= fips_provider_version(libctx
, &prov
)) <= 0)
116 return major
!= prov
.major
|| minor
!= prov
.minor
|| patch
!= prov
.patch
;
119 int fips_provider_version_le(OSSL_LIB_CTX
*libctx
, int major
, int minor
, int patch
)
124 if ((res
= fips_provider_version(libctx
, &prov
)) <= 0)
126 return prov
.major
< major
127 || (prov
.major
== major
128 && (prov
.minor
< minor
129 || (prov
.minor
== minor
&& prov
.patch
<= patch
)));
132 int fips_provider_version_gt(OSSL_LIB_CTX
*libctx
, int major
, int minor
, int patch
)
137 if ((res
= fips_provider_version(libctx
, &prov
)) <= 0)
139 return prov
.major
> major
140 || (prov
.major
== major
141 && (prov
.minor
> minor
142 || (prov
.minor
== minor
&& prov
.patch
> patch
)));
145 int fips_provider_version_match(OSSL_LIB_CTX
*libctx
, const char *versions
)
148 int major
, minor
, patch
, r
;
150 MODE_EQ
, MODE_NE
, MODE_LE
, MODE_GT
153 while (*versions
!= '\0') {
154 for (; isspace(*versions
); versions
++)
156 if (*versions
== '\0')
158 for (p
= versions
; *versions
!= '\0' && !isspace(*versions
); versions
++)
163 } else if (*p
== '=') {
166 } else if (*p
== '<' && p
[1] == '=') {
169 } else if (*p
== '>') {
172 } else if (isdigit(*p
)) {
175 TEST_info("Error matching FIPS version: mode %s\n", p
);
178 if (sscanf(p
, "%d.%d.%d", &major
, &minor
, &patch
) != 3) {
179 TEST_info("Error matching FIPS version: version %s\n", p
);
184 r
= fips_provider_version_eq(libctx
, major
, minor
, patch
);
187 r
= fips_provider_version_ne(libctx
, major
, minor
, patch
);
190 r
= fips_provider_version_le(libctx
, major
, minor
, patch
);
193 r
= fips_provider_version_gt(libctx
, major
, minor
, patch
);
197 TEST_info("Error matching FIPS version: internal error\n");