src/share/classes/com/sun/org/apache/xml/internal/security/algorithms/implementations/SignatureECDSA.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright  1999-2004 The Apache Software Foundation.
   7  *
   8  *  Licensed under the Apache License, Version 2.0 (the "License");
   9  *  you may not use this file except in compliance with the License.
  10  *  You may obtain a copy of the License at


  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  *  Unless required by applicable law or agreed to in writing, software
  15  *  distributed under the License is distributed on an "AS IS" BASIS,
  16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  *  See the License for the specific language governing permissions and
  18  *  limitations under the License.
  19  *
  20  */
  21 package com.sun.org.apache.xml.internal.security.algorithms.implementations;
  22 
  23 
  24 
  25 import java.io.IOException;
  26 import java.security.InvalidAlgorithmParameterException;
  27 import java.security.InvalidKeyException;
  28 import java.security.Key;
  29 import java.security.NoSuchProviderException;
  30 import java.security.PrivateKey;
  31 import java.security.PublicKey;
  32 import java.security.SecureRandom;
  33 import java.security.Signature;
  34 import java.security.SignatureException;
  35 import java.security.spec.AlgorithmParameterSpec;
  36 
  37 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
  38 import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithmSpi;
  39 import com.sun.org.apache.xml.internal.security.signature.XMLSignature;
  40 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
  41 import com.sun.org.apache.xml.internal.security.utils.Base64;
  42 
  43 
  44 /**
  45  *
  46  * @author $Author: mullan $

  47  */
  48 public abstract class SignatureECDSA extends SignatureAlgorithmSpi {
  49 
  50    /** {@link java.util.logging} logging facility */
  51     static java.util.logging.Logger log =
  52         java.util.logging.Logger.getLogger(SignatureECDSA.class.getName());
  53 
  54     /** @inheritDoc */
  55    public abstract String engineGetURI();
  56 
  57    /** Field algorithm */
  58    private java.security.Signature _signatureAlgorithm = null;
  59 
  60    /**
  61     * Converts an ASN.1 ECDSA value to a XML Signature ECDSA Value.
  62     *
  63     * The JAVA JCE ECDSA Signature algorithm creates ASN.1 encoded (r,s) value
  64     * pairs; the XML Signature requires the core BigInteger values.
  65     *
  66     * @param asn1Bytes
  67     * @return the decode bytes
  68     *
  69     * @throws IOException
  70     * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
  71     * @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
  72     */
  73    private static byte[] convertASN1toXMLDSIG(byte asn1Bytes[])
  74            throws IOException {











  75 
  76       byte rLength = asn1Bytes[3];
  77       int i;
  78 
  79       for (i = rLength; (i > 0) && (asn1Bytes[(4 + rLength) - i] == 0); i--);
  80 
  81       byte sLength = asn1Bytes[5 + rLength];
  82       int j;
  83 
  84       for (j = sLength;
  85               (j > 0) && (asn1Bytes[(6 + rLength + sLength) - j] == 0); j--);


  86 
  87       if ((asn1Bytes[0] != 48) || (asn1Bytes[1] != asn1Bytes.length - 2)
  88               || (asn1Bytes[2] != 2) || (i > 24)
  89               || (asn1Bytes[4 + rLength] != 2) || (j > 24)) {

  90          throw new IOException("Invalid ASN.1 format of ECDSA signature");
  91       }
  92       byte xmldsigBytes[] = new byte[48];
  93 
  94       System.arraycopy(asn1Bytes, (4 + rLength) - i, xmldsigBytes, 24 - i,
  95                           i);
  96       System.arraycopy(asn1Bytes, (6 + rLength + sLength) - j, xmldsigBytes,
  97                           48 - j, j);
  98 
  99        return xmldsigBytes;
 100    }
 101 
 102    /**
 103     * Converts a XML Signature ECDSA Value to an ASN.1 DSA value.
 104     *
 105     * The JAVA JCE ECDSA Signature algorithm creates ASN.1 encoded (r,s) value
 106     * pairs; the XML Signature requires the core BigInteger values.
 107     *
 108     * @param xmldsigBytes
 109     * @return the encoded ASN.1 bytes
 110     *
 111     * @throws IOException
 112     * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
 113     * @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
 114     */
 115    private static byte[] convertXMLDSIGtoASN1(byte xmldsigBytes[])
 116            throws IOException {
 117 
 118       if (xmldsigBytes.length != 48) {
 119          throw new IOException("Invalid XMLDSIG format of ECDSA signature");
 120       }
 121 
 122       int i;
 123 
 124       for (i = 24; (i > 0) && (xmldsigBytes[24 - i] == 0); i--);
 125 
 126       int j = i;
 127 
 128       if (xmldsigBytes[24 - i] < 0) {
 129          j += 1;
 130       }
 131 
 132       int k;
 133 
 134       for (k = 24; (k > 0) && (xmldsigBytes[48 - k] == 0); k--);
 135 
 136       int l = k;
 137 
 138       if (xmldsigBytes[48 - k] < 0) {
 139          l += 1;
 140       }
 141 
 142       byte asn1Bytes[] = new byte[6 + j + l];
 143 












 144       asn1Bytes[0] = 48;
 145       asn1Bytes[1] = (byte) (4 + j + l);
 146       asn1Bytes[2] = 2;
 147       asn1Bytes[3] = (byte) j;
 148 
 149       System.arraycopy(xmldsigBytes, 24 - i, asn1Bytes, (4 + j) - i, i);
 150 
 151       asn1Bytes[4 + j] = 2;
 152       asn1Bytes[5 + j] = (byte) l;
 153 
 154       System.arraycopy(xmldsigBytes, 48 - k, asn1Bytes, (6 + j + l) - k, k);



 155 
 156       return asn1Bytes;
 157    }
 158 
 159    /**
 160     * Constructor SignatureRSA
 161     *
 162     * @throws XMLSignatureException
 163     */
 164    public SignatureECDSA() throws XMLSignatureException {
 165 
 166       String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
 167 
 168       if (log.isLoggable(java.util.logging.Level.FINE))
 169         log.log(java.util.logging.Level.FINE, "Created SignatureECDSA using " + algorithmID);
 170       String provider=JCEMapper.getProviderId();

 171       try {
 172          if (provider==null) {
 173                 this._signatureAlgorithm = Signature.getInstance(algorithmID);
 174          } else {
 175                 this._signatureAlgorithm = Signature.getInstance(algorithmID,provider);
 176          }
 177       } catch (java.security.NoSuchAlgorithmException ex) {
 178          Object[] exArgs = { algorithmID,
 179                              ex.getLocalizedMessage() };
 180 
 181          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
 182       } catch (NoSuchProviderException ex) {
 183          Object[] exArgs = { algorithmID,
 184                                                  ex.getLocalizedMessage() };
 185 
 186          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
 187         }
 188    }
 189 
 190    /** @inheritDoc */
 191    protected void engineSetParameter(AlgorithmParameterSpec params)
 192            throws XMLSignatureException {
 193 
 194       try {
 195          this._signatureAlgorithm.setParameter(params);
 196       } catch (InvalidAlgorithmParameterException ex) {
 197          throw new XMLSignatureException("empty", ex);
 198       }
 199    }
 200 
 201    /** @inheritDoc */
 202    protected boolean engineVerify(byte[] signature)
 203            throws XMLSignatureException {
 204 
 205       try {
 206          byte[] jcebytes = SignatureECDSA.convertXMLDSIGtoASN1(signature);
 207 
 208          if (log.isLoggable(java.util.logging.Level.FINE))
 209             log.log(java.util.logging.Level.FINE, "Called ECDSA.verify() on " + Base64.encode(signature));

 210 
 211          return this._signatureAlgorithm.verify(jcebytes);
 212       } catch (SignatureException ex) {
 213          throw new XMLSignatureException("empty", ex);
 214       } catch (IOException ex) {
 215          throw new XMLSignatureException("empty", ex);
 216       }
 217    }
 218 
 219    /** @inheritDoc */
 220    protected void engineInitVerify(Key publicKey) throws XMLSignatureException {
 221 
 222       if (!(publicKey instanceof PublicKey)) {
 223          String supplied = publicKey.getClass().getName();
 224          String needed = PublicKey.class.getName();
 225          Object exArgs[] = { supplied, needed };
 226 
 227          throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
 228                                          exArgs);
 229       }
 230 
 231       try {
 232          this._signatureAlgorithm.initVerify((PublicKey) publicKey);
 233       } catch (InvalidKeyException ex) {
 234             // reinstantiate Signature object to work around bug in JDK
 235             // see: http://bugs.sun.com/view_bug.do?bug_id=4953555
 236             Signature sig = this._signatureAlgorithm;
 237             try {
 238                 this._signatureAlgorithm = Signature.getInstance
 239                     (_signatureAlgorithm.getAlgorithm());
 240             } catch (Exception e) {
 241                 // this shouldn't occur, but if it does, restore previous
 242                 // Signature
 243                 if (log.isLoggable(java.util.logging.Level.FINE)) {
 244                     log.log(java.util.logging.Level.FINE, "Exception when reinstantiating Signature:" + e);
 245                 }
 246                 this._signatureAlgorithm = sig;
 247             }
 248             throw new XMLSignatureException("empty", ex);
 249       }
 250    }
 251 
 252    /** @inheritDoc */
 253    protected byte[] engineSign() throws XMLSignatureException {
 254 
 255       try {
 256          byte jcebytes[] = this._signatureAlgorithm.sign();
 257 
 258          return SignatureECDSA.convertASN1toXMLDSIG(jcebytes);
 259       } catch (SignatureException ex) {
 260          throw new XMLSignatureException("empty", ex);
 261       } catch (IOException ex) {
 262           throw new XMLSignatureException("empty", ex);
 263       }
 264    }
 265 
 266    /** @inheritDoc */
 267    protected void engineInitSign(Key privateKey, SecureRandom secureRandom)
 268            throws XMLSignatureException {
 269 
 270       if (!(privateKey instanceof PrivateKey)) {
 271          String supplied = privateKey.getClass().getName();
 272          String needed = PrivateKey.class.getName();
 273          Object exArgs[] = { supplied, needed };
 274 
 275          throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
 276                                          exArgs);
 277       }
 278 
 279       try {
 280          this._signatureAlgorithm.initSign((PrivateKey) privateKey,
 281                                            secureRandom);
 282       } catch (InvalidKeyException ex) {
 283          throw new XMLSignatureException("empty", ex);
 284       }
 285    }
 286 
 287    /** @inheritDoc */
 288    protected void engineInitSign(Key privateKey) throws XMLSignatureException {
 289 
 290       if (!(privateKey instanceof PrivateKey)) {
 291          String supplied = privateKey.getClass().getName();
 292          String needed = PrivateKey.class.getName();
 293          Object exArgs[] = { supplied, needed };
 294 
 295          throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
 296                                          exArgs);
 297       }
 298 
 299       try {
 300          this._signatureAlgorithm.initSign((PrivateKey) privateKey);
 301       } catch (InvalidKeyException ex) {
 302          throw new XMLSignatureException("empty", ex);
 303       }
 304    }
 305 
 306    /** @inheritDoc */
 307    protected void engineUpdate(byte[] input) throws XMLSignatureException {
 308 
 309       try {
 310          this._signatureAlgorithm.update(input);
 311       } catch (SignatureException ex) {
 312          throw new XMLSignatureException("empty", ex);
 313       }
 314    }
 315 
 316    /** @inheritDoc */
 317    protected void engineUpdate(byte input) throws XMLSignatureException {
 318 
 319       try {
 320          this._signatureAlgorithm.update(input);
 321       } catch (SignatureException ex) {
 322          throw new XMLSignatureException("empty", ex);
 323       }
 324    }
 325 
 326    /** @inheritDoc */
 327    protected void engineUpdate(byte buf[], int offset, int len)
 328            throws XMLSignatureException {
 329 
 330       try {
 331          this._signatureAlgorithm.update(buf, offset, len);
 332       } catch (SignatureException ex) {
 333          throw new XMLSignatureException("empty", ex);
 334       }
 335    }
 336 
 337    /** @inheritDoc */
 338    protected String engineGetJCEAlgorithmString() {
 339       return this._signatureAlgorithm.getAlgorithm();
 340    }
 341 
 342    /** @inheritDoc */
 343    protected String engineGetJCEProviderName() {
 344       return this._signatureAlgorithm.getProvider().getName();
 345    }
 346 
 347    /** @inheritDoc */
 348    protected void engineSetHMACOutputLength(int HMACOutputLength)
 349            throws XMLSignatureException {
 350       throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC");
 351    }
 352 
 353    /** @inheritDoc */
 354    protected void engineInitSign(
 355            Key signingKey, AlgorithmParameterSpec algorithmParameterSpec)
 356               throws XMLSignatureException {
 357       throw new XMLSignatureException(
 358          "algorithms.CannotUseAlgorithmParameterSpecOnRSA");
 359    }
 360 
 361    /**
 362     * Class SignatureRSASHA1
 363     *
 364     * @author $Author: mullan $
 365     * @version $Revision: 1.2 $
 366     */
 367    public static class SignatureECDSASHA1 extends SignatureECDSA {
 368 
 369       /**
 370        * Constructor SignatureRSASHA1
 371        *
 372        * @throws XMLSignatureException
 373        */
 374       public SignatureECDSASHA1() throws XMLSignatureException {
 375          super();
 376       }
 377 
 378       /** @inheritDoc */
 379       public String engineGetURI() {
 380          return XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA1;
 381       }
 382    }


































































 383 
 384 }
   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /**
   6  * Licensed to the Apache Software Foundation (ASF) under one
   7  * or more contributor license agreements. See the NOTICE file
   8  * distributed with this work for additional information
   9  * regarding copyright ownership. The ASF licenses this file
  10  * to you under the Apache License, Version 2.0 (the
  11  * "License"); you may not use this file except in compliance
  12  * with the License. You may obtain a copy of the License at
  13  *
  14  * http://www.apache.org/licenses/LICENSE-2.0
  15  *
  16  * Unless required by applicable law or agreed to in writing,
  17  * software distributed under the License is distributed on an
  18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19  * KIND, either express or implied. See the License for the
  20  * specific language governing permissions and limitations
  21  * under the License.
  22  */
  23 package com.sun.org.apache.xml.internal.security.algorithms.implementations;
  24 


  25 import java.io.IOException;
  26 import java.security.InvalidAlgorithmParameterException;
  27 import java.security.InvalidKeyException;
  28 import java.security.Key;
  29 import java.security.NoSuchProviderException;
  30 import java.security.PrivateKey;
  31 import java.security.PublicKey;
  32 import java.security.SecureRandom;
  33 import java.security.Signature;
  34 import java.security.SignatureException;
  35 import java.security.spec.AlgorithmParameterSpec;
  36 
  37 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
  38 import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithmSpi;
  39 import com.sun.org.apache.xml.internal.security.signature.XMLSignature;
  40 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
  41 import com.sun.org.apache.xml.internal.security.utils.Base64;
  42 

  43 /**
  44  *
  45  * @author $Author: raul $
  46  * @author Alex Dupre
  47  */
  48 public abstract class SignatureECDSA extends SignatureAlgorithmSpi {
  49 
  50     /** {@link org.apache.commons.logging} logging facility */
  51     private static java.util.logging.Logger log = 
  52         java.util.logging.Logger.getLogger(SignatureECDSA.class.getName());
  53 
  54     /** @inheritDoc */
  55     public abstract String engineGetURI();
  56 
  57     /** Field algorithm */
  58     private java.security.Signature signatureAlgorithm = null;
  59 
  60     /**
  61      * Converts an ASN.1 ECDSA value to a XML Signature ECDSA Value.
  62      *
  63      * The JAVA JCE ECDSA Signature algorithm creates ASN.1 encoded (r,s) value
  64      * pairs; the XML Signature requires the core BigInteger values.
  65      *
  66      * @param asn1Bytes
  67      * @return the decode bytes
  68      *
  69      * @throws IOException
  70      * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
  71      * @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
  72      */
  73     public static byte[] convertASN1toXMLDSIG(byte asn1Bytes[]) throws IOException {
  74 
  75         if (asn1Bytes.length < 8 || asn1Bytes[0] != 48) {
  76             throw new IOException("Invalid ASN.1 format of ECDSA signature");
  77         }
  78         int offset;
  79         if (asn1Bytes[1] > 0) {
  80             offset = 2;
  81         } else if (asn1Bytes[1] == (byte) 0x81) {
  82             offset = 3;
  83         } else {
  84             throw new IOException("Invalid ASN.1 format of ECDSA signature");
  85         }
  86 
  87         byte rLength = asn1Bytes[offset + 1];
  88         int i;
  89 
  90         for (i = rLength; (i > 0) && (asn1Bytes[(offset + 2 + rLength) - i] == 0); i--);
  91 
  92         byte sLength = asn1Bytes[offset + 2 + rLength + 1];
  93         int j;
  94 
  95         for (j = sLength;
  96             (j > 0) && (asn1Bytes[(offset + 2 + rLength + 2 + sLength) - j] == 0); j--);
  97 
  98         int rawLen = Math.max(i, j);
  99 
 100         if ((asn1Bytes[offset - 1] & 0xff) != asn1Bytes.length - offset
 101             || (asn1Bytes[offset - 1] & 0xff) != 2 + rLength + 2 + sLength
 102             || asn1Bytes[offset] != 2
 103             || asn1Bytes[offset + 2 + rLength] != 2) {
 104             throw new IOException("Invalid ASN.1 format of ECDSA signature");
 105         } 
 106         byte xmldsigBytes[] = new byte[2*rawLen];
 107 
 108         System.arraycopy(asn1Bytes, (offset + 2 + rLength) - i, xmldsigBytes, rawLen - i, i);
 109         System.arraycopy(asn1Bytes, (offset + 2 + rLength + 2 + sLength) - j, xmldsigBytes,
 110                          2*rawLen - j, j);

 111 
 112         return xmldsigBytes;      
 113     }
 114 
 115     /**
 116      * Converts a XML Signature ECDSA Value to an ASN.1 DSA value.
 117      *
 118      * The JAVA JCE ECDSA Signature algorithm creates ASN.1 encoded (r,s) value
 119      * pairs; the XML Signature requires the core BigInteger values.
 120      *
 121      * @param xmldsigBytes
 122      * @return the encoded ASN.1 bytes
 123      *
 124      * @throws IOException
 125      * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
 126      * @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
 127      */
 128     public static byte[] convertXMLDSIGtoASN1(byte xmldsigBytes[]) throws IOException {

 129 
 130         int rawLen = xmldsigBytes.length/2;


 131 
 132         int i;
 133 
 134         for (i = rawLen; (i > 0) && (xmldsigBytes[rawLen - i] == 0); i--);
 135 
 136         int j = i;
 137 
 138         if (xmldsigBytes[rawLen - i] < 0) {
 139             j += 1;
 140         }
 141 
 142         int k;
 143 
 144         for (k = rawLen; (k > 0) && (xmldsigBytes[2*rawLen - k] == 0); k--);
 145 
 146         int l = k;
 147 
 148         if (xmldsigBytes[2*rawLen - k] < 0) {
 149             l += 1;
 150         }
 151 
 152         int len = 2 + j + 2 + l;
 153         if (len > 255) {
 154             throw new IOException("Invalid XMLDSIG format of ECDSA signature");
 155         }
 156         int offset;
 157         byte asn1Bytes[];
 158         if (len < 128) {
 159             asn1Bytes = new byte[2 + 2 + j + 2 + l];
 160             offset = 1;
 161         } else {
 162             asn1Bytes = new byte[3 + 2 + j + 2 + l];
 163             asn1Bytes[1] = (byte) 0x81;
 164             offset = 2;
 165         }
 166         asn1Bytes[0] = 48;
 167         asn1Bytes[offset++] = (byte) len;
 168         asn1Bytes[offset++] = 2;
 169         asn1Bytes[offset++] = (byte) j;
 170 
 171         System.arraycopy(xmldsigBytes, rawLen - i, asn1Bytes, (offset + j) - i, i);
 172 
 173         offset += j;

 174 
 175         asn1Bytes[offset++] = 2;
 176         asn1Bytes[offset++] = (byte) l;
 177 
 178         System.arraycopy(xmldsigBytes, 2*rawLen - k, asn1Bytes, (offset + l) - k, k);
 179 
 180         return asn1Bytes;
 181     }
 182 
 183     /**
 184      * Constructor SignatureRSA
 185      *
 186      * @throws XMLSignatureException
 187      */
 188     public SignatureECDSA() throws XMLSignatureException {
 189 
 190         String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
 191 
 192         if (log.isLoggable(java.util.logging.Level.FINE)) {
 193             log.log(java.util.logging.Level.FINE, "Created SignatureECDSA using " + algorithmID);
 194         }
 195         String provider = JCEMapper.getProviderId();
 196         try {
 197             if (provider == null) {
 198                 this.signatureAlgorithm = Signature.getInstance(algorithmID);
 199             } else {
 200                 this.signatureAlgorithm = Signature.getInstance(algorithmID,provider);
 201             }
 202         } catch (java.security.NoSuchAlgorithmException ex) {
 203             Object[] exArgs = { algorithmID, ex.getLocalizedMessage() };

 204 
 205             throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
 206         } catch (NoSuchProviderException ex) {
 207             Object[] exArgs = { algorithmID, ex.getLocalizedMessage() };

 208 
 209             throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
 210         }
 211     }
 212 
 213     /** @inheritDoc */
 214     protected void engineSetParameter(AlgorithmParameterSpec params)
 215         throws XMLSignatureException {

 216         try {
 217             this.signatureAlgorithm.setParameter(params);
 218         } catch (InvalidAlgorithmParameterException ex) {
 219             throw new XMLSignatureException("empty", ex);
 220         }
 221     }
 222 
 223     /** @inheritDoc */
 224     protected boolean engineVerify(byte[] signature) throws XMLSignatureException {


 225         try {
 226             byte[] jcebytes = SignatureECDSA.convertXMLDSIGtoASN1(signature);
 227 
 228             if (log.isLoggable(java.util.logging.Level.FINE)) {
 229                 log.log(java.util.logging.Level.FINE, "Called ECDSA.verify() on " + Base64.encode(signature));
 230             }
 231 
 232             return this.signatureAlgorithm.verify(jcebytes);
 233         } catch (SignatureException ex) {
 234             throw new XMLSignatureException("empty", ex);
 235         } catch (IOException ex) {
 236             throw new XMLSignatureException("empty", ex);
 237         }
 238     }
 239 
 240     /** @inheritDoc */
 241     protected void engineInitVerify(Key publicKey) throws XMLSignatureException {
 242 
 243         if (!(publicKey instanceof PublicKey)) {
 244             String supplied = publicKey.getClass().getName();
 245             String needed = PublicKey.class.getName();
 246             Object exArgs[] = { supplied, needed };
 247 
 248             throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs);

 249         }
 250 
 251         try {
 252             this.signatureAlgorithm.initVerify((PublicKey) publicKey);
 253         } catch (InvalidKeyException ex) {
 254             // reinstantiate Signature object to work around bug in JDK
 255             // see: http://bugs.sun.com/view_bug.do?bug_id=4953555
 256             Signature sig = this.signatureAlgorithm;
 257             try {
 258                 this.signatureAlgorithm = Signature.getInstance(signatureAlgorithm.getAlgorithm());

 259             } catch (Exception e) {
 260                 // this shouldn't occur, but if it does, restore previous
 261                 // Signature
 262                 if (log.isLoggable(java.util.logging.Level.FINE)) {
 263                     log.log(java.util.logging.Level.FINE, "Exception when reinstantiating Signature:" + e);
 264                 }
 265                 this.signatureAlgorithm = sig;
 266             }
 267             throw new XMLSignatureException("empty", ex);
 268         }
 269     }
 270 
 271     /** @inheritDoc */
 272     protected byte[] engineSign() throws XMLSignatureException {

 273         try {
 274             byte jcebytes[] = this.signatureAlgorithm.sign();
 275 
 276             return SignatureECDSA.convertASN1toXMLDSIG(jcebytes);
 277         } catch (SignatureException ex) {
 278             throw new XMLSignatureException("empty", ex);
 279         } catch (IOException ex) {
 280             throw new XMLSignatureException("empty", ex);
 281         }
 282     }
 283 
 284     /** @inheritDoc */
 285     protected void engineInitSign(Key privateKey, SecureRandom secureRandom)
 286         throws XMLSignatureException {

 287         if (!(privateKey instanceof PrivateKey)) {
 288             String supplied = privateKey.getClass().getName();
 289             String needed = PrivateKey.class.getName();
 290             Object exArgs[] = { supplied, needed };
 291 
 292             throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs);

 293         }
 294 
 295         try {
 296             this.signatureAlgorithm.initSign((PrivateKey) privateKey, secureRandom);

 297         } catch (InvalidKeyException ex) {
 298             throw new XMLSignatureException("empty", ex);
 299         }
 300     }
 301 
 302     /** @inheritDoc */
 303     protected void engineInitSign(Key privateKey) throws XMLSignatureException {

 304         if (!(privateKey instanceof PrivateKey)) {
 305             String supplied = privateKey.getClass().getName();
 306             String needed = PrivateKey.class.getName();
 307             Object exArgs[] = { supplied, needed };
 308 
 309             throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs);

 310         }
 311 
 312         try {
 313             this.signatureAlgorithm.initSign((PrivateKey) privateKey);
 314         } catch (InvalidKeyException ex) {
 315             throw new XMLSignatureException("empty", ex);
 316         }
 317     }
 318 
 319     /** @inheritDoc */
 320     protected void engineUpdate(byte[] input) throws XMLSignatureException {

 321         try {
 322             this.signatureAlgorithm.update(input);
 323         } catch (SignatureException ex) {
 324             throw new XMLSignatureException("empty", ex);
 325         }
 326     }
 327 
 328     /** @inheritDoc */
 329     protected void engineUpdate(byte input) throws XMLSignatureException {

 330         try {
 331             this.signatureAlgorithm.update(input);
 332         } catch (SignatureException ex) {
 333             throw new XMLSignatureException("empty", ex);
 334         }
 335     }
 336 
 337     /** @inheritDoc */
 338     protected void engineUpdate(byte buf[], int offset, int len) throws XMLSignatureException {


 339         try {
 340             this.signatureAlgorithm.update(buf, offset, len);
 341         } catch (SignatureException ex) {
 342             throw new XMLSignatureException("empty", ex);
 343         }
 344     }
 345 
 346     /** @inheritDoc */
 347     protected String engineGetJCEAlgorithmString() {
 348         return this.signatureAlgorithm.getAlgorithm();
 349     }
 350 
 351     /** @inheritDoc */
 352     protected String engineGetJCEProviderName() {
 353         return this.signatureAlgorithm.getProvider().getName();
 354     }
 355 
 356     /** @inheritDoc */
 357     protected void engineSetHMACOutputLength(int HMACOutputLength)
 358         throws XMLSignatureException {
 359         throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC");
 360     }
 361 
 362     /** @inheritDoc */
 363     protected void engineInitSign(
 364         Key signingKey, AlgorithmParameterSpec algorithmParameterSpec
 365     ) throws XMLSignatureException {
 366         throw new XMLSignatureException("algorithms.CannotUseAlgorithmParameterSpecOnRSA");

 367     }
 368 
 369     /**
 370      * Class SignatureRSASHA1
 371      *
 372      * @author $Author: marcx $

 373      */
 374     public static class SignatureECDSASHA1 extends SignatureECDSA {

 375         /**
 376          * Constructor SignatureRSASHA1
 377          *
 378          * @throws XMLSignatureException
 379          */
 380         public SignatureECDSASHA1() throws XMLSignatureException {
 381             super();
 382         }
 383 
 384         /** @inheritDoc */
 385         public String engineGetURI() {
 386             return XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA1;
 387         }
 388     }
 389 
 390     /**
 391      * Class SignatureRSASHA256
 392      *
 393      * @author Alex Dupre
 394      */
 395     public static class SignatureECDSASHA256 extends SignatureECDSA {
 396 
 397         /**
 398          * Constructor SignatureRSASHA256
 399          *
 400          * @throws XMLSignatureException
 401          */
 402         public SignatureECDSASHA256() throws XMLSignatureException {
 403             super();
 404         }
 405 
 406         /** @inheritDoc */
 407         public String engineGetURI() {
 408             return XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA256;
 409         }
 410     }
 411 
 412     /**
 413      * Class SignatureRSASHA384
 414      *
 415      * @author Alex Dupre
 416      */
 417     public static class SignatureECDSASHA384 extends SignatureECDSA {
 418 
 419         /**
 420          * Constructor SignatureRSASHA384
 421          *
 422          * @throws XMLSignatureException
 423          */
 424         public SignatureECDSASHA384() throws XMLSignatureException {
 425             super();
 426         }
 427 
 428         /** @inheritDoc */
 429         public String engineGetURI() {
 430             return XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA384;
 431         }
 432     }
 433 
 434     /**
 435      * Class SignatureRSASHA512
 436      *
 437      * @author Alex Dupre
 438      */
 439     public static class SignatureECDSASHA512 extends SignatureECDSA {
 440 
 441         /**
 442          * Constructor SignatureRSASHA512
 443          *
 444          * @throws XMLSignatureException
 445          */
 446         public SignatureECDSASHA512() throws XMLSignatureException {
 447             super();
 448         }
 449 
 450         /** @inheritDoc */
 451         public String engineGetURI() {
 452             return XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA512;
 453         }
 454     }
 455 
 456 }