1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 1999-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 */ 21 package com.sun.org.apache.xml.internal.security.algorithms; 22 23 import java.security.MessageDigest; 24 import java.security.NoSuchProviderException; 25 import java.util.HashMap; 26 import java.util.Map; 27 28 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; 29 import com.sun.org.apache.xml.internal.security.utils.Constants; 30 import com.sun.org.apache.xml.internal.security.utils.EncryptionConstants; 31 import org.w3c.dom.Document; 32 33 34 /** 35 * Digest Message wrapper & selector class. 36 * 37 * <pre> 38 * MessageDigestAlgorithm.getInstance() 39 * </pre> 40 * 41 */ 42 public class MessageDigestAlgorithm extends Algorithm { 43 44 /** Message Digest - NOT RECOMMENDED MD5*/ 45 public static final String ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5 = Constants.MoreAlgorithmsSpecNS + "md5"; 46 /** Digest - Required SHA1*/ 47 public static final String ALGO_ID_DIGEST_SHA1 = Constants.SignatureSpecNS + "sha1"; 48 /** Message Digest - RECOMMENDED SHA256*/ 49 public static final String ALGO_ID_DIGEST_SHA256 = EncryptionConstants.EncryptionSpecNS + "sha256"; 50 /** Message Digest - OPTIONAL SHA384*/ 51 public static final String ALGO_ID_DIGEST_SHA384 = Constants.MoreAlgorithmsSpecNS + "sha384"; 52 /** Message Digest - OPTIONAL SHA512*/ 53 public static final String ALGO_ID_DIGEST_SHA512 = EncryptionConstants.EncryptionSpecNS + "sha512"; 54 /** Message Digest - OPTIONAL RIPEMD-160*/ 55 public static final String ALGO_ID_DIGEST_RIPEMD160 = EncryptionConstants.EncryptionSpecNS + "ripemd160"; 56 57 /** Field algorithm stores the actual {@link java.security.MessageDigest} */ 58 java.security.MessageDigest algorithm = null; 59 60 /** 61 * Constructor for the brave who pass their own message digest algorithms and the corresponding URI. 62 * @param doc 63 * @param messageDigest 64 * @param algorithmURI 65 */ 66 private MessageDigestAlgorithm(Document doc, MessageDigest messageDigest, 67 String algorithmURI) { 68 69 super(doc, algorithmURI); 70 71 this.algorithm = messageDigest; 72 } 73 74 static ThreadLocal<Map<String, MessageDigest>> instances=new 75 ThreadLocal<Map<String, MessageDigest>>() { 76 protected Map<String, MessageDigest> initialValue() { 77 return new HashMap<String, MessageDigest>(); 78 }; 79 }; 80 81 /** 82 * Factory method for constructing a message digest algorithm by name. 83 * 84 * @param doc 85 * @param algorithmURI 86 * @return The MessageDigestAlgorithm element to attach in document and to digest 87 * @throws XMLSignatureException 88 */ 89 public static MessageDigestAlgorithm getInstance( 90 Document doc, String algorithmURI) throws XMLSignatureException { 91 MessageDigest md = getDigestInstance(algorithmURI); 92 return new MessageDigestAlgorithm(doc, md, algorithmURI); 93 } 94 95 private static MessageDigest getDigestInstance(String algorithmURI) throws XMLSignatureException { 96 MessageDigest result= instances.get().get(algorithmURI); 97 if (result!=null) 98 return result; 99 String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI); 100 101 if (algorithmID == null) { 102 Object[] exArgs = { algorithmURI }; 103 throw new XMLSignatureException("algorithms.NoSuchMap", exArgs); 104 } 105 106 MessageDigest md; 107 String provider=JCEMapper.getProviderId(); 108 try { 109 if (provider==null) { 110 md = MessageDigest.getInstance(algorithmID); 111 } else { 112 md = MessageDigest.getInstance(algorithmID,provider); 113 } 114 } catch (java.security.NoSuchAlgorithmException ex) { 115 Object[] exArgs = { algorithmID, 116 ex.getLocalizedMessage() }; 117 118 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 119 } catch (NoSuchProviderException ex) { 120 Object[] exArgs = { algorithmID, 121 ex.getLocalizedMessage() }; 122 123 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 124 } 125 instances.get().put(algorithmURI, md); 126 return md; 127 } 128 129 /** 130 * Returns the actual {@link java.security.MessageDigest} algorithm object 131 * 132 * @return the actual {@link java.security.MessageDigest} algorithm object 133 */ 134 public java.security.MessageDigest getAlgorithm() { 135 return this.algorithm; 136 } 137 138 /** 139 * Proxy method for {@link java.security.MessageDigest#isEqual} 140 * which is executed on the internal {@link java.security.MessageDigest} object. 141 * 142 * @param digesta 143 * @param digestb 144 * @return the result of the {@link java.security.MessageDigest#isEqual} method 145 */ 146 public static boolean isEqual(byte[] digesta, byte[] digestb) { 147 return java.security.MessageDigest.isEqual(digesta, digestb); 148 } 149 150 /** 151 * Proxy method for {@link java.security.MessageDigest#digest()} 152 * which is executed on the internal {@link java.security.MessageDigest} object. 153 * 154 * @return the result of the {@link java.security.MessageDigest#digest()} method 155 */ 156 public byte[] digest() { 157 return this.algorithm.digest(); 158 } 159 160 /** 161 * Proxy method for {@link java.security.MessageDigest#digest(byte[])} 162 * which is executed on the internal {@link java.security.MessageDigest} object. 163 * 164 * @param input 165 * @return the result of the {@link java.security.MessageDigest#digest(byte[])} method 166 */ 167 public byte[] digest(byte input[]) { 168 return this.algorithm.digest(input); 169 } 170 171 /** 172 * Proxy method for {@link java.security.MessageDigest#digest(byte[], int, int)} 173 * which is executed on the internal {@link java.security.MessageDigest} object. 174 * 175 * @param buf 176 * @param offset 177 * @param len 178 * @return the result of the {@link java.security.MessageDigest#digest(byte[], int, int)} method 179 * @throws java.security.DigestException 180 */ 181 public int digest(byte buf[], int offset, int len) 182 throws java.security.DigestException { 183 return this.algorithm.digest(buf, offset, len); 184 } 185 186 /** 187 * Proxy method for {@link java.security.MessageDigest#getAlgorithm} 188 * which is executed on the internal {@link java.security.MessageDigest} object. 189 * 190 * @return the result of the {@link java.security.MessageDigest#getAlgorithm} method 191 */ 192 public String getJCEAlgorithmString() { 193 return this.algorithm.getAlgorithm(); 194 } 195 196 /** 197 * Proxy method for {@link java.security.MessageDigest#getProvider} 198 * which is executed on the internal {@link java.security.MessageDigest} object. 199 * 200 * @return the result of the {@link java.security.MessageDigest#getProvider} method 201 */ 202 public java.security.Provider getJCEProvider() { 203 return this.algorithm.getProvider(); 204 } 205 206 /** 207 * Proxy method for {@link java.security.MessageDigest#getDigestLength} 208 * which is executed on the internal {@link java.security.MessageDigest} object. 209 * 210 * @return the result of the {@link java.security.MessageDigest#getDigestLength} method 211 */ 212 public int getDigestLength() { 213 return this.algorithm.getDigestLength(); 214 } 215 216 /** 217 * Proxy method for {@link java.security.MessageDigest#reset} 218 * which is executed on the internal {@link java.security.MessageDigest} object. 219 * 220 */ 221 public void reset() { 222 this.algorithm.reset(); 223 } 224 225 /** 226 * Proxy method for {@link java.security.MessageDigest#update(byte[])} 227 * which is executed on the internal {@link java.security.MessageDigest} object. 228 * 229 * @param input 230 */ 231 public void update(byte[] input) { 232 this.algorithm.update(input); 233 } 234 235 /** 236 * Proxy method for {@link java.security.MessageDigest#update(byte)} 237 * which is executed on the internal {@link java.security.MessageDigest} object. 238 * 239 * @param input 240 */ 241 public void update(byte input) { 242 this.algorithm.update(input); 243 } 244 245 /** 246 * Proxy method for {@link java.security.MessageDigest#update(byte[], int, int)} 247 * which is executed on the internal {@link java.security.MessageDigest} object. 248 * 249 * @param buf 250 * @param offset 251 * @param len 252 */ 253 public void update(byte buf[], int offset, int len) { 254 this.algorithm.update(buf, offset, len); 255 } 256 257 /** @inheritDoc */ 258 public String getBaseNamespace() { 259 return Constants.SignatureSpecNS; 260 } 261 262 /** @inheritDoc */ 263 public String getBaseLocalName() { 264 return Constants._TAG_DIGESTMETHOD; 265 } 266 } | 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; 24 25 import java.security.MessageDigest; 26 import java.security.NoSuchProviderException; 27 28 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; 29 import com.sun.org.apache.xml.internal.security.utils.Constants; 30 import com.sun.org.apache.xml.internal.security.utils.EncryptionConstants; 31 import org.w3c.dom.Document; 32 33 /** 34 * Digest Message wrapper & selector class. 35 * 36 * <pre> 37 * MessageDigestAlgorithm.getInstance() 38 * </pre> 39 */ 40 public class MessageDigestAlgorithm extends Algorithm { 41 42 /** Message Digest - NOT RECOMMENDED MD5*/ 43 public static final String ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5 = 44 Constants.MoreAlgorithmsSpecNS + "md5"; 45 /** Digest - Required SHA1*/ 46 public static final String ALGO_ID_DIGEST_SHA1 = Constants.SignatureSpecNS + "sha1"; 47 /** Message Digest - RECOMMENDED SHA256*/ 48 public static final String ALGO_ID_DIGEST_SHA256 = 49 EncryptionConstants.EncryptionSpecNS + "sha256"; 50 /** Message Digest - OPTIONAL SHA384*/ 51 public static final String ALGO_ID_DIGEST_SHA384 = 52 Constants.MoreAlgorithmsSpecNS + "sha384"; 53 /** Message Digest - OPTIONAL SHA512*/ 54 public static final String ALGO_ID_DIGEST_SHA512 = 55 EncryptionConstants.EncryptionSpecNS + "sha512"; 56 /** Message Digest - OPTIONAL RIPEMD-160*/ 57 public static final String ALGO_ID_DIGEST_RIPEMD160 = 58 EncryptionConstants.EncryptionSpecNS + "ripemd160"; 59 60 /** Field algorithm stores the actual {@link java.security.MessageDigest} */ 61 private final MessageDigest algorithm; 62 63 /** 64 * Constructor for the brave who pass their own message digest algorithms and the 65 * corresponding URI. 66 * @param doc 67 * @param algorithmURI 68 */ 69 private MessageDigestAlgorithm(Document doc, String algorithmURI) 70 throws XMLSignatureException { 71 super(doc, algorithmURI); 72 73 algorithm = getDigestInstance(algorithmURI); 74 } 75 76 /** 77 * Factory method for constructing a message digest algorithm by name. 78 * 79 * @param doc 80 * @param algorithmURI 81 * @return The MessageDigestAlgorithm element to attach in document and to digest 82 * @throws XMLSignatureException 83 */ 84 public static MessageDigestAlgorithm getInstance( 85 Document doc, String algorithmURI 86 ) throws XMLSignatureException { 87 return new MessageDigestAlgorithm(doc, algorithmURI); 88 } 89 90 private static MessageDigest getDigestInstance(String algorithmURI) throws XMLSignatureException { 91 String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI); 92 93 if (algorithmID == null) { 94 Object[] exArgs = { algorithmURI }; 95 throw new XMLSignatureException("algorithms.NoSuchMap", exArgs); 96 } 97 98 MessageDigest md; 99 String provider = JCEMapper.getProviderId(); 100 try { 101 if (provider == null) { 102 md = MessageDigest.getInstance(algorithmID); 103 } else { 104 md = MessageDigest.getInstance(algorithmID, provider); 105 } 106 } catch (java.security.NoSuchAlgorithmException ex) { 107 Object[] exArgs = { algorithmID, ex.getLocalizedMessage() }; 108 109 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 110 } catch (NoSuchProviderException ex) { 111 Object[] exArgs = { algorithmID, ex.getLocalizedMessage() }; 112 113 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 114 } 115 116 return md; 117 } 118 119 /** 120 * Returns the actual {@link java.security.MessageDigest} algorithm object 121 * 122 * @return the actual {@link java.security.MessageDigest} algorithm object 123 */ 124 public java.security.MessageDigest getAlgorithm() { 125 return algorithm; 126 } 127 128 /** 129 * Proxy method for {@link java.security.MessageDigest#isEqual} 130 * which is executed on the internal {@link java.security.MessageDigest} object. 131 * 132 * @param digesta 133 * @param digestb 134 * @return the result of the {@link java.security.MessageDigest#isEqual} method 135 */ 136 public static boolean isEqual(byte[] digesta, byte[] digestb) { 137 return java.security.MessageDigest.isEqual(digesta, digestb); 138 } 139 140 /** 141 * Proxy method for {@link java.security.MessageDigest#digest()} 142 * which is executed on the internal {@link java.security.MessageDigest} object. 143 * 144 * @return the result of the {@link java.security.MessageDigest#digest()} method 145 */ 146 public byte[] digest() { 147 return algorithm.digest(); 148 } 149 150 /** 151 * Proxy method for {@link java.security.MessageDigest#digest(byte[])} 152 * which is executed on the internal {@link java.security.MessageDigest} object. 153 * 154 * @param input 155 * @return the result of the {@link java.security.MessageDigest#digest(byte[])} method 156 */ 157 public byte[] digest(byte input[]) { 158 return algorithm.digest(input); 159 } 160 161 /** 162 * Proxy method for {@link java.security.MessageDigest#digest(byte[], int, int)} 163 * which is executed on the internal {@link java.security.MessageDigest} object. 164 * 165 * @param buf 166 * @param offset 167 * @param len 168 * @return the result of the {@link java.security.MessageDigest#digest(byte[], int, int)} method 169 * @throws java.security.DigestException 170 */ 171 public int digest(byte buf[], int offset, int len) throws java.security.DigestException { 172 return algorithm.digest(buf, offset, len); 173 } 174 175 /** 176 * Proxy method for {@link java.security.MessageDigest#getAlgorithm} 177 * which is executed on the internal {@link java.security.MessageDigest} object. 178 * 179 * @return the result of the {@link java.security.MessageDigest#getAlgorithm} method 180 */ 181 public String getJCEAlgorithmString() { 182 return algorithm.getAlgorithm(); 183 } 184 185 /** 186 * Proxy method for {@link java.security.MessageDigest#getProvider} 187 * which is executed on the internal {@link java.security.MessageDigest} object. 188 * 189 * @return the result of the {@link java.security.MessageDigest#getProvider} method 190 */ 191 public java.security.Provider getJCEProvider() { 192 return algorithm.getProvider(); 193 } 194 195 /** 196 * Proxy method for {@link java.security.MessageDigest#getDigestLength} 197 * which is executed on the internal {@link java.security.MessageDigest} object. 198 * 199 * @return the result of the {@link java.security.MessageDigest#getDigestLength} method 200 */ 201 public int getDigestLength() { 202 return algorithm.getDigestLength(); 203 } 204 205 /** 206 * Proxy method for {@link java.security.MessageDigest#reset} 207 * which is executed on the internal {@link java.security.MessageDigest} object. 208 * 209 */ 210 public void reset() { 211 algorithm.reset(); 212 } 213 214 /** 215 * Proxy method for {@link java.security.MessageDigest#update(byte[])} 216 * which is executed on the internal {@link java.security.MessageDigest} object. 217 * 218 * @param input 219 */ 220 public void update(byte[] input) { 221 algorithm.update(input); 222 } 223 224 /** 225 * Proxy method for {@link java.security.MessageDigest#update(byte)} 226 * which is executed on the internal {@link java.security.MessageDigest} object. 227 * 228 * @param input 229 */ 230 public void update(byte input) { 231 algorithm.update(input); 232 } 233 234 /** 235 * Proxy method for {@link java.security.MessageDigest#update(byte[], int, int)} 236 * which is executed on the internal {@link java.security.MessageDigest} object. 237 * 238 * @param buf 239 * @param offset 240 * @param len 241 */ 242 public void update(byte buf[], int offset, int len) { 243 algorithm.update(buf, offset, len); 244 } 245 246 /** @inheritDoc */ 247 public String getBaseNamespace() { 248 return Constants.SignatureSpecNS; 249 } 250 251 /** @inheritDoc */ 252 public String getBaseLocalName() { 253 return Constants._TAG_DIGESTMETHOD; 254 } 255 } |