| 1 |
/* ============================================================================= |
| 2 |
* Copyright (c) 2017-2021 Michael Baeuerle |
| 3 |
* Copyright (c) 2003 G.J. Andruk |
| 4 |
* |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 8 |
* a copy of this software and associated documentation files (the |
| 9 |
* "Software"), to deal in the Software without restriction, including |
| 10 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 11 |
* distribute, and/or sell copies of the Software, and to permit persons |
| 12 |
* to whom the Software is furnished to do so, provided that the above |
| 13 |
* copyright notice(s) and this permission notice appear in all copies of |
| 14 |
* the Software and that both the above copyright notice(s) and this |
| 15 |
* permission notice appear in supporting documentation. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT |
| 20 |
* OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 21 |
* HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY |
| 22 |
* SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER |
| 23 |
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 24 |
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 25 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 26 |
* |
| 27 |
* Except as contained in this notice, the name of a copyright holder |
| 28 |
* shall not be used in advertising or otherwise to promote the sale, use |
| 29 |
* or other dealings in this Software without prior written authorization |
| 30 |
* of the copyright holder. |
| 31 |
* |
| 32 |
* SPDX-License-Identifier: ICU |
| 33 |
*/ |
| 34 |
|
| 35 |
/* Test program for new API available since version 3 */ |
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include "canlock.h" |
| 41 |
|
| 42 |
|
| 43 |
#define BUFFSIZE 128 |
| 44 |
|
| 45 |
|
| 46 |
/* ========================================================================== */ |
| 47 |
/* Get <scheme> element */ |
| 48 |
|
| 49 |
static const char *check_scheme(cl_hash_version hash) |
| 50 |
{ |
| 51 |
const char *scheme; |
| 52 |
|
| 53 |
switch (hash) |
| 54 |
{ |
| 55 |
case CL_SHA1: |
| 56 |
scheme = "sha1"; |
| 57 |
break; |
| 58 |
case CL_SHA224: |
| 59 |
scheme = "sha224"; |
| 60 |
break; |
| 61 |
case CL_SHA256: |
| 62 |
scheme = "sha256"; |
| 63 |
break; |
| 64 |
case CL_SHA384: |
| 65 |
scheme = "sha384"; |
| 66 |
break; |
| 67 |
case CL_SHA512: |
| 68 |
scheme = "sha512"; |
| 69 |
break; |
| 70 |
default: |
| 71 |
/* Not supported */ |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
return scheme; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/* ========================================================================== */ |
| 80 |
/* Check whether Cancel-Key matches Cancel-Lock */ |
| 81 |
|
| 82 |
static int checker(cl_hash_version hash, char *key, char *lock) |
| 83 |
{ |
| 84 |
int res = -1; |
| 85 |
cl_hash_version hash_key, hash_lock; |
| 86 |
char *string_key, *string_lock; |
| 87 |
|
| 88 |
printf("%s\n%s,%s\n", "Check Cancel-Key,Cancel-Lock:", key, lock); |
| 89 |
|
| 90 |
hash_key = cl_split(key, &string_key); |
| 91 |
hash_lock = cl_split(lock, &string_lock); |
| 92 |
|
| 93 |
if (CL_INVALID != hash_key && CL_INVALID != hash_lock) |
| 94 |
{ |
| 95 |
/* Check whether <scheme> matches */ |
| 96 |
if (hash_key == hash_lock) |
| 97 |
{ |
| 98 |
if (0 == cl_verify(hash, string_key, string_lock)) |
| 99 |
{ |
| 100 |
printf("\nGOOD\n"); |
| 101 |
res = 0; |
| 102 |
} |
| 103 |
else |
| 104 |
printf("\nBAD\n"); |
| 105 |
} |
| 106 |
else |
| 107 |
printf("\nBAD: Scheme mismatch (%s/%s)\n", |
| 108 |
check_scheme(hash_key), check_scheme(hash_lock)); |
| 109 |
} |
| 110 |
else |
| 111 |
printf("\nBAD: Scheme not supported\n"); |
| 112 |
|
| 113 |
return res; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/* ========================================================================== */ |
| 118 |
/* Test program for new API from version 3 */ |
| 119 |
|
| 120 |
int main(void) |
| 121 |
{ |
| 122 |
char *c_key = NULL, *c_lock = NULL; |
| 123 |
const char sec[] = "ExampleSecret"; |
| 124 |
const char sec2[] = "AnotherSecret"; |
| 125 |
const char mid[] = "<[email protected]>"; |
| 126 |
const char uid[] = "JaneDoe"; |
| 127 |
char *uid_mid = NULL; |
| 128 |
int rv; |
| 129 |
int failed = 0; |
| 130 |
|
| 131 |
/* First test (SHA256 without UID) */ |
| 132 |
printf("Test 1 (SHA256 without UID)\n\n"); |
| 133 |
printf("Secret : %s\n", sec); |
| 134 |
printf("Message-ID: %s\n", mid); |
| 135 |
c_key = cl_get_key(CL_SHA256, (const unsigned char *) sec, strlen(sec), |
| 136 |
(const unsigned char *) mid, strlen(mid)); |
| 137 |
c_lock = cl_get_lock(CL_SHA256, (const unsigned char *) sec, strlen(sec), |
| 138 |
(const unsigned char *) mid, strlen(mid)); |
| 139 |
if (NULL == c_key || NULL == c_lock) |
| 140 |
{ |
| 141 |
printf("BAD: Return value NULL from API function\n"); |
| 142 |
failed = 1; |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
printf("\n"); |
| 147 |
printf("%s\n%s,%s\n\n", "Expected Cancel-Key,Cancel-Lock:", |
| 148 |
"sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA=", |
| 149 |
"sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc="); |
| 150 |
if (strcmp(c_key, "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA=")) |
| 151 |
{ |
| 152 |
printf("BAD: Cancel-Key not as expected (%s)\n", c_key); |
| 153 |
failed = 1; |
| 154 |
} |
| 155 |
else |
| 156 |
{ |
| 157 |
if (strcmp(c_lock, |
| 158 |
"sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc=")) |
| 159 |
{ |
| 160 |
printf("BAD: Cancel-Lock not as expected (%s)\n", c_lock); |
| 161 |
failed = 1; |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
rv = checker(CL_SHA256, c_key, c_lock); |
| 166 |
if (rv) failed = 1; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
free((void *) c_lock); |
| 171 |
free((void *) c_key); |
| 172 |
printf("\n----------------------------------------" |
| 173 |
"----------------------------------------\n\n"); |
| 174 |
|
| 175 |
/* Second test (SHA256 with UID) */ |
| 176 |
printf("Test 2 (SHA256 with UID)\n\n"); |
| 177 |
printf("Secret : %s\n", sec2); |
| 178 |
printf("User-ID : %s\n", uid); |
| 179 |
printf("Message-ID: %s\n", mid); |
| 180 |
uid_mid = (char *) malloc(strlen(uid) + strlen(mid) + (size_t) 1); |
| 181 |
if (NULL == uid_mid) |
| 182 |
{ |
| 183 |
printf("BAD: Out of memory\n"); |
| 184 |
failed = 1; |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
strcpy(uid_mid, uid); |
| 189 |
strcat(uid_mid, mid); |
| 190 |
c_key = cl_get_key(CL_SHA256, |
| 191 |
(const unsigned char *) sec2, strlen(sec2), |
| 192 |
(const unsigned char *) uid_mid, strlen(uid_mid)); |
| 193 |
c_lock = cl_get_lock(CL_SHA256, |
| 194 |
(const unsigned char *) sec2, strlen(sec2), |
| 195 |
(const unsigned char *) uid_mid, strlen(uid_mid)); |
| 196 |
free((void *) uid_mid); |
| 197 |
if (NULL == c_key || NULL == c_lock) |
| 198 |
{ |
| 199 |
printf("BAD: Return value NULL from API function\n"); |
| 200 |
failed = 1; |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
printf("\n"); |
| 205 |
printf("%s\n%s,%s\n\n", "Expected Cancel-Key,Cancel-Lock:", |
| 206 |
"sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys=", |
| 207 |
"sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE="); |
| 208 |
|
| 209 |
if (strcmp(c_key, |
| 210 |
"sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys=")) |
| 211 |
{ |
| 212 |
printf("BAD: Cancel-Key not as expected (%s)\n", c_key); |
| 213 |
failed = 1; |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
if (strcmp(c_lock, |
| 218 |
"sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE=")) |
| 219 |
{ |
| 220 |
printf("BAD: Cancel-Lock not as expected (%s)\n", c_lock); |
| 221 |
failed = 1; |
| 222 |
} |
| 223 |
else |
| 224 |
{ |
| 225 |
rv = checker(CL_SHA256, c_key, c_lock); |
| 226 |
if (rv) failed = 1; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
free((void *) c_lock); |
| 231 |
free((void *) c_key); |
| 232 |
} |
| 233 |
printf("\n----------------------------------------" |
| 234 |
"----------------------------------------\n\n"); |
| 235 |
|
| 236 |
/* Test 3 (Check SHA256) */ |
| 237 |
printf("Test 3 (Check SHA256)\n\n"); |
| 238 |
c_key = "shA256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8="; |
| 239 |
c_lock = "sHa256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo="; |
| 240 |
rv = checker(CL_SHA256, c_key, c_lock); |
| 241 |
if (rv) failed = 1; |
| 242 |
printf("\n----------------------------------------" |
| 243 |
"----------------------------------------\n\n"); |
| 244 |
|
| 245 |
/* Test 4 (Check SHA1) */ |
| 246 |
printf("Test 4 (Check SHA1)\n\n"); |
| 247 |
c_key = "ShA1:aaaBBBcccDDDeeeFFF"; |
| 248 |
c_lock = "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4="; |
| 249 |
rv = checker(CL_SHA1, c_key, c_lock); |
| 250 |
if (rv) failed = 1; |
| 251 |
printf("\n----------------------------------------" |
| 252 |
"----------------------------------------\n\n"); |
| 253 |
|
| 254 |
/* Test 5 (Check SHA1 with <clue-string> element) */ |
| 255 |
printf("Test 5 (Check SHA1 with <clue-string> element)\n\n"); |
| 256 |
#if 0 /* Skip because this requires V2 legacy API */ |
| 257 |
c_key = "ShA1:aaaBBBcccDDDeeeFFF:bN"; |
| 258 |
c_lock = "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4="; |
| 259 |
rv = checker(CL_SHA1, c_key, c_lock); |
| 260 |
if (rv) failed = 1; |
| 261 |
#endif |
| 262 |
printf("SKIP: <clue-string> is obsolete since >20 years\n"); |
| 263 |
printf("\n----------------------------------------" |
| 264 |
"----------------------------------------\n\n"); |
| 265 |
|
| 266 |
/* Test 6 (Check SHA512) */ |
| 267 |
printf("Test 6 (Check SHA512)\n\n"); |
| 268 |
c_key = "sha512:ryoikFW3wKefmYr+zDzKn16ngNf1eYbZ0DN+3yqCbkid3HxU5K99G7RcNEx1UxiL3ZQfwg1+TDhH96D+tCcXGQ=="; |
| 269 |
c_lock = "sha512:Hq6MQ2JMzGf56agcqYPEMnoWHbQMSAG0eE0ABHgktP8cKL6/A4bvydjUAa0h7sHUU8vdfWXK7eUYG/pnDxgitg=="; |
| 270 |
rv = checker(CL_SHA512, c_key, c_lock); |
| 271 |
if (rv) failed = 1; |
| 272 |
printf("\n----------------------------------------" |
| 273 |
"----------------------------------------\n\n"); |
| 274 |
|
| 275 |
/* Test 7 (Check SHA256 with wrong key) */ |
| 276 |
printf("Test 7 (Check SHA256 with wrong key)\n\n"); |
| 277 |
c_key = "shA256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK9="; |
| 278 |
c_lock = "sHa256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo="; |
| 279 |
rv = checker(CL_SHA256, c_key, c_lock); |
| 280 |
/* BAD is expected and the correct result for this test */ |
| 281 |
printf("(Note: BAD is expected and the correct result for this test)\n"); |
| 282 |
if (!rv) failed = 1; |
| 283 |
printf("\n----------------------------------------" |
| 284 |
"----------------------------------------\n\n"); |
| 285 |
|
| 286 |
/* Test 8 (Check with unknown <scheme>) */ |
| 287 |
printf("Test 8 (Check with unknown <scheme>)\n\n"); |
| 288 |
c_key = "shA257:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8="; |
| 289 |
c_lock = "sHa256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo="; |
| 290 |
rv = checker(CL_SHA256, c_key, c_lock); |
| 291 |
/* BAD is expected and the correct result for this test */ |
| 292 |
printf("(Note: BAD is expected and the correct result for this test)\n"); |
| 293 |
if (!rv) failed = 1; |
| 294 |
printf("\n----------------------------------------" |
| 295 |
"----------------------------------------\n\n"); |
| 296 |
|
| 297 |
/* Test 9 (Check with <scheme> mismatch) */ |
| 298 |
printf("Test 9 (Check with <scheme> mismatch)\n\n"); |
| 299 |
c_key = "shA256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8="; |
| 300 |
c_lock = "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4="; |
| 301 |
rv = checker(CL_SHA256, c_key, c_lock); |
| 302 |
/* BAD is expected and the correct result for this test */ |
| 303 |
printf("(Note: BAD is expected and the correct result for this test)\n"); |
| 304 |
if (!rv) failed = 1; |
| 305 |
printf("\n----------------------------------------" |
| 306 |
"----------------------------------------\n\n"); |
| 307 |
|
| 308 |
/* Check for success */ |
| 309 |
if (!failed) exit(EXIT_SUCCESS); |
| 310 |
exit(EXIT_FAILURE); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
/* EOF */ |