| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include <string> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
#include <gtest/gtest.h> |
| 11 |
|
| 12 |
#include <openssl/base64.h> |
| 13 |
#include <openssl/crypto.h> |
| 14 |
#include <openssl/err.h> |
| 15 |
|
| 16 |
#include "../internal.h" |
| 17 |
#include "../test/test_util.h" |
| 18 |
|
| 19 |
|
| 20 |
enum encoding_relation { |
| 21 |
// canonical indicates that the encoding is the expected encoding of the |
| 22 |
// input. |
| 23 |
canonical, |
| 24 |
// valid indicates that the encoding is /a/ valid encoding of the input, but |
| 25 |
// need not be the canonical one. |
| 26 |
valid, |
| 27 |
// invalid indicates that the encoded data is valid. |
| 28 |
invalid, |
| 29 |
}; |
| 30 |
|
| 31 |
struct Base64TestVector { |
| 32 |
enum encoding_relation relation; |
| 33 |
const char *decoded; |
| 34 |
const char *encoded; |
| 35 |
}; |
| 36 |
|
| 37 |
// Test vectors from RFC 4648. |
| 38 |
static const Base64TestVector kTestVectors[] = { |
| 39 |
{canonical, "", ""}, |
| 40 |
{canonical, "f", "Zg==\n"}, |
| 41 |
{canonical, "fo", "Zm8=\n"}, |
| 42 |
{canonical, "foo", "Zm9v\n"}, |
| 43 |
{canonical, "foob", "Zm9vYg==\n"}, |
| 44 |
{canonical, "fooba", "Zm9vYmE=\n"}, |
| 45 |
{canonical, "foobar", "Zm9vYmFy\n"}, |
| 46 |
{valid, "foobar", "Zm9vYmFy\n\n"}, |
| 47 |
{valid, "foobar", " Zm9vYmFy\n\n"}, |
| 48 |
{valid, "foobar", " Z m 9 v Y m F y\n\n"}, |
| 49 |
{invalid, "", "Zm9vYmFy=\n"}, |
| 50 |
{invalid, "", "Zm9vYmFy==\n"}, |
| 51 |
{invalid, "", "Zm9vYmFy===\n"}, |
| 52 |
{invalid, "", "Z"}, |
| 53 |
{invalid, "", "Z\n"}, |
| 54 |
{invalid, "", "ab!c"}, |
| 55 |
{invalid, "", "ab=c"}, |
| 56 |
{invalid, "", "abc"}, |
| 57 |
|
| 58 |
{canonical, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 59 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA==\n"}, |
| 60 |
{valid, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 61 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA\n==\n"}, |
| 62 |
{valid, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 63 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA=\n=\n"}, |
| 64 |
{invalid, "", |
| 65 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA=\n==\n"}, |
| 66 |
{canonical, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 67 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4\neHh4eHh" |
| 68 |
"4eHh4eHh4\n"}, |
| 69 |
{canonical, |
| 70 |
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 71 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4\neHh4eHh" |
| 72 |
"4eHh4eHh4eHh4eA==\n"}, |
| 73 |
{valid, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 74 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh\n4eHh4eHh" |
| 75 |
"4eHh4eHh4eHh4eA==\n"}, |
| 76 |
{valid, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 77 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4e" |
| 78 |
"Hh4eHh4eHh4eA==\n"}, |
| 79 |
{invalid, "", |
| 80 |
"eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eA==" |
| 81 |
"\neHh4eHh4eHh4eHh4eHh4eHh4\n"}, |
| 82 |
|
| 83 |
// A '-' has traditionally been treated as the end of the data by OpenSSL |
| 84 |
// and anything following would be ignored. BoringSSL does not accept this |
| 85 |
// non-standard extension. |
| 86 |
{invalid, "", "Zm9vYmFy-anythinggoes"}, |
| 87 |
{invalid, "", "Zm9vYmFy\n-anythinggoes"}, |
| 88 |
|
| 89 |
// CVE-2015-0292 |
| 90 |
{invalid, "", |
| 91 |
"ZW5jb2RlIG1lCg===========================================================" |
| 92 |
"=======\n"}, |
| 93 |
}; |
| 94 |
|
| 95 |
class Base64Test : public testing::TestWithParam<Base64TestVector> {}; |
| 96 |
|
| 97 |
INSTANTIATE_TEST_SUITE_P(All, Base64Test, testing::ValuesIn(kTestVectors)); |
| 98 |
|
| 99 |
// RemoveNewlines returns a copy of |in| with all '\n' characters removed. |
| 100 |
static std::string RemoveNewlines(const char *in) { |
| 101 |
std::string ret; |
| 102 |
const size_t in_len = strlen(in); |
| 103 |
|
| 104 |
for (size_t i = 0; i < in_len; i++) { |
| 105 |
if (in[i] != '\n') { |
| 106 |
ret.push_back(in[i]); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return ret; |
| 111 |
} |
| 112 |
|
| 113 |
TEST_P(Base64Test, EncodeBlock) { |
| 114 |
const Base64TestVector &t = GetParam(); |
| 115 |
if (t.relation != canonical) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
const size_t decoded_len = strlen(t.decoded); |
| 120 |
size_t max_encoded_len; |
| 121 |
ASSERT_TRUE(EVP_EncodedLength(&max_encoded_len, decoded_len)); |
| 122 |
|
| 123 |
std::vector<uint8_t> out_vec(max_encoded_len); |
| 124 |
uint8_t *out = out_vec.data(); |
| 125 |
size_t len = EVP_EncodeBlock(out, (const uint8_t *)t.decoded, decoded_len); |
| 126 |
|
| 127 |
std::string encoded(RemoveNewlines(t.encoded)); |
| 128 |
EXPECT_EQ(Bytes(encoded), Bytes(out, len)); |
| 129 |
} |
| 130 |
|
| 131 |
TEST_P(Base64Test, DecodeBase64) { |
| 132 |
const Base64TestVector &t = GetParam(); |
| 133 |
if (t.relation == valid) { |
| 134 |
// The non-canonical encodings will generally have odd whitespace etc |
| 135 |
// that |EVP_DecodeBase64| will reject. |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
const std::string encoded(RemoveNewlines(t.encoded)); |
| 140 |
std::vector<uint8_t> out_vec(encoded.size()); |
| 141 |
uint8_t *out = out_vec.data(); |
| 142 |
|
| 143 |
size_t len; |
| 144 |
int ok = EVP_DecodeBase64(out, &len, out_vec.size(), |
| 145 |
(const uint8_t *)encoded.data(), encoded.size()); |
| 146 |
|
| 147 |
if (t.relation == invalid) { |
| 148 |
EXPECT_FALSE(ok); |
| 149 |
} else if (t.relation == canonical) { |
| 150 |
ASSERT_TRUE(ok); |
| 151 |
EXPECT_EQ(Bytes(t.decoded), Bytes(out, len)); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
TEST_P(Base64Test, DecodeBlock) { |
| 156 |
const Base64TestVector &t = GetParam(); |
| 157 |
if (t.relation != canonical) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
std::string encoded(RemoveNewlines(t.encoded)); |
| 162 |
|
| 163 |
std::vector<uint8_t> out_vec(encoded.size()); |
| 164 |
uint8_t *out = out_vec.data(); |
| 165 |
|
| 166 |
// Test that the padding behavior of the deprecated API is preserved. |
| 167 |
int ret = |
| 168 |
EVP_DecodeBlock(out, (const uint8_t *)encoded.data(), encoded.size()); |
| 169 |
ASSERT_GE(ret, 0); |
| 170 |
// EVP_DecodeBlock should ignore padding. |
| 171 |
ASSERT_EQ(0, ret % 3); |
| 172 |
size_t expected_len = strlen(t.decoded); |
| 173 |
if (expected_len % 3 != 0) { |
| 174 |
ret -= 3 - (expected_len % 3); |
| 175 |
} |
| 176 |
EXPECT_EQ(Bytes(t.decoded), Bytes(out, static_cast<size_t>(ret))); |
| 177 |
} |
| 178 |
|
| 179 |
TEST_P(Base64Test, EncodeDecode) { |
| 180 |
const Base64TestVector &t = GetParam(); |
| 181 |
|
| 182 |
EVP_ENCODE_CTX ctx; |
| 183 |
const size_t decoded_len = strlen(t.decoded); |
| 184 |
|
| 185 |
if (t.relation == canonical) { |
| 186 |
size_t max_encoded_len; |
| 187 |
ASSERT_TRUE(EVP_EncodedLength(&max_encoded_len, decoded_len)); |
| 188 |
|
| 189 |
// EVP_EncodeUpdate will output new lines every 64 bytes of output so we |
| 190 |
// need slightly more than |EVP_EncodedLength| returns. */ |
| 191 |
max_encoded_len += (max_encoded_len + 63) >> 6; |
| 192 |
std::vector<uint8_t> out_vec(max_encoded_len); |
| 193 |
uint8_t *out = out_vec.data(); |
| 194 |
|
| 195 |
EVP_EncodeInit(&ctx); |
| 196 |
|
| 197 |
int out_len; |
| 198 |
int ret = EVP_EncodeUpdate(&ctx, out, &out_len, |
| 199 |
reinterpret_cast<const uint8_t *>(t.decoded), |
| 200 |
decoded_len); |
| 201 |
EXPECT_EQ(ret, (strlen(t.encoded) > 0 ? 1 : 0)); |
| 202 |
size_t total = out_len; |
| 203 |
|
| 204 |
EVP_EncodeFinal(&ctx, out + total, &out_len); |
| 205 |
total += out_len; |
| 206 |
|
| 207 |
EXPECT_EQ(Bytes(t.encoded), Bytes(out, total)); |
| 208 |
} |
| 209 |
|
| 210 |
std::vector<uint8_t> out_vec(strlen(t.encoded)); |
| 211 |
uint8_t *out = out_vec.data(); |
| 212 |
|
| 213 |
EVP_DecodeInit(&ctx); |
| 214 |
int out_len; |
| 215 |
size_t total = 0; |
| 216 |
int ret = EVP_DecodeUpdate(&ctx, out, &out_len, |
| 217 |
reinterpret_cast<const uint8_t *>(t.encoded), |
| 218 |
strlen(t.encoded)); |
| 219 |
if (ret != -1) { |
| 220 |
total = out_len; |
| 221 |
ret = EVP_DecodeFinal(&ctx, out + total, &out_len); |
| 222 |
total += out_len; |
| 223 |
} |
| 224 |
|
| 225 |
switch (t.relation) { |
| 226 |
case canonical: |
| 227 |
case valid: |
| 228 |
ASSERT_NE(-1, ret); |
| 229 |
EXPECT_EQ(Bytes(t.decoded), Bytes(out, total)); |
| 230 |
break; |
| 231 |
|
| 232 |
case invalid: |
| 233 |
EXPECT_EQ(-1, ret); |
| 234 |
break; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
TEST_P(Base64Test, DecodeUpdateStreaming) { |
| 239 |
const Base64TestVector &t = GetParam(); |
| 240 |
if (t.relation == invalid) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
const size_t encoded_len = strlen(t.encoded); |
| 245 |
|
| 246 |
std::vector<uint8_t> out(encoded_len); |
| 247 |
|
| 248 |
for (size_t chunk_size = 1; chunk_size <= encoded_len; chunk_size++) { |
| 249 |
SCOPED_TRACE(chunk_size); |
| 250 |
size_t out_len = 0; |
| 251 |
EVP_ENCODE_CTX ctx; |
| 252 |
EVP_DecodeInit(&ctx); |
| 253 |
|
| 254 |
for (size_t i = 0; i < encoded_len;) { |
| 255 |
size_t todo = encoded_len - i; |
| 256 |
if (todo > chunk_size) { |
| 257 |
todo = chunk_size; |
| 258 |
} |
| 259 |
|
| 260 |
int bytes_written; |
| 261 |
int ret = EVP_DecodeUpdate( |
| 262 |
&ctx, out.data() + out_len, &bytes_written, |
| 263 |
reinterpret_cast<const uint8_t *>(t.encoded + i), todo); |
| 264 |
i += todo; |
| 265 |
|
| 266 |
switch (ret) { |
| 267 |
case -1: |
| 268 |
FAIL() << "EVP_DecodeUpdate failed"; |
| 269 |
case 0: |
| 270 |
out_len += bytes_written; |
| 271 |
if (i == encoded_len || |
| 272 |
(i + 1 == encoded_len && t.encoded[i] == '\n') || |
| 273 |
// If there was an '-' in the input (which means “EOF”) then |
| 274 |
// this loop will continue to test that |EVP_DecodeUpdate| will |
| 275 |
// ignore the remainder of the input. |
| 276 |
strchr(t.encoded, '-') != nullptr) { |
| 277 |
break; |
| 278 |
} |
| 279 |
|
| 280 |
FAIL() |
| 281 |
<< "EVP_DecodeUpdate returned zero before end of encoded data."; |
| 282 |
case 1: |
| 283 |
out_len += bytes_written; |
| 284 |
break; |
| 285 |
default: |
| 286 |
FAIL() << "Invalid return value " << ret; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
int bytes_written; |
| 291 |
int ret = EVP_DecodeFinal(&ctx, out.data() + out_len, &bytes_written); |
| 292 |
ASSERT_NE(ret, -1); |
| 293 |
out_len += bytes_written; |
| 294 |
|
| 295 |
EXPECT_EQ(Bytes(t.decoded), Bytes(out.data(), out_len)); |
| 296 |
} |
| 297 |
} |