1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 package com.sun.org.apache.xml.internal.security.algorithms.implementations; 24 25 import java.security.InvalidAlgorithmParameterException; 26 import java.security.InvalidKeyException; 27 import java.security.Key; 28 import java.security.NoSuchProviderException; 29 import java.security.PrivateKey; 30 import java.security.PublicKey; 31 import java.security.SecureRandom; 32 import java.security.Signature; 33 import java.security.SignatureException; 34 import java.security.spec.AlgorithmParameterSpec; 35 36 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper; 37 import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithmSpi; 38 import com.sun.org.apache.xml.internal.security.signature.XMLSignature; 39 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; 40 41 public abstract class SignatureBaseRSA extends SignatureAlgorithmSpi { 42 43 /** {@link org.apache.commons.logging} logging facility */ 44 private static java.util.logging.Logger log = 45 java.util.logging.Logger.getLogger(SignatureBaseRSA.class.getName()); 46 47 /** @inheritDoc */ 48 public abstract String engineGetURI(); 49 50 /** Field algorithm */ 51 private java.security.Signature signatureAlgorithm = null; 52 53 /** 54 * Constructor SignatureRSA 55 * 56 * @throws XMLSignatureException 57 */ 58 public SignatureBaseRSA() throws XMLSignatureException { 59 String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI()); 60 61 if (log.isLoggable(java.util.logging.Level.FINE)) { 62 log.log(java.util.logging.Level.FINE, "Created SignatureRSA using " + algorithmID); 63 } 64 String provider = JCEMapper.getProviderId(); 65 try { 66 if (provider == null) { 67 this.signatureAlgorithm = Signature.getInstance(algorithmID); 68 } else { 69 this.signatureAlgorithm = Signature.getInstance(algorithmID,provider); 70 } 71 } catch (java.security.NoSuchAlgorithmException ex) { 72 Object[] exArgs = { algorithmID, ex.getLocalizedMessage() }; 73 74 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 75 } catch (NoSuchProviderException ex) { 76 Object[] exArgs = { algorithmID, ex.getLocalizedMessage() }; 77 78 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 79 } 80 } 81 82 /** @inheritDoc */ 83 protected void engineSetParameter(AlgorithmParameterSpec params) 84 throws XMLSignatureException { 85 try { 86 this.signatureAlgorithm.setParameter(params); 87 } catch (InvalidAlgorithmParameterException ex) { 88 throw new XMLSignatureException("empty", ex); 89 } 90 } 91 92 /** @inheritDoc */ 93 protected boolean engineVerify(byte[] signature) throws XMLSignatureException { 94 try { 95 return this.signatureAlgorithm.verify(signature); 96 } catch (SignatureException ex) { 97 throw new XMLSignatureException("empty", ex); 98 } 99 } 100 101 /** @inheritDoc */ 102 protected void engineInitVerify(Key publicKey) throws XMLSignatureException { 103 if (!(publicKey instanceof PublicKey)) { 104 String supplied = publicKey.getClass().getName(); 105 String needed = PublicKey.class.getName(); 106 Object exArgs[] = { supplied, needed }; 107 108 throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs); 109 } 110 111 try { 112 this.signatureAlgorithm.initVerify((PublicKey) publicKey); 113 } catch (InvalidKeyException ex) { 114 // reinstantiate Signature object to work around bug in JDK 115 // see: http://bugs.sun.com/view_bug.do?bug_id=4953555 116 Signature sig = this.signatureAlgorithm; 117 try { 118 this.signatureAlgorithm = Signature.getInstance(signatureAlgorithm.getAlgorithm()); 119 } catch (Exception e) { 120 // this shouldn't occur, but if it does, restore previous 121 // Signature 122 if (log.isLoggable(java.util.logging.Level.FINE)) { 123 log.log(java.util.logging.Level.FINE, "Exception when reinstantiating Signature:" + e); 124 } 125 this.signatureAlgorithm = sig; 126 } 127 throw new XMLSignatureException("empty", ex); 128 } 129 } 130 131 /** @inheritDoc */ 132 protected byte[] engineSign() throws XMLSignatureException { 133 try { 134 return this.signatureAlgorithm.sign(); 135 } catch (SignatureException ex) { 136 throw new XMLSignatureException("empty", ex); 137 } 138 } 139 140 /** @inheritDoc */ 141 protected void engineInitSign(Key privateKey, SecureRandom secureRandom) 142 throws XMLSignatureException { 143 if (!(privateKey instanceof PrivateKey)) { 144 String supplied = privateKey.getClass().getName(); 145 String needed = PrivateKey.class.getName(); 146 Object exArgs[] = { supplied, needed }; 147 148 throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs); 149 } 150 151 try { 152 this.signatureAlgorithm.initSign((PrivateKey) privateKey, secureRandom); 153 } catch (InvalidKeyException ex) { 154 throw new XMLSignatureException("empty", ex); 155 } 156 } 157 158 /** @inheritDoc */ 159 protected void engineInitSign(Key privateKey) throws XMLSignatureException { 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", exArgs); 166 } 167 168 try { 169 this.signatureAlgorithm.initSign((PrivateKey) privateKey); 170 } catch (InvalidKeyException ex) { 171 throw new XMLSignatureException("empty", ex); 172 } 173 } 174 175 /** @inheritDoc */ 176 protected void engineUpdate(byte[] input) throws XMLSignatureException { 177 try { 178 this.signatureAlgorithm.update(input); 179 } catch (SignatureException ex) { 180 throw new XMLSignatureException("empty", ex); 181 } 182 } 183 184 /** @inheritDoc */ 185 protected void engineUpdate(byte input) throws XMLSignatureException { 186 try { 187 this.signatureAlgorithm.update(input); 188 } catch (SignatureException ex) { 189 throw new XMLSignatureException("empty", ex); 190 } 191 } 192 193 /** @inheritDoc */ 194 protected void engineUpdate(byte buf[], int offset, int len) throws XMLSignatureException { 195 try { 196 this.signatureAlgorithm.update(buf, offset, len); 197 } catch (SignatureException ex) { 198 throw new XMLSignatureException("empty", ex); 199 } 200 } 201 202 /** @inheritDoc */ 203 protected String engineGetJCEAlgorithmString() { 204 return this.signatureAlgorithm.getAlgorithm(); 205 } 206 207 /** @inheritDoc */ 208 protected String engineGetJCEProviderName() { 209 return this.signatureAlgorithm.getProvider().getName(); 210 } 211 212 /** @inheritDoc */ 213 protected void engineSetHMACOutputLength(int HMACOutputLength) 214 throws XMLSignatureException { 215 throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC"); 216 } 217 218 /** @inheritDoc */ 219 protected void engineInitSign( 220 Key signingKey, AlgorithmParameterSpec algorithmParameterSpec 221 ) throws XMLSignatureException { 222 throw new XMLSignatureException("algorithms.CannotUseAlgorithmParameterSpecOnRSA"); 223 } 224 225 /** 226 * Class SignatureRSASHA1 227 */ 228 public static class SignatureRSASHA1 extends SignatureBaseRSA { 229 230 /** 231 * Constructor SignatureRSASHA1 232 * 233 * @throws XMLSignatureException 234 */ 235 public SignatureRSASHA1() throws XMLSignatureException { 236 super(); 237 } 238 239 /** @inheritDoc */ 240 public String engineGetURI() { 241 return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1; 242 } 243 } 244 245 /** 246 * Class SignatureRSASHA256 247 */ 248 public static class SignatureRSASHA256 extends SignatureBaseRSA { 249 250 /** 251 * Constructor SignatureRSASHA256 252 * 253 * @throws XMLSignatureException 254 */ 255 public SignatureRSASHA256() throws XMLSignatureException { 256 super(); 257 } 258 259 /** @inheritDoc */ 260 public String engineGetURI() { 261 return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256; 262 } 263 } 264 265 /** 266 * Class SignatureRSASHA384 267 */ 268 public static class SignatureRSASHA384 extends SignatureBaseRSA { 269 270 /** 271 * Constructor SignatureRSASHA384 272 * 273 * @throws XMLSignatureException 274 */ 275 public SignatureRSASHA384() throws XMLSignatureException { 276 super(); 277 } 278 279 /** @inheritDoc */ 280 public String engineGetURI() { 281 return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384; 282 } 283 } 284 285 /** 286 * Class SignatureRSASHA512 287 */ 288 public static class SignatureRSASHA512 extends SignatureBaseRSA { 289 290 /** 291 * Constructor SignatureRSASHA512 292 * 293 * @throws XMLSignatureException 294 */ 295 public SignatureRSASHA512() throws XMLSignatureException { 296 super(); 297 } 298 299 /** @inheritDoc */ 300 public String engineGetURI() { 301 return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512; 302 } 303 } 304 305 /** 306 * Class SignatureRSARIPEMD160 307 */ 308 public static class SignatureRSARIPEMD160 extends SignatureBaseRSA { 309 310 /** 311 * Constructor SignatureRSARIPEMD160 312 * 313 * @throws XMLSignatureException 314 */ 315 public SignatureRSARIPEMD160() throws XMLSignatureException { 316 super(); 317 } 318 319 /** @inheritDoc */ 320 public String engineGetURI() { 321 return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160; 322 } 323 } 324 325 /** 326 * Class SignatureRSAMD5 327 */ 328 public static class SignatureRSAMD5 extends SignatureBaseRSA { 329 330 /** 331 * Constructor SignatureRSAMD5 332 * 333 * @throws XMLSignatureException 334 */ 335 public SignatureRSAMD5() throws XMLSignatureException { 336 super(); 337 } 338 339 /** @inheritDoc */ 340 public String engineGetURI() { 341 return XMLSignature.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5; 342 } 343 } 344 }