-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
crypto: Deprecate createCipher for createCipheriv #13941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
cc8976b
3d0b5a5
1e810d0
8f2b81f
7f913ec
b7abe64
b16018a
39a2112
d0f2778
14e0566
3f0fdb2
b0fe314
0025983
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3340,7 +3340,6 @@ void CipherBase::Init(const char* cipher_type, | |
| } | ||
|
|
||
| unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
| unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
|
|
||
| int key_len = EVP_BytesToKey(cipher_, | ||
| EVP_md5(), | ||
|
|
@@ -3349,11 +3348,19 @@ void CipherBase::Init(const char* cipher_type, | |
| key_buf_len, | ||
| 1, | ||
| key, | ||
| iv); | ||
| nullptr); | ||
|
|
||
| EVP_CIPHER_CTX_init(&ctx_); | ||
| const bool encrypt = (kind_ == kCipher); | ||
|
|
||
| EVP_CipherInit_ex(&ctx_, cipher_, nullptr, nullptr, nullptr, encrypt); | ||
|
|
||
| const int iv_len = EVP_CIPHER_iv_length(cipher_); | ||
|
|
||
| if (encrypt && iv_len != 0) { | ||
| return env()->ThrowError("crypto.createCipher() is no longer supported with ciphers that require initialization vectors. Generate a random IV and pass it to crypto.createCipheriv()."); | ||
| } | ||
|
|
||
| if (!EVP_CIPHER_CTX_set_key_length(&ctx_, key_len)) { | ||
| EVP_CIPHER_CTX_cleanup(&ctx_); | ||
| return env()->ThrowError("Invalid key length"); | ||
|
|
@@ -3363,7 +3370,7 @@ void CipherBase::Init(const char* cipher_type, | |
| nullptr, | ||
| nullptr, | ||
| reinterpret_cast<unsigned char*>(key), | ||
| reinterpret_cast<unsigned char*>(iv), | ||
| nullptr, | ||
| kind_ == kCipher); | ||
| initialised_ = true; | ||
| } | ||
|
|
@@ -3443,14 +3450,22 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) { | |
|
|
||
| THROW_AND_RETURN_IF_NOT_STRING(args[0], "Cipher type"); | ||
| THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Key"); | ||
| THROW_AND_RETURN_IF_NOT_BUFFER(args[2], "IV"); | ||
|
|
||
| if (!Buffer::HasInstance(args[2]) && !args[2]->IsNull()) { | ||
| return env->ThrowTypeError("IV must be a buffer or null"); | ||
| } | ||
|
||
|
|
||
| const node::Utf8Value cipher_type(env->isolate(), args[0]); | ||
| ssize_t key_len = Buffer::Length(args[1]); | ||
| const char* key_buf = Buffer::Data(args[1]); | ||
| ssize_t iv_len = Buffer::Length(args[2]); | ||
| const char* iv_buf = Buffer::Data(args[2]); | ||
| cipher->InitIv(*cipher_type, key_buf, key_len, iv_buf, iv_len); | ||
|
|
||
| if (args[2]->IsNull()) { | ||
| cipher->InitIv(*cipher_type, key_buf, key_len, nullptr, 0); | ||
| } else { | ||
| ssize_t iv_len = Buffer::Length(args[2]); | ||
| const char* iv_buf = Buffer::Data(args[2]); | ||
| cipher->InitIv(*cipher_type, key_buf, key_len, iv_buf, iv_len); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No comments on whether this is something we should do (ping @nodejs/crypto) ... but this is not the correct way of doing it. There are two kinds of deprecations we use: documentation and runtime. A runtime deprecation uses either the
util.deprecate()orprocess.emitWarning('...', 'DeprecationWarning')API to emit a warning at runtime, but the code still continues to operate as it did in every other respect.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My PR intends to immediately prohibit the use of this API for ciphers that require an IV. The deprecation of this API entirely hasn’t been done yet. I’m guessing that would go in lib/crypto.js?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, and that's the point: Our deprecation policy requires that APIs go through a proper deprecation cycle so this PR would not be able to land as is. The first step is to do a docs or runtime deprecation as a semver-major, which would go into Node.js 9.0.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do have provisions for making exceptions for security-related issues but as this is more about protecting users against themselves, it's up for debate whether those provisions apply.
I'm personally leaning towards 'acceptable in node 9' (raising an exception, that is) but I won't hold it against anyone who feels differently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This prohibits users from using not only counter modes but also cbc modes. I'm not sure if the cbc mode has a security issue in this API.