|
4 | 4 | #include "MGLCipherHostObject.h"
|
5 | 5 |
|
6 | 6 | #ifdef ANDROID
|
| 7 | +#include "JSIUtils/MGLJSIUtils.h" |
7 | 8 | #include "JSIUtils/MGLTypedArray.h"
|
8 | 9 | #else
|
| 10 | +#include "MGLJSIUtils.h" |
9 | 11 | #include "MGLTypedArray.h"
|
10 | 12 | #endif
|
11 | 13 |
|
@@ -106,7 +108,7 @@ MGLCipherHostObject::MGLCipherHostObject(
|
106 | 108 |
|
107 | 109 | int key_len =
|
108 | 110 | EVP_BytesToKey(cipher, EVP_md5(), nullptr, cipher_key->data(runtime),
|
109 |
| - cipher_key->size(runtime), 1, key, iv); |
| 111 | + static_cast<int>(cipher_key->size(runtime)), 1, key, iv); |
110 | 112 |
|
111 | 113 | // TODO(osp) this looks like a macro, check if necessary
|
112 | 114 | // CHECK_NE(key_len, 0);
|
@@ -445,62 +447,76 @@ void MGLCipherHostObject::installMethods() {
|
445 | 447 | }));
|
446 | 448 |
|
447 | 449 | // setAuthTag
|
448 |
| - this->fields.push_back(HOST_LAMBDA("setAuthTag", { |
449 |
| - if (count != 1 || !arguments[0].isObject() || |
450 |
| - !arguments[0].asObject(runtime).isArrayBuffer(runtime)) { |
451 |
| - throw jsi::JSError( |
452 |
| - runtime, "cipher.setAuthTag requires an ArrayBuffer tag argument"); |
453 |
| - } |
| 450 | + this->fields.push_back(buildPair( |
| 451 | + "setAuthTag", JSIF([=]) { |
| 452 | + if (count != 1 || !arguments[0].isObject() || |
| 453 | + !arguments[0].asObject(runtime).isArrayBuffer(runtime)) { |
| 454 | + jsi::detail::throwJSError( |
| 455 | + runtime, |
| 456 | + "cipher.setAuthTag requires an ArrayBuffer tag argument"); |
| 457 | + throw jsi::JSError( |
| 458 | + runtime, |
| 459 | + "cipher.setAuthTag requires an ArrayBuffer tag argument"); |
| 460 | + } |
454 | 461 |
|
455 |
| - if (!ctx_ || !IsAuthenticatedMode() || isCipher_ || |
456 |
| - auth_tag_state_ != kAuthTagUnknown) { |
457 |
| - return false; |
458 |
| - } |
| 462 | + if (!ctx_ || !IsAuthenticatedMode() || isCipher_ || |
| 463 | + auth_tag_state_ != kAuthTagUnknown) { |
| 464 | + return false; |
| 465 | + } |
459 | 466 |
|
460 |
| - auto authTagArrayBuffer = |
461 |
| - arguments[0].asObject(runtime).getArrayBuffer(runtime); |
462 |
| - const unsigned char *data = authTagArrayBuffer.data(runtime); |
463 |
| - auto tag_len = authTagArrayBuffer.length(runtime); |
| 467 | + auto authTagArrayBuffer = |
| 468 | + arguments[0].asObject(runtime).getArrayBuffer(runtime); |
| 469 | + if (!CheckSizeInt32(runtime, authTagArrayBuffer)) { |
| 470 | + jsi::detail::throwJSError( |
| 471 | + runtime, |
| 472 | + "cipher.setAuthTag requires an ArrayBuffer tag argument"); |
| 473 | + throw jsi::JSError( |
| 474 | + runtime, |
| 475 | + "cipher.setAuthTag requires an ArrayBuffer tag argument"); |
| 476 | + } |
| 477 | + // const unsigned char *data = authTagArrayBuffer.data(runtime); |
| 478 | + unsigned int tag_len = |
| 479 | + static_cast<unsigned int>(authTagArrayBuffer.length(runtime)); |
464 | 480 |
|
465 |
| - // ArrayBufferOrViewContents<char> auth_tag(args[0]); |
466 |
| - // TODO(osp) implement this check |
467 |
| - // if (UNLIKELY(!auth_tag.CheckSizeInt32())) |
468 |
| - // return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big"); |
| 481 | + // ArrayBufferOrViewContents<char> auth_tag(args[0]); |
| 482 | + // TODO(osp) implement this check |
| 483 | + // if (UNLIKELY(!auth_tag.CheckSizeInt32())) |
| 484 | + // return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big"); |
469 | 485 |
|
470 |
| - // unsigned int tag_len = auth_tag.size(); |
| 486 | + // unsigned int tag_len = auth_tag.size(); |
471 | 487 |
|
472 |
| - const int mode = EVP_CIPHER_CTX_mode(ctx_); |
473 |
| - bool is_valid; |
474 |
| - if (mode == EVP_CIPH_GCM_MODE) { |
475 |
| - // Restrict GCM tag lengths according to NIST 800-38d, page 9. |
476 |
| - is_valid = |
477 |
| - (auth_tag_len_ == kNoAuthTagLength || auth_tag_len_ == tag_len) && |
478 |
| - IsValidGCMTagLength(tag_len); |
479 |
| - } else { |
480 |
| - // At this point, the tag length is already known and must match the |
481 |
| - // length of the given authentication tag. |
482 |
| - // TODO(osp) add CHECK here |
483 |
| - IsSupportedAuthenticatedMode(ctx_); |
484 |
| - // CHECK_NE(cipher->auth_tag_len_, kNoAuthTagLength); |
485 |
| - is_valid = auth_tag_len_ == tag_len; |
486 |
| - } |
| 488 | + const int mode = EVP_CIPHER_CTX_mode(ctx_); |
| 489 | + bool is_valid; |
| 490 | + if (mode == EVP_CIPH_GCM_MODE) { |
| 491 | + // Restrict GCM tag lengths according to NIST 800-38d, page 9. |
| 492 | + is_valid = |
| 493 | + (auth_tag_len_ == kNoAuthTagLength || auth_tag_len_ == tag_len) && |
| 494 | + IsValidGCMTagLength(tag_len); |
| 495 | + } else { |
| 496 | + // At this point, the tag length is already known and must match the |
| 497 | + // length of the given authentication tag. |
| 498 | + // TODO(osp) add CHECK here |
| 499 | + IsSupportedAuthenticatedMode(ctx_); |
| 500 | + // CHECK_NE(cipher->auth_tag_len_, kNoAuthTagLength); |
| 501 | + is_valid = auth_tag_len_ == tag_len; |
| 502 | + } |
487 | 503 |
|
488 |
| - if (!is_valid) { |
489 |
| - throw jsi::JSError(runtime, |
490 |
| - "Invalid authentication tag length: " + tag_len); |
491 |
| - // return THROW_ERR_CRYPTO_INVALID_AUTH_TAG( |
492 |
| - // env, "Invalid authentication tag length: %u", tag_len); |
493 |
| - } |
| 504 | + if (!is_valid) { |
| 505 | + jsi::detail::throwJSError( |
| 506 | + runtime, "Invalid authentication tag length" + tag_len); |
| 507 | + throw jsi::JSError(runtime, |
| 508 | + "Invalid authentication tag length: " + tag_len); |
| 509 | + } |
494 | 510 |
|
495 |
| - auth_tag_len_ = tag_len; |
496 |
| - auth_tag_state_ = kAuthTagKnown; |
497 |
| - // CHECK_LE(cipher->auth_tag_len_, sizeof(cipher->auth_tag_)); |
| 511 | + auth_tag_len_ = tag_len; |
| 512 | + auth_tag_state_ = kAuthTagKnown; |
| 513 | + // CHECK_LE(cipher->auth_tag_len_, sizeof(cipher->auth_tag_)); |
498 | 514 |
|
499 |
| - memset(auth_tag_, 0, sizeof(auth_tag_)); |
500 |
| - CopyTo(runtime, &authTagArrayBuffer, auth_tag_, auth_tag_len_); |
| 515 | + memset(auth_tag_, 0, sizeof(auth_tag_)); |
| 516 | + CopyTo(runtime, &authTagArrayBuffer, auth_tag_, auth_tag_len_); |
501 | 517 |
|
502 |
| - return true; |
503 |
| - })); |
| 518 | + return true; |
| 519 | + })); |
504 | 520 | }
|
505 | 521 |
|
506 | 522 | bool MGLCipherHostObject::MaybePassAuthTagToOpenSSL() {
|
|
0 commit comments