| 1 |
/* Copyright (c) 2014, Google Inc. |
| 2 |
* |
| 3 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
* purpose with or without fee is hereby granted, provided that the above |
| 5 |
* copyright notice and this permission notice appear in all copies. |
| 6 |
* |
| 7 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 |
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 |
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include <string> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#include <openssl/base64.h> |
| 22 |
#include <openssl/crypto.h> |
| 23 |
#include <openssl/err.h> |
| 24 |
|
| 25 |
|
| 26 |
enum encoding_relation { |
| 27 |
// canonical indicates that the encoding is the expected encoding of the |
| 28 |
// input. |
| 29 |
canonical, |
| 30 |
// valid indicates that the encoding is /a/ valid encoding of the input, but |
| 31 |
// need not be the canonical one. |
| 32 |
valid, |
| 33 |
// invalid indicates that the encoded data is valid. |
| 34 |
invalid, |
| 35 |
}; |
| 36 |
|
| 37 |
struct TestVector { |
| 38 |
enum encoding_relation relation; |
| 39 |
const char *decoded; |
| 40 |
const char *encoded; |
| 41 |
}; |
| 42 |
|
| 43 |
// Test vectors from RFC 4648. |
| 44 |
static const TestVector kTestVectors[] = { |
| 45 |
{canonical, "", ""}, |
| 46 |
{canonical, "f", "Zg==\n"}, |
| 47 |
{canonical, "fo", "Zm8=\n"}, |
| 48 |
{canonical, "foo", "Zm9v\n"}, |
| 49 |
{canonical, "foob", "Zm9vYg==\n"}, |
| 50 |
{canonical, "fooba", "Zm9vYmE=\n"}, |
| 51 |
{canonical, "foobar", "Zm9vYmFy\n"}, |
| 52 |
{valid, "foobar", "Zm9vYmFy\n\n"}, |
| 53 |
{valid, "foobar", " Zm9vYmFy\n\n"}, |
| 54 |
{valid, "foobar", " Z m 9 v Y m F y\n\n"}, |
| 55 |
{invalid, "", "Zm9vYmFy=\n"}, |
| 56 |
{invalid, "", "Zm9vYmFy==\n"}, |
| 57 |
{invalid, "", "Zm9vYmFy===\n"}, |
| 58 |
{invalid, "", "Z"}, |
| 59 |
{invalid, "", "Z\n"}, |
| 60 |
{invalid, "", "ab!c"}, |
| 61 |
{invalid, "", "ab=c"}, |
| 62 |
{invalid, "", "abc"}, |
| 63 |
|
| 64 |
{canonical, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 65 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA==\n"}, |
| 66 |
{valid, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 67 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA\n==\n"}, |
| 68 |
{valid, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 69 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA=\n=\n"}, |
| 70 |
{invalid, "", |
| 71 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA=\n==\n"}, |
| 72 |
{canonical, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 73 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4\neHh4eHh" |
| 74 |
"4eHh4eHh4\n"}, |
| 75 |
{canonical, |
| 76 |
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 77 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4\neHh4eHh" |
| 78 |
"4eHh4eHh4eHh4eA==\n"}, |
| 79 |
{valid, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 80 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh\n4eHh4eHh" |
| 81 |
"4eHh4eHh4eHh4eA==\n"}, |
| 82 |
{valid, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 83 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4e" |
| 84 |
"Hh4eHh4eHh4eA==\n"}, |
| 85 |
{invalid, "", |
| 86 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA==" |
| 87 |
"\neHh4eHh4eHh4eHh4eHh4eHh4\n"}, |
| 88 |
|
| 89 |
// A '-' has traditionally been treated as the end of the data by OpenSSL |
| 90 |
// and anything following would be ignored. BoringSSL does not accept this |
| 91 |
// non-standard extension. |
| 92 |
{invalid, "", "Zm9vYmFy-anythinggoes"}, |
| 93 |
{invalid, "", "Zm9vYmFy\n-anythinggoes"}, |
| 94 |
|
| 95 |
// CVE-2015-0292 |
| 96 |
{invalid, "", |
| 97 |
"ZW5jb2RlIG1lCg===========================================================" |
| 98 |
"=======\n"}, |
| 99 |
}; |
| 100 |
|
| 101 |
static const size_t kNumTests = sizeof(kTestVectors) / sizeof(kTestVectors[0]); |
| 102 |
|
| 103 |
// RemoveNewlines returns a copy of |in| with all '\n' characters removed. |
| 104 |
static std::string RemoveNewlines(const char *in) { |
| 105 |
std::string ret; |
| 106 |
const size_t in_len = strlen(in); |
| 107 |
|
| 108 |
size_t i; |
| 109 |
for (i = 0; i < in_len; i++) { |
| 110 |
if (in[i] != '\n') { |
| 111 |
ret.push_back(in[i]); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
static bool TestEncodeBlock() { |
| 119 |
for (unsigned i = 0; i < kNumTests; i++) { |
| 120 |
const TestVector *t = &kTestVectors[i]; |
| 121 |
if (t->relation != canonical) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
const size_t decoded_len = strlen(t->decoded); |
| 126 |
size_t max_encoded_len; |
| 127 |
if (!EVP_EncodedLength(&max_encoded_len, decoded_len)) { |
| 128 |
fprintf(stderr, "#%u: EVP_EncodedLength failed\n", i); |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
std::vector<uint8_t> out_vec(max_encoded_len); |
| 133 |
uint8_t *out = out_vec.data(); |
| 134 |
size_t len = EVP_EncodeBlock(out, (const uint8_t *)t->decoded, decoded_len); |
| 135 |
|
| 136 |
std::string encoded(RemoveNewlines(t->encoded)); |
| 137 |
if (len != encoded.size() || |
| 138 |
memcmp(out, encoded.data(), len) != 0) { |
| 139 |
fprintf(stderr, "encode(\"%s\") = \"%.*s\", want \"%s\"\n", |
| 140 |
t->decoded, (int)len, (const char*)out, encoded.c_str()); |
| 141 |
return false; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
static bool TestDecodeBase64() { |
| 149 |
size_t len; |
| 150 |
|
| 151 |
for (unsigned i = 0; i < kNumTests; i++) { |
| 152 |
const TestVector *t = &kTestVectors[i]; |
| 153 |
|
| 154 |
if (t->relation == valid) { |
| 155 |
// The non-canonical encodings will generally have odd whitespace etc |
| 156 |
// that |EVP_DecodeBase64| will reject. |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
const std::string encoded(RemoveNewlines(t->encoded)); |
| 161 |
std::vector<uint8_t> out_vec(encoded.size()); |
| 162 |
uint8_t *out = out_vec.data(); |
| 163 |
|
| 164 |
int ok = EVP_DecodeBase64(out, &len, out_vec.size(), |
| 165 |
(const uint8_t *)encoded.data(), encoded.size()); |
| 166 |
|
| 167 |
if (t->relation == invalid) { |
| 168 |
if (ok) { |
| 169 |
fprintf(stderr, "decode(\"%s\") didn't fail but should have\n", |
| 170 |
encoded.c_str()); |
| 171 |
return false; |
| 172 |
} |
| 173 |
} else if (t->relation == canonical) { |
| 174 |
if (!ok) { |
| 175 |
fprintf(stderr, "decode(\"%s\") failed\n", encoded.c_str()); |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
if (len != strlen(t->decoded) || |
| 180 |
memcmp(out, t->decoded, len) != 0) { |
| 181 |
fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n", |
| 182 |
encoded.c_str(), (int)len, (const char*)out, t->decoded); |
| 183 |
return false; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
static bool TestDecodeBlock() { |
| 192 |
for (unsigned i = 0; i < kNumTests; i++) { |
| 193 |
const TestVector *t = &kTestVectors[i]; |
| 194 |
if (t->relation != canonical) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
std::string encoded(RemoveNewlines(t->encoded)); |
| 199 |
|
| 200 |
std::vector<uint8_t> out_vec(encoded.size()); |
| 201 |
uint8_t *out = out_vec.data(); |
| 202 |
|
| 203 |
// Test that the padding behavior of the deprecated API is preserved. |
| 204 |
int ret = |
| 205 |
EVP_DecodeBlock(out, (const uint8_t *)encoded.data(), encoded.size()); |
| 206 |
if (ret < 0) { |
| 207 |
fprintf(stderr, "EVP_DecodeBlock(\"%s\") failed\n", t->encoded); |
| 208 |
return false; |
| 209 |
} |
| 210 |
if (ret % 3 != 0) { |
| 211 |
fprintf(stderr, "EVP_DecodeBlock did not ignore padding\n"); |
| 212 |
return false; |
| 213 |
} |
| 214 |
size_t expected_len = strlen(t->decoded); |
| 215 |
if (expected_len % 3 != 0) { |
| 216 |
ret -= 3 - (expected_len % 3); |
| 217 |
} |
| 218 |
if (static_cast<size_t>(ret) != strlen(t->decoded) || |
| 219 |
memcmp(out, t->decoded, ret) != 0) { |
| 220 |
fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n", |
| 221 |
t->encoded, ret, (const char*)out, t->decoded); |
| 222 |
return false; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
static bool TestEncodeDecode() { |
| 230 |
for (unsigned test_num = 0; test_num < kNumTests; test_num++) { |
| 231 |
const TestVector *t = &kTestVectors[test_num]; |
| 232 |
|
| 233 |
EVP_ENCODE_CTX ctx; |
| 234 |
const size_t decoded_len = strlen(t->decoded); |
| 235 |
|
| 236 |
if (t->relation == canonical) { |
| 237 |
size_t max_encoded_len; |
| 238 |
if (!EVP_EncodedLength(&max_encoded_len, decoded_len)) { |
| 239 |
fprintf(stderr, "#%u: EVP_EncodedLength failed\n", test_num); |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
// EVP_EncodeUpdate will output new lines every 64 bytes of output so we |
| 244 |
// need slightly more than |EVP_EncodedLength| returns. */ |
| 245 |
max_encoded_len += (max_encoded_len + 63) >> 6; |
| 246 |
std::vector<uint8_t> out_vec(max_encoded_len); |
| 247 |
uint8_t *out = out_vec.data(); |
| 248 |
|
| 249 |
EVP_EncodeInit(&ctx); |
| 250 |
|
| 251 |
int out_len; |
| 252 |
EVP_EncodeUpdate(&ctx, out, &out_len, |
| 253 |
reinterpret_cast<const uint8_t *>(t->decoded), |
| 254 |
decoded_len); |
| 255 |
size_t total = out_len; |
| 256 |
|
| 257 |
EVP_EncodeFinal(&ctx, out + total, &out_len); |
| 258 |
total += out_len; |
| 259 |
|
| 260 |
if (total != strlen(t->encoded) || memcmp(out, t->encoded, total) != 0) { |
| 261 |
fprintf(stderr, "#%u: EVP_EncodeUpdate produced different output: '%s' (%u)\n", |
| 262 |
test_num, out, static_cast<unsigned>(total)); |
| 263 |
return false; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
std::vector<uint8_t> out_vec(strlen(t->encoded)); |
| 268 |
uint8_t *out = out_vec.data(); |
| 269 |
|
| 270 |
EVP_DecodeInit(&ctx); |
| 271 |
int out_len; |
| 272 |
size_t total = 0; |
| 273 |
int ret = EVP_DecodeUpdate(&ctx, out, &out_len, |
| 274 |
reinterpret_cast<const uint8_t *>(t->encoded), |
| 275 |
strlen(t->encoded)); |
| 276 |
if (ret != -1) { |
| 277 |
total = out_len; |
| 278 |
ret = EVP_DecodeFinal(&ctx, out + total, &out_len); |
| 279 |
total += out_len; |
| 280 |
} |
| 281 |
|
| 282 |
switch (t->relation) { |
| 283 |
case canonical: |
| 284 |
case valid: |
| 285 |
if (ret == -1) { |
| 286 |
fprintf(stderr, "#%u: EVP_DecodeUpdate failed\n", test_num); |
| 287 |
return false; |
| 288 |
} |
| 289 |
if (total != decoded_len || memcmp(out, t->decoded, decoded_len)) { |
| 290 |
fprintf(stderr, "#%u: EVP_DecodeUpdate produced incorrect output\n", |
| 291 |
test_num); |
| 292 |
return false; |
| 293 |
} |
| 294 |
break; |
| 295 |
|
| 296 |
case invalid: |
| 297 |
if (ret != -1) { |
| 298 |
fprintf(stderr, "#%u: EVP_DecodeUpdate was successful but shouldn't have been\n", test_num); |
| 299 |
return false; |
| 300 |
} |
| 301 |
break; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
return true; |
| 306 |
} |
| 307 |
|
| 308 |
static bool TestDecodeUpdateStreaming() { |
| 309 |
for (unsigned test_num = 0; test_num < kNumTests; test_num++) { |
| 310 |
const TestVector *t = &kTestVectors[test_num]; |
| 311 |
if (t->relation == invalid) { |
| 312 |
continue; |
| 313 |
} |
| 314 |
|
| 315 |
const size_t encoded_len = strlen(t->encoded); |
| 316 |
|
| 317 |
std::vector<uint8_t> out(encoded_len); |
| 318 |
|
| 319 |
for (size_t chunk_size = 1; chunk_size <= encoded_len; chunk_size++) { |
| 320 |
size_t out_len = 0; |
| 321 |
EVP_ENCODE_CTX ctx; |
| 322 |
EVP_DecodeInit(&ctx); |
| 323 |
|
| 324 |
for (size_t i = 0; i < encoded_len;) { |
| 325 |
size_t todo = encoded_len - i; |
| 326 |
if (todo > chunk_size) { |
| 327 |
todo = chunk_size; |
| 328 |
} |
| 329 |
|
| 330 |
int bytes_written; |
| 331 |
int ret = EVP_DecodeUpdate( |
| 332 |
&ctx, out.data() + out_len, &bytes_written, |
| 333 |
reinterpret_cast<const uint8_t *>(t->encoded + i), todo); |
| 334 |
i += todo; |
| 335 |
|
| 336 |
switch (ret) { |
| 337 |
case -1: |
| 338 |
fprintf(stderr, "#%u: EVP_DecodeUpdate returned error\n", test_num); |
| 339 |
return 0; |
| 340 |
case 0: |
| 341 |
out_len += bytes_written; |
| 342 |
if (i == encoded_len || |
| 343 |
(i + 1 == encoded_len && t->encoded[i] == '\n') || |
| 344 |
/* If there was an '-' in the input (which means “EOF”) then |
| 345 |
* this loop will continue to test that |EVP_DecodeUpdate| will |
| 346 |
* ignore the remainder of the input. */ |
| 347 |
strchr(t->encoded, '-') != nullptr) { |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
fprintf(stderr, |
| 352 |
"#%u: EVP_DecodeUpdate returned zero before end of " |
| 353 |
"encoded data\n", |
| 354 |
test_num); |
| 355 |
return 0; |
| 356 |
default: |
| 357 |
out_len += bytes_written; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
int bytes_written; |
| 362 |
int ret = EVP_DecodeFinal(&ctx, out.data() + out_len, &bytes_written); |
| 363 |
if (ret == -1) { |
| 364 |
fprintf(stderr, "#%u: EVP_DecodeFinal returned error\n", test_num); |
| 365 |
return 0; |
| 366 |
} |
| 367 |
out_len += bytes_written; |
| 368 |
|
| 369 |
if (out_len != strlen(t->decoded) || |
| 370 |
memcmp(out.data(), t->decoded, out_len) != 0) { |
| 371 |
fprintf(stderr, "#%u: incorrect output\n", test_num); |
| 372 |
return 0; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
return true; |
| 378 |
} |
| 379 |
|
| 380 |
int main(void) { |
| 381 |
CRYPTO_library_init(); |
| 382 |
|
| 383 |
if (!TestEncodeBlock() || |
| 384 |
!TestDecodeBase64() || |
| 385 |
!TestDecodeBlock() || |
| 386 |
!TestDecodeUpdateStreaming() || |
| 387 |
!TestEncodeDecode()) { |
| 388 |
return 1; |
| 389 |
} |
| 390 |
|
| 391 |
printf("PASS\n"); |
| 392 |
return 0; |
| 393 |
} |