1 17 package com.sun.org.apache.xml.internal.security.algorithms.implementations; 18 19 20 21 import java.io.IOException ; 22 import java.security.InvalidAlgorithmParameterException ; 23 import java.security.InvalidKeyException ; 24 import java.security.Key ; 25 import java.security.PrivateKey ; 26 import java.security.PublicKey ; 27 import java.security.SecureRandom ; 28 import java.security.Signature ; 29 import java.security.SignatureException ; 30 import java.security.spec.AlgorithmParameterSpec ; 31 32 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper; 33 import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithmSpi; 34 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; 35 import com.sun.org.apache.xml.internal.security.utils.Base64; 36 import com.sun.org.apache.xml.internal.security.utils.Constants; 37 38 39 43 public class SignatureDSA extends SignatureAlgorithmSpi { 44 45 46 static java.util.logging.Logger log = 47 java.util.logging.Logger.getLogger(SignatureDSA.class.getName()); 48 49 50 public static final String _URI = Constants.SignatureSpecNS + "dsa-sha1"; 51 52 53 private java.security.Signature _signatureAlgorithm = null; 54 55 60 protected String engineGetURI() { 61 return SignatureDSA._URI; 62 } 63 64 69 public SignatureDSA() throws XMLSignatureException { 70 71 String algorithmID = JCEMapper.translateURItoJCEID(SignatureDSA._URI); 72 if (true) 73 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Created SignatureDSA using " + algorithmID); 74 75 try { 76 this._signatureAlgorithm = Signature.getInstance(algorithmID); 77 } catch (java.security.NoSuchAlgorithmException ex) { 78 Object [] exArgs = { algorithmID, 79 ex.getLocalizedMessage() }; 80 81 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 82 } 83 } 84 85 88 protected void engineSetParameter(AlgorithmParameterSpec params) 89 throws XMLSignatureException { 90 91 try { 92 this._signatureAlgorithm.setParameter(params); 93 } catch (InvalidAlgorithmParameterException ex) { 94 throw new XMLSignatureException("empty", ex); 95 } 96 } 97 98 101 protected boolean engineVerify(byte[] signature) 102 throws XMLSignatureException { 103 104 try { 105 if (true) 106 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Called DSA.verify() on " + Base64.encode(signature)); 107 108 byte[] jcebytes = SignatureDSA.convertXMLDSIGtoASN1(signature); 109 110 return this._signatureAlgorithm.verify(jcebytes); 111 } catch (SignatureException ex) { 112 throw new XMLSignatureException("empty", ex); 113 } catch (IOException ex) { 114 throw new XMLSignatureException("empty", ex); 115 } 116 } 117 118 121 protected void engineInitVerify(Key publicKey) throws XMLSignatureException { 122 123 if (!(publicKey instanceof PublicKey )) { 124 String supplied = publicKey.getClass().getName(); 125 String needed = PublicKey .class.getName(); 126 Object exArgs[] = { supplied, needed }; 127 128 throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", 129 exArgs); 130 } 131 132 try { 133 this._signatureAlgorithm.initVerify((PublicKey ) publicKey); 134 } catch (InvalidKeyException ex) { 135 throw new XMLSignatureException("empty", ex); 136 } 137 } 138 139 142 protected byte[] engineSign() throws XMLSignatureException { 143 144 try { 145 byte jcebytes[] = this._signatureAlgorithm.sign(); 146 147 return SignatureDSA.convertASN1toXMLDSIG(jcebytes); 148 } catch (IOException ex) { 149 throw new XMLSignatureException("empty", ex); 150 } catch (SignatureException ex) { 151 throw new XMLSignatureException("empty", ex); 152 } 153 } 154 155 158 protected void engineInitSign(Key privateKey, SecureRandom secureRandom) 159 throws XMLSignatureException { 160 161 if (!(privateKey instanceof PrivateKey )) { 162 String supplied = privateKey.getClass().getName(); 163 String needed = PrivateKey .class.getName(); 164 Object exArgs[] = { supplied, needed }; 165 166 throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", 167 exArgs); 168 } 169 170 try { 171 this._signatureAlgorithm.initSign((PrivateKey ) privateKey, 172 secureRandom); 173 } catch (InvalidKeyException ex) { 174 throw new XMLSignatureException("empty", ex); 175 } 176 } 177 178 181 protected void engineInitSign(Key privateKey) throws XMLSignatureException { 182 183 if (!(privateKey instanceof PrivateKey )) { 184 String supplied = privateKey.getClass().getName(); 185 String needed = PrivateKey .class.getName(); 186 Object exArgs[] = { supplied, needed }; 187 188 throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", 189 exArgs); 190 } 191 192 try { 193 this._signatureAlgorithm.initSign((PrivateKey ) privateKey); 194 } catch (InvalidKeyException ex) { 195 throw new XMLSignatureException("empty", ex); 196 } 197 } 198 199 202 protected void engineUpdate(byte[] input) throws XMLSignatureException { 203 204 try { 205 this._signatureAlgorithm.update(input); 206 } catch (SignatureException ex) { 207 throw new XMLSignatureException("empty", ex); 208 } 209 } 210 211 214 protected void engineUpdate(byte input) throws XMLSignatureException { 215 216 try { 217 this._signatureAlgorithm.update(input); 218 } catch (SignatureException ex) { 219 throw new XMLSignatureException("empty", ex); 220 } 221 } 222 223 226 protected void engineUpdate(byte buf[], int offset, int len) 227 throws XMLSignatureException { 228 229 try { 230 this._signatureAlgorithm.update(buf, offset, len); 231 } catch (SignatureException ex) { 232 throw new XMLSignatureException("empty", ex); 233 } 234 } 235 236 241 protected String engineGetJCEAlgorithmString() { 242 return this._signatureAlgorithm.getAlgorithm(); 243 } 244 245 250 protected String engineGetJCEProviderName() { 251 return this._signatureAlgorithm.getProvider().getName(); 252 } 253 254 255 267 private static byte[] convertASN1toXMLDSIG(byte asn1Bytes[]) 268 throws IOException { 269 270 byte rLength = asn1Bytes[3]; 271 int i; 272 273 for (i = rLength; (i > 0) && (asn1Bytes[(4 + rLength) - i] == 0); i--); 274 275 byte sLength = asn1Bytes[5 + rLength]; 276 int j; 277 278 for (j = sLength; 279 (j > 0) && (asn1Bytes[(6 + rLength + sLength) - j] == 0); j--); 280 281 if ((asn1Bytes[0] != 48) || (asn1Bytes[1] != asn1Bytes.length - 2) 282 || (asn1Bytes[2] != 2) || (i > 20) 283 || (asn1Bytes[4 + rLength] != 2) || (j > 20)) { 284 throw new IOException ("Invalid ASN.1 format of DSA signature"); 285 } 286 byte xmldsigBytes[] = new byte[40]; 287 288 System.arraycopy(asn1Bytes, (4 + rLength) - i, xmldsigBytes, 20 - i, 289 i); 290 System.arraycopy(asn1Bytes, (6 + rLength + sLength) - j, xmldsigBytes, 291 40 - j, j); 292 293 return xmldsigBytes; 294 } 295 296 308 private static byte[] convertXMLDSIGtoASN1(byte xmldsigBytes[]) 309 throws IOException { 310 311 if (xmldsigBytes.length != 40) { 312 throw new IOException ("Invalid XMLDSIG format of DSA signature"); 313 } 314 315 int i; 316 317 for (i = 20; (i > 0) && (xmldsigBytes[20 - i] == 0); i--); 318 319 int j = i; 320 321 if (xmldsigBytes[20 - i] < 0) { 322 j += 1; 323 } 324 325 int k; 326 327 for (k = 20; (k > 0) && (xmldsigBytes[40 - k] == 0); k--); 328 329 int l = k; 330 331 if (xmldsigBytes[40 - k] < 0) { 332 l += 1; 333 } 334 335 byte asn1Bytes[] = new byte[6 + j + l]; 336 337 asn1Bytes[0] = 48; 338 asn1Bytes[1] = (byte) (4 + j + l); 339 asn1Bytes[2] = 2; 340 asn1Bytes[3] = (byte) j; 341 342 System.arraycopy(xmldsigBytes, 20 - i, asn1Bytes, (4 + j) - i, i); 343 344 asn1Bytes[4 + j] = 2; 345 asn1Bytes[5 + j] = (byte) l; 346 347 System.arraycopy(xmldsigBytes, 40 - k, asn1Bytes, (6 + j + l) - k, k); 348 349 return asn1Bytes; 350 } 351 352 358 protected void engineSetHMACOutputLength(int HMACOutputLength) 359 throws XMLSignatureException { 360 throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC"); 361 } 362 363 370 protected void engineInitSign( 371 Key signingKey, AlgorithmParameterSpec algorithmParameterSpec) 372 throws XMLSignatureException { 373 throw new XMLSignatureException( 374 "algorithms.CannotUseAlgorithmParameterSpecOnDSA"); 375 } 376 } 377 | Popular Tags |