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.util.Map; 26 import java.util.concurrent.ConcurrentHashMap; 27 28 import com.sun.org.apache.xml.internal.security.signature.XMLSignature; 29 import com.sun.org.apache.xml.internal.security.utils.JavaUtils; 30 import org.w3c.dom.Element; 31 32 33 /** 34 * This class maps algorithm identifier URIs to JAVA JCE class names. 35 */ 36 public class JCEMapper { 37 38 private static final com.sun.org.slf4j.internal.Logger LOG = 39 com.sun.org.slf4j.internal.LoggerFactory.getLogger(JCEMapper.class); 40 41 private static Map<String, Algorithm> algorithmsMap = 42 new ConcurrentHashMap<String, Algorithm>(); 43 44 private static String providerName; 45 46 /** 47 * Method register 48 * 49 * @param id 50 * @param algorithm 51 * @throws SecurityException if a security manager is installed and the 52 * caller does not have permission to register the JCE algorithm 53 */ register(String id, Algorithm algorithm)54 public static void register(String id, Algorithm algorithm) { 55 JavaUtils.checkRegisterPermission(); 56 algorithmsMap.put(id, algorithm); 57 } 58 59 /** 60 * This method registers the default algorithms. 61 */ registerDefaultAlgorithms()62 public static void registerDefaultAlgorithms() { 63 // Digest algorithms 64 algorithmsMap.put( 65 MessageDigestAlgorithm.ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5, 66 new Algorithm("", "MD5", "MessageDigest") 67 ); 68 algorithmsMap.put( 69 MessageDigestAlgorithm.ALGO_ID_DIGEST_RIPEMD160, 70 new Algorithm("", "RIPEMD160", "MessageDigest") 71 ); 72 algorithmsMap.put( 73 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1, 74 new Algorithm("", "SHA-1", "MessageDigest") 75 ); 76 algorithmsMap.put( 77 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA224, 78 new Algorithm("", "SHA-224", "MessageDigest") 79 ); 80 algorithmsMap.put( 81 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256, 82 new Algorithm("", "SHA-256", "MessageDigest") 83 ); 84 algorithmsMap.put( 85 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA384, 86 new Algorithm("", "SHA-384", "MessageDigest") 87 ); 88 algorithmsMap.put( 89 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA512, 90 new Algorithm("", "SHA-512", "MessageDigest") 91 ); 92 algorithmsMap.put( 93 MessageDigestAlgorithm.ALGO_ID_DIGEST_WHIRLPOOL, 94 new Algorithm("", "WHIRLPOOL", "MessageDigest") 95 ); 96 algorithmsMap.put( 97 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA3_224, 98 new Algorithm("", "SHA3-224", "MessageDigest") 99 ); 100 algorithmsMap.put( 101 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA3_256, 102 new Algorithm("", "SHA3-256", "MessageDigest") 103 ); 104 algorithmsMap.put( 105 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA3_384, 106 new Algorithm("", "SHA3-384", "MessageDigest") 107 ); 108 algorithmsMap.put( 109 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA3_512, 110 new Algorithm("", "SHA3-512", "MessageDigest") 111 ); 112 // Signature algorithms 113 algorithmsMap.put( 114 XMLSignature.ALGO_ID_SIGNATURE_DSA, 115 new Algorithm("DSA", "SHA1withDSA", "Signature") 116 ); 117 algorithmsMap.put( 118 XMLSignature.ALGO_ID_SIGNATURE_DSA_SHA256, 119 new Algorithm("DSA", "SHA256withDSA", "Signature") 120 ); 121 algorithmsMap.put( 122 XMLSignature.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5, 123 new Algorithm("RSA", "MD5withRSA", "Signature") 124 ); 125 algorithmsMap.put( 126 XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160, 127 new Algorithm("RSA", "RIPEMD160withRSA", "Signature") 128 ); 129 algorithmsMap.put( 130 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1, 131 new Algorithm("RSA", "SHA1withRSA", "Signature") 132 ); 133 algorithmsMap.put( 134 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA224, 135 new Algorithm("RSA", "SHA224withRSA", "Signature") 136 ); 137 algorithmsMap.put( 138 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256, 139 new Algorithm("RSA", "SHA256withRSA", "Signature") 140 ); 141 algorithmsMap.put( 142 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384, 143 new Algorithm("RSA", "SHA384withRSA", "Signature") 144 ); 145 algorithmsMap.put( 146 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512, 147 new Algorithm("RSA", "SHA512withRSA", "Signature") 148 ); 149 algorithmsMap.put( 150 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1_MGF1, 151 new Algorithm("RSA", "SHA1withRSAandMGF1", "Signature") 152 ); 153 algorithmsMap.put( 154 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA224_MGF1, 155 new Algorithm("RSA", "SHA224withRSAandMGF1", "Signature") 156 ); 157 algorithmsMap.put( 158 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256_MGF1, 159 new Algorithm("RSA", "SHA256withRSAandMGF1", "Signature") 160 ); 161 algorithmsMap.put( 162 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384_MGF1, 163 new Algorithm("RSA", "SHA384withRSAandMGF1", "Signature") 164 ); 165 algorithmsMap.put( 166 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512_MGF1, 167 new Algorithm("RSA", "SHA512withRSAandMGF1", "Signature") 168 ); 169 algorithmsMap.put( 170 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA3_224_MGF1, 171 new Algorithm("RSA", "SHA3-224withRSAandMGF1", "Signature") 172 ); 173 algorithmsMap.put( 174 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA3_256_MGF1, 175 new Algorithm("RSA", "SHA3-256withRSAandMGF1", "Signature") 176 ); 177 algorithmsMap.put( 178 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA3_384_MGF1, 179 new Algorithm("RSA", "SHA3-384withRSAandMGF1", "Signature") 180 ); 181 algorithmsMap.put( 182 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA3_512_MGF1, 183 new Algorithm("RSA", "SHA3-512withRSAandMGF1", "Signature") 184 ); 185 algorithmsMap.put( 186 XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA1, 187 new Algorithm("EC", "SHA1withECDSA", "Signature") 188 ); 189 algorithmsMap.put( 190 XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA224, 191 new Algorithm("EC", "SHA224withECDSA", "Signature") 192 ); 193 algorithmsMap.put( 194 XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA256, 195 new Algorithm("EC", "SHA256withECDSA", "Signature") 196 ); 197 algorithmsMap.put( 198 XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA384, 199 new Algorithm("EC", "SHA384withECDSA", "Signature") 200 ); 201 algorithmsMap.put( 202 XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA512, 203 new Algorithm("EC", "SHA512withECDSA", "Signature") 204 ); 205 algorithmsMap.put( 206 XMLSignature.ALGO_ID_SIGNATURE_ECDSA_RIPEMD160, 207 new Algorithm("EC", "RIPEMD160withECDSA", "Signature") 208 ); 209 algorithmsMap.put( 210 XMLSignature.ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5, 211 new Algorithm("", "HmacMD5", "Mac", 0, 0) 212 ); 213 algorithmsMap.put( 214 XMLSignature.ALGO_ID_MAC_HMAC_RIPEMD160, 215 new Algorithm("", "HMACRIPEMD160", "Mac", 0, 0) 216 ); 217 algorithmsMap.put( 218 XMLSignature.ALGO_ID_MAC_HMAC_SHA1, 219 new Algorithm("", "HmacSHA1", "Mac", 0, 0) 220 ); 221 algorithmsMap.put( 222 XMLSignature.ALGO_ID_MAC_HMAC_SHA224, 223 new Algorithm("", "HmacSHA224", "Mac", 0, 0) 224 ); 225 algorithmsMap.put( 226 XMLSignature.ALGO_ID_MAC_HMAC_SHA256, 227 new Algorithm("", "HmacSHA256", "Mac", 0, 0) 228 ); 229 algorithmsMap.put( 230 XMLSignature.ALGO_ID_MAC_HMAC_SHA384, 231 new Algorithm("", "HmacSHA384", "Mac", 0, 0) 232 ); 233 algorithmsMap.put( 234 XMLSignature.ALGO_ID_MAC_HMAC_SHA512, 235 new Algorithm("", "HmacSHA512", "Mac", 0, 0) 236 ); 237 } 238 239 /** 240 * Method translateURItoJCEID 241 * 242 * @param algorithmURI 243 * @return the JCE standard name corresponding to the given URI 244 */ translateURItoJCEID(String algorithmURI)245 public static String translateURItoJCEID(String algorithmURI) { 246 Algorithm algorithm = getAlgorithm(algorithmURI); 247 if (algorithm != null) { 248 return algorithm.jceName; 249 } 250 return null; 251 } 252 253 /** 254 * Method getAlgorithmClassFromURI 255 * @param algorithmURI 256 * @return the class name that implements this algorithm 257 */ getAlgorithmClassFromURI(String algorithmURI)258 public static String getAlgorithmClassFromURI(String algorithmURI) { 259 Algorithm algorithm = getAlgorithm(algorithmURI); 260 if (algorithm != null) { 261 return algorithm.algorithmClass; 262 } 263 return null; 264 } 265 266 /** 267 * Returns the keylength in bits for a particular algorithm. 268 * 269 * @param algorithmURI 270 * @return The length of the key used in the algorithm 271 */ getKeyLengthFromURI(String algorithmURI)272 public static int getKeyLengthFromURI(String algorithmURI) { 273 Algorithm algorithm = getAlgorithm(algorithmURI); 274 if (algorithm != null) { 275 return algorithm.keyLength; 276 } 277 return 0; 278 } 279 getIVLengthFromURI(String algorithmURI)280 public static int getIVLengthFromURI(String algorithmURI) { 281 Algorithm algorithm = getAlgorithm(algorithmURI); 282 if (algorithm != null) { 283 return algorithm.ivLength; 284 } 285 return 0; 286 } 287 288 /** 289 * Method getJCEKeyAlgorithmFromURI 290 * 291 * @param algorithmURI 292 * @return The KeyAlgorithm for the given URI. 293 */ getJCEKeyAlgorithmFromURI(String algorithmURI)294 public static String getJCEKeyAlgorithmFromURI(String algorithmURI) { 295 Algorithm algorithm = getAlgorithm(algorithmURI); 296 if (algorithm != null) { 297 return algorithm.requiredKey; 298 } 299 return null; 300 } 301 302 /** 303 * Method getJCEProviderFromURI 304 * 305 * @param algorithmURI 306 * @return The JCEProvider for the given URI. 307 */ getJCEProviderFromURI(String algorithmURI)308 public static String getJCEProviderFromURI(String algorithmURI) { 309 Algorithm algorithm = getAlgorithm(algorithmURI); 310 if (algorithm != null) { 311 return algorithm.jceProvider; 312 } 313 return null; 314 } 315 316 /** 317 * Method getAlgorithm 318 * 319 * @param algorithmURI 320 * @return The Algorithm object for the given URI. 321 */ getAlgorithm(String algorithmURI)322 private static Algorithm getAlgorithm(String algorithmURI) { 323 LOG.debug("Request for URI {}", algorithmURI); 324 325 if (algorithmURI != null) { 326 return algorithmsMap.get(algorithmURI); 327 } 328 return null; 329 } 330 331 /** 332 * Gets the default Provider for obtaining the security algorithms 333 * @return the default providerId. 334 */ getProviderId()335 public static String getProviderId() { 336 return providerName; 337 } 338 339 /** 340 * Sets the default Provider for obtaining the security algorithms 341 * @param provider the default providerId. 342 * @throws SecurityException if a security manager is installed and the 343 * caller does not have permission to register the JCE algorithm 344 */ setProviderId(String provider)345 public static void setProviderId(String provider) { 346 JavaUtils.checkRegisterPermission(); 347 providerName = provider; 348 } 349 350 /** 351 * Represents the Algorithm xml element 352 */ 353 public static class Algorithm { 354 355 final String requiredKey; 356 final String jceName; 357 final String algorithmClass; 358 final int keyLength; 359 final int ivLength; 360 final String jceProvider; 361 362 /** 363 * Gets data from element 364 * @param el 365 */ Algorithm(Element el)366 public Algorithm(Element el) { 367 requiredKey = el.getAttributeNS(null, "RequiredKey"); 368 jceName = el.getAttributeNS(null, "JCEName"); 369 algorithmClass = el.getAttributeNS(null, "AlgorithmClass"); 370 jceProvider = el.getAttributeNS(null, "JCEProvider"); 371 if (el.hasAttribute("KeyLength")) { 372 keyLength = Integer.parseInt(el.getAttributeNS(null, "KeyLength")); 373 } else { 374 keyLength = 0; 375 } 376 if (el.hasAttribute("IVLength")) { 377 ivLength = Integer.parseInt(el.getAttributeNS(null, "IVLength")); 378 } else { 379 ivLength = 0; 380 } 381 } 382 Algorithm(String requiredKey, String jceName)383 public Algorithm(String requiredKey, String jceName) { 384 this(requiredKey, jceName, null, 0, 0); 385 } 386 Algorithm(String requiredKey, String jceName, String algorithmClass)387 public Algorithm(String requiredKey, String jceName, String algorithmClass) { 388 this(requiredKey, jceName, algorithmClass, 0, 0); 389 } 390 Algorithm(String requiredKey, String jceName, int keyLength)391 public Algorithm(String requiredKey, String jceName, int keyLength) { 392 this(requiredKey, jceName, null, keyLength, 0); 393 } 394 Algorithm(String requiredKey, String jceName, String algorithmClass, int keyLength, int ivLength)395 public Algorithm(String requiredKey, String jceName, String algorithmClass, int keyLength, int ivLength) { 396 this(requiredKey, jceName, algorithmClass, keyLength, ivLength, null); 397 } 398 Algorithm(String requiredKey, String jceName, String algorithmClass, int keyLength, int ivLength, String jceProvider)399 public Algorithm(String requiredKey, String jceName, 400 String algorithmClass, int keyLength, int ivLength, String jceProvider) { 401 this.requiredKey = requiredKey; 402 this.jceName = jceName; 403 this.algorithmClass = algorithmClass; 404 this.keyLength = keyLength; 405 this.ivLength = ivLength; 406 this.jceProvider = jceProvider; 407 } 408 } 409 } 410