Repository navigation
Add a feature to parse SubjectPublicKeyInfo into (signature::Algorithm, SubjectPublicKey) - #378
marshallpierce wants to merge 5 commits into
Conversation
|
|
||
| pub fn bit_string_with_no_unused_bits<'a>(input: &mut untrusted::Reader<'a>) | ||
| -> Result<untrusted::Input<'a>, | ||
| error::Unspecified> { |
There was a problem hiding this comment.
Returning Unspecified here just gets turned into BadDER elsewhere... maybe in the future it would be nice to have DER-related functions return a DER error for easier mapping?
| )] | ||
| #![deny( | ||
| const_err, | ||
| dead_code, |
There was a problem hiding this comment.
Just temporary while things are in flux.
There was a problem hiding this comment.
Note to self: make sure this was reverted.
| @@ -0,0 +1,21 @@ | |||
| These files contain the binary DER encoding of the *values* of some | |||
There was a problem hiding this comment.
Copied from webpki.
|
|
||
| // XXX: Some of the BadDER tests should have better error codes, maybe? | ||
|
|
||
| // TODO need ECDSA_P256_SHA512 for these tests |
There was a problem hiding this comment.
is it intended to ever support P256 with SHA512? If not I'll just delete these (commented out) tests.
There was a problem hiding this comment.
No, we probably won't support ECDSA_P256_SHA512 at this point.
A note about these tests: webpki is not developed to the same quality standard as ring yet, though we're hoping to change that. When I first shared it, the only thing I wanted to really make sure was that we did something reasonable for the signature verification part. We copied these chromium tests because that was the fastest way to ship the first webpki prototype.
Now in ring we should reconsider what we're doing w.r.t. testing this.
There was a problem hiding this comment.
I was planning on scripting a few openssl invocations to generate keys of all the types in spki and all corresponding digests, generating sample signatures, and then writing tests to verify that signatures match/don't match as appropriate so that every algorithm/curve/digest flavor has at least some coverage. Open to suggestions, though!
| Err(VerifyWithSPKIError::UnsupportedSignatureAlgorithm)); | ||
| test_verify_signed_data_signature_outer!( | ||
| test_ecdsa_prime256v1_sha512_unused_bits_signature, | ||
| "ecdsa-prime256v1-sha512-unused-bits-signature.pem", |
There was a problem hiding this comment.
Test deleted: the signature DER is invalid, so not relevant for signature validation since we can't decode the signature itself.
| Err(VerifyWithSPKIError::UnsupportedSignatureAlgorithm)); | ||
| test_verify_signed_data!( | ||
| test_ecdsa_prime256v1_sha512_using_rsa_algorithm, | ||
| "ecdsa-prime256v1-sha512-using-rsa-algorithm.pem", |
There was a problem hiding this comment.
Test deleted: error is in Algorithm field, but that's not used for plain signature validation (only SignedData).
| &spki::RSA_PKCS1_2048_8192_SHA1, | ||
| Err(VerifyWithSPKIError::UnsupportedSignatureAlgorithmForPublicKey)); | ||
| test_verify_signed_data!(test_rsa_pkcs1_sha1_wrong_algorithm, | ||
| "rsa-pkcs1-sha1-wrong-algorithm.pem", |
There was a problem hiding this comment.
Test deleted: error is in Algorithm, which we don't use
| &spki::RSA_PKCS1_2048_8192_SHA256, | ||
| Err(VerifyWithSPKIError::InvalidSignatureForPublicKey)); | ||
| // TODO this only tests DER decoding, not signature logic | ||
| test_parse_spki_bad_outer!(test_rsa_pkcs1_sha256_key_encoded_ber, |
There was a problem hiding this comment.
Should these parse_spki_bad_outer tests be moved to the DER module? It doesn't really have anything to do with signatures.
There was a problem hiding this comment.
The existence of those tests in webpki is just an artifact of history, really. I think we should just leave them all out of the ring test suite.
There was a problem hiding this comment.
Works for me. I'll check to make sure that functionality has some tests but if so I'll delete these.
There was a problem hiding this comment.
There weren't tests that I saaw for that der method so I moved those tests over. This involved some mildly unpleasant compromises: I moved read_pem_section over to der::tests and used it from there for spki's testing needs. I felt it was worth it to not lose out on some real-world-ish test coverage for the various flavors of incorrect DER expressed by those tests, but I'll ditch 'em if you'd rather.
| test_verify_signed_data!( | ||
| test_rsa_pkcs1_sha256_using_ecdsa_algorithm, | ||
| "rsa-pkcs1-sha256-using-ecdsa-algorithm.pem", | ||
| Err(VerifyWithSPKIError::UnsupportedSignatureAlgorithmForPublicKey)); |
There was a problem hiding this comment.
Test deleted: error in Algorithm
| Err(VerifyWithSPKIError::UnsupportedSignatureAlgorithmForPublicKey)); | ||
|
|
||
| // Chromium's PSS test are for parameter combinations we don't support. | ||
| test_verify_signed_data!(test_rsa_pss_sha1_salt20_using_pss_key_no_params, |
There was a problem hiding this comment.
PSS tests deleted: no point in a bunch more tests that only check that 1024 bit keys aren't supported...
| /// A signature algorithm. | ||
| pub struct SignatureAlgorithm { | ||
| public_key_alg_id: AlgorithmIdentifier, | ||
| signature_alg_id: AlgorithmIdentifier, |
There was a problem hiding this comment.
This field is now unused since we don't need to select algorithms by digest. What do you think is the right abstraction? Should we remove this field (and all the associated constants)? Leave it, but document its use in SignedData?
It seems good to have some way to represent signature info, but I'm not sure how widely applicable those signature alg OIDs are.
There was a problem hiding this comment.
It seems like the right thing to remove signature_alg_id. That means that webpki would have to continue to define all of these things, as a wrapper around ring::signature::spki::SignatureAlgorithm and signature_alg_id.
There was a problem hiding this comment.
It might be a good idea to rename SignatureAlgorithm to AlgorithmIdentifier, leaving the Signature part implied by the fact that it is nested in ring::signature.
There was a problem hiding this comment.
OK, I'll work on that. A shame that "Signature" is overloaded both to be the general concept, the noun, and also the creation (as opposed to verification) of the thing...
Also, ICYMI, I made a few comments on an individual commit as I was massaging the tests that show as outdated.
There was a problem hiding this comment.
AlgorithmIdentifier was already claimed in spki so I went with Algorithm.
|
Note that in 0.7.x I'll be moving lots (most? almost all) of ring tests to tests/ because they're really integration tests. In particular, all tests that only use the public API will move to tests/. It seems like it would be great to do this from the beginning for this new feature. |
2 similar comments
2 similar comments
1 similar comment
3 similar comments
| // with SHA-1) say that parameters must be omitted. RFC 4055 Section 5 and RFC | ||
| // 3279 Section 2.2.1 both say that parameters for RSA must be encoded as NULL; | ||
| // we relax that requirement by allowing the NULL to be omitted, to match all | ||
| // the other signature algorithms we support and for compatibility. |
There was a problem hiding this comment.
Is this comment still relevant? The RSA_ENCRYPTION alg id has this structure:
0:d=0 hl=2 l= 9 prim: OBJECT :rsaEncryption
11:d=0 hl=2 l= 0 prim: NULL
It looks like we do require the NULL...
There was a problem hiding this comment.
I believe that comment is referring to signature algorithm identifiers, not key algorithm identifiers, so it won't apply in any case since we remove signature_alg_id in a later commit. And yes, it appears to be outdated. I just removed it in webpki: briansmith/webpki@2419bb7 and it should be removed here as well.
There was a problem hiding this comment.
It was out of date. I removed the comment in briansmith/webpki@2419bb7.
df359e3 to
899d57e
Compare
There was a problem hiding this comment.
Let me know what you think of this approach. I wanted to have some simple sanity checks that basic signature validation for all the supported algorithm combinations, and this seemed like a simple way to get that (assuming openssl's output is correct, of course).
There was a problem hiding this comment.
openssl's CLI doesn't support PSS signatures AFAICT but there's some C in http://openssl.6102.n7.nabble.com/RSASSA-PSS-Signature-Question-td22572.html that should be a good start to making a little single-purpose tool we can use for generating signatures.
There was a problem hiding this comment.
This was in the original tests; I'm not sure what it means at this point
There was a problem hiding this comment.
This is using the various inputs, signatures, etc generated by the gen-test-signatures.sh script to test success and failure modes for a given mode.
There was a problem hiding this comment.
Your comment in the PR about this using the gen-test-signature.sh script output as the inputs for the tests should be a comment in this file somewhere, perhaps at the top of the file.
There was a problem hiding this comment.
No ECDSA signing support yet, so we don't have an equivalent ECDSA "sign then verify the signature" scenario.
There was a problem hiding this comment.
Are the rsa_sign_and_verify_spki tests essential for covering the code? Normally we don't add this kind of test because, for example, we want all the verification tests to run even when RSA signing isn't enabled. Or, in other words, if this test is testing something important about signature verification then we need to find a way to do it even when RSA signing is disabled and also for ECDSA and EdDSA, but if it testing something important about signature verification then it seems like we should remove it and/or defer it. WDYT?
There was a problem hiding this comment.
I don't think the tests are essential. The other tests verify signatures generated with openssl's CLI, which does work and is certainly valuable from an interop perspective. However, it felt comforting to verify that using SPKI pub keys also works with signatures that ring generates, given that RSA signature generation is probabilistic so ring will not generate the same signatures as the openssl ones. I'd also be happy to add support for the other algorithms, but until ECDSA signing is supported, RSA is as far as I can go. For EdDSA, spki.rs doesn't currently have any algorithms for that suite; is that planned/desired?
Anyway, if the somewhat vague value proposition is not compelling, it wouldn't break my heart to remove the rsa sign-and-verify tests.
There was a problem hiding this comment.
Anyway, if the somewhat vague value proposition is not compelling, it wouldn't break my heart to remove the rsa sign-and-verify tests.
I suggest we pull them out for now and maybe put them in another PR so we can measure the code coverage without them.
There was a problem hiding this comment.
OK, the tests are in spki-rsa-sign-and-verify-tests for posterity.
There was a problem hiding this comment.
signature::RSAKeyPair::from_der fails when loading this 8192-bit key.
There was a problem hiding this comment.
copy-and-paste of equivalent code in der tests
There was a problem hiding this comment.
I think I understand that we need to copy/paste because these integration tests need to be run against the production version of ring, and we don't want these common functions to be part of the public ring API. Let's add a comment explaining this to this file.
Another way to do this, which I prefer, would be to put the common logic in a separate source file, e.g. tests/der_tests_common.rs, and then include! that file in ring::der::tests and in this file. WDYT?
Otherwise, we at least need to add some comments "Keep this in sync with the copy in <other file's relative path>" to both src/der.rs and this file, for each item that is duplicated.
There was a problem hiding this comment.
I like the include! approach. While I'm in there I might as well solve #452 because it will be trivial.
There was a problem hiding this comment.
It looks like in webpki it was convenient to carry around just the inner two components of SPKI, but the file that people would tend to load from an openssl exported key would start with the top level sequence. Both seem like pretty valid things to loosely call "SPKI". Thoughts on how to handle this? Two functions? Helper function to strip the outer layer of DER?
There was a problem hiding this comment.
If I understand you correctly, the current webpki strips the outermost SEQUENCE in a higher-level function and saves the insides, and only the inside parts are passed to the verification function. However, you're also saying ring's new verification function that takes SPKIs needs to strip the SEQUENCE itself. It would be nice if webpki could pass the entire SEQUENCE to the ring verification function so that two different functions aren't needed. Am I understanding things correctly?
If so, the webpki code can be changed to something like this:
...
let spki_begin reader.mark();
try!(der::nested(... der::SEQUENCE, |spki| Ok(())));
let spki_end = reader.mark();
let spki = try!(reader.get_input_between_marks(spki_begin, spki_end));
...Then spki would contain the outer later of the sequence. In fact, this logic should probably be encapsulated in ring::der as a function called something like expect_tag_and_get_tlv().
Then webpki could just use the public ring function, IIUC. WDYT?
There was a problem hiding this comment.
I think we're on the same page, but just to be sure I'll elaborate.
Tracing the data flow up from verify_signed_data:
EndEntityCert::verify_is_valid_tls_server_cert // top of call stack, takes a &[TrustAnchor]
verify_cert::build_chain // takes a &[TrustAnchor]
verify_cert::check_signatures // called on TrustAnchor.spki
signed_data::verify_signed_data
signed_data::verify_signature // function now in ring
And TrustAnchor is created in trust_anchor_util::parse_cert_v1 with an spki of:
let spki =
try!(der::expect_tag_and_get_value(tbs,
der::Tag::Sequence));
or via trust_anchor_from_cert in parse_cert_internal with identical code.
webpki's representation of spki as just the contents of the sequence is sensible because it saves a few mostly-meaningless bytes, especially when all the code that uses it is internal.
I think the choices are as follows:
- Leave webpki's representation as it is, and have ring's function continue to expect that as input. For users that haven't already stripped off the outer sequence (as when they're loading a key from a file), document the verification function to explain that
expect_tag_and_get_valueshould be called first on the DER. - Switch webpki to keep the complete SPKI DER internally, and switch ring's function accordingly. Now both webpki and average joes working with a public key have no intervening steps to process the key data from the point of loading the bytes. This is nice cleanliness-wise, but costs a few bytes of memory to hang onto the key, and a few cycles for every verification to strip off the sequence.
Ironically given how I started this feature request, I'm slightly inclined towards the former choice given the low-level nature and performance sensitivity of this library, but the latter is also pleasingly tidy. Either seems quite defensible, though. I think you're suggesting going with the second option; is that right?
|
Coverage decreased (-0.3%) to 92.922% when pulling 757f052ebc113f10ca2f7793e647f6122d23489a on marshallpierce:port-webpki-spki-signature-verification into 53fd7f9 on briansmith:master. |
1 similar comment
|
Coverage decreased (-0.3%) to 92.922% when pulling 757f052ebc113f10ca2f7793e647f6122d23489a on marshallpierce:port-webpki-spki-signature-verification into 53fd7f9 on briansmith:master. |
|
Coverage decreased (-0.3%) to 92.922% when pulling 757f052ebc113f10ca2f7793e647f6122d23489a on marshallpierce:port-webpki-spki-signature-verification into 53fd7f9 on briansmith:master. |
|
Coverage decreased (-0.3%) to 92.922% when pulling 757f052ebc113f10ca2f7793e647f6122d23489a on marshallpierce:port-webpki-spki-signature-verification into 53fd7f9 on briansmith:master. |
1 similar comment
|
Coverage decreased (-0.3%) to 92.922% when pulling 757f052ebc113f10ca2f7793e647f6122d23489a on marshallpierce:port-webpki-spki-signature-verification into 53fd7f9 on briansmith:master. |
Yes. If people are building other DER-parsing things, they might have to do the awkward thing I propose for webpki, but we could just make that less awkward in the future, if so. |
marshallpierce
left a comment
There was a problem hiding this comment.
I think this is now pretty close to done. There are a handful of comments that could use your input, especially the failure to load 8192-bit keypairs.
There was a problem hiding this comment.
Is it planned to support RSA SHA1 signatures? If not I'll remove the todo.
There was a problem hiding this comment.
We do support verifying RSA PKCS#1 SHA-1 signatures but not RSA PSS SHA-1 signatures. We don't support signing with SHA-1 at all.
There was a problem hiding this comment.
Is it planned to expose PSS modes for the 3072-8192bit key RSA algorithm? If not I'll remove this todo.
There was a problem hiding this comment.
Go ahead and remove that comment. I am not sure what we're doing with the 3072_8192 stuff yet.
There was a problem hiding this comment.
From here on are tests that existed already in webpki's version of this code
There was a problem hiding this comment.
I made this field public because it seemed like it might be generally useful.
There was a problem hiding this comment.
This one I didn't make public because I'm not sure why anyone outside this library would need it. But on the other hand, it feels odd to have one private field in a public struct...
There was a problem hiding this comment.
This one I made public because now people need to access it to use with the result of parse_spki.
There was a problem hiding this comment.
These are failing on android because the files are not present. I'll see if I can track down why.
There was a problem hiding this comment.
Looks like travis.sh needs to copy over the chromium *.pem files like it copies over the txt test files. However, the current setup also isn't running integration tests (like the new spki ones) since it only copies over target/(release|debug)/ring-* and the test file for spki is target/(release|debug)/signature_spki_tests-*.
There was a problem hiding this comment.
Android tests are now running all test targets.
2d16fd1 to
f3c8538
Compare
|
Coverage decreased (-0.2%) to 93.32% when pulling fe5cb4cbf152e3e5adb21ae7b4f40f5a85ee7d55 on marshallpierce:port-webpki-spki-signature-verification into a891756 on briansmith:master. |
|
Coverage decreased (-0.3%) to 93.268% when pulling fe5cb4cbf152e3e5adb21ae7b4f40f5a85ee7d55 on marshallpierce:port-webpki-spki-signature-verification into a891756 on briansmith:master. |
|
Hi, I'm getting caught up on my PRs now and Id like to review and merge this but I'm not certain what the state of this is. I think we had some agreement to replace Please rebase the changes on top of the current master (I just tested it myself locally and there was only one conflict). Also, please clean up the commits by folding fixes into the commits they fix. For example, fold the "Fix a comment typo;" commit into the commit that introduced the typo. Similarly, instead of having a commit that introduces the new I know that rewriting the PRs is a PITA (I do it all the time), but it's going to be too hard and time consuming for me to review the PR in its current state, and the harder it is to review the longer it will take me to find time to merge it. Thanks! |
contents of signed_data.rs, plus one der support function. webpki::Error was moved over and renamed to VerifyWithSPKIError (with some unused options trimmed), but otherwise modifications were limited to getting names resolving in the new structure. I agree to license my contributions to each file under the terms given at the top of each file I changed.
fe5cb4c to
a002497
Compare
…ication (not signature or digest algorithm selection, etc). Inapplicable tests deleted. Remove now-unused code for handling SignedData. Delete tests that would requre ECDSA p256 SHA512 as that will not be supported. I agree to license my contributions to each file under the terms given at the top of each file I changed.
… signing I agree to license my contributions to each file under the terms given at the top of each file I changed.
…licable here I agree to license my contributions to each file under the terms given at the top of each file I changed.
Rename SignatureAlgorithm to just Algorithm, and add some comments Generate test data, keys, and signatures for a variety of rsa and ecdsa parameters. Add integration tests for RSA and ECDSA spki signature verification. Move pem decoding logic to a helper module in `tests/common`. Turn warnings back on. Copy third-party/chromium/**.pem into android work dir. Tweak how the android build pushes test binaries into the emulator. I agree to license my contributions to each file under the terms given at the top of each file I changed.
a002497 to
e93d02f
Compare
|
Perhaps your local clone is out of date, or I'm misunderstanding what you're after? I've rebased and squashed so that the interstitial form of |
briansmith
left a comment
There was a problem hiding this comment.
Hi, it looks like maybe you just recently updated this PR? It happens that I've been working through it locally and I made some non-trivial changes to it, so I'm not sure your most recent changes are going to be captured in the PR. Mostly my modifications involve just getting rid of the moving of the tests from one place to another because they were unwieldy to review. I also suggest a change to the API. I also modified the webpki crate to use this code and made some modifications based on that experience. Please check out https://github.com/briansmith/ring/tree/port-webpki-spki-signature-verification and LMK what you think.
There was a problem hiding this comment.
When merging, I reformatting this and also moved it ahead of nonnegative_integer so that all the integer-parsing code stays grouped together.
|
|
||
| [target.'cfg(any(target_os = "redox", all(unix, not(any(target_os = "macos", target_os = "ios")))))'.dependencies] | ||
| lazy_static = "0.2.1" | ||
| [dev-dependencies] |
There was a problem hiding this comment.
When merging this, I put a blank line ahead of this new section.
|
|
||
| # JetBrains Junk | ||
| .idea | ||
| *.iml |
There was a problem hiding this comment.
I think I merged an earlier version of this change already, since .gitignore already contains these exact lines underneath "# Cargo Junk" below, so I removed this part of the change.
There was a problem hiding this comment.
I went ahead and removed annotate_test_data.py during merging since I really don't expect us to be using it in the future.
|
Sorry I haven't gotten to this; I probably won't have time to go through your changes any time soon. I'm happy to let you do what you like with this PR's code as you see fit to make it usable for webpki, etc. |
Thanks for your work on this. I'm going to close this issue and use what we learned from this to implement the feature later. |
Not yet ready for merging. I wanted to start adding notes and questions, though.