Commit 33a9ae49 by Thomas Fitzsimmons Committed by Thomas Fitzsimmons

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
parent 9a6411ed
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,
......
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -51,7 +51,6 @@ import java.security.InvalidKeyException; ...@@ -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,54 +66,49 @@ public class DSASignature extends SignatureSpi ...@@ -67,54 +66,49 @@ 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();
} }
public void engineInitVerify(PublicKey publicKey) public void engineInitVerify (PublicKey publicKey)
throws InvalidKeyException throws InvalidKeyException
{ {
if( publicKey instanceof DSAPublicKey ) if (publicKey instanceof DSAPublicKey)
this.publicKey = (DSAPublicKey)publicKey; this.publicKey = (DSAPublicKey) publicKey;
else else
throw new InvalidKeyException(); throw new InvalidKeyException();
init(); init();
} }
public void engineInitSign(PrivateKey privateKey) public void engineInitSign (PrivateKey privateKey)
throws InvalidKeyException throws InvalidKeyException
{ {
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();
} }
public void engineInitSign(PrivateKey privateKey, public void engineInitSign (PrivateKey privateKey,
SecureRandom random) SecureRandom random)
throws InvalidKeyException throws InvalidKeyException
{ {
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,141 +117,135 @@ public class DSASignature extends SignatureSpi ...@@ -123,141 +117,135 @@ public class DSASignature extends SignatureSpi
public void engineUpdate(byte b) public void engineUpdate(byte b)
throws SignatureException throws SignatureException
{ {
if( digest == null ) digest.update (b);
throw new SignatureException();
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 ) digest.update (b, off, len);
throw new SignatureException();
digest.update( b, off, len );
} }
public byte[] engineSign() public byte[] engineSign() throws SignatureException
throws SignatureException
{ {
if( digest == null ) if (privateKey == null)
throw new SignatureException(); throw new SignatureException ("not initialized for signing");
if( privateKey == null)
throw new SignatureException();
try { try
{
BigInteger g = privateKey.getParams().getG();
BigInteger p = privateKey.getParams().getP();
BigInteger q = privateKey.getParams().getQ();
BigInteger g = privateKey.getParams().getG(); BigInteger x = privateKey.getX();
BigInteger p = privateKey.getParams().getP();
BigInteger q = privateKey.getParams().getQ();
BigInteger x = privateKey.getX(); BigInteger k = new BigInteger (159, appRandom != null ? appRandom : random);
BigInteger k = new BigInteger( 159, (Random)appRandom ); BigInteger r = g.modPow(k, p);
r = r.mod(q);
BigInteger r = g.modPow(k, p); byte bytes[] = digest.digest();
r = r.mod(q); BigInteger sha = new BigInteger (1, bytes);
byte bytes[] = digest.digest(); BigInteger s = sha.add (x.multiply (r));
BigInteger sha = new BigInteger(1, bytes); s = s.multiply (k.modInverse(q)).mod (q);
BigInteger s = sha.add( x.multiply( r ) ); ByteArrayOutputStream bout = new ByteArrayOutputStream();
s = s.multiply( k.modInverse(q) ).mod( q ); ArrayList seq = new ArrayList (2);
seq.add(0, new DERValue (DER.INTEGER, r));
ByteArrayOutputStream bout = new ByteArrayOutputStream(); seq.add(1, new DERValue (DER.INTEGER, s));
ArrayList seq = new ArrayList(2); DERWriter.write (bout, new DERValue (DER.CONSTRUCTED | DER.SEQUENCE, seq));
seq.set(0, new DERValue(DER.INTEGER, r)); return bout.toByteArray();
seq.set(1, new DERValue(DER.INTEGER, s)); }
DERWriter.write(bout, new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, seq)); catch (IOException ioe)
return bout.toByteArray(); {
} catch (IOException ioe) { SignatureException se = new SignatureException();
throw new SignatureException(); se.initCause (ioe);
} catch ( ArithmeticException ae ) { throw se;
throw new SignatureException(); }
} catch (ArithmeticException ae)
{
SignatureException se = new SignatureException();
se.initCause (ae);
throw se;
}
} }
public int engineSign(byte[] outbuf, int offset, int len) public int engineSign (byte[] outbuf, int offset, int len)
throws SignatureException throws SignatureException
{ {
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;
} }
public boolean engineVerify(byte[] sigBytes) public boolean engineVerify (byte[] sigBytes)
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); {
DERValue val = in.read(); DERReader in = new DERReader (sigBytes);
if (!val.isConstructed()) DERValue val = in.read();
throw new SignatureException("badly formed signature"); if (!val.isConstructed())
BigInteger r = (BigInteger) in.read().getValue(); throw new SignatureException ("badly formed signature");
BigInteger s = (BigInteger) in.read().getValue(); BigInteger r = (BigInteger) in.read().getValue();
BigInteger s = (BigInteger) in.read().getValue();
BigInteger g = publicKey.getParams().getG(); BigInteger g = publicKey.getParams().getG();
BigInteger p = publicKey.getParams().getP(); BigInteger p = publicKey.getParams().getP();
BigInteger q = publicKey.getParams().getQ(); BigInteger q = publicKey.getParams().getQ();
BigInteger y = publicKey.getY(); BigInteger y = publicKey.getY();
BigInteger w = s.modInverse( q ); BigInteger w = s.modInverse (q);
byte bytes[] = digest.digest(); byte bytes[] = digest.digest();
BigInteger sha = new BigInteger(1, bytes); BigInteger sha = new BigInteger (1, bytes);
BigInteger u1 = w.multiply( sha ).mod( q ); BigInteger u1 = w.multiply (sha).mod ( q );
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;
}
} }
public void engineSetParameter(String param, public void engineSetParameter (String param,
Object value) Object value)
throws InvalidParameterException throws InvalidParameterException
{ {
throw new InvalidParameterException(); throw new InvalidParameterException();
} }
public void engineSetParameter(AlgorithmParameterSpec params) public void engineSetParameter (AlgorithmParameterSpec params)
throws InvalidAlgorithmParameterException throws InvalidAlgorithmParameterException
{ {
throw new InvalidParameterException(); throw new InvalidParameterException();
} }
public Object engineGetParameter(String param) public Object engineGetParameter (String param)
throws InvalidParameterException throws InvalidParameterException
{ {
throw new InvalidParameterException(); throw new InvalidParameterException();
} }
public Object clone() public Object clone() throws CloneNotSupportedException
//throws CloneNotSupportedException
{
return new DSASignature( this );
}
private DSASignature( DSASignature copy )
{ {
this(); return super.clone();
this.publicKey = copy.publicKey;
this.privateKey = copy.privateKey;
this.digest = copy.digest;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment