Imported GNU Classpath 0.90
Imported GNU Classpath 0.90
* scripts/makemake.tcl: Set gnu/java/awt/peer/swing to ignore.
* gnu/classpath/jdwp/VMFrame.java (SIZE): New constant.
* java/lang/VMCompiler.java: Use gnu.java.security.hash.MD5.
* java/lang/Math.java: New override file.
* java/lang/Character.java: Merged from Classpath.
(start, end): Now 'int's.
(canonicalName): New field.
(CANONICAL_NAME, NO_SPACES_NAME, CONSTANT_NAME): New constants.
(UnicodeBlock): Added argument.
(of): New overload.
(forName): New method.
Updated unicode blocks.
(sets): Updated.
* sources.am: Regenerated.
* Makefile.in: Likewise.
From-SVN: r111942
This commit is contained in:
@@ -0,0 +1,347 @@
|
||||
/* DSSSignature.java --
|
||||
Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
|
||||
|
||||
This file is a part of GNU Classpath.
|
||||
|
||||
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
|
||||
the Free Software Foundation; either version 2 of the License, or (at
|
||||
your option) any later version.
|
||||
|
||||
GNU Classpath is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GNU Classpath; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
||||
USA
|
||||
|
||||
Linking this library statically or dynamically with other modules is
|
||||
making a combined work based on this library. Thus, the terms and
|
||||
conditions of the GNU General Public License cover the whole
|
||||
combination.
|
||||
|
||||
As a special exception, the copyright holders of this library give you
|
||||
permission to link this library with independent modules to produce an
|
||||
executable, regardless of the license terms of these independent
|
||||
modules, and to copy and distribute the resulting executable under
|
||||
terms of your choice, provided that you also meet, for each linked
|
||||
independent module, the terms and conditions of the license of that
|
||||
module. An independent module is a module which is not derived from
|
||||
or based on this library. If you modify this library, you may extend
|
||||
this exception to your version of the library, but you are not
|
||||
obligated to do so. If you do not wish to do so, delete this
|
||||
exception statement from your version. */
|
||||
|
||||
|
||||
package gnu.java.security.sig.dss;
|
||||
|
||||
import gnu.java.security.Registry;
|
||||
import gnu.java.security.hash.IMessageDigest;
|
||||
import gnu.java.security.hash.Sha160;
|
||||
import gnu.java.security.prng.IRandom;
|
||||
import gnu.java.security.sig.BaseSignature;
|
||||
import gnu.java.security.sig.ISignature;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.interfaces.DSAPrivateKey;
|
||||
import java.security.interfaces.DSAPublicKey;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* <p>The DSS (Digital Signature Standard) algorithm makes use of the following
|
||||
* parameters:</p>
|
||||
*
|
||||
* <ol>
|
||||
* <li>p: A prime modulus, where <code>2<sup>L-1</sup> < p < 2<sup>L</sup>
|
||||
* </code> for <code>512 <= L <= 1024</code> and <code>L</code> a
|
||||
* multiple of <code>64</code>.</li>
|
||||
* <li>q: A prime divisor of <code>p - 1</code>, where <code>2<sup>159</sup>
|
||||
* < q < 2<sup>160</sup></code>.</li>
|
||||
* <li>g: Where <code>g = h<sup>(p-1)</sup>/q mod p</code>, where
|
||||
* <code>h</code> is any integer with <code>1 < h < p - 1</code> such
|
||||
* that <code>h<sup> (p-1)</sup>/q mod p > 1</code> (<code>g</code> has order
|
||||
* <code>q mod p</code>).</li>
|
||||
* <li>x: A randomly or pseudorandomly generated integer with <code>0 < x
|
||||
* < q</code>.</li>
|
||||
* <li>y: <code>y = g<sup>x</sup> mod p</code>.</li>
|
||||
* <li>k: A randomly or pseudorandomly generated integer with <code>0 < k
|
||||
* < q</code>.</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>The integers <code>p</code>, <code>q</code>, and <code>g</code> can be
|
||||
* public and can be common to a group of users. A user's private and public
|
||||
* keys are <code>x</code> and <code>y</code>, respectively. They are normally
|
||||
* fixed for a period of time. Parameters <code>x</code> and <code>k</code> are
|
||||
* used for signature generation only, and must be kept secret. Parameter
|
||||
* <code>k</code> must be regenerated for each signature.</p>
|
||||
*
|
||||
* <p>The signature of a message <code>M</code> is the pair of numbers <code>r</code>
|
||||
* and <code>s</code> computed according to the equations below:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li><code>r = (g<sup>k</sup> mod p) mod q</code> and</li>
|
||||
* <li><code>s = (k<sup>-1</sup>(SHA(M) + xr)) mod q</code>.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>In the above, <code>k<sup>-1</sup></code> is the multiplicative inverse of
|
||||
* <code>k</code>, <code>mod q</code>; i.e., <code>(k<sup>-1</sup> k) mod q = 1
|
||||
* </code> and <code>0 < k-1 < q</code>. The value of <code>SHA(M)</code>
|
||||
* is a 160-bit string output by the Secure Hash Algorithm specified in FIPS 180.
|
||||
* For use in computing <code>s</code>, this string must be converted to an
|
||||
* integer.</p>
|
||||
*
|
||||
* <p>As an option, one may wish to check if <code>r == 0</code> or <code>s == 0
|
||||
* </code>. If either <code>r == 0</code> or <code>s == 0</code>, a new value
|
||||
* of <code>k</code> should be generated and the signature should be
|
||||
* recalculated (it is extremely unlikely that <code>r == 0</code> or <code>s ==
|
||||
* 0</code> if signatures are generated properly).</p>
|
||||
*
|
||||
* <p>The signature is transmitted along with the message to the verifier.</p>
|
||||
*
|
||||
* <p>References:</p>
|
||||
* <ol>
|
||||
* <li><a href="http://www.itl.nist.gov/fipspubs/fip186.htm">Digital
|
||||
* Signature Standard (DSS)</a>, Federal Information Processing Standards
|
||||
* Publication 186. National Institute of Standards and Technology.</li>
|
||||
* </ol>
|
||||
*
|
||||
* @version $Revision: 1.1 $
|
||||
*/
|
||||
public class DSSSignature extends BaseSignature
|
||||
{
|
||||
|
||||
// Constants and variables
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Constructor(s)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Trivial 0-arguments constructor. */
|
||||
public DSSSignature()
|
||||
{
|
||||
super(Registry.DSS_SIG, new Sha160());
|
||||
}
|
||||
|
||||
/** Private constructor for cloning purposes. */
|
||||
private DSSSignature(DSSSignature that)
|
||||
{
|
||||
this();
|
||||
|
||||
this.publicKey = that.publicKey;
|
||||
this.privateKey = that.privateKey;
|
||||
this.md = (IMessageDigest) that.md.clone();
|
||||
}
|
||||
|
||||
// Class methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
public static final BigInteger[] sign(final DSAPrivateKey k, final byte[] h)
|
||||
{
|
||||
final DSSSignature sig = new DSSSignature();
|
||||
final Map attributes = new HashMap();
|
||||
attributes.put(ISignature.SIGNER_KEY, k);
|
||||
sig.setupSign(attributes);
|
||||
|
||||
return sig.computeRS(h);
|
||||
}
|
||||
|
||||
public static final BigInteger[] sign(final DSAPrivateKey k, final byte[] h,
|
||||
Random rnd)
|
||||
{
|
||||
final DSSSignature sig = new DSSSignature();
|
||||
final Map attributes = new HashMap();
|
||||
attributes.put(ISignature.SIGNER_KEY, k);
|
||||
if (rnd != null)
|
||||
{
|
||||
attributes.put(ISignature.SOURCE_OF_RANDOMNESS, rnd);
|
||||
}
|
||||
sig.setupSign(attributes);
|
||||
|
||||
return sig.computeRS(h);
|
||||
}
|
||||
|
||||
public static final BigInteger[] sign(final DSAPrivateKey k, final byte[] h,
|
||||
IRandom irnd)
|
||||
{
|
||||
final DSSSignature sig = new DSSSignature();
|
||||
final Map attributes = new HashMap();
|
||||
attributes.put(ISignature.SIGNER_KEY, k);
|
||||
if (irnd != null)
|
||||
{
|
||||
attributes.put(ISignature.SOURCE_OF_RANDOMNESS, irnd);
|
||||
}
|
||||
sig.setupSign(attributes);
|
||||
|
||||
return sig.computeRS(h);
|
||||
}
|
||||
|
||||
public static final boolean verify(final DSAPublicKey k, final byte[] h,
|
||||
final BigInteger[] rs)
|
||||
{
|
||||
final DSSSignature sig = new DSSSignature();
|
||||
final Map attributes = new HashMap();
|
||||
attributes.put(ISignature.VERIFIER_KEY, k);
|
||||
sig.setupVerify(attributes);
|
||||
|
||||
return sig.checkRS(rs, h);
|
||||
}
|
||||
|
||||
// Implementation of abstract methods in superclass
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
public Object clone()
|
||||
{
|
||||
return new DSSSignature(this);
|
||||
}
|
||||
|
||||
protected void setupForVerification(PublicKey k)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if (!(k instanceof DSAPublicKey))
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.publicKey = k;
|
||||
}
|
||||
|
||||
protected void setupForSigning(PrivateKey k) throws IllegalArgumentException
|
||||
{
|
||||
if (!(k instanceof DSAPrivateKey))
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.privateKey = k;
|
||||
}
|
||||
|
||||
protected Object generateSignature() throws IllegalStateException
|
||||
{
|
||||
// BigInteger p = ((DSAPrivateKey) privateKey).getParams().getP();
|
||||
// BigInteger q = ((DSAPrivateKey) privateKey).getParams().getQ();
|
||||
// BigInteger g = ((DSAPrivateKey) privateKey).getParams().getG();
|
||||
// BigInteger x = ((DSAPrivateKey) privateKey).getX();
|
||||
// BigInteger m = new BigInteger(1, md.digest());
|
||||
// BigInteger k, r, s;
|
||||
//
|
||||
// byte[] kb = new byte[20]; // we'll use 159 bits only
|
||||
// while (true) {
|
||||
// this.nextRandomBytes(kb);
|
||||
// k = new BigInteger(1, kb);
|
||||
// k.clearBit(159);
|
||||
// r = g.modPow(k, p).mod(q);
|
||||
// if (r.equals(BigInteger.ZERO)) {
|
||||
// continue;
|
||||
// }
|
||||
// s = m.add(x.multiply(r)).multiply(k.modInverse(q)).mod(q);
|
||||
// if (s.equals(BigInteger.ZERO)) {
|
||||
// continue;
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
final BigInteger[] rs = computeRS(md.digest());
|
||||
|
||||
// return encodeSignature(r, s);
|
||||
return encodeSignature(rs[0], rs[1]);
|
||||
}
|
||||
|
||||
protected boolean verifySignature(Object sig) throws IllegalStateException
|
||||
{
|
||||
final BigInteger[] rs = decodeSignature(sig);
|
||||
// BigInteger r = rs[0];
|
||||
// BigInteger s = rs[1];
|
||||
//
|
||||
// BigInteger g = ((DSAPublicKey) publicKey).getParams().getG();
|
||||
// BigInteger p = ((DSAPublicKey) publicKey).getParams().getP();
|
||||
// BigInteger q = ((DSAPublicKey) publicKey).getParams().getQ();
|
||||
// BigInteger y = ((DSAPublicKey) publicKey).getY();
|
||||
// BigInteger w = s.modInverse(q);
|
||||
//
|
||||
// byte bytes[] = md.digest();
|
||||
// BigInteger u1 = w.multiply(new BigInteger(1, bytes)).mod(q);
|
||||
// BigInteger u2 = r.multiply(w).mod(q);
|
||||
//
|
||||
// BigInteger v = g.modPow(u1, p).multiply(y.modPow(u2, p)).mod(p).mod(q);
|
||||
// return v.equals(r);
|
||||
return checkRS(rs, md.digest());
|
||||
}
|
||||
|
||||
// Other instance methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the output of a signature generation phase.<p>
|
||||
*
|
||||
* @return an object encapsulating the DSS signature pair <code>r</code> and
|
||||
* <code>s</code>.
|
||||
*/
|
||||
private Object encodeSignature(BigInteger r, BigInteger s)
|
||||
{
|
||||
return new BigInteger[] { r, s };
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the output of a previously generated signature object as a pair
|
||||
* of {@link java.math.BigInteger}.<p>
|
||||
*
|
||||
* @return the DSS signature pair <code>r</code> and <code>s</code>.
|
||||
*/
|
||||
private BigInteger[] decodeSignature(Object signature)
|
||||
{
|
||||
return (BigInteger[]) signature;
|
||||
}
|
||||
|
||||
private BigInteger[] computeRS(final byte[] digestBytes)
|
||||
{
|
||||
final BigInteger p = ((DSAPrivateKey) privateKey).getParams().getP();
|
||||
final BigInteger q = ((DSAPrivateKey) privateKey).getParams().getQ();
|
||||
final BigInteger g = ((DSAPrivateKey) privateKey).getParams().getG();
|
||||
final BigInteger x = ((DSAPrivateKey) privateKey).getX();
|
||||
final BigInteger m = new BigInteger(1, digestBytes);
|
||||
BigInteger k, r, s;
|
||||
|
||||
final byte[] kb = new byte[20]; // we'll use 159 bits only
|
||||
while (true)
|
||||
{
|
||||
this.nextRandomBytes(kb);
|
||||
k = new BigInteger(1, kb);
|
||||
k.clearBit(159);
|
||||
r = g.modPow(k, p).mod(q);
|
||||
if (r.equals(BigInteger.ZERO))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
s = m.add(x.multiply(r)).multiply(k.modInverse(q)).mod(q);
|
||||
if (s.equals(BigInteger.ZERO))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return new BigInteger[] { r, s };
|
||||
}
|
||||
|
||||
private boolean checkRS(final BigInteger[] rs, final byte[] digestBytes)
|
||||
{
|
||||
final BigInteger r = rs[0];
|
||||
final BigInteger s = rs[1];
|
||||
|
||||
final BigInteger g = ((DSAPublicKey) publicKey).getParams().getG();
|
||||
final BigInteger p = ((DSAPublicKey) publicKey).getParams().getP();
|
||||
final BigInteger q = ((DSAPublicKey) publicKey).getParams().getQ();
|
||||
final BigInteger y = ((DSAPublicKey) publicKey).getY();
|
||||
final BigInteger w = s.modInverse(q);
|
||||
|
||||
final BigInteger u1 = w.multiply(new BigInteger(1, digestBytes)).mod(q);
|
||||
final BigInteger u2 = r.multiply(w).mod(q);
|
||||
|
||||
final BigInteger v = g.modPow(u1, p).multiply(y.modPow(u2, p)).mod(p).mod(q);
|
||||
return v.equals(r);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
/* DSSSignatureRawCodec.java --
|
||||
Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
|
||||
|
||||
This file is a part of GNU Classpath.
|
||||
|
||||
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
|
||||
the Free Software Foundation; either version 2 of the License, or (at
|
||||
your option) any later version.
|
||||
|
||||
GNU Classpath is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GNU Classpath; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
||||
USA
|
||||
|
||||
Linking this library statically or dynamically with other modules is
|
||||
making a combined work based on this library. Thus, the terms and
|
||||
conditions of the GNU General Public License cover the whole
|
||||
combination.
|
||||
|
||||
As a special exception, the copyright holders of this library give you
|
||||
permission to link this library with independent modules to produce an
|
||||
executable, regardless of the license terms of these independent
|
||||
modules, and to copy and distribute the resulting executable under
|
||||
terms of your choice, provided that you also meet, for each linked
|
||||
independent module, the terms and conditions of the license of that
|
||||
module. An independent module is a module which is not derived from
|
||||
or based on this library. If you modify this library, you may extend
|
||||
this exception to your version of the library, but you are not
|
||||
obligated to do so. If you do not wish to do so, delete this
|
||||
exception statement from your version. */
|
||||
|
||||
|
||||
package gnu.java.security.sig.dss;
|
||||
|
||||
import gnu.java.security.Registry;
|
||||
import gnu.java.security.sig.ISignatureCodec;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* <p>An object that implements the {@link ISignatureCodec} operations for the
|
||||
* <i>Raw</i> format to use with DSS signatures.</p>
|
||||
*
|
||||
* @version $Revision: 1.1 $
|
||||
*/
|
||||
public class DSSSignatureRawCodec implements ISignatureCodec
|
||||
{
|
||||
|
||||
// Constants and variables
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Constructor(s)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// implicit 0-arguments constructor
|
||||
|
||||
// Class methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Instance methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// gnu.crypto.sig.ISignatureCodec interface implementation -----------------
|
||||
|
||||
public int getFormatID()
|
||||
{
|
||||
return RAW_FORMAT;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns the encoded form of the designated DSS (Digital Signature
|
||||
* Standard) signature object according to the <i>Raw</i> format supported by
|
||||
* this library.</p>
|
||||
*
|
||||
* <p>The <i>Raw</i> format for a DSA signature, in this implementation, is a
|
||||
* byte sequence consisting of the following:</p>
|
||||
*
|
||||
* <ol>
|
||||
* <li>4-byte magic consisting of the value of the literal
|
||||
* {@link Registry#MAGIC_RAW_DSS_SIGNATURE},</li>
|
||||
* <li>1-byte version consisting of the constant: 0x01,</li>
|
||||
* <li>4-byte count of following bytes representing the DSS parameter
|
||||
* <code>r</code> in internet order,</li>
|
||||
* <li>n-bytes representation of a {@link BigInteger} obtained by invoking
|
||||
* the <code>toByteArray()</code> method on the DSS parameter <code>r</code>,</li>
|
||||
* <li>4-byte count of following bytes representing the DSS parameter
|
||||
* <code>s</code>,</li>
|
||||
* <li>n-bytes representation of a {@link BigInteger} obtained by invoking
|
||||
* the <code>toByteArray()</code> method on the DSS parameter <code>s</code>.</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param signature the signature to encode, consisting of the two DSS
|
||||
* parameters <code>r</code> and <code>s</code> as a {@link java.math.BigInteger}
|
||||
* array.
|
||||
* @return the <i>Raw</i> format encoding of the designated signature.
|
||||
* @exception IllegalArgumentException if the designated signature is not a
|
||||
* DSS (Digital Signature Standard) one.
|
||||
*/
|
||||
public byte[] encodeSignature(Object signature)
|
||||
{
|
||||
BigInteger r, s;
|
||||
try
|
||||
{
|
||||
BigInteger[] sig = (BigInteger[]) signature;
|
||||
r = sig[0];
|
||||
s = sig[1];
|
||||
}
|
||||
catch (Exception x)
|
||||
{
|
||||
throw new IllegalArgumentException("key");
|
||||
}
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
// magic
|
||||
baos.write(Registry.MAGIC_RAW_DSS_SIGNATURE[0]);
|
||||
baos.write(Registry.MAGIC_RAW_DSS_SIGNATURE[1]);
|
||||
baos.write(Registry.MAGIC_RAW_DSS_SIGNATURE[2]);
|
||||
baos.write(Registry.MAGIC_RAW_DSS_SIGNATURE[3]);
|
||||
|
||||
// version
|
||||
baos.write(0x01);
|
||||
|
||||
// r
|
||||
byte[] buffer = r.toByteArray();
|
||||
int length = buffer.length;
|
||||
baos.write(length >>> 24);
|
||||
baos.write((length >>> 16) & 0xFF);
|
||||
baos.write((length >>> 8) & 0xFF);
|
||||
baos.write(length & 0xFF);
|
||||
baos.write(buffer, 0, length);
|
||||
|
||||
// s
|
||||
buffer = s.toByteArray();
|
||||
length = buffer.length;
|
||||
baos.write(length >>> 24);
|
||||
baos.write((length >>> 16) & 0xFF);
|
||||
baos.write((length >>> 8) & 0xFF);
|
||||
baos.write(length & 0xFF);
|
||||
baos.write(buffer, 0, length);
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public Object decodeSignature(byte[] k)
|
||||
{
|
||||
// magic
|
||||
if (k[0] != Registry.MAGIC_RAW_DSS_SIGNATURE[0]
|
||||
|| k[1] != Registry.MAGIC_RAW_DSS_SIGNATURE[1]
|
||||
|| k[2] != Registry.MAGIC_RAW_DSS_SIGNATURE[2]
|
||||
|| k[3] != Registry.MAGIC_RAW_DSS_SIGNATURE[3])
|
||||
{
|
||||
throw new IllegalArgumentException("magic");
|
||||
}
|
||||
|
||||
// version
|
||||
if (k[4] != 0x01)
|
||||
{
|
||||
throw new IllegalArgumentException("version");
|
||||
}
|
||||
|
||||
int i = 5;
|
||||
int l;
|
||||
byte[] buffer;
|
||||
|
||||
// r
|
||||
l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
|
||||
| (k[i++] & 0xFF);
|
||||
buffer = new byte[l];
|
||||
System.arraycopy(k, i, buffer, 0, l);
|
||||
i += l;
|
||||
BigInteger r = new BigInteger(1, buffer);
|
||||
|
||||
// s
|
||||
l = k[i++] << 24 | (k[i++] & 0xFF) << 16 | (k[i++] & 0xFF) << 8
|
||||
| (k[i++] & 0xFF);
|
||||
buffer = new byte[l];
|
||||
System.arraycopy(k, i, buffer, 0, l);
|
||||
i += l;
|
||||
BigInteger s = new BigInteger(1, buffer);
|
||||
|
||||
return new BigInteger[] { r, s };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
/* DSSSignatureX509Codec.java -- X.509 encoder/decoder for DSS signatures
|
||||
Copyright (C) 2006 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Classpath.
|
||||
|
||||
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
|
||||
the Free Software Foundation; either version 2, or (at your option)
|
||||
any later version.
|
||||
|
||||
GNU Classpath is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GNU Classpath; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
02110-1301 USA.
|
||||
|
||||
Linking this library statically or dynamically with other modules is
|
||||
making a combined work based on this library. Thus, the terms and
|
||||
conditions of the GNU General Public License cover the whole
|
||||
combination.
|
||||
|
||||
As a special exception, the copyright holders of this library give you
|
||||
permission to link this library with independent modules to produce an
|
||||
executable, regardless of the license terms of these independent
|
||||
modules, and to copy and distribute the resulting executable under
|
||||
terms of your choice, provided that you also meet, for each linked
|
||||
independent module, the terms and conditions of the license of that
|
||||
module. An independent module is a module which is not derived from
|
||||
or based on this library. If you modify this library, you may extend
|
||||
this exception to your version of the library, but you are not
|
||||
obligated to do so. If you do not wish to do so, delete this
|
||||
exception statement from your version. */
|
||||
|
||||
|
||||
package gnu.java.security.sig.dss;
|
||||
|
||||
import gnu.java.security.Registry;
|
||||
import gnu.java.security.der.DER;
|
||||
import gnu.java.security.der.DERReader;
|
||||
import gnu.java.security.der.DERValue;
|
||||
import gnu.java.security.der.DERWriter;
|
||||
import gnu.java.security.sig.ISignatureCodec;
|
||||
import gnu.java.security.util.DerUtil;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* An implementation of an {@link ISignatureCodec} that knows to encode and
|
||||
* decode DSS signatures into the raw bytes which would constitute a DER-encoded
|
||||
* form of the ASN.1 structure defined in RFC-2459, and RFC-2313 as described in
|
||||
* the next paragraphs.
|
||||
* <p>
|
||||
* Digital signatures when transmitted in an X.509 certificates are encoded
|
||||
* in DER (Distinguished Encoding Rules) as a BIT STRING; i.e.
|
||||
*
|
||||
* <pre>
|
||||
* Certificate ::= SEQUENCE {
|
||||
* tbsCertificate TBSCertificate,
|
||||
* signatureAlgorithm AlgorithmIdentifier,
|
||||
* signature BIT STRING
|
||||
* }
|
||||
* </pre>
|
||||
* <p>
|
||||
* The output of the encoder, and the input of the decoder, of this codec are
|
||||
* then the <i>raw</i> bytes of such a BIT STRING; i.e. not the DER-encoded
|
||||
* form itself.
|
||||
* <p>
|
||||
* RFC-2459 states that, for the Digital Signature Standard (DSS), which
|
||||
* generates two MPIs, commonly called <code>r</code> and <code>s</code>, as the
|
||||
* result of digitally signing a message, these two numbers will be transferred
|
||||
* as the following ASN.1 structure:
|
||||
*
|
||||
* <pre>
|
||||
* Dss-Sig-Value ::= SEQUENCE {
|
||||
* r INTEGER,
|
||||
* s INTEGER
|
||||
* }
|
||||
* </pre>
|
||||
* <p>
|
||||
* Client code that needs to build a DER BIT STRING <b>MUST</b> construct such
|
||||
* an ASN.1 value. The following is an example of how to do this:
|
||||
* <p>
|
||||
* <pre>
|
||||
* ...
|
||||
* import gnu.java.security.der.BitString;
|
||||
* import gnu.java.security.der.DER;
|
||||
* import gnu.java.security.der.DERValue;
|
||||
* ...
|
||||
* DERValue bitString = new DERValue(DER.BIT_STRING, new BitString(sigBytes));
|
||||
* ...
|
||||
* </pre>
|
||||
*/
|
||||
public class DSSSignatureX509Codec
|
||||
implements ISignatureCodec
|
||||
{
|
||||
// implicit 0-arguments constructor
|
||||
|
||||
public int getFormatID()
|
||||
{
|
||||
return Registry.X509_ENCODING_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes a DSS Signature output as the <i>signature</i> raw bytes which can
|
||||
* be used to construct an ASN.1 DER-encoded BIT STRING as defined in the
|
||||
* documentation of this class.
|
||||
*
|
||||
* @param signature the output of the DSS signature algorithm; i.e. the value
|
||||
* returned by the invocation of
|
||||
* {@link gnu.java.security.sig.ISignature#sign()} method. In the
|
||||
* case of a DSS signature this is an array of two MPIs called
|
||||
* <code>r</code> and <code>s</code>.
|
||||
* @return the raw bytes of a DSS signature which could be then used as the
|
||||
* contents of a BIT STRING as per rfc-2459.
|
||||
* @throws InvalidParameterException if an exception occurs during the
|
||||
* marshalling process.
|
||||
*/
|
||||
public byte[] encodeSignature(Object signature)
|
||||
{
|
||||
BigInteger[] rs = (BigInteger[]) signature;
|
||||
|
||||
DERValue derR = new DERValue(DER.INTEGER, rs[0]);
|
||||
DERValue derS = new DERValue(DER.INTEGER, rs[1]);
|
||||
|
||||
ArrayList dssSigValue = new ArrayList(2);
|
||||
dssSigValue.add(derR);
|
||||
dssSigValue.add(derS);
|
||||
DERValue derDssSigValue = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
|
||||
dssSigValue);
|
||||
byte[] result;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try
|
||||
{
|
||||
DERWriter.write(baos, derDssSigValue);
|
||||
result = baos.toByteArray();
|
||||
}
|
||||
catch (IOException x)
|
||||
{
|
||||
InvalidParameterException y = new InvalidParameterException();
|
||||
y.initCause(x);
|
||||
throw y;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a <i>signature</i> as defined in the documentation of this class.
|
||||
*
|
||||
* @param input the byte array to unmarshall into a valid DSS signature
|
||||
* instance; i.e. an array of two MPIs. MUST NOT be null.
|
||||
* @return an array of two MPIs, <code>r</code> and <code>s</code> in this
|
||||
* order, decoded from the designated <code>input</code>.
|
||||
* @throw InvalidParameterException if an exception occurs during the
|
||||
* unmarshalling process.
|
||||
*/
|
||||
public Object decodeSignature(byte[] input)
|
||||
{
|
||||
if (input == null)
|
||||
throw new InvalidParameterException("Input bytes MUST NOT be null");
|
||||
|
||||
BigInteger r, s;
|
||||
DERReader der = new DERReader(input);
|
||||
try
|
||||
{
|
||||
DERValue derDssSigValue = der.read();
|
||||
DerUtil.checkIsConstructed(derDssSigValue, "Wrong Dss-Sig-Value field");
|
||||
|
||||
DERValue val = der.read();
|
||||
DerUtil.checkIsBigInteger(val, "Wrong R field");
|
||||
r = (BigInteger) val.getValue();
|
||||
val = der.read();
|
||||
DerUtil.checkIsBigInteger(val, "Wrong S field");
|
||||
s = (BigInteger) val.getValue();
|
||||
}
|
||||
catch (IOException x)
|
||||
{
|
||||
InvalidParameterException y = new InvalidParameterException();
|
||||
y.initCause(x);
|
||||
throw y;
|
||||
}
|
||||
|
||||
return new BigInteger[] { r, s };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user