1 17 package com.sun.org.apache.xml.internal.security.algorithms; 18 19 20 import java.security.Key ; 21 import java.security.SecureRandom ; 22 import java.security.spec.AlgorithmParameterSpec ; 23 import java.util.HashMap ; 24 25 import com.sun.org.apache.xml.internal.security.algorithms.implementations.IntegrityHmac; 26 import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException; 27 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 28 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; 29 import com.sun.org.apache.xml.internal.security.utils.Constants; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 33 34 39 public class SignatureAlgorithm extends Algorithm { 40 41 42 static java.util.logging.Logger log = 43 java.util.logging.Logger.getLogger(SignatureAlgorithm.class.getName()); 44 45 46 static boolean _alreadyInitialized = false; 47 48 49 static HashMap _algorithmHash = null; 50 51 52 protected SignatureAlgorithmSpi _signatureAlgorithm = null; 53 54 61 public SignatureAlgorithm(Document doc, String algorithmURI) 62 throws XMLSecurityException { 63 64 super(doc, algorithmURI); 65 66 try { 67 Class implementingClass = 68 SignatureAlgorithm.getImplementingClass(algorithmURI); 69 if (true) 70 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Create URI \"" + algorithmURI + "\" class \"" 71 + implementingClass + "\""); 72 73 this._signatureAlgorithm = 74 (SignatureAlgorithmSpi) implementingClass.newInstance(); 75 } catch (IllegalAccessException ex) { 76 Object exArgs[] = { algorithmURI, ex.getMessage() }; 77 78 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, 79 ex); 80 } catch (InstantiationException ex) { 81 Object exArgs[] = { algorithmURI, ex.getMessage() }; 82 83 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, 84 ex); 85 } catch (NullPointerException ex) { 86 Object exArgs[] = { algorithmURI, ex.getMessage() }; 87 88 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, 89 ex); 90 } 91 } 92 93 101 public SignatureAlgorithm( 102 Document doc, String algorithmURI, int HMACOutputLength) 103 throws XMLSecurityException { 104 105 this(doc, algorithmURI); 106 107 this._signatureAlgorithm.engineSetHMACOutputLength(HMACOutputLength); 108 ((IntegrityHmac)this._signatureAlgorithm) 109 .engineAddContextToElement(this._constructionElement); 110 } 111 112 119 public SignatureAlgorithm(Element element, String BaseURI) 120 throws XMLSecurityException { 121 122 super(element, BaseURI); 123 124 String algorithmURI = this.getURI(); 125 126 try { 127 Class implementingClass = 128 SignatureAlgorithm.getImplementingClass(algorithmURI); 129 if (true) 130 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Create URI \"" + algorithmURI + "\" class \"" 131 + implementingClass + "\""); 132 133 this._signatureAlgorithm = 134 (SignatureAlgorithmSpi) implementingClass.newInstance(); 135 136 this._signatureAlgorithm 137 .engineGetContextFromElement(this._constructionElement); 138 } catch (IllegalAccessException ex) { 139 Object exArgs[] = { algorithmURI, ex.getMessage() }; 140 141 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, 142 ex); 143 } catch (InstantiationException ex) { 144 Object exArgs[] = { algorithmURI, ex.getMessage() }; 145 146 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, 147 ex); 148 } catch (NullPointerException ex) { 149 Object exArgs[] = { algorithmURI, ex.getMessage() }; 150 151 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, 152 ex); 153 } 154 } 155 156 163 public byte[] sign() throws XMLSignatureException { 164 return this._signatureAlgorithm.engineSign(); 165 } 166 167 173 public String getJCEAlgorithmString() { 174 return this._signatureAlgorithm.engineGetJCEAlgorithmString(); 175 } 176 177 182 public String getJCEProviderName() { 183 return this._signatureAlgorithm.engineGetJCEProviderName(); 184 } 185 186 193 public void update(byte[] input) throws XMLSignatureException { 194 this._signatureAlgorithm.engineUpdate(input); 195 } 196 197 204 public void update(byte input) throws XMLSignatureException { 205 this._signatureAlgorithm.engineUpdate(input); 206 } 207 208 217 public void update(byte buf[], int offset, int len) 218 throws XMLSignatureException { 219 this._signatureAlgorithm.engineUpdate(buf, offset, len); 220 } 221 222 229 public void initSign(Key signingKey) throws XMLSignatureException { 230 this._signatureAlgorithm.engineInitSign(signingKey); 231 } 232 233 241 public void initSign(Key signingKey, SecureRandom secureRandom) 242 throws XMLSignatureException { 243 this._signatureAlgorithm.engineInitSign(signingKey, secureRandom); 244 } 245 246 254 public void initSign( 255 Key signingKey, AlgorithmParameterSpec algorithmParameterSpec) 256 throws XMLSignatureException { 257 this._signatureAlgorithm.engineInitSign(signingKey, 258 algorithmParameterSpec); 259 } 260 261 268 public void setParameter(AlgorithmParameterSpec params) 269 throws XMLSignatureException { 270 this._signatureAlgorithm.engineSetParameter(params); 271 } 272 273 280 public void initVerify(Key verificationKey) throws XMLSignatureException { 281 this._signatureAlgorithm.engineInitVerify(verificationKey); 282 } 283 284 293 public boolean verify(byte[] signature) throws XMLSignatureException { 294 return this._signatureAlgorithm.engineVerify(signature); 295 } 296 297 302 public final String getURI() { 303 return this._constructionElement.getAttributeNS(null, 304 Constants._ATT_ALGORITHM); 305 } 306 307 311 public static void providerInit() { 312 313 if (SignatureAlgorithm.log == null) { 314 SignatureAlgorithm.log = 315 java.util.logging.Logger 316 .getLogger(SignatureAlgorithm.class.getName()); 317 } 318 319 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Init() called"); 320 321 if (!SignatureAlgorithm._alreadyInitialized) { 322 SignatureAlgorithm._algorithmHash = new HashMap (10); 323 SignatureAlgorithm._alreadyInitialized = true; 324 } 325 } 326 327 335 public static void register(String algorithmURI, String implementingClass) 336 throws AlgorithmAlreadyRegisteredException,XMLSignatureException { 337 338 { 339 if (true) 340 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Try to register " + algorithmURI + " " + implementingClass); 341 342 Class registeredClassClass = 344 SignatureAlgorithm.getImplementingClass(algorithmURI); 345 if (registeredClassClass!=null) { 346 String registeredClass = registeredClassClass.getName(); 347 348 if ((registeredClass != null) && (registeredClass.length() != 0)) { 349 Object exArgs[] = { algorithmURI, registeredClass }; 350 351 throw new AlgorithmAlreadyRegisteredException( 352 "algorithm.alreadyRegistered", exArgs); 353 } 354 } 355 try { 356 SignatureAlgorithm._algorithmHash.put(algorithmURI, Class.forName(implementingClass)); 357 } catch (ClassNotFoundException ex) { 358 Object exArgs[] = { algorithmURI, ex.getMessage() }; 359 360 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, 361 ex); 362 } catch (NullPointerException ex) { 363 Object exArgs[] = { algorithmURI, ex.getMessage() }; 364 365 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, 366 ex); 367 } 368 369 } 370 } 371 372 378 private static Class getImplementingClass(String URI) { 379 380 if (SignatureAlgorithm._algorithmHash == null) { 381 return null; 382 } 383 384 return (Class ) SignatureAlgorithm._algorithmHash.get(URI); 385 } 386 387 392 public String getBaseNamespace() { 393 return Constants.SignatureSpecNS; 394 } 395 396 401 public String getBaseLocalName() { 402 return Constants._TAG_SIGNATUREMETHOD; 403 } 404 } 405 | Popular Tags |