pjfanning commented on code in PR #1595:
URL: https://github.com/apache/poi/pull/1595#discussion_r4726512894
##########
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java:
##########
@@ -201,14 +201,22 @@ public void setUriDereferencer(URIDereferencer
uriDereferencer) {
}
/**
- * @return true, if first signature part is valid
+ * @return true, if the package has at least one signature and all of its
signatures are valid
*/
public boolean verifySignature() {
initXmlProvider();
//
http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html
- // only validate first part
- Iterator<SignaturePart> iter = getSignatureParts().iterator();
- return iter.hasNext() && iter.next().validate();
+ // a package can hold several signatures, e.g. when a document is
co-signed, and which one
+ // comes first follows from the ids of the signature origin's
relationships, so validating
+ // one of them says nothing about the state of the others
+ boolean signed = false;
+ for (SignaturePart sp : getSignatureParts()) {
+ if (!sp.validate()) {
Review Comment:
Two things about calling `validate()` on every part:
1. `SignaturePart.validate()` throws `EncryptedDocumentException` rather
than returning false for malformed XML, unmarshalling errors and most
`XMLSignatureException` causes. A package with a valid first signature and a
second one that POI cannot process (truncated XML, unsupported transform or
algorithm) used to give true and would now throw. Is an exception the intended
outcome there, or should it be false? Either way it should be in the javadoc
(`@throws`) and covered by a test.
2. With `SignatureConfig.setUpdateConfigOnValidate(true)`, each successful
`validate()` overwrites the certificate chain, digest algorithm, execution time
etc. on the shared config. After this change the config describes the last
valid signature instead of the first, and if a later signature is invalid the
method returns false with the config still holding the earlier signer's
details. Should the config only be updated once, or should this be documented?
##########
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java:
##########
@@ -201,14 +201,22 @@ public void setUriDereferencer(URIDereferencer
uriDereferencer) {
}
/**
- * @return true, if first signature part is valid
+ * @return true, if the package has at least one signature and all of its
signatures are valid
*/
public boolean verifySignature() {
initXmlProvider();
//
http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html
- // only validate first part
- Iterator<SignaturePart> iter = getSignatureParts().iterator();
- return iter.hasNext() && iter.next().validate();
+ // a package can hold several signatures, e.g. when a document is
co-signed, and which one
+ // comes first follows from the ids of the signature origin's
relationships, so validating
+ // one of them says nothing about the state of the others
+ boolean signed = false;
+ for (SignaturePart sp : getSignatureParts()) {
Review Comment:
The loop now reaches signature relationships that the old code never looked
at, and the iterator is lenient/fragile about ones it cannot resolve:
1. A relationship in `origin.sigs.rels` whose target part does not exist
(e.g. `Target="sig2.xml"` with no such part): I'd expect `getRelatedPart` to
throw `IllegalArgumentException`, so `verifySignature()` throws instead of
returning a boolean. If the last relationship has an invalid or external
target, `next()` looks like it would throw `NoSuchElementException` after
`hasNext()` returned true.
2. A relationship with an invalid target that is *not* the last one is
logged and skipped by the iterator. The loop never sees it, so
`verifySignature()` returns true although a registered signature was not
checked.
For a method that now promises that all signatures are valid, I think a
signature relationship that does not resolve to a part should make the result
false (or a documented exception), rather than depend on its position. Could
that be handled here, with tests for a missing `sig2.xml` part and an
unresolvable relationship before the valid one?
##########
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java:
##########
@@ -201,14 +201,22 @@ public void setUriDereferencer(URIDereferencer
uriDereferencer) {
}
/**
- * @return true, if first signature part is valid
+ * @return true, if the package has at least one signature and all of its
signatures are valid
Review Comment:
This is a behaviour change for existing callers: a co-signed document with a
valid first signature and a stale or invalid additional one flips from true to
false. I think that is the right direction, but it should be called out beyond
this javadoc line, e.g. in the encryption/signature docs, pointing to
`getSignatureParts()` for per-signature results.
##########
poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java:
##########
@@ -704,6 +704,65 @@ void testDuplicatePackageObjectId() throws Exception {
}
}
+ @Test
+ void testSecondSignatureIsValidated() throws Exception {
+ // a copy of the signature part, registered as a second signature of
the same document
+ try (OPCPackage pkg = OPCPackage.open(addSecondSignature(false))) {
+ SignatureInfo si = new SignatureInfo();
+ si.setOpcPackage(pkg);
+ si.setSignatureConfig(new SignatureConfig());
+ assertTrue(si.verifySignature(), "a copy of a valid signature is
valid as well");
+ }
+
+ // the same copy with a reference digest its signature value doesn't
cover anymore
+ try (OPCPackage pkg = OPCPackage.open(addSecondSignature(true))) {
Review Comment:
The corrupt signature is always the second one (`rId2`) here. Could the test
also cover the corrupt signature first with the valid one second, since the
code comment is about the order not mattering? A per-part check through
`getSignatureParts()` (first valid, second not) would also show that it is the
second signature that fails the result.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]