-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
crypto: add sign/verify support for RSASSA-PSS #11705
Changes from 16 commits
a361f53
e467227
5a874a5
8a23882
15abaae
d75601e
5e78d9c
5e5f2e4
b9a6779
fcd7053
287e2d6
8dc383e
1679e0c
8b64c04
44b71e9
4f695aa
8772278
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 |
---|---|---|
|
@@ -975,10 +975,21 @@ Calculates the signature on all the data passed through using either | |
|
||
The `private_key` argument can be an object or a string. If `private_key` is a | ||
string, it is treated as a raw key with no passphrase. If `private_key` is an | ||
object, it is interpreted as a hash containing two properties: | ||
object, it must contain one or more of the following properties: | ||
|
||
* `key`: {string} - PEM encoded private key | ||
* `key`: {string} - PEM encoded private key (required) | ||
* `passphrase`: {string} - passphrase for the private key | ||
* `padding`: {integer} - Optional padding value for RSA, one of the following: | ||
* `crypto.constants.RSA_PKCS1_PADDING` (default) | ||
* `crypto.constants.RSA_PKCS1_PSS_PADDING` | ||
|
||
Note that `RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function | ||
used to sign the message as specified in section 3.1 of [RFC 4055][]. | ||
* `saltLength`: {integer} - salt length for when padding is | ||
`RSA_PKCS1_PSS_PADDING`. The special value | ||
`crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest | ||
size, `crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN` (default) sets it to the | ||
maximum permissible value. | ||
|
||
The `output_format` can specify one of `'latin1'`, `'hex'` or `'base64'`. If | ||
`output_format` is provided a string is returned; otherwise a [`Buffer`][] is | ||
|
@@ -1074,18 +1085,32 @@ This can be called many times with new data as it is streamed. | |
<!-- YAML | ||
added: v0.1.92 | ||
--> | ||
- `object` {string} | ||
- `object` {string | Object} | ||
- `signature` {string | Buffer | Uint8Array} | ||
- `signature_format` {string} | ||
|
||
Verifies the provided data using the given `object` and `signature`. | ||
The `object` argument is a string containing a PEM encoded object, which can be | ||
an RSA public key, a DSA public key, or an X.509 certificate. | ||
The `object` argument can be either a string containing a PEM encoded object, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is also needed.
|
||
which can be an RSA public key, a DSA public key, or an X.509 certificate, | ||
or an object with one or more of the following properties: | ||
|
||
* `key`: {string} - PEM encoded private key (required) | ||
* `padding`: {integer} - Optional padding value for RSA, one of the following: | ||
* `crypto.constants.RSA_PKCS1_PADDING` (default) | ||
* `crypto.constants.RSA_PKCS1_PSS_PADDING` | ||
|
||
Note that `RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function | ||
used to verify the message as specified in section 3.1 of [RFC 4055][]. | ||
* `saltLength`: {integer} - salt length for when padding is | ||
`RSA_PKCS1_PSS_PADDING`. The special value | ||
`crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest | ||
size, `crypto.constants.RSA_PSS_SALTLEN_AUTO` (default) causes it to be | ||
determined automatically. | ||
|
||
The `signature` argument is the previously calculated signature for the data, in | ||
the `signature_format` which can be `'latin1'`, `'hex'` or `'base64'`. | ||
If a `signature_format` is specified, the `signature` is expected to be a | ||
string; otherwise `signature` is expected to be a [`Buffer`][] or | ||
`Uint8Array`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shigeki Yes, I can fix that while landing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tniessen Actually, can you address this and also add short There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @addaleax Okay, please take care of this with your approval. Thanks. |
||
string; otherwise `signature` is expected to be a [`Buffer`][]. | ||
|
||
Returns `true` or `false` depending on the validity of the signature for | ||
the data and public key. | ||
|
@@ -2044,6 +2069,21 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL. | |
<td><code>RSA_PKCS1_PSS_PADDING</code></td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td><code>RSA_PSS_SALTLEN_DIGEST</code></td> | ||
<td>Sets the salt length for `RSA_PKCS1_PSS_PADDING` to the digest size | ||
when signing or verifying.</td> | ||
</tr> | ||
<tr> | ||
<td><code>RSA_PSS_SALTLEN_MAX_SIGN</code></td> | ||
<td>Sets the salt length for `RSA_PKCS1_PSS_PADDING` to the maximum | ||
permissible value when signing data.</td> | ||
</tr> | ||
<tr> | ||
<td><code>RSA_PSS_SALTLEN_AUTO</code></td> | ||
<td>Causes the salt length for `RSA_PKCS1_PSS_PADDING` to be determined | ||
automatically when verifying a signature.</td> | ||
</tr> | ||
<tr> | ||
<td><code>POINT_CONVERSION_COMPRESSED</code></td> | ||
<td></td> | ||
|
@@ -2119,6 +2159,7 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL. | |
[publicly trusted list of CAs]: https://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt | ||
[RFC 2412]: https://www.rfc-editor.org/rfc/rfc2412.txt | ||
[RFC 3526]: https://www.rfc-editor.org/rfc/rfc3526.txt | ||
[RFC 4055]: https://www.rfc-editor.org/rfc/rfc4055.txt | ||
[stream]: stream.html | ||
[stream-writable-write]: stream.html#stream_writable_write_chunk_encoding_callback | ||
[Crypto Constants]: #crypto_crypto_constants_1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,7 +303,28 @@ Sign.prototype.sign = function sign(options, encoding) { | |
|
||
var key = options.key || options; | ||
var passphrase = options.passphrase || null; | ||
var ret = this._handle.sign(toBuf(key), null, passphrase); | ||
|
||
// Options specific to RSA | ||
var rsaPadding = constants.RSA_PKCS1_PADDING; | ||
if (options.hasOwnProperty('padding')) { | ||
if (options.padding === options.padding >> 0) { | ||
rsaPadding = options.padding; | ||
} else { | ||
throw new TypeError('padding must be an integer'); | ||
} | ||
} | ||
|
||
var pssSaltLength = constants.RSA_PSS_SALTLEN_AUTO; | ||
if (options.hasOwnProperty('saltLength')) { | ||
if (options.saltLength === options.saltLength >> 0) { | ||
pssSaltLength = options.saltLength; | ||
} else { | ||
throw new TypeError('saltLength must be an integer'); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could perhaps factor this out into a separate function but OTOH, this is fine too. |
||
|
||
var ret = this._handle.sign(toBuf(key), null, passphrase, rsaPadding, | ||
pssSaltLength); | ||
|
||
encoding = encoding || exports.DEFAULT_ENCODING; | ||
if (encoding && encoding !== 'buffer') | ||
|
@@ -329,9 +350,31 @@ util.inherits(Verify, stream.Writable); | |
Verify.prototype._write = Sign.prototype._write; | ||
Verify.prototype.update = Sign.prototype.update; | ||
|
||
Verify.prototype.verify = function verify(object, signature, sigEncoding) { | ||
Verify.prototype.verify = function verify(options, signature, sigEncoding) { | ||
var key = options.key || options; | ||
sigEncoding = sigEncoding || exports.DEFAULT_ENCODING; | ||
return this._handle.verify(toBuf(object), toBuf(signature, sigEncoding)); | ||
|
||
// Options specific to RSA | ||
var rsaPadding = constants.RSA_PKCS1_PADDING; | ||
if (options.hasOwnProperty('padding')) { | ||
if (options.padding === options.padding >> 0) { | ||
rsaPadding = options.padding; | ||
} else { | ||
throw new TypeError('padding must be an integer'); | ||
} | ||
} | ||
|
||
var pssSaltLength = constants.RSA_PSS_SALTLEN_AUTO; | ||
if (options.hasOwnProperty('saltLength')) { | ||
if (options.saltLength === options.saltLength >> 0) { | ||
pssSaltLength = options.saltLength; | ||
} else { | ||
throw new TypeError('saltLength must be an integer'); | ||
} | ||
} | ||
|
||
return this._handle.verify(toBuf(key), toBuf(signature, sigEncoding), null, | ||
rsaPadding, pssSaltLength); | ||
}; | ||
|
||
function rsaPublic(method, defaultPadding) { | ||
|
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.
Adding a blank line for line wrapping looks better.