FileDocCategorySizeDatePackage
SignatureDSA.javaAPI DocJava SE 6 API11017Tue Jun 10 00:23:00 BST 2008com.sun.org.apache.xml.internal.security.algorithms.implementations

SignatureDSA

public class SignatureDSA extends SignatureAlgorithmSpi
author
$Author: vishal $

Fields Summary
static Logger
log
{@link java.util.logging} logging facility
public static final String
_URI
Field _URI
private Signature
_signatureAlgorithm
Field algorithm
Constructors Summary
public SignatureDSA()
Constructor SignatureDSA

throws
XMLSignatureException


      String algorithmID = JCEMapper.translateURItoJCEID(SignatureDSA._URI);
      if (true)
      	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "Created SignatureDSA using " + algorithmID);

      try {
         this._signatureAlgorithm = Signature.getInstance(algorithmID);
      } catch (java.security.NoSuchAlgorithmException ex) {
         Object[] exArgs = { algorithmID,
                             ex.getLocalizedMessage() };

         throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
      }
   
Methods Summary
private static byte[]convertASN1toXMLDSIG(byte[] asn1Bytes)
Converts an ASN.1 DSA value to a XML Signature DSA Value. The JAVA JCE DSA Signature algorithm creates ASN.1 encoded (r,s) value pairs; the XML Signature requires the core BigInteger values.

param
asn1Bytes
return
the decode bytes
throws
IOException
see
6.4.1 DSA


      byte rLength = asn1Bytes[3];
      int i;

      for (i = rLength; (i > 0) && (asn1Bytes[(4 + rLength) - i] == 0); i--);

      byte sLength = asn1Bytes[5 + rLength];
      int j;

      for (j = sLength;
              (j > 0) && (asn1Bytes[(6 + rLength + sLength) - j] == 0); j--);

      if ((asn1Bytes[0] != 48) || (asn1Bytes[1] != asn1Bytes.length - 2)
              || (asn1Bytes[2] != 2) || (i > 20)
              || (asn1Bytes[4 + rLength] != 2) || (j > 20)) {
         throw new IOException("Invalid ASN.1 format of DSA signature");
      } 
      byte xmldsigBytes[] = new byte[40];

      System.arraycopy(asn1Bytes, (4 + rLength) - i, xmldsigBytes, 20 - i,
                          i);
      System.arraycopy(asn1Bytes, (6 + rLength + sLength) - j, xmldsigBytes,
                          40 - j, j);

       return xmldsigBytes;      
   
private static byte[]convertXMLDSIGtoASN1(byte[] xmldsigBytes)
Converts a XML Signature DSA Value to an ASN.1 DSA value. The JAVA JCE DSA Signature algorithm creates ASN.1 encoded (r,s) value pairs; the XML Signature requires the core BigInteger values.

param
xmldsigBytes
return
the encoded ASN.1 bytes
throws
IOException
see
6.4.1 DSA


      if (xmldsigBytes.length != 40) {
         throw new IOException("Invalid XMLDSIG format of DSA signature");
      }

      int i;

      for (i = 20; (i > 0) && (xmldsigBytes[20 - i] == 0); i--);

      int j = i;

      if (xmldsigBytes[20 - i] < 0) {
         j += 1;
      }

      int k;

      for (k = 20; (k > 0) && (xmldsigBytes[40 - k] == 0); k--);

      int l = k;

      if (xmldsigBytes[40 - k] < 0) {
         l += 1;
      }

      byte asn1Bytes[] = new byte[6 + j + l];

      asn1Bytes[0] = 48;
      asn1Bytes[1] = (byte) (4 + j + l);
      asn1Bytes[2] = 2;
      asn1Bytes[3] = (byte) j;

      System.arraycopy(xmldsigBytes, 20 - i, asn1Bytes, (4 + j) - i, i);

      asn1Bytes[4 + j] = 2;
      asn1Bytes[5 + j] = (byte) l;

      System.arraycopy(xmldsigBytes, 40 - k, asn1Bytes, (6 + j + l) - k, k);

      return asn1Bytes;
   
