J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 | |
| 24 | |
| 25 | import java.util.HashMap; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | |
| 29 | import com.sun.org.apache.xml.internal.security.Init; |
| 30 | import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
| 31 | import org.w3c.dom.Element; |
| 32 | |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * This class maps algorithm identifier URIs to JAVA JCE class names. |
| 37 | * |
| 38 | * @author $Author: raul $ |
| 39 | */ |
| 40 | public class JCEMapper { |
| 41 | |
| 42 | /** {@link java.util.logging} logging facility */ |
| 43 | static java.util.logging.Logger log = |
| 44 | java.util.logging.Logger.getLogger(JCEMapper.class.getName()); |
| 45 | |
| 46 | |
| 47 | |
| 48 | private static Map uriToJCEName = new HashMap(); |
| 49 | |
| 50 | private static Map algorithmsMap = new HashMap(); |
| 51 | |
| 52 | private static String providerName = null; |
| 53 | /** |
| 54 | * Method init |
| 55 | * |
| 56 | * @param mappingElement |
| 57 | * @throws Exception |
| 58 | */ |
| 59 | public static void init(Element mappingElement) throws Exception { |
| 60 | |
| 61 | loadAlgorithms((Element)mappingElement.getElementsByTagName("Algorithms").item(0)); |
| 62 | } |
| 63 | |
| 64 | static void loadAlgorithms( Element algorithmsEl) { |
| 65 | Element[] algorithms = XMLUtils.selectNodes(algorithmsEl.getFirstChild(),Init.CONF_NS,"Algorithm"); |
| 66 | for (int i = 0 ;i < algorithms.length ;i ++) { |
| 67 | Element el = algorithms[i]; |
| 68 | String id = el.getAttribute("URI"); |
| 69 | String jceName = el.getAttribute("JCEName"); |
| 70 | uriToJCEName.put(id, jceName); |
| 71 | algorithmsMap.put(id, new Algorithm(el)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static Algorithm getAlgorithmMapping(String algoURI) { |
| 76 | return ((Algorithm)algorithmsMap.get(algoURI)); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Method translateURItoJCEID |
| 81 | * |
| 82 | * @param AlgorithmURI |
| 83 | * @return the JCE standard name corresponding to the given URI |
| 84 | * |
| 85 | */ |
| 86 | public static String translateURItoJCEID(String AlgorithmURI) { |
| 87 | if (true) |
| 88 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Request for URI " + AlgorithmURI); |
| 89 | |
| 90 | String jceName = (String) uriToJCEName.get(AlgorithmURI); |
| 91 | return jceName; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Method getAlgorithmClassFromURI |
| 96 | * NOTE(Raul Benito) It seems a buggy function the loop doesn't do |
| 97 | * anything?? |
| 98 | * @param AlgorithmURI |
| 99 | * @return the class name that implements this algorithm |
| 100 | * |
| 101 | */ |
| 102 | public static String getAlgorithmClassFromURI(String AlgorithmURI) { |
| 103 | if (true) |
| 104 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Request for URI " + AlgorithmURI); |
| 105 | |
| 106 | return ((Algorithm) algorithmsMap.get(AlgorithmURI)).algorithmClass; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the keylength in bit for a particular algorithm. |
| 111 | * |
| 112 | * @param AlgorithmURI |
| 113 | * @return The length of the key used in the alogrithm |
| 114 | */ |
| 115 | public static int getKeyLengthFromURI(String AlgorithmURI) { |
| 116 | return Integer.parseInt(((Algorithm) algorithmsMap.get(AlgorithmURI)).keyLength); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Method getJCEKeyAlgorithmFromURI |
| 121 | * |
| 122 | * @param AlgorithmURI |
| 123 | * @return The KeyAlgorithm for the given URI. |
| 124 | * |
| 125 | */ |
| 126 | public static String getJCEKeyAlgorithmFromURI(String AlgorithmURI) { |
| 127 | |
| 128 | return ((Algorithm) algorithmsMap.get(AlgorithmURI)).requiredKey; |
| 129 | |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Gets the default Provider for obtaining the security algorithms |
| 134 | * @return the default providerId. |
| 135 | */ |
| 136 | public static String getProviderId() { |
| 137 | return providerName; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Sets the default Provider for obtaining the security algorithms |
| 142 | * @param provider the default providerId. |
| 143 | */ |
| 144 | public static void setProviderId(String provider) { |
| 145 | providerName=provider; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Represents the Algorithm xml element |
| 150 | */ |
| 151 | public static class Algorithm { |
| 152 | String algorithmClass; |
| 153 | String keyLength; |
| 154 | String requiredKey; |
| 155 | /** |
| 156 | * Gets data from element |
| 157 | * @param el |
| 158 | */ |
| 159 | public Algorithm(Element el) { |
| 160 | algorithmClass=el.getAttribute("AlgorithmClass"); |
| 161 | keyLength=el.getAttribute("KeyLength"); |
| 162 | requiredKey=el.getAttribute("RequiredKey"); |
| 163 | } |
| 164 | } |
| 165 | } |