re PR libgcj/19729 (libgcj DSASignature.java null pointer exception)

2005-05-18  Thomas Fitzsimmons  <fitzsim@redhat.com>

	PR libgcj/19729
	* gnu/java/security/provider/DSASignature.java: Import updates
	from GNU Crypto.

From-SVN: r99904
This commit is contained in:
Thomas Fitzsimmons 2005-05-18 15:36:07 +00:00 committed by Thomas Fitzsimmons
parent 9a6411ed30
commit 33a9ae4927
2 changed files with 105 additions and 111 deletions

View File

@ -1,3 +1,9 @@
2005-05-18 Thomas Fitzsimmons <fitzsim@redhat.com>
PR libgcj/19729
* gnu/java/security/provider/DSASignature.java: Import updates
from GNU Crypto.
2005-05-18 Anthony Green <green@redhat.com> 2005-05-18 Anthony Green <green@redhat.com>
* jni/gtk-peer/gtk_jawt.c (classpath_jawt_object_lock, * jni/gtk-peer/gtk_jawt.c (classpath_jawt_object_lock,

View File

@ -51,7 +51,6 @@ import java.security.InvalidKeyException;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey; import java.security.PrivateKey;
import java.security.PublicKey; import java.security.PublicKey;
import java.security.SecureRandom; import java.security.SecureRandom;
@ -67,22 +66,17 @@ public class DSASignature extends SignatureSpi
{ {
private DSAPublicKey publicKey; private DSAPublicKey publicKey;
private DSAPrivateKey privateKey; private DSAPrivateKey privateKey;
private MessageDigest digest = null; private final MessageDigest digest;
private final SecureRandom random;
public DSASignature() public DSASignature() throws NoSuchAlgorithmException
{} {
random = new SecureRandom();
digest = MessageDigest.getInstance ("SHA1");
}
private void init() private void init()
{ {
if( digest == null ) {
try {
digest = MessageDigest.getInstance( "SHA1", "GNU" );
} catch ( NoSuchAlgorithmException nsae ) {
digest = null;
} catch ( NoSuchProviderException nspe ) {
digest = null;
}
}
digest.reset(); digest.reset();
} }
@ -102,7 +96,7 @@ public class DSASignature extends SignatureSpi
if (privateKey instanceof DSAPrivateKey) if (privateKey instanceof DSAPrivateKey)
this.privateKey = (DSAPrivateKey) privateKey; this.privateKey = (DSAPrivateKey) privateKey;
else else
throw new InvalidKeyException(); throw new InvalidKeyException ("not a DSA private key");
init(); init();
} }
@ -114,7 +108,7 @@ public class DSASignature extends SignatureSpi
if (privateKey instanceof DSAPrivateKey) if (privateKey instanceof DSAPrivateKey)
this.privateKey = (DSAPrivateKey) privateKey; this.privateKey = (DSAPrivateKey) privateKey;
else else
throw new InvalidKeyException(); throw new InvalidKeyException ("not a DSA private key");
appRandom = random; appRandom = random;
init(); init();
@ -123,38 +117,29 @@ public class DSASignature extends SignatureSpi
public void engineUpdate(byte b) public void engineUpdate(byte b)
throws SignatureException throws SignatureException
{ {
if( digest == null )
throw new SignatureException();
digest.update (b); digest.update (b);
} }
public void engineUpdate (byte[] b, int off, int len) public void engineUpdate (byte[] b, int off, int len)
throws SignatureException throws SignatureException
{ {
if( digest == null )
throw new SignatureException();
digest.update (b, off, len); digest.update (b, off, len);
} }
public byte[] engineSign() public byte[] engineSign() throws SignatureException
throws SignatureException
{ {
if( digest == null )
throw new SignatureException();
if (privateKey == null) if (privateKey == null)
throw new SignatureException(); throw new SignatureException ("not initialized for signing");
try {
try
{
BigInteger g = privateKey.getParams().getG(); BigInteger g = privateKey.getParams().getG();
BigInteger p = privateKey.getParams().getP(); BigInteger p = privateKey.getParams().getP();
BigInteger q = privateKey.getParams().getQ(); BigInteger q = privateKey.getParams().getQ();
BigInteger x = privateKey.getX(); BigInteger x = privateKey.getX();
BigInteger k = new BigInteger( 159, (Random)appRandom ); BigInteger k = new BigInteger (159, appRandom != null ? appRandom : random);
BigInteger r = g.modPow(k, p); BigInteger r = g.modPow(k, p);
r = r.mod(q); r = r.mod(q);
@ -167,14 +152,22 @@ public class DSASignature extends SignatureSpi
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
ArrayList seq = new ArrayList (2); ArrayList seq = new ArrayList (2);
seq.set(0, new DERValue(DER.INTEGER, r)); seq.add(0, new DERValue (DER.INTEGER, r));
seq.set(1, new DERValue(DER.INTEGER, s)); seq.add(1, new DERValue (DER.INTEGER, s));
DERWriter.write (bout, new DERValue (DER.CONSTRUCTED | DER.SEQUENCE, seq)); DERWriter.write (bout, new DERValue (DER.CONSTRUCTED | DER.SEQUENCE, seq));
return bout.toByteArray(); return bout.toByteArray();
} catch (IOException ioe) { }
throw new SignatureException(); catch (IOException ioe)
} catch ( ArithmeticException ae ) { {
throw new SignatureException(); SignatureException se = new SignatureException();
se.initCause (ioe);
throw se;
}
catch (ArithmeticException ae)
{
SignatureException se = new SignatureException();
se.initCause (ae);
throw se;
} }
} }
@ -183,7 +176,7 @@ public class DSASignature extends SignatureSpi
{ {
byte tmp[] = engineSign(); byte tmp[] = engineSign();
if (tmp.length > len) if (tmp.length > len)
throw new SignatureException(); throw new SignatureException ("output buffer too short");
System.arraycopy (tmp, 0, outbuf, offset, tmp.length); System.arraycopy (tmp, 0, outbuf, offset, tmp.length);
return tmp.length; return tmp.length;
} }
@ -192,7 +185,8 @@ public class DSASignature extends SignatureSpi
throws SignatureException throws SignatureException
{ {
// Decode sigBytes from ASN.1 DER encoding // Decode sigBytes from ASN.1 DER encoding
try { try
{
DERReader in = new DERReader (sigBytes); DERReader in = new DERReader (sigBytes);
DERValue val = in.read(); DERValue val = in.read();
if (!val.isConstructed()) if (!val.isConstructed())
@ -215,15 +209,18 @@ public class DSASignature extends SignatureSpi
BigInteger u2 = r.multiply (w).mod(q); BigInteger u2 = r.multiply (w).mod(q);
//This should test the compiler :)
BigInteger v = g.modPow (u1, p).multiply (y.modPow (u2, p)).mod (p).mod (q); BigInteger v = g.modPow (u1, p).multiply (y.modPow (u2, p)).mod (p).mod (q);
if (v.equals (r)) if (v.equals (r))
return true; return true;
else else
return false; return false;
} catch (IOException ioe) { }
throw new SignatureException("badly formed signature"); catch (IOException ioe)
{
SignatureException se = new SignatureException ("badly formed signature");
se.initCause (ioe);
throw se;
} }
} }
@ -247,17 +244,8 @@ public class DSASignature extends SignatureSpi
throw new InvalidParameterException(); throw new InvalidParameterException();
} }
public Object clone() public Object clone() throws CloneNotSupportedException
//throws CloneNotSupportedException
{ {
return new DSASignature( this ); return super.clone();
}
private DSASignature( DSASignature copy )
{
this();
this.publicKey = copy.publicKey;
this.privateKey = copy.privateKey;
this.digest = copy.digest;
} }
} }