GeezerVison is showing github.com — crypto: check for OpenSSL errors when signing · nodejs/node@7fe6dd8
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_crypto.cc +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3583,7 +3583,11 @@ SignBase::Error Sign::SignFinal(const char* key_pem, |
3583 | 3583 | nullptr, |
3584 | 3584 | CryptoPemCallback, |
3585 | 3585 | const_cast<char*>(passphrase)); |
3586 | | - if (pkey == nullptr) |
| 3586 | + |
| 3587 | + // Errors might be injected into OpenSSL's error stack |
| 3588 | + // without `pkey` being set to nullptr; |
| 3589 | + // cf. the test of `test_bad_rsa_privkey.pem` for an example. |
| 3590 | + if (pkey == nullptr || 0 != ERR_peek_error()) |
3587 | 3591 | goto exit; |
3588 | 3592 |
|
3589 | 3593 | if (EVP_SignFinal(&mdctx_, *sig, sig_len, pkey)) |
@@ -3631,6 +3635,9 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) { |
3631 | 3635 | md_len = 8192; // Maximum key size is 8192 bits |
3632 | 3636 | md_value = new unsigned char[md_len]; |
3633 | 3637 |
|
| 3638 | + ClearErrorOnReturn clear_error_on_return; |
| 3639 | + (void) &clear_error_on_return; // Silence compiler warning. |
| 3640 | + |
3634 | 3641 | Error err = sign->SignFinal( |
3635 | 3642 | buf, |
3636 | 3643 | buf_len, |
@@ -3965,6 +3972,9 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) { |
3965 | 3972 | unsigned char* out_value = nullptr; |
3966 | 3973 | size_t out_len = 0; |
3967 | 3974 |
|
| 3975 | + ClearErrorOnReturn clear_error_on_return; |
| 3976 | + (void) &clear_error_on_return; // Silence compiler warning. |
| 3977 | + |
3968 | 3978 | bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>( |
3969 | 3979 | kbuf, |
3970 | 3980 | klen, |
|
Collapse file
Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +-----BEGIN RSA PRIVATE KEY----- |
| 2 | +MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAz0ZHmXyxQSdWk6NF |
| 3 | +GRotTax0O94iHv843su0mOynV9QLvlAwMrUk9k4+/SwyLu0eE3iYsYgXstXi3t2u |
| 4 | +rDSIMwIDAQABAkAH4ag/Udp7m79TBdZOygwG9BPHYv7xJstGzYAkgHssf7Yd5ZuC |
| 5 | +hpKtBvWdPXZaAFbwF8NSisMl98Q/9zgB/q5BAiEA5zXuwMnwt4hE2YqzBDRFB4g9 |
| 6 | +I+v+l1soy6x7Wdqo9esCIQDlf15qDb26uRDurBioE3IpZstWIIvLDdKqviZXKMs8 |
| 7 | +2QIgWeC5QvA9RtsOCJLGLCg1fUwUmFYwzZ1+Kk6OVMuPSqkCIDIWFSXyL8kzoKVm |
| 8 | +O89axxyQCaqXWcsMDkEjVLzK82gpAiB7lzdDHr7MoMWwV2wC/heEFC2p0Rw4wg9j |
| 9 | +1V8QbL0Q0A== |
| 10 | +-----END RSA PRIVATE KEY----- |
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-crypto.js +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -124,5 +124,21 @@ assert.throws(function() { |
124 | 124 | crypto.createSign('RSA-SHA256').update('test').sign(priv); |
125 | 125 | }, /RSA_sign:digest too big for rsa key/); |
126 | 126 |
|
| 127 | +assert.throws(function() { |
| 128 | + // The correct header inside `test_bad_rsa_privkey.pem` should have been |
| 129 | + // -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- |
| 130 | + // instead of |
| 131 | + // -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY----- |
| 132 | + // It is generated in this way: |
| 133 | + // $ openssl genrsa -out mykey.pem 512; |
| 134 | + // $ openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem \ |
| 135 | + // -out private_key.pem -nocrypt; |
| 136 | + // Then open private_key.pem and change its header and footer. |
| 137 | + var sha1_privateKey = fs.readFileSync(common.fixturesDir + |
| 138 | + '/test_bad_rsa_privkey.pem', 'ascii'); |
| 139 | + // this would inject errors onto OpenSSL's error stack |
| 140 | + crypto.createSign('sha1').sign(sha1_privateKey); |
| 141 | +}, /asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag/); |
| 142 | + |
127 | 143 | // Make sure memory isn't released before being returned |
128 | 144 | console.log(crypto.randomBytes(16)); |