| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <assert.h> |
| 16 | #include <errno.h> |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/base64.h> |
| 21 | #include <openssl/bio.h> |
| 22 | #include <openssl/buffer.h> |
| 23 | #include <openssl/evp.h> |
| 24 | #include <openssl/mem.h> |
| 25 |
| 26 | #include "../../crypto/internal.h" |
| 27 |
| 28 |
| 29 | #define B64_BLOCK_SIZE 1024 |
| 30 | #define B64_BLOCK_SIZE2 768 |
| 31 | #define B64_NONE 0 |
| 32 | #define B64_ENCODE 1 |
| 33 | #define B64_DECODE 2 |
| 34 | #define EVP_ENCODE_LENGTH(l) (((l + 2) / 3 * 4) + (l / 48 + 1) * 2 + 80) |
| 35 |
| 36 | typedef struct b64_struct { |
| 37 | int buf_len; |
| 38 | int buf_off; |
| 39 | int tmp_len; // used to find the start when decoding |
| 40 | int tmp_nl; // If true, scan until '\n' |
| 41 | int encode; |
| 42 | int start; // have we started decoding yet? |
| 43 | int cont; // <= 0 when finished |
| 44 | EVP_ENCODE_CTX base64; |
| 45 | char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10]; |
| 46 | char tmp[B64_BLOCK_SIZE]; |
| 47 | } BIO_B64_CTX; |
| 48 |
| 49 | static int b64_new(BIO *bio) { |
| 50 | BIO_B64_CTX *ctx = |
| 51 | reinterpret_cast<BIO_B64_CTX *>(OPENSSL_zalloc(sizeof(*ctx))); |
| 52 | if (ctx == NULL) { |
| 53 | return 0; |
| 54 | } |
| 55 |
| 56 | ctx->cont = 1; |
| 57 | ctx->start = 1; |
| 58 |
| 59 | bio->init = 1; |
| 60 | bio->ptr = (char *)ctx; |
| 61 | return 1; |
| 62 | } |
| 63 |
| 64 | static int b64_free(BIO *bio) { |
| 65 | if (bio == NULL) { |
| 66 | return 0; |
| 67 | } |
| 68 | OPENSSL_free(bio->ptr); |
| 69 | bio->ptr = NULL; |
| 70 | bio->init = 0; |
| 71 | bio->flags = 0; |
| 72 | return 1; |
| 73 | } |
| 74 |
| 75 | static int b64_read(BIO *b, char *out, int outl) { |
| 76 | int ret = 0, i, ii, j, k, x, n, num, ret_code = 0; |
| 77 | BIO_B64_CTX *ctx; |
| 78 | uint8_t *p, *q; |
| 79 |
| 80 | if (out == NULL) { |
| 81 | return 0; |
| 82 | } |
| 83 | ctx = (BIO_B64_CTX *)b->ptr; |
| 84 |
| 85 | if (ctx == NULL || b->next_bio == NULL) { |
| 86 | return 0; |
| 87 | } |
| 88 |
| 89 | BIO_clear_retry_flags(b); |
| 90 |
| 91 | if (ctx->encode != B64_DECODE) { |
| 92 | ctx->encode = B64_DECODE; |
| 93 | ctx->buf_len = 0; |
| 94 | ctx->buf_off = 0; |
| 95 | ctx->tmp_len = 0; |
| 96 | EVP_DecodeInit(&ctx->base64); |
| 97 | } |
| 98 |
| 99 | // First check if there are bytes decoded/encoded |
| 100 | if (ctx->buf_len > 0) { |
| 101 | assert(ctx->buf_len >= ctx->buf_off); |
| 102 | i = ctx->buf_len - ctx->buf_off; |
| 103 | if (i > outl) { |
| 104 | i = outl; |
| 105 | } |
| 106 | assert(ctx->buf_off + i < (int)sizeof(ctx->buf)); |
| 107 | OPENSSL_memcpy(out, &ctx->buf[ctx->buf_off], i); |
| 108 | ret = i; |
| 109 | out += i; |
| 110 | outl -= i; |
| 111 | ctx->buf_off += i; |
| 112 | if (ctx->buf_len == ctx->buf_off) { |
| 113 | ctx->buf_len = 0; |
| 114 | ctx->buf_off = 0; |
| 115 | } |
| 116 | } |
| 117 |
| 118 | // At this point, we have room of outl bytes and an empty buffer, so we |
| 119 | // should read in some more. |
| 120 |
| 121 | ret_code = 0; |
| 122 | while (outl > 0) { |
| 123 | if (ctx->cont <= 0) { |
| 124 | break; |
| 125 | } |
| 126 |
| 127 | i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]), |
| 128 | B64_BLOCK_SIZE - ctx->tmp_len); |
| 129 |
| 130 | if (i <= 0) { |
| 131 | ret_code = i; |
| 132 |
| 133 | // Should we continue next time we are called? |
| 134 | if (!BIO_should_retry(b->next_bio)) { |
| 135 | ctx->cont = i; |
| 136 | // If buffer empty break |
| 137 | if (ctx->tmp_len == 0) { |
| 138 | break; |
| 139 | } else { |
| 140 | // Fall through and process what we have |
| 141 | i = 0; |
| 142 | } |
| 143 | } else { |
| 144 | // else we retry and add more data to buffer |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | i += ctx->tmp_len; |
| 149 | ctx->tmp_len = i; |
| 150 |
| 151 | // We need to scan, a line at a time until we have a valid line if we are |
| 152 | // starting. |
| 153 | if (ctx->start && (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL))) { |
| 154 | // ctx->start = 1; |
| 155 | ctx->tmp_len = 0; |
| 156 | } else if (ctx->start) { |
| 157 | q = p = (uint8_t *)ctx->tmp; |
| 158 | num = 0; |
| 159 | for (j = 0; j < i; j++) { |
| 160 | if (*(q++) != '\n') { |
| 161 | continue; |
| 162 | } |
| 163 |
| 164 | // due to a previous very long line, we need to keep on scanning for a |
| 165 | // '\n' before we even start looking for base64 encoded stuff. |
| 166 | if (ctx->tmp_nl) { |
| 167 | p = q; |
| 168 | ctx->tmp_nl = 0; |
| 169 | continue; |
| 170 | } |
| 171 |
| 172 | k = EVP_DecodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &num, p, |
| 173 | q - p); |
| 174 |
| 175 | if (k <= 0 && num == 0 && ctx->start) { |
| 176 | EVP_DecodeInit(&ctx->base64); |
| 177 | } else { |
| 178 | if (p != (uint8_t *)&(ctx->tmp[0])) { |
| 179 | i -= (p - (uint8_t *)&(ctx->tmp[0])); |
| 180 | for (x = 0; x < i; x++) { |
| 181 | ctx->tmp[x] = p[x]; |
| 182 | } |
| 183 | } |
| 184 | EVP_DecodeInit(&ctx->base64); |
| 185 | ctx->start = 0; |
| 186 | break; |
| 187 | } |
| 188 | p = q; |
| 189 | } |
| 190 |
| 191 | // we fell off the end without starting |
| 192 | if (j == i && num == 0) { |
| 193 | // Is this is one long chunk?, if so, keep on reading until a new |
| 194 | // line. |
| 195 | if (p == (uint8_t *)&(ctx->tmp[0])) { |
| 196 | // Check buffer full |
| 197 | if (i == B64_BLOCK_SIZE) { |
| 198 | ctx->tmp_nl = 1; |
| 199 | ctx->tmp_len = 0; |
| 200 | } |
| 201 | } else if (p != q) { // finished on a '\n' |
| 202 | n = q - p; |
| 203 | for (ii = 0; ii < n; ii++) { |
| 204 | ctx->tmp[ii] = p[ii]; |
| 205 | } |
| 206 | ctx->tmp_len = n; |
| 207 | } |
| 208 | // else finished on a '\n' |
| 209 | continue; |
| 210 | } else { |
| 211 | ctx->tmp_len = 0; |
| 212 | } |
| 213 | } else if (i < B64_BLOCK_SIZE && ctx->cont > 0) { |
| 214 | // If buffer isn't full and we can retry then restart to read in more |
| 215 | // data. |
| 216 | continue; |
| 217 | } |
| 218 |
| 219 | if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) { |
| 220 | int z, jj; |
| 221 |
| 222 | jj = i & ~3; // process per 4 |
| 223 | z = EVP_DecodeBlock((uint8_t *)ctx->buf, (uint8_t *)ctx->tmp, jj); |
| 224 | if (jj > 2) { |
| 225 | if (ctx->tmp[jj - 1] == '=') { |
| 226 | z--; |
| 227 | if (ctx->tmp[jj - 2] == '=') { |
| 228 | z--; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | // z is now number of output bytes and jj is the number consumed. |
| 233 | if (jj != i) { |
| 234 | OPENSSL_memmove(ctx->tmp, &ctx->tmp[jj], i - jj); |
| 235 | ctx->tmp_len = i - jj; |
| 236 | } |
| 237 | ctx->buf_len = 0; |
| 238 | if (z > 0) { |
| 239 | ctx->buf_len = z; |
| 240 | } |
| 241 | i = z; |
| 242 | } else { |
| 243 | i = EVP_DecodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &ctx->buf_len, |
| 244 | (uint8_t *)ctx->tmp, i); |
| 245 | ctx->tmp_len = 0; |
| 246 | } |
| 247 | ctx->buf_off = 0; |
| 248 | if (i < 0) { |
| 249 | ret_code = 0; |
| 250 | ctx->buf_len = 0; |
| 251 | break; |
| 252 | } |
| 253 |
| 254 | if (ctx->buf_len <= outl) { |
| 255 | i = ctx->buf_len; |
| 256 | } else { |
| 257 | i = outl; |
| 258 | } |
| 259 |
| 260 | OPENSSL_memcpy(out, ctx->buf, i); |
| 261 | ret += i; |
| 262 | ctx->buf_off = i; |
| 263 | if (ctx->buf_off == ctx->buf_len) { |
| 264 | ctx->buf_len = 0; |
| 265 | ctx->buf_off = 0; |
| 266 | } |
| 267 | outl -= i; |
| 268 | out += i; |
| 269 | } |
| 270 |
| 271 | BIO_copy_next_retry(b); |
| 272 | return ret == 0 ? ret_code : ret; |
| 273 | } |
| 274 |
| 275 | static int b64_write(BIO *b, const char *in, int inl) { |
| 276 | int ret = 0, n, i; |
| 277 | BIO_B64_CTX *ctx; |
| 278 |
| 279 | ctx = (BIO_B64_CTX *)b->ptr; |
| 280 | BIO_clear_retry_flags(b); |
| 281 |
| 282 | if (ctx->encode != B64_ENCODE) { |
| 283 | ctx->encode = B64_ENCODE; |
| 284 | ctx->buf_len = 0; |
| 285 | ctx->buf_off = 0; |
| 286 | ctx->tmp_len = 0; |
| 287 | EVP_EncodeInit(&(ctx->base64)); |
| 288 | } |
| 289 |
| 290 | assert(ctx->buf_off < (int)sizeof(ctx->buf)); |
| 291 | assert(ctx->buf_len <= (int)sizeof(ctx->buf)); |
| 292 | assert(ctx->buf_len >= ctx->buf_off); |
| 293 |
| 294 | n = ctx->buf_len - ctx->buf_off; |
| 295 | while (n > 0) { |
| 296 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); |
| 297 | if (i <= 0) { |
| 298 | BIO_copy_next_retry(b); |
| 299 | return i; |
| 300 | } |
| 301 | assert(i <= n); |
| 302 | ctx->buf_off += i; |
| 303 | assert(ctx->buf_off <= (int)sizeof(ctx->buf)); |
| 304 | assert(ctx->buf_len >= ctx->buf_off); |
| 305 | n -= i; |
| 306 | } |
| 307 |
| 308 | // at this point all pending data has been written. |
| 309 | ctx->buf_off = 0; |
| 310 | ctx->buf_len = 0; |
| 311 |
| 312 | if (in == NULL || inl <= 0) { |
| 313 | return 0; |
| 314 | } |
| 315 |
| 316 | while (inl > 0) { |
| 317 | n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl; |
| 318 |
| 319 | if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) { |
| 320 | if (ctx->tmp_len > 0) { |
| 321 | assert(ctx->tmp_len <= 3); |
| 322 | n = 3 - ctx->tmp_len; |
| 323 | // There's a theoretical possibility of this. |
| 324 | if (n > inl) { |
| 325 | n = inl; |
| 326 | } |
| 327 | OPENSSL_memcpy(&(ctx->tmp[ctx->tmp_len]), in, n); |
| 328 | ctx->tmp_len += n; |
| 329 | ret += n; |
| 330 | if (ctx->tmp_len < 3) { |
| 331 | break; |
| 332 | } |
| 333 | ctx->buf_len = EVP_EncodeBlock((uint8_t *)ctx->buf, (uint8_t *)ctx->tmp, |
| 334 | ctx->tmp_len); |
| 335 | assert(ctx->buf_len <= (int)sizeof(ctx->buf)); |
| 336 | assert(ctx->buf_len >= ctx->buf_off); |
| 337 |
| 338 | // Since we're now done using the temporary buffer, the length should |
| 339 | // be zeroed. |
| 340 | ctx->tmp_len = 0; |
| 341 | } else { |
| 342 | if (n < 3) { |
| 343 | OPENSSL_memcpy(ctx->tmp, in, n); |
| 344 | ctx->tmp_len = n; |
| 345 | ret += n; |
| 346 | break; |
| 347 | } |
| 348 | n -= n % 3; |
| 349 | ctx->buf_len = |
| 350 | EVP_EncodeBlock((uint8_t *)ctx->buf, (const uint8_t *)in, n); |
| 351 | assert(ctx->buf_len <= (int)sizeof(ctx->buf)); |
| 352 | assert(ctx->buf_len >= ctx->buf_off); |
| 353 | ret += n; |
| 354 | } |
| 355 | } else { |
| 356 | EVP_EncodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &ctx->buf_len, |
| 357 | (uint8_t *)in, n); |
| 358 | assert(ctx->buf_len <= (int)sizeof(ctx->buf)); |
| 359 | assert(ctx->buf_len >= ctx->buf_off); |
| 360 | ret += n; |
| 361 | } |
| 362 | inl -= n; |
| 363 | in += n; |
| 364 |
| 365 | ctx->buf_off = 0; |
| 366 | n = ctx->buf_len; |
| 367 |
| 368 | while (n > 0) { |
| 369 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); |
| 370 | if (i <= 0) { |
| 371 | BIO_copy_next_retry(b); |
| 372 | return ret == 0 ? i : ret; |
| 373 | } |
| 374 | assert(i <= n); |
| 375 | n -= i; |
| 376 | ctx->buf_off += i; |
| 377 | assert(ctx->buf_off <= (int)sizeof(ctx->buf)); |
| 378 | assert(ctx->buf_len >= ctx->buf_off); |
| 379 | } |
| 380 | ctx->buf_len = 0; |
| 381 | ctx->buf_off = 0; |
| 382 | } |
| 383 | return ret; |
| 384 | } |
| 385 |
| 386 | static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) { |
| 387 | BIO_B64_CTX *ctx; |
| 388 | long ret = 1; |
| 389 | int i; |
| 390 |
| 391 | ctx = (BIO_B64_CTX *)b->ptr; |
| 392 |
| 393 | switch (cmd) { |
| 394 | case BIO_CTRL_RESET: |
| 395 | ctx->cont = 1; |
| 396 | ctx->start = 1; |
| 397 | ctx->encode = B64_NONE; |
| 398 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 399 | break; |
| 400 |
| 401 | case BIO_CTRL_EOF: // More to read |
| 402 | if (ctx->cont <= 0) { |
| 403 | ret = 1; |
| 404 | } else { |
| 405 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 406 | } |
| 407 | break; |
| 408 |
| 409 | case BIO_CTRL_WPENDING: // More to write in buffer |
| 410 | assert(ctx->buf_len >= ctx->buf_off); |
| 411 | ret = ctx->buf_len - ctx->buf_off; |
| 412 | if ((ret == 0) && (ctx->encode != B64_NONE) && |
| 413 | (ctx->base64.data_used != 0)) { |
| 414 | ret = 1; |
| 415 | } else if (ret <= 0) { |
| 416 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 417 | } |
| 418 | break; |
| 419 |
| 420 | case BIO_CTRL_PENDING: // More to read in buffer |
| 421 | assert(ctx->buf_len >= ctx->buf_off); |
| 422 | ret = ctx->buf_len - ctx->buf_off; |
| 423 | if (ret <= 0) { |
| 424 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 425 | } |
| 426 | break; |
| 427 |
| 428 | case BIO_CTRL_FLUSH: |
| 429 | // do a final write |
| 430 | again: |
| 431 | while (ctx->buf_len != ctx->buf_off) { |
| 432 | i = b64_write(b, NULL, 0); |
| 433 | if (i < 0) { |
| 434 | return i; |
| 435 | } |
| 436 | } |
| 437 | if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) { |
| 438 | if (ctx->tmp_len != 0) { |
| 439 | ctx->buf_len = EVP_EncodeBlock((uint8_t *)ctx->buf, |
| 440 | (uint8_t *)ctx->tmp, ctx->tmp_len); |
| 441 | ctx->buf_off = 0; |
| 442 | ctx->tmp_len = 0; |
| 443 | goto again; |
| 444 | } |
| 445 | } else if (ctx->encode != B64_NONE && ctx->base64.data_used != 0) { |
| 446 | ctx->buf_off = 0; |
| 447 | EVP_EncodeFinal(&(ctx->base64), (uint8_t *)ctx->buf, &(ctx->buf_len)); |
| 448 | // push out the bytes |
| 449 | goto again; |
| 450 | } |
| 451 | // Finally flush the underlying BIO |
| 452 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 453 | break; |
| 454 |
| 455 | case BIO_C_DO_STATE_MACHINE: |
| 456 | BIO_clear_retry_flags(b); |
| 457 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 458 | BIO_copy_next_retry(b); |
| 459 | break; |
| 460 |
| 461 | case BIO_CTRL_INFO: |
| 462 | case BIO_CTRL_GET: |
| 463 | case BIO_CTRL_SET: |
| 464 | default: |
| 465 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 466 | break; |
| 467 | } |
| 468 | return ret; |
| 469 | } |
| 470 |
| 471 | static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb fp) { |
| 472 | if (b->next_bio == NULL) { |
| 473 | return 0; |
| 474 | } |
| 475 | return BIO_callback_ctrl(b->next_bio, cmd, fp); |
| 476 | } |
| 477 |
| 478 | static const BIO_METHOD b64_method = { |
| 479 | BIO_TYPE_BASE64, "base64 encoding", b64_write, b64_read, NULL /* puts */, |
| 480 | NULL /* gets */, b64_ctrl, b64_new, b64_free, b64_callback_ctrl, |
| 481 | }; |
| 482 |
| 483 | const BIO_METHOD *BIO_f_base64(void) { return &b64_method; } |
| 484 |