org.jcp.xml.dsig.internal.dom.DOMDSASignatureMethod Maven / Gradle / Ivy
The newest version!
/* */ package org.jcp.xml.dsig.internal.dom;
/* */
/* */ import java.io.IOException;
/* */ import java.security.InvalidAlgorithmParameterException;
/* */ import java.security.InvalidKeyException;
/* */ import java.security.Key;
/* */ import java.security.NoSuchAlgorithmException;
/* */ import java.security.PrivateKey;
/* */ import java.security.PublicKey;
/* */ import java.security.Signature;
/* */ import java.security.SignatureException;
/* */ import java.security.spec.AlgorithmParameterSpec;
/* */ import java.util.logging.Level;
/* */ import java.util.logging.Logger;
/* */ import javax.xml.crypto.MarshalException;
/* */ import javax.xml.crypto.dsig.XMLSignContext;
/* */ import javax.xml.crypto.dsig.XMLSignatureException;
/* */ import javax.xml.crypto.dsig.XMLValidateContext;
/* */ import javax.xml.crypto.dsig.spec.SignatureMethodParameterSpec;
/* */ import org.jcp.xml.dsig.internal.SignerOutputStream;
/* */ import org.w3c.dom.Element;
/* */
/* */ public final class DOMDSASignatureMethod extends DOMSignatureMethod
/* */ {
/* 53 */ private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom");
/* */ private Signature signature;
/* */
/* */ public DOMDSASignatureMethod(AlgorithmParameterSpec params)
/* */ throws InvalidAlgorithmParameterException
/* */ {
/* 66 */ super("http://www.w3.org/2000/09/xmldsig#dsa-sha1", params);
/* */ }
/* */
/* */ public DOMDSASignatureMethod(Element smElem)
/* */ throws MarshalException
/* */ {
/* 75 */ super(smElem);
/* */ }
/* */
/* */ protected void checkParams(SignatureMethodParameterSpec params) throws InvalidAlgorithmParameterException
/* */ {
/* 80 */ if (params != null)
/* 81 */ throw new InvalidAlgorithmParameterException("no parameters should be specified for DSA signature algorithm");
/* */ }
/* */
/* */ protected SignatureMethodParameterSpec unmarshalParams(Element paramsElem)
/* */ throws MarshalException
/* */ {
/* 88 */ throw new MarshalException("no parameters should be specified for DSA signature algorithm");
/* */ }
/* */
/* */ protected void marshalParams(Element parent, String prefix)
/* */ throws MarshalException
/* */ {
/* 95 */ throw new MarshalException("no parameters should be specified for DSA signature algorithm");
/* */ }
/* */
/* */ protected boolean paramsEqual(AlgorithmParameterSpec spec)
/* */ {
/* 101 */ return getParameterSpec() == spec;
/* */ }
/* */
/* */ public boolean verify(Key key, DOMSignedInfo si, byte[] sig, XMLValidateContext context)
/* */ throws InvalidKeyException, SignatureException, XMLSignatureException
/* */ {
/* 107 */ if (key == null)
/* 108 */ throw new NullPointerException("key cannot be null");
/* 109 */ if (sig == null)
/* 110 */ throw new NullPointerException("signature cannot be null");
/* 111 */ if (si == null) {
/* 112 */ throw new NullPointerException("signedInfo cannot be null");
/* */ }
/* 114 */ if (this.signature == null)
/* */ try {
/* 116 */ this.signature = Signature.getInstance("SHA1withDSA");
/* */ } catch (NoSuchAlgorithmException nsae) {
/* 118 */ throw new SignatureException("SHA1withDSA Signature not found");
/* */ }
/* */ try
/* */ {
/* 122 */ if (!(key instanceof PublicKey)) {
/* 123 */ throw new InvalidKeyException("key must be PublicKey");
/* */ }
/* 125 */ this.signature.initVerify((PublicKey)key);
/* 126 */ si.canonicalize(context, new SignerOutputStream(this.signature));
/* */
/* 129 */ if (log.isLoggable(Level.FINE)) {
/* 130 */ log.log(Level.FINE, "verifying with key: " + key);
/* */ }
/* 132 */ return this.signature.verify(convertXMLDSIGtoASN1(sig));
/* */ }
/* */ catch (IOException ioex) {
/* 135 */ throw new RuntimeException(ioex.getMessage());
/* */ }
/* */ }
/* */
/* */ public byte[] sign(Key key, DOMSignedInfo si, XMLSignContext context) throws InvalidKeyException, XMLSignatureException
/* */ {
/* 141 */ if ((key == null) || (si == null)) {
/* 142 */ throw new NullPointerException();
/* */ }
/* */
/* 145 */ if (!(key instanceof PrivateKey)) {
/* 146 */ throw new InvalidKeyException("key must be PrivateKey");
/* */ }
/* 148 */ if (this.signature == null) {
/* */ try {
/* 150 */ this.signature = Signature.getInstance("SHA1withDSA");
/* */ } catch (NoSuchAlgorithmException nsae) {
/* 152 */ throw new InvalidKeyException("SHA1withDSA Signature not found");
/* */ }
/* */
/* */ }
/* */
/* 157 */ if (log.isLoggable(Level.FINE)) {
/* 158 */ log.log(Level.FINE, "Signing with key: " + key);
/* */ }
/* 160 */ this.signature.initSign((PrivateKey)key);
/* 161 */ si.canonicalize(context, new SignerOutputStream(this.signature));
/* */ try
/* */ {
/* 164 */ return convertASN1toXMLDSIG(this.signature.sign());
/* */ }
/* */ catch (SignatureException se) {
/* 167 */ throw new RuntimeException(se.getMessage());
/* */ }
/* */ catch (IOException ioex) {
/* 170 */ throw new RuntimeException(ioex.getMessage());
/* */ }
/* */ }
/* */
/* */ private static byte[] convertASN1toXMLDSIG(byte[] asn1Bytes)
/* */ throws IOException
/* */ {
/* 189 */ byte rLength = asn1Bytes[3];
/* */
/* 192 */ for (int i = rLength; (i > 0) && (asn1Bytes[(4 + rLength - i)] == 0); i--);
/* 194 */ byte sLength = asn1Bytes[(5 + rLength)];
/* */
/* 197 */ int j = sLength;
/* 198 */ while ((j > 0) && (asn1Bytes[(6 + rLength + sLength - j)] == 0)) j--;
/* */
/* 200 */ if ((asn1Bytes[0] != 48) || (asn1Bytes[1] != asn1Bytes.length - 2) || (asn1Bytes[2] != 2) || (i > 20) || (asn1Bytes[(4 + rLength)] != 2) || (j > 20))
/* */ {
/* 203 */ throw new IOException("Invalid ASN.1 format of DSA signature");
/* */ }
/* 205 */ byte[] xmldsigBytes = new byte[40];
/* */
/* 207 */ System.arraycopy(asn1Bytes, 4 + rLength - i, xmldsigBytes, 20 - i, i);
/* 208 */ System.arraycopy(asn1Bytes, 6 + rLength + sLength - j, xmldsigBytes, 40 - j, j);
/* */
/* 211 */ return xmldsigBytes;
/* */ }
/* */
/* */ private static byte[] convertXMLDSIGtoASN1(byte[] xmldsigBytes)
/* */ throws IOException
/* */ {
/* 230 */ if (xmldsigBytes.length != 40) {
/* 231 */ throw new IOException("Invalid XMLDSIG format of DSA signature");
/* */ }
/* */
/* 236 */ for (int i = 20; (i > 0) && (xmldsigBytes[(20 - i)] == 0); i--);
/* 238 */ int j = i;
/* */
/* 240 */ if (xmldsigBytes[(20 - i)] < 0) {
/* 241 */ j++;
/* */ }
/* */
/* 246 */ for (int k = 20; (k > 0) && (xmldsigBytes[(40 - k)] == 0); k--);
/* 248 */ int l = k;
/* */
/* 250 */ if (xmldsigBytes[(40 - k)] < 0) {
/* 251 */ l++;
/* */ }
/* */
/* 254 */ byte[] asn1Bytes = new byte[6 + j + l];
/* */
/* 256 */ asn1Bytes[0] = 48;
/* 257 */ asn1Bytes[1] = ((byte)(4 + j + l));
/* 258 */ asn1Bytes[2] = 2;
/* 259 */ asn1Bytes[3] = ((byte)j);
/* */
/* 261 */ System.arraycopy(xmldsigBytes, 20 - i, asn1Bytes, 4 + j - i, i);
/* */
/* 263 */ asn1Bytes[(4 + j)] = 2;
/* 264 */ asn1Bytes[(5 + j)] = ((byte)l);
/* */
/* 266 */ System.arraycopy(xmldsigBytes, 40 - k, asn1Bytes, 6 + j + l - k, k);
/* */
/* 268 */ return asn1Bytes;
/* */ }
/* */ }
/* Location: E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
* Qualified Name: org.jcp.xml.dsig.internal.dom.DOMDSASignatureMethod
* JD-Core Version: 0.6.2
*/