@@ -2539,6 +2539,12 @@ int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) { |
2539 | 2539 | return 1; |
2540 | 2540 | } |
2541 | 2541 |
|
| 2542 | +static bool IsSupportedAuthenticatedMode(int mode) { |
| 2543 | + return mode == EVP_CIPH_CCM_MODE || |
| 2544 | + mode == EVP_CIPH_GCM_MODE || |
| 2545 | + mode == EVP_CIPH_OCB_MODE; |
| 2546 | +} |
| 2547 | + |
2542 | 2548 | void CipherBase::Initialize(Environment* env, Local<Object> target) { |
2543 | 2549 | Local<FunctionTemplate> t = env->NewFunctionTemplate(New); |
2544 | 2550 |
|
@@ -2565,6 +2571,43 @@ void CipherBase::New(const FunctionCallbackInfo<Value>& args) { |
2565 | 2571 | new CipherBase(env, args.This(), kind); |
2566 | 2572 | } |
2567 | 2573 |
|
| 2574 | +void CipherBase::CommonInit(const char* cipher_type, |
| 2575 | + const EVP_CIPHER* cipher, |
| 2576 | + const unsigned char* key, |
| 2577 | + int key_len, |
| 2578 | + const unsigned char* iv, |
| 2579 | + int iv_len, |
| 2580 | + unsigned int auth_tag_len) { |
| 2581 | + CHECK(!ctx_); |
| 2582 | + ctx_.reset(EVP_CIPHER_CTX_new()); |
| 2583 | + |
| 2584 | + const int mode = EVP_CIPHER_mode(cipher); |
| 2585 | + if (mode == EVP_CIPH_WRAP_MODE) |
| 2586 | + EVP_CIPHER_CTX_set_flags(ctx_.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); |
| 2587 | + |
| 2588 | + const bool encrypt = (kind_ == kCipher); |
| 2589 | + if (1 != EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, |
| 2590 | + nullptr, nullptr, encrypt)) { |
| 2591 | + return ThrowCryptoError(env(), ERR_get_error(), |
| 2592 | + "Failed to initialize cipher"); |
| 2593 | + } |
| 2594 | + |
| 2595 | + if (IsSupportedAuthenticatedMode(mode)) { |
| 2596 | + CHECK_GE(iv_len, 0); |
| 2597 | + if (!InitAuthenticated(cipher_type, iv_len, auth_tag_len)) |
| 2598 | + return; |
| 2599 | + } |
| 2600 | + |
| 2601 | + if (!EVP_CIPHER_CTX_set_key_length(ctx_.get(), key_len)) { |
| 2602 | + ctx_.reset(); |
| 2603 | + return env()->ThrowError("Invalid key length"); |
| 2604 | + } |
| 2605 | + |
| 2606 | + if (1 != EVP_CipherInit_ex(ctx_.get(), nullptr, nullptr, key, iv, encrypt)) { |
| 2607 | + return ThrowCryptoError(env(), ERR_get_error(), |
| 2608 | + "Failed to initialize cipher"); |
| 2609 | + } |
| 2610 | +} |
2568 | 2611 |
|
2569 | 2612 | void CipherBase::Init(const char* cipher_type, |
2570 | 2613 | const char* key_buf, |
@@ -2580,7 +2623,6 @@ void CipherBase::Init(const char* cipher_type, |
2580 | 2623 | } |
2581 | 2624 | #endif // NODE_FIPS_MODE |
2582 | 2625 |
|
2583 | | - CHECK(!ctx_); |
2584 | 2626 | const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type); |
2585 | 2627 | if (cipher == nullptr) |
2586 | 2628 | return env()->ThrowError("Unknown cipher"); |
@@ -2598,45 +2640,19 @@ void CipherBase::Init(const char* cipher_type, |
2598 | 2640 | iv); |
2599 | 2641 | CHECK_NE(key_len, 0); |
2600 | 2642 |
|
2601 | | - ctx_.reset(EVP_CIPHER_CTX_new()); |
2602 | | - |
2603 | 2643 | const int mode = EVP_CIPHER_mode(cipher); |
2604 | | - if (mode == EVP_CIPH_WRAP_MODE) |
2605 | | - EVP_CIPHER_CTX_set_flags(ctx_.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); |
2606 | | - |
2607 | | - const bool encrypt = (kind_ == kCipher); |
2608 | | - if (1 != EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, |
2609 | | - nullptr, nullptr, encrypt)) { |
2610 | | - return ThrowCryptoError(env(), ERR_get_error(), |
2611 | | - "Failed to initialize cipher"); |
2612 | | - } |
2613 | | - |
2614 | | - if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE || |
2615 | | - mode == EVP_CIPH_CCM_MODE)) { |
| 2644 | + if (kind_ == kCipher && (mode == EVP_CIPH_CTR_MODE || |
| 2645 | + mode == EVP_CIPH_GCM_MODE || |
| 2646 | + mode == EVP_CIPH_CCM_MODE)) { |
2616 | 2647 | // Ignore the return value (i.e. possible exception) because we are |
2617 | 2648 | // not calling back into JS anyway. |
2618 | 2649 | ProcessEmitWarning(env(), |
2619 | 2650 | "Use Cipheriv for counter mode of %s", |
2620 | 2651 | cipher_type); |
2621 | 2652 | } |
2622 | 2653 |
|
2623 | | - if (IsAuthenticatedMode()) { |
2624 | | - if (!InitAuthenticated(cipher_type, EVP_CIPHER_iv_length(cipher), |
2625 | | - auth_tag_len)) |
2626 | | - return; |
2627 | | - } |
2628 | | - |
2629 | | - CHECK_EQ(1, EVP_CIPHER_CTX_set_key_length(ctx_.get(), key_len)); |
2630 | | - |
2631 | | - if (1 != EVP_CipherInit_ex(ctx_.get(), |
2632 | | - nullptr, |
2633 | | - nullptr, |
2634 | | - reinterpret_cast<unsigned char*>(key), |
2635 | | - reinterpret_cast<unsigned char*>(iv), |
2636 | | - encrypt)) { |
2637 | | - return ThrowCryptoError(env(), ERR_get_error(), |
2638 | | - "Failed to initialize cipher"); |
2639 | | - } |
| 2654 | + CommonInit(cipher_type, cipher, key, key_len, iv, |
| 2655 | + EVP_CIPHER_iv_length(cipher), auth_tag_len); |
2640 | 2656 | } |
2641 | 2657 |
|
2642 | 2658 |
|
@@ -2663,16 +2679,10 @@ void CipherBase::Init(const FunctionCallbackInfo<Value>& args) { |
2663 | 2679 | cipher->Init(*cipher_type, key_buf, key_buf_len, auth_tag_len); |
2664 | 2680 | } |
2665 | 2681 |
|
2666 | | -static bool IsSupportedAuthenticatedMode(int mode) { |
2667 | | - return mode == EVP_CIPH_CCM_MODE || |
2668 | | - mode == EVP_CIPH_GCM_MODE || |
2669 | | - mode == EVP_CIPH_OCB_MODE; |
2670 | | -} |
2671 | | - |
2672 | 2682 | void CipherBase::InitIv(const char* cipher_type, |
2673 | | - const char* key, |
| 2683 | + const unsigned char* key, |
2674 | 2684 | int key_len, |
2675 | | - const char* iv, |
| 2685 | + const unsigned char* iv, |
2676 | 2686 | int iv_len, |
2677 | 2687 | unsigned int auth_tag_len) { |
2678 | 2688 | HandleScope scope(env()->isolate()); |
@@ -2700,38 +2710,7 @@ void CipherBase::InitIv(const char* cipher_type, |
2700 | 2710 | return env()->ThrowError("Invalid IV length"); |
2701 | 2711 | } |
2702 | 2712 |
|
2703 | | - ctx_.reset(EVP_CIPHER_CTX_new()); |
2704 | | - |
2705 | | - if (mode == EVP_CIPH_WRAP_MODE) |
2706 | | - EVP_CIPHER_CTX_set_flags(ctx_.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); |
2707 | | - |
2708 | | - const bool encrypt = (kind_ == kCipher); |
2709 | | - if (1 != EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, |
2710 | | - nullptr, nullptr, encrypt)) { |
2711 | | - return ThrowCryptoError(env(), ERR_get_error(), |
2712 | | - "Failed to initialize cipher"); |
2713 | | - } |
2714 | | - |
2715 | | - if (is_authenticated_mode) { |
2716 | | - CHECK(has_iv); |
2717 | | - if (!InitAuthenticated(cipher_type, iv_len, auth_tag_len)) |
2718 | | - return; |
2719 | | - } |
2720 | | - |
2721 | | - if (!EVP_CIPHER_CTX_set_key_length(ctx_.get(), key_len)) { |
2722 | | - ctx_.reset(); |
2723 | | - return env()->ThrowError("Invalid key length"); |
2724 | | - } |
2725 | | - |
2726 | | - if (1 != EVP_CipherInit_ex(ctx_.get(), |
2727 | | - nullptr, |
2728 | | - nullptr, |
2729 | | - reinterpret_cast<const unsigned char*>(key), |
2730 | | - reinterpret_cast<const unsigned char*>(iv), |
2731 | | - encrypt)) { |
2732 | | - return ThrowCryptoError(env(), ERR_get_error(), |
2733 | | - "Failed to initialize cipher"); |
2734 | | - } |
| 2713 | + CommonInit(cipher_type, cipher, key, key_len, iv, iv_len, auth_tag_len); |
2735 | 2714 | } |
2736 | 2715 |
|
2737 | 2716 |
|
@@ -2744,14 +2723,15 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) { |
2744 | 2723 |
|
2745 | 2724 | const node::Utf8Value cipher_type(env->isolate(), args[0]); |
2746 | 2725 | ssize_t key_len = Buffer::Length(args[1]); |
2747 | | - const char* key_buf = Buffer::Data(args[1]); |
| 2726 | + const unsigned char* key_buf = reinterpret_cast<unsigned char*>( |
| 2727 | + Buffer::Data(args[1])); |
2748 | 2728 | ssize_t iv_len; |
2749 | | - const char* iv_buf; |
| 2729 | + const unsigned char* iv_buf; |
2750 | 2730 | if (args[2]->IsNull()) { |
2751 | 2731 | iv_buf = nullptr; |
2752 | 2732 | iv_len = -1; |
2753 | 2733 | } else { |
2754 | | - iv_buf = Buffer::Data(args[2]); |
| 2734 | + iv_buf = reinterpret_cast<unsigned char*>(Buffer::Data(args[2])); |
2755 | 2735 | iv_len = Buffer::Length(args[2]); |
2756 | 2736 | } |
2757 | 2737 |
|
|