Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_parsing.c
Go to the documentation of this file.
1/*
2 * Copyright 2015-2018 Yubico AB
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 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "yubihsm.h"
18
19#ifdef NDEBUG
20#undef NDEBUG
21#endif
22#include <assert.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <string.h>
26
27static void test_domains1(void) {
28 struct {
29 const char *string;
31 } tests[] = {
32 {"1", 1}, {"1,2:3,4|5,6;7,8,9,10,11,12,13,14,15,16", 0xffff},
33 {"1,16", 0x8001}, {"16", 0x8000},
34 {"16,15", 0xc000}, {"1,0xf", 0x4001},
35 {"0x1,0x2", 3}, {"0x8888", 0x8888},
36 {"0", 0}, {"all", 0xffff},
37 {"2", 2}, {"2:4", 10},
38 };
39
40 for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
41 uint16_t d = 0;
42 assert(yh_string_to_domains(tests[i].string, &d) == YHR_SUCCESS);
43 assert(d == tests[i].domains);
44 }
45}
46
47static void test_domains2(void) {
48 struct {
50 const char *string;
51 } tests[] = {
52 {1, "1"},
53 {0x8001, "1:16"},
54 {0, ""},
55 {0xffff, "1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16"},
56 };
57
58 for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
59 char s[256];
60 assert(yh_domains_to_string(tests[i].domains, s, 255) == YHR_SUCCESS);
61 assert(strcmp(s, tests[i].string) == 0);
62 }
63}
64
65static void test_capabilities1(void) {
66 struct {
67 const char *string;
69 } tests[] = {
70 {"get-opaque", {"\x00\x00\x00\x00\x00\x00\x00\x01"}},
71 {"sign-hmac:verify-hmac|exportable-under-wrap,",
72 {"\x00\x00\x00\x00\x00\xc1\x00\x00"}},
73 {",,unwrap-data|:wrap-data,,,", {"\x00\x00\x00\x60\x00\x00\x00\x00"}},
74 {"0x7fffffffffffffff", {"\x7f\xff\xff\xff\xff\xff\xff\xff"}},
75 {"0xffffffffffffffff", {"\xff\xff\xff\xff\xff\xff\xff\xff"}},
76 };
77
78 for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
79 yh_capabilities c = {{0}};
80 assert(yh_string_to_capabilities(tests[i].string, &c) == YHR_SUCCESS);
81 assert(memcmp(&c, &tests[i].capabilities, sizeof(c)) == 0);
82 }
83}
84
85static void test_capabilities2(void) {
87
89 const char *capabilities_array[8];
90 size_t capabilities_array_len;
91 char *capabilities_list[] = {"sign-pkcs", "decrypt-pkcs",
92 "export-wrapped", "set-option",
93 "get-pseudo-random", "sign-hmac",
94 "verify-hmac", "get-log-entries"};
95 char capabilities_string[1024];
96
97 size_t len = 0;
98 for (size_t i = 0;
99 i < sizeof(capabilities_list) / sizeof(capabilities_list[0]); i++) {
100 sprintf(capabilities_string + len, "%s:", capabilities_list[i]);
101 len += strlen(capabilities_list[i]) + 1;
102 }
103 capabilities_string[len - 1] = '\0';
104
105 yrc = yh_string_to_capabilities(capabilities_string, &capabilities);
106 assert(yrc == YHR_SUCCESS);
107
108 assert(yh_check_capability(&capabilities, "something") == false);
109 assert(yh_check_capability(&capabilities, "sign-pss") == false);
110
111 assert(yh_check_capability(&capabilities, "sign-pkcs") == true);
112 assert(yh_check_capability(&capabilities, "decrypt-pkcs") == true);
113 assert(yh_check_capability(&capabilities, "export-wrapped") == true);
114 assert(yh_check_capability(&capabilities, "set-option") == true);
115 assert(yh_check_capability(&capabilities, "get-pseudo-random") == true);
116 assert(yh_check_capability(&capabilities, "sign-hmac") == true);
117 assert(yh_check_capability(&capabilities, "verify-hmac") == true);
118 assert(yh_check_capability(&capabilities, "get-log-entries") == true);
119 assert(yh_check_capability(&capabilities, "verify-hmac:get-log-entries") ==
120 true);
121
122 capabilities_array_len = 1;
123 yrc = yh_capabilities_to_strings(&capabilities, capabilities_array,
124 &capabilities_array_len);
125 assert(yrc == YHR_BUFFER_TOO_SMALL);
126
127 capabilities_array_len =
128 sizeof(capabilities_array) / sizeof(capabilities_array[0]);
129 yrc = yh_capabilities_to_strings(&capabilities, capabilities_array,
130 &capabilities_array_len);
131 assert(yrc == YHR_SUCCESS);
132 for (size_t i = 0;
133 i < sizeof(capabilities_list) / sizeof(capabilities_list[0]); i++) {
134 size_t j;
135 for (j = 0; j < capabilities_array_len; j++) {
136 if (strcmp(capabilities_list[i], capabilities_array[j]) == 0) {
137 break;
138 }
139 }
140 assert(j < capabilities_array_len);
141 }
142}
143
144static void test_capabilities3(void) {
145 const char *cap1 = "sign-pkcs,sign-pss";
146 const char *cap2 = "decrypt-pkcs,decrypt-oaep";
147 const char *cap3 = "sign-pss,decrypt-oaep";
148 yh_capabilities c1 = {{0}};
149 yh_capabilities c2 = {{0}};
150 yh_capabilities c3 = {{0}};
151 yh_capabilities res = {{0}};
152
153 assert(yh_string_to_capabilities(cap1, &c1) == YHR_SUCCESS);
154 assert(yh_string_to_capabilities(cap2, &c2) == YHR_SUCCESS);
155 assert(yh_string_to_capabilities(cap3, &c3) == YHR_SUCCESS);
156
157 assert(yh_merge_capabilities(&c1, &c2, &res) == YHR_SUCCESS);
158 assert(yh_check_capability(&res, "sign-pkcs") == true);
159 assert(yh_check_capability(&res, "sign-pss") == true);
160 assert(yh_check_capability(&res, "decrypt-pkcs") == true);
161 assert(yh_check_capability(&res, "decrypt-oaep") == true);
162 assert(yh_check_capability(&res, "sign-hmac") == false);
163
164 assert(yh_filter_capabilities(&res, &c3, &res) == YHR_SUCCESS);
165 assert(yh_check_capability(&res, "sign-pkcs") == false);
166 assert(yh_check_capability(&res, "sign-pss") == true);
167 assert(yh_check_capability(&res, "decrypt-pkcs") == false);
168 assert(yh_check_capability(&res, "decrypt-oaep") == true);
169 assert(yh_check_capability(&res, "sign-hmac") == false);
170}
171
172static void test_algorithms(void) {
173 yh_rc yrc;
174
175 assert(yh_is_hmac(YH_ALGO_RSA_2048) == false);
176 assert(yh_is_hmac(YH_ALGO_HMAC_SHA1) == true);
177 assert(yh_is_hmac(YH_ALGO_HMAC_SHA256) == true);
178 assert(yh_is_hmac(YH_ALGO_HMAC_SHA384) == true);
179 assert(yh_is_hmac(YH_ALGO_HMAC_SHA512) == true);
180
183 assert(yrc == YHR_INVALID_PARAMETERS);
184 yrc = yh_string_to_algo("something", NULL);
185 assert(yrc == YHR_INVALID_PARAMETERS);
186 yrc = yh_string_to_algo("something", &algorithm);
187 assert(yrc == YHR_INVALID_PARAMETERS);
188 yrc = yh_string_to_algo("rsa-pkcs1-sha1", &algorithm);
190 yrc = yh_string_to_algo("rsa2048", &algorithm);
191 assert(yrc == YHR_SUCCESS && algorithm == YH_ALGO_RSA_2048);
192 yrc = yh_string_to_algo("ecp384", &algorithm);
193 assert(yrc == YHR_SUCCESS && algorithm == YH_ALGO_EC_P384);
194 yrc = yh_string_to_algo("mgf1-sha512", &algorithm);
196}
197
198static void test_options(void) {
199 yh_rc yrc;
202 assert(yrc == YHR_INVALID_PARAMETERS);
203 yrc = yh_string_to_option("something", NULL);
204 assert(yrc == YHR_INVALID_PARAMETERS);
205 yrc = yh_string_to_option("something", &option);
206 assert(yrc == YHR_INVALID_PARAMETERS);
207 yrc = yh_string_to_option("force-audit", &option);
209}
210
211static void test_types(void) {
212 yh_rc yrc;
214 yrc = yh_string_to_type(NULL, &type);
215 assert(yrc == YHR_INVALID_PARAMETERS);
216 yrc = yh_string_to_type("something", NULL);
217 assert(yrc == YHR_INVALID_PARAMETERS);
218 yrc = yh_string_to_type("something", &type);
219 assert(yrc == YHR_INVALID_PARAMETERS);
220 yrc = yh_string_to_type("opaque", &type);
221 assert(yrc == YHR_SUCCESS && type == YH_OPAQUE);
222 yrc = yh_string_to_type("authentication-key", &type);
223 assert(yrc == YHR_SUCCESS && type == YH_AUTHENTICATION_KEY);
224 yrc = yh_string_to_type("asymmetric-key", &type);
225 assert(yrc == YHR_SUCCESS && type == YH_ASYMMETRIC_KEY);
226 yrc = yh_string_to_type("wrap-key", &type);
227 assert(yrc == YHR_SUCCESS && type == YH_WRAP_KEY);
228 yrc = yh_string_to_type("hmac-key", &type);
229 assert(yrc == YHR_SUCCESS && type == YH_HMAC_KEY);
230 yrc = yh_string_to_type("template", &type);
231 assert(yrc == YHR_SUCCESS && type == YH_TEMPLATE);
232 yrc = yh_string_to_type("otp-aead-key", &type);
233 assert(yrc == YHR_SUCCESS && type == YH_OTP_AEAD_KEY);
234
235 const char *string;
236 yrc = yh_type_to_string(0, NULL);
237 assert(yrc == YHR_INVALID_PARAMETERS);
238 yrc = yh_type_to_string(99, &string);
239 assert(yrc == YHR_SUCCESS && strcmp(string, "Unknown") == 0);
240 yrc = yh_type_to_string(YH_OPAQUE, &string);
241 assert(yrc == YHR_SUCCESS && strcmp(string, "opaque") == 0);
243 assert(yrc == YHR_SUCCESS && strcmp(string, "authentication-key") == 0);
245 assert(yrc == YHR_SUCCESS && strcmp(string, "asymmetric-key") == 0);
247 assert(yrc == YHR_SUCCESS && strcmp(string, "wrap-key") == 0);
249 assert(yrc == YHR_SUCCESS && strcmp(string, "hmac-key") == 0);
251 assert(yrc == YHR_SUCCESS && strcmp(string, "template") == 0);
253 assert(yrc == YHR_SUCCESS && strcmp(string, "otp-aead-key") == 0);
254}
255
256int main(void) {
257 yh_init();
258 test_domains1();
259 test_domains2();
260 test_capabilities1();
261 test_capabilities2();
262 test_capabilities3();
263 test_algorithms();
264 test_options();
265 test_types();
266}
unsigned short uint16_t
Definition stdint.h:125
Capabilities representation.
Definition yubihsm.h:162
std::tuple< uint64_t, std::array< char, 6733 > > test_types
int main(void)
yh_rc yh_domains_to_string(uint16_t domains, char *string, size_t max_len)
Definition yubihsm.c:4587
yh_rc yh_init(void)
Definition yubihsm.c:3857
yh_rc yh_string_to_option(const char *string, yh_option *option)
Definition yubihsm.c:4463
yh_rc yh_string_to_type(const char *string, yh_object_type *type)
Definition yubihsm.c:4442
yh_rc yh_filter_capabilities(const yh_capabilities *capabilities, const yh_capabilities *filter, yh_capabilities *result)
Definition yubihsm.c:4231
yh_rc yh_string_to_domains(const char *domains, uint16_t *result)
Definition yubihsm.c:4535
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
Definition yubihsm.c:4115
yh_rc yh_string_to_algo(const char *string, yh_algorithm *algo)
Definition yubihsm.c:4403
bool yh_check_capability(const yh_capabilities *capabilities, const char *capability)
Definition yubihsm.c:4198
yh_rc yh_capabilities_to_strings(const yh_capabilities *num, const char *result[], size_t *n_result)
Definition yubihsm.c:4168
bool yh_is_hmac(yh_algorithm algorithm)
Definition yubihsm.c:4293
yh_rc yh_merge_capabilities(const yh_capabilities *a, const yh_capabilities *b, yh_capabilities *result)
Definition yubihsm.c:4219
yh_rc yh_type_to_string(yh_object_type type, char const **result)
Definition yubihsm.c:4424
yh_option option
Definition yubihsm.h:685
yh_object_type type
Definition yubihsm.h:672
yh_object_type
Definition yubihsm.h:359
@ YH_OTP_AEAD_KEY
OTP AEAD Key is a secret key used to decrypt Yubico OTP values.
Definition yubihsm.h:376
@ YH_WRAP_KEY
Definition yubihsm.h:369
@ YH_OPAQUE
Definition yubihsm.h:362
@ YH_HMAC_KEY
HMAC Key is a secret key used when computing and verifying HMAC signatures.
Definition yubihsm.h:371
@ YH_TEMPLATE
Definition yubihsm.h:374
@ YH_ASYMMETRIC_KEY
Asymmetric Key is the private key of an asymmetric key-pair.
Definition yubihsm.h:366
@ YH_AUTHENTICATION_KEY
Authentication Key is used to establish Sessions with a device.
Definition yubihsm.h:364
yh_algorithm
Definition yubihsm.h:390
@ YH_ALGO_MGF1_SHA512
mgf1-sha512
Definition yubihsm.h:460
@ YH_ALGO_RSA_PKCS1_SHA1
rsa-pkcs1-sha1
Definition yubihsm.h:392
@ YH_ALGO_EC_P384
ecp384
Definition yubihsm.h:416
@ YH_ALGO_HMAC_SHA512
hmac-sha512
Definition yubihsm.h:434
@ YH_ALGO_HMAC_SHA384
hmac-sha384
Definition yubihsm.h:432
@ YH_ALGO_HMAC_SHA1
hmac-sha1
Definition yubihsm.h:428
@ YH_ALGO_RSA_2048
rsa2048
Definition yubihsm.h:408
@ YH_ALGO_HMAC_SHA256
hmac-sha256
Definition yubihsm.h:430
yh_algorithm algorithm
Definition yubihsm.h:619
yh_option
Definition yubihsm.h:490
@ YH_OPTION_FORCE_AUDIT
Enable/Disable Forced Audit mode.
Definition yubihsm.h:492
yh_rc
Definition yubihsm.h:170
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
Definition yubihsm.h:228
@ YHR_SUCCESS
Returned value when function was successful.
Definition yubihsm.h:172
@ YHR_INVALID_PARAMETERS
Returned value when an argument to a function is invalid.
Definition yubihsm.h:182
@ YHR_BUFFER_TOO_SMALL
Returned value when there is not enough space to store data.
Definition yubihsm.h:187
yh_capabilities capabilities
CK_ULONG d
char * s
uint16_t j
uint16_t domains
size_t len
yh_rc yrc