protected java.lang.StringengineGetJCEAlgorithmString()
Method engineGetJCEAlgorithmString

inheritDoc

      return this._signatureAlgorithm.getAlgorithm();
   
protected java.lang.StringengineGetJCEProviderName()
Method engineGetJCEProviderName

inheritDoc

      return this._signatureAlgorithm.getProvider().getName();
   
protected java.lang.StringengineGetURI()
Method engineGetURI

inheritDoc


          
      
      return SignatureDSA._URI;
   
protected voidengineInitSign(java.security.Key signingKey, java.security.spec.AlgorithmParameterSpec algorithmParameterSpec)
Method engineInitSign

param
signingKey
param
algorithmParameterSpec
throws
XMLSignatureException

      throw new XMLSignatureException(
         "algorithms.CannotUseAlgorithmParameterSpecOnDSA");
   
protected voidengineInitSign(java.security.Key privateKey, java.security.SecureRandom secureRandom)

inheritDoc


      if (!(privateKey instanceof PrivateKey)) {
         String supplied = privateKey.getClass().getName();
         String needed = PrivateKey.class.getName();
         Object exArgs[] = { supplied, needed };

         throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
                                         exArgs);
      }

      try {
         this._signatureAlgorithm.initSign((PrivateKey) privateKey,
                                           secureRandom);
      } catch (InvalidKeyException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineInitSign(java.security.Key privateKey)

inheritDoc


      if (!(privateKey instanceof PrivateKey)) {
         String supplied = privateKey.getClass().getName();
         String needed = PrivateKey.class.getName();
         Object exArgs[] = { supplied, needed };

         throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
                                         exArgs);
      }

      try {
         this._signatureAlgorithm.initSign((PrivateKey) privateKey);
      } catch (InvalidKeyException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineInitVerify(java.security.Key publicKey)

inheritDoc


      if (!(publicKey instanceof PublicKey)) {
         String supplied = publicKey.getClass().getName();
         String needed = PublicKey.class.getName();
         Object exArgs[] = { supplied, needed };

         throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
                                         exArgs);
      }

      try {
         this._signatureAlgorithm.initVerify((PublicKey) publicKey);
      } catch (InvalidKeyException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineSetHMACOutputLength(int HMACOutputLength)
Method engineSetHMACOutputLength

param
HMACOutputLength
throws
XMLSignatureException

      throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC");
   
protected voidengineSetParameter(java.security.spec.AlgorithmParameterSpec params)

inheritDoc


      try {
         this._signatureAlgorithm.setParameter(params);
      } catch (InvalidAlgorithmParameterException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected byte[]engineSign()

inheritDoc


      try {
         byte jcebytes[] = this._signatureAlgorithm.sign();

         return SignatureDSA.convertASN1toXMLDSIG(jcebytes);
      } catch (IOException ex) {
         throw new XMLSignatureException("empty", ex);
      } catch (SignatureException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineUpdate(byte input)

inheritDoc


      try {
         this._signatureAlgorithm.update(input);
      } catch (SignatureException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineUpdate(byte[] buf, int offset, int len)

inheritDoc


      try {
         this._signatureAlgorithm.update(buf, offset, len);
      } catch (SignatureException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineUpdate(byte[] input)

inheritDoc


      try {
         this._signatureAlgorithm.update(input);
      } catch (SignatureException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected booleanengineVerify(byte[] signature)

inheritDoc


      try {
         if (true)
         	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "Called DSA.verify() on " + Base64.encode(signature));

         byte[] jcebytes = SignatureDSA.convertXMLDSIGtoASN1(signature);

         return this._signatureAlgorithm.verify(jcebytes);
      } catch (SignatureException ex) {
         throw new XMLSignatureException("empty", ex);
      } catch (IOException ex) {
         throw new XMLSignatureException("empty", ex);
      }