1 17 package com.sun.org.apache.xml.internal.security.algorithms.implementations; 18 19 20 21 import java.security.InvalidAlgorithmParameterException ; 22 import java.security.InvalidKeyException ; 23 import java.security.Key ; 24 import java.security.NoSuchProviderException ; 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.XMLSignature; 35 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; 36 37 38 42 public abstract class SignatureBaseRSA extends SignatureAlgorithmSpi { 43 44 45 static java.util.logging.Logger log = 46 java.util.logging.Logger.getLogger(SignatureBaseRSA.class.getName()); 47 48 49 public abstract String engineGetURI(); 50 51 52 private java.security.Signature _signatureAlgorithm = null; 53 54 59 public SignatureBaseRSA() throws XMLSignatureException { 60 61 String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI()); 62 63 if (true) 64 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Created SignatureDSA using " + algorithmID); 65 String provider=JCEMapper.getProviderId(); 66 try { 67 if (provider==null) { 68 this._signatureAlgorithm = Signature.getInstance(algorithmID); 69 } else { 70 this._signatureAlgorithm = Signature.getInstance(algorithmID,provider); 71 } 72 } catch (java.security.NoSuchAlgorithmException ex) { 73 Object [] exArgs = { algorithmID, 74 ex.getLocalizedMessage() }; 75 76 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 77 } catch (NoSuchProviderException ex) { 78 Object [] exArgs = { algorithmID, 79 ex.getLocalizedMessage() }; 80 81 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 82 } 83 } 84 85 86 protected void engineSetParameter(AlgorithmParameterSpec params) 87 throws XMLSignatureException { 88 89 try { 90 this._signatureAlgorithm.setParameter(params); 91 } catch (InvalidAlgorithmParameterException ex) { 92 throw new XMLSignatureException("empty", ex); 93 } 94 } 95 96 97 protected boolean engineVerify(byte[] signature) 98 throws XMLSignatureException { 99 100 try { 101 return this._signatureAlgorithm.verify(signature); 102 } catch (SignatureException ex) { 103 throw new XMLSignatureException("empty", ex); 104 } 105 } 106 107 108 protected void engineInitVerify(Key publicKey) throws XMLSignatureException { 109 110 if (!(publicKey instanceof PublicKey )) { 111 String supplied = publicKey.getClass().getName(); 112 String needed = PublicKey .class.getName(); 113 Object exArgs[] = { supplied, needed }; 114 115 throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", 116 exArgs); 117 } 118 119 try { 120 this._signatureAlgorithm.initVerify((PublicKey ) publicKey); 121 } catch (InvalidKeyException ex) { 122 throw new XMLSignatureException("empty", ex); 123 } 124 } 125 126 127 protected byte[] engineSign() throws XMLSignatureException { 128 129 try { 130 return this._signatureAlgorithm.sign(); 131 } catch (SignatureException ex) { 132 throw new XMLSignatureException("empty", ex); 133 } 134 } 135 136 137 protected void engineInitSign(Key privateKey, SecureRandom secureRandom) 138 throws XMLSignatureException { 139 140 if (!(privateKey instanceof PrivateKey )) { 141 String supplied = privateKey.getClass().getName(); 142 String needed = PrivateKey .class.getName(); 143 Object exArgs[] = { supplied, needed }; 144 145 throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", 146 exArgs); 147 } 148 149 try { 150 this._signatureAlgorithm.initSign((PrivateKey ) privateKey, 151 secureRandom); 152 } catch (InvalidKeyException ex) { 153 throw new XMLSignatureException("empty", ex); 154 } 155 } 156 157 158 protected void engineInitSign(Key privateKey) throws XMLSignatureException { 159 160 if (!(privateKey instanceof PrivateKey )) { 161 String supplied = privateKey.getClass().getName(); 162 String needed = PrivateKey .class.getName(); 163 Object exArgs[] = { supplied, needed }; 164 165 throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", 166 exArgs); 167 } 168 169 try { 170 this._signatureAlgorithm.initSign((PrivateKey ) privateKey); 171 } catch (InvalidKeyException ex) { 172 throw new XMLSignatureException("empty", ex); 173 } 174 } 175 176 177 protected void engineUpdate(byte[] input) throws XMLSignatureException { 178 179 try { 180 this._signatureAlgorithm.update(input); 181 } catch (SignatureException ex) { 182 throw new XMLSignatureException("empty", ex); 183 } 184 } 185 186 187 protected void engineUpdate(byte input) throws XMLSignatureException { 188 189 try { 190 this._signatureAlgorithm.update(input); 191 } catch (SignatureException ex) { 192 throw new XMLSignatureException("empty", ex); 193 } 194 } 195 196 197 protected void engineUpdate(byte buf[], int offset, int len) 198 throws XMLSignatureException { 199 200 try { 201 this._signatureAlgorithm.update(buf, offset, len); 202 } catch (SignatureException ex) { 203 throw new XMLSignatureException("empty", ex); 204 } 205 } 206 207 208 protected String engineGetJCEAlgorithmString() { 209 return this._signatureAlgorithm.getAlgorithm(); 210 } 211 212 213 protected String engineGetJCEProviderName() { 214 return this._signatureAlgorithm.getProvider().getName(); 215 } 216 217 218 protected void engineSetHMACOutputLength(int HMACOutputLength) 219 throws XMLSignatureException { 220 throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC"); 221 } 222 223 224 protected void engineInitSign( 225 Key signingKey, AlgorithmParameterSpec algorithmParameterSpec) 226 throws XMLSignatureException { 227 throw new XMLSignatureException( 228 "algorithms.CannotUseAlgorithmParameterSpecOnRSA"); 229 } 230 231 237 public static class SignatureRSASHA1 extends SignatureBaseRSA { 238 239 244 public SignatureRSASHA1() throws XMLSignatureException { 245 super(); 246 } 247 248 249 public String engineGetURI() { 250 return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1; 251 } 252 } 253 254 260 public static class SignatureRSASHA256 extends SignatureBaseRSA { 261 262 267 public SignatureRSASHA256() throws XMLSignatureException { 268 super(); 269 } 270 271 272 public String engineGetURI() { 273 return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256; 274 } 275 } 276 277 283 public static class SignatureRSASHA384 extends SignatureBaseRSA { 284 285 290 public SignatureRSASHA384() throws XMLSignatureException { 291 super(); 292 } 293 294 295 public String engineGetURI() { 296 return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384; 297 } 298 } 299 300 306 public static class SignatureRSASHA512 extends SignatureBaseRSA { 307 308 313 public SignatureRSASHA512() throws XMLSignatureException { 314 super(); 315 } 316 317 318 public String engineGetURI() { 319 return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512; 320 } 321 } 322 323 329 public static class SignatureRSARIPEMD160 extends SignatureBaseRSA { 330 331 336 public SignatureRSARIPEMD160() throws XMLSignatureException { 337 super(); 338 } 339 340 341 public String engineGetURI() { 342 return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160; 343 } 344 } 345 346 352 public static class SignatureRSAMD5 extends SignatureBaseRSA { 353 354 359 public SignatureRSAMD5() throws XMLSignatureException { 360 super(); 361 } 362 363 364 public String engineGetURI() { 365 return XMLSignature.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5; 366 } 367 } 368 } 369 | Popular Tags |