1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. */ /* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ /* * $Id: DOMDSASignatureMethod.java,v 1.14.4.4 2005/09/14 20:44:33 mullan Exp $ */ package org.jcp.xml.dsig.internal.dom; import javax.xml.crypto.*; import javax.xml.crypto.dsig.*; import javax.xml.crypto.dsig.spec.SignatureMethodParameterSpec; import java.io.IOException; import java.security.InvalidKeyException; import java.security.Key; import java.security.PrivateKey; import java.security.PublicKey; import java.security.NoSuchAlgorithmException; import java.security.InvalidAlgorithmParameterException; import java.security.Signature; import java.security.SignatureException; import java.security.spec.AlgorithmParameterSpec; import java.util.logging.Level; import java.util.logging.Logger; import org.w3c.dom.Element; import org.jcp.xml.dsig.internal.SignerOutputStream; /** * DOM-based implementation of SignatureMethod for DSA algorithm. * Use DOMHMACSignatureMethod for HMAC algorithms. * * @author Sean Mullan */ public final class DOMDSASignatureMethod extends DOMSignatureMethod { private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom"); private Signature signature; /** * Creates a <code>DOMDSASignatureMethod</code> with the specified * input parameters. * * @param params algorithm-specific parameters (may be null) * @throws InvalidAlgorithmParameterException if the parameters are not * appropriate for this signature method */ public DOMDSASignatureMethod(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { super(SignatureMethod.DSA_SHA1, params); } /** * Creates a <code>DOMDSASignatureMethod</code> from an element. * * @param smElem a SignatureMethod element */ public DOMDSASignatureMethod(Element smElem) throws MarshalException { super(smElem); } protected void checkParams(SignatureMethodParameterSpec params) throws InvalidAlgorithmParameterException { if (params != null) { throw new InvalidAlgorithmParameterException("no parameters " + "should be specified for DSA signature algorithm"); } } protected SignatureMethodParameterSpec unmarshalParams(Element paramsElem) throws MarshalException { throw new MarshalException("no parameters should " + "be specified for DSA signature algorithm"); } protected void marshalParams(Element parent, String prefix) throws MarshalException { // should never get invoked throw new MarshalException("no parameters should " + "be specified for DSA signature algorithm"); } protected boolean paramsEqual(AlgorithmParameterSpec spec) { // params should always be null return (getParameterSpec() == spec); } public boolean verify(Key key, DOMSignedInfo si, byte[] sig, XMLValidateContext context) throws InvalidKeyException, SignatureException, XMLSignatureException { if (key == null) { throw new NullPointerException("key cannot be null"); } else if (sig == null) { throw new NullPointerException("signature cannot be null"); } else if (si == null) { throw new NullPointerException("signedInfo cannot be null"); } if (signature == null) { try { signature = Signature.getInstance("SHA1withDSA"); } catch (NoSuchAlgorithmException nsae) { throw new SignatureException("SHA1withDSA Signature not found"); } } try { if (!(key instanceof PublicKey)) { throw new InvalidKeyException("key must be PublicKey"); } signature.initVerify((PublicKey) key); si.canonicalize(context, new SignerOutputStream(signature)); // avoid overhead of converting key to String unless necessary if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "verifying with key: " + key); } return signature.verify(convertXMLDSIGtoASN1(sig)); } catch (IOException ioex) { // should never occur! throw new RuntimeException(ioex.getMessage()); } } public byte[] sign(Key key, DOMSignedInfo si, XMLSignContext context) throws InvalidKeyException, XMLSignatureException { if (key == null || si == null) { throw new NullPointerException(); } if (!(key instanceof PrivateKey)) { throw new InvalidKeyException("key must be PrivateKey"); } if (signature == null) { try { signature = Signature.getInstance("SHA1withDSA"); } catch (NoSuchAlgorithmException nsae) { throw new InvalidKeyException("SHA1withDSA Signature not found"); } } // avoid overhead of converting key to String unless necessary if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "Signing with key: " + key); } signature.initSign((PrivateKey) key); si.canonicalize(context, new SignerOutputStream(signature)); try { return convertASN1toXMLDSIG(signature.sign()); } catch (SignatureException se) { // should never occur! throw new RuntimeException(se.getMessage()); } catch (IOException ioex) { // should never occur! throw new RuntimeException(ioex.getMessage()); } } /** * 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 * * @throws IOException * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A> */ private static byte[] convertASN1toXMLDSIG(byte asn1Bytes[]) throws IOException { // THIS CODE IS COPIED FROM APACHE (see copyright at top of file) 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"); } else { 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; } } /** * 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 * * @throws IOException * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A> */ private static byte[] convertXMLDSIGtoASN1(byte xmldsigBytes[]) throws IOException { // THIS CODE IS COPIED FROM APACHE (see copyright at top of file) 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; } }