You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by mu...@apache.org on 2021/10/04 21:14:19 UTC
[santuario-xml-security-java] 03/03: ECDSA SignatureValue do not
always have the specified length.
This is an automated email from the ASF dual-hosted git repository.
mullan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/santuario-xml-security-java.git
commit 9f78197fcb74f44b3ba056d27b3528b2c826592a
Author: Sean Mullan <se...@oracle.com>
AuthorDate: Mon Oct 4 17:12:42 2021 -0400
ECDSA SignatureValue do not always have the specified length.
---
.../jcp/xml/dsig/internal/dom/DOMSignatureMethod.java | 8 +++++++-
.../security/algorithms/implementations/ECDSAUtils.java | 16 ++++++++++++----
.../algorithms/implementations/SignatureECDSA.java | 16 +++++++++++++---
.../stax/impl/algorithms/PKISignatureAlgorithm.java | 17 ++++++++++++++++-
4 files changed, 48 insertions(+), 9 deletions(-)
diff --git a/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMSignatureMethod.java b/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMSignatureMethod.java
index a0f8a6a..ef8c9f0 100644
--- a/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMSignatureMethod.java
+++ b/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMSignatureMethod.java
@@ -28,6 +28,7 @@ import javax.xml.crypto.dsig.spec.SignatureMethodParameterSpec;
import java.io.IOException;
import java.security.*;
import java.security.interfaces.DSAKey;
+import java.security.interfaces.ECPrivateKey;
import java.security.spec.AlgorithmParameterSpec;
import org.w3c.dom.Element;
@@ -444,7 +445,12 @@ public abstract class DOMSignatureMethod extends AbstractDOMSignatureMethod {
// If signature is in ASN.1 (i.e., if the fallback algorithm
// was used), convert the signature to the P1363 format
if (asn1) {
- return SignatureECDSA.convertASN1toXMLDSIG(sig);
+ int rawLen = -1;
+ if (key instanceof ECPrivateKey) {
+ ECPrivateKey ecKey = (ECPrivateKey)key;
+ rawLen = (ecKey.getParams().getCurve().getField().getFieldSize() + 7) / 8;
+ }
+ return SignatureECDSA.convertASN1toXMLDSIG(sig, rawLen);
} else {
return sig;
}
diff --git a/src/main/java/org/apache/xml/security/algorithms/implementations/ECDSAUtils.java b/src/main/java/org/apache/xml/security/algorithms/implementations/ECDSAUtils.java
index 2523540..a3b845d 100644
--- a/src/main/java/org/apache/xml/security/algorithms/implementations/ECDSAUtils.java
+++ b/src/main/java/org/apache/xml/security/algorithms/implementations/ECDSAUtils.java
@@ -38,13 +38,15 @@ public final class ECDSAUtils {
* The JAVA JCE ECDSA Signature algorithm creates ASN.1 encoded (r, s) value
* pairs; the XML Signature requires the core BigInteger values.
*
- * @param asn1Bytes
- * @return the decode bytes
+ * @param asn1Bytes the ASN.1 encoded bytes
+ * @param rawLen the intended length of decoded bytes for an integer.
+ * If -1, choose one automatically.
+ * @return the decoded bytes
* @throws IOException
* @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
* @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
*/
- public static byte[] convertASN1toXMLDSIG(byte[] asn1Bytes) throws IOException {
+ public static byte[] convertASN1toXMLDSIG(byte[] asn1Bytes, int rawLen) throws IOException {
if (asn1Bytes.length < 8 || asn1Bytes[0] != 48) {
throw new IOException("Invalid ASN.1 format of ECDSA signature");
@@ -68,7 +70,13 @@ public final class ECDSAUtils {
for (j = sLength; j > 0 && asn1Bytes[offset + 2 + rLength + 2 + sLength - j] == 0; j--); //NOPMD
- int rawLen = Math.max(i, j);
+ int maxLen = Math.max(i, j);
+
+ if (rawLen < 0) {
+ rawLen = maxLen;
+ } else if (rawLen < maxLen) {
+ throw new IOException("Invalid signature length");
+ }
if ((asn1Bytes[offset - 1] & 0xff) != asn1Bytes.length - offset
|| (asn1Bytes[offset - 1] & 0xff) != 2 + rLength + 2 + sLength
diff --git a/src/main/java/org/apache/xml/security/algorithms/implementations/SignatureECDSA.java b/src/main/java/org/apache/xml/security/algorithms/implementations/SignatureECDSA.java
index d1aaa82..b7799b2 100644
--- a/src/main/java/org/apache/xml/security/algorithms/implementations/SignatureECDSA.java
+++ b/src/main/java/org/apache/xml/security/algorithms/implementations/SignatureECDSA.java
@@ -20,6 +20,7 @@ package org.apache.xml.security.algorithms.implementations;
import java.io.IOException;
import java.security.*;
+import java.security.interfaces.ECPrivateKey;
import java.security.spec.AlgorithmParameterSpec;
import org.apache.xml.security.algorithms.JCEMapper;
@@ -38,6 +39,9 @@ public abstract class SignatureECDSA extends SignatureAlgorithmSpi {
private final Signature signatureAlgorithm;
+ /** Length for each integer in signature */
+ private int signIntLen = -1;
+
/**
* Converts an ASN.1 ECDSA value to a XML Signature ECDSA Value.
*
@@ -45,14 +49,15 @@ public abstract class SignatureECDSA extends SignatureAlgorithmSpi {
* pairs; the XML Signature requires the core BigInteger values.
*
* @param asn1Bytes
+ * @param rawLen
* @return the decode bytes
*
* @throws IOException
* @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
* @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
*/
- public static byte[] convertASN1toXMLDSIG(byte[] asn1Bytes) throws IOException {
- return ECDSAUtils.convertASN1toXMLDSIG(asn1Bytes);
+ public static byte[] convertASN1toXMLDSIG(byte[] asn1Bytes, int rawLen) throws IOException {
+ return ECDSAUtils.convertASN1toXMLDSIG(asn1Bytes, rawLen);
}
/**
@@ -140,7 +145,7 @@ public abstract class SignatureECDSA extends SignatureAlgorithmSpi {
try {
byte[] jcebytes = this.signatureAlgorithm.sign();
- return SignatureECDSA.convertASN1toXMLDSIG(jcebytes);
+ return SignatureECDSA.convertASN1toXMLDSIG(jcebytes, signIntLen);
} catch (SignatureException | IOException ex) {
throw new XMLSignatureException(ex);
}
@@ -149,6 +154,11 @@ public abstract class SignatureECDSA extends SignatureAlgorithmSpi {
/** {@inheritDoc} */
protected void engineInitSign(Key privateKey, SecureRandom secureRandom)
throws XMLSignatureException {
+ if (privateKey instanceof ECPrivateKey) {
+ ECPrivateKey ecKey = (ECPrivateKey) privateKey;
+ signIntLen = (ecKey.getParams().getCurve().getField().getFieldSize() + 7) / 8;
+ // If not ECPrivateKey, signIntLen remains -1
+ }
engineInitSign(privateKey, secureRandom, this.signatureAlgorithm);
}
diff --git a/src/main/java/org/apache/xml/security/stax/impl/algorithms/PKISignatureAlgorithm.java b/src/main/java/org/apache/xml/security/stax/impl/algorithms/PKISignatureAlgorithm.java
index d348561..f636b84 100644
--- a/src/main/java/org/apache/xml/security/stax/impl/algorithms/PKISignatureAlgorithm.java
+++ b/src/main/java/org/apache/xml/security/stax/impl/algorithms/PKISignatureAlgorithm.java
@@ -24,6 +24,7 @@ import org.apache.xml.security.utils.JavaUtils;
import java.io.IOException;
import java.security.*;
+import java.security.interfaces.ECPrivateKey;
import java.security.spec.AlgorithmParameterSpec;
/**
@@ -33,6 +34,9 @@ public class PKISignatureAlgorithm implements SignatureAlgorithm {
private final String jceName;
private final Signature signature;
+ /** Length for each integer in signature */
+ private int signIntLen = -1;
+
public PKISignatureAlgorithm(String jceName, String jceProvider) throws NoSuchProviderException, NoSuchAlgorithmException {
this.jceName = jceName;
if (jceProvider != null) {
@@ -71,6 +75,7 @@ public class PKISignatureAlgorithm implements SignatureAlgorithm {
@Override
public void engineInitSign(Key signingKey) throws XMLSecurityException {
+ initSignIntLen(signingKey);
try {
signature.initSign((PrivateKey) signingKey);
} catch (InvalidKeyException e) {
@@ -80,6 +85,7 @@ public class PKISignatureAlgorithm implements SignatureAlgorithm {
@Override
public void engineInitSign(Key signingKey, SecureRandom secureRandom) throws XMLSecurityException {
+ initSignIntLen(signingKey);
try {
signature.initSign((PrivateKey) signingKey, secureRandom);
} catch (InvalidKeyException e) {
@@ -89,6 +95,7 @@ public class PKISignatureAlgorithm implements SignatureAlgorithm {
@Override
public void engineInitSign(Key signingKey, AlgorithmParameterSpec algorithmParameterSpec) throws XMLSecurityException {
+ initSignIntLen(signingKey);
try {
signature.initSign((PrivateKey) signingKey);
} catch (InvalidKeyException e) {
@@ -96,12 +103,20 @@ public class PKISignatureAlgorithm implements SignatureAlgorithm {
}
}
+ private void initSignIntLen(Key signingKey) {
+ if (signingKey instanceof ECPrivateKey) {
+ ECPrivateKey ecKey = (ECPrivateKey) signingKey;
+ signIntLen = (ecKey.getParams().getCurve().getField().getFieldSize() + 7) / 8;
+ // If not ECPrivateKey, signIntLen remains -1
+ }
+ }
+
@Override
public byte[] engineSign() throws XMLSecurityException {
try {
byte[] jcebytes = signature.sign();
if (this.jceName.contains("ECDSA")) {
- return ECDSAUtils.convertASN1toXMLDSIG(jcebytes);
+ return ECDSAUtils.convertASN1toXMLDSIG(jcebytes, signIntLen);
} else if (this.jceName.contains("DSA")) {
return JavaUtils.convertDsaASN1toXMLDSIG(jcebytes, 20);
}