1 /* 2 * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.] 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.opensaml.xml.security; 18 19 import java.security.KeyException; 20 import java.security.KeyPair; 21 import java.security.NoSuchAlgorithmException; 22 import java.security.NoSuchProviderException; 23 import java.security.PrivateKey; 24 import java.security.PublicKey; 25 import java.security.cert.CRLException; 26 import java.security.cert.CertificateException; 27 import java.security.interfaces.DSAPrivateKey; 28 import java.security.interfaces.DSAPublicKey; 29 import java.security.interfaces.RSAPrivateKey; 30 import java.security.interfaces.RSAPublicKey; 31 import java.security.spec.KeySpec; 32 33 import javax.crypto.SecretKey; 34 35 import org.opensaml.xml.security.credential.Credential; 36 import org.opensaml.xml.security.keyinfo.KeyInfoCredentialResolver; 37 38 /** 39 * @deprecated 40 * Some utility methods for doing security, credential, key and crypto-related tests. 41 */ 42 public final class SecurityTestHelper { 43 44 /** Constructor. */ 45 private SecurityTestHelper() { } 46 47 /** 48 * @deprecated 49 * Build Java certificate from base64 encoding. 50 * 51 * @param base64Cert base64-encoded certificate 52 * @return a native Java X509 certificate 53 * @throws CertificateException thrown if there is an error constructing certificate 54 */ 55 public static java.security.cert.X509Certificate buildJavaX509Cert(String base64Cert) throws CertificateException { 56 return SecurityHelper.buildJavaX509Cert(base64Cert); 57 } 58 59 /** 60 * @deprecated 61 * Build Java CRL from base64 encoding. 62 * 63 * @param base64CRL base64-encoded CRL 64 * @return a native Java X509 CRL 65 * @throws CertificateException thrown if there is an error constructing certificate 66 * @throws CRLException thrown if there is an error constructing CRL 67 */ 68 public static java.security.cert.X509CRL buildJavaX509CRL(String base64CRL) 69 throws CertificateException, CRLException { 70 return SecurityHelper.buildJavaX509CRL(base64CRL); 71 } 72 73 /** 74 * @deprecated 75 * Build Java DSA public key from base64 encoding. 76 * 77 * @param base64EncodedKey base64-encoded DSA public key 78 * @return a native Java DSAPublicKey 79 * @throws KeyException thrown if there is an error constructing key 80 */ 81 public static DSAPublicKey buildJavaDSAPublicKey(String base64EncodedKey) throws KeyException { 82 return SecurityHelper.buildJavaDSAPublicKey(base64EncodedKey); 83 } 84 85 /** 86 * @deprecated 87 * Build Java RSA public key from base64 encoding. 88 * 89 * @param base64EncodedKey base64-encoded RSA public key 90 * @return a native Java RSAPublicKey 91 * @throws KeyException thrown if there is an error constructing key 92 */ 93 public static RSAPublicKey buildJavaRSAPublicKey(String base64EncodedKey) throws KeyException { 94 return SecurityHelper.buildJavaRSAPublicKey(base64EncodedKey); 95 } 96 97 /** 98 * @deprecated 99 * Build Java RSA private key from base64 encoding. 100 * 101 * @param base64EncodedKey base64-encoded RSA private key 102 * @return a native Java RSAPrivateKey 103 * @throws KeyException thrown if there is an error constructing key 104 */ 105 public static RSAPrivateKey buildJavaRSAPrivateKey(String base64EncodedKey) throws KeyException { 106 return SecurityHelper.buildJavaRSAPrivateKey(base64EncodedKey); 107 } 108 109 /** 110 * @deprecated 111 * Build Java DSA private key from base64 encoding. 112 * 113 * @param base64EncodedKey base64-encoded DSA private key 114 * @return a native Java DSAPrivateKey 115 * @throws KeyException thrown if there is an error constructing key 116 */ 117 public static DSAPrivateKey buildJavaDSAPrivateKey(String base64EncodedKey) throws KeyException { 118 return SecurityHelper.buildJavaDSAPrivateKey(base64EncodedKey); 119 } 120 121 /** 122 * @deprecated 123 * Build Java private key from base64 encoding. The key should have no password. 124 * 125 * @param base64EncodedKey base64-encoded private key 126 * @return a native Java PrivateKey 127 * @throws KeyException thrown if there is an error constructing key 128 */ 129 public static PrivateKey buildJavaPrivateKey(String base64EncodedKey) throws KeyException { 130 return SecurityHelper.buildJavaPrivateKey(base64EncodedKey); 131 } 132 133 /** 134 * @deprecated 135 * Generates a public key from the given key spec. 136 * 137 * @param keySpec {@link KeySpec} specification for the key 138 * @param keyAlgorithm key generation algorithm, only DSA and RSA supported 139 * 140 * @return the generated {@link PublicKey} 141 * 142 * @throws KeyException thrown if the key algorithm is not supported by the JCE or the key spec does not 143 * contain valid information 144 */ 145 public static PublicKey buildKey(KeySpec keySpec, String keyAlgorithm) throws KeyException { 146 return SecurityHelper.buildKey(keySpec, keyAlgorithm); 147 } 148 149 /** 150 * @deprecated 151 * Randomly generates a Java JCE symmetric Key object from the specified XML Encryption algorithm URI. 152 * 153 * @param algoURI The XML Encryption algorithm URI 154 * @return a randomly-generated symmteric key 155 * @throws NoSuchProviderException provider not found 156 * @throws NoSuchAlgorithmException algorithm not found 157 */ 158 public static SecretKey generateKeyFromURI(String algoURI) 159 throws NoSuchAlgorithmException, NoSuchProviderException { 160 return SecurityHelper.generateKeyFromURI(algoURI); 161 } 162 163 /** 164 * @deprecated 165 * Randomly generates a Java JCE KeyPair object from the specified XML Encryption algorithm URI. 166 * 167 * @param algoURI The XML Encryption algorithm URI 168 * @param keyLength the length of key to generate 169 * @return a randomly-generated KeyPair 170 * @throws NoSuchProviderException provider not found 171 * @throws NoSuchAlgorithmException algorithm not found 172 */ 173 public static KeyPair generateKeyPairFromURI(String algoURI, int keyLength) 174 throws NoSuchAlgorithmException, NoSuchProviderException { 175 return SecurityHelper.generateKeyPairFromURI(algoURI, keyLength); 176 } 177 178 /** 179 * @deprecated 180 * Generate a random symmetric key. 181 * 182 * @param algo key algorithm 183 * @param keyLength key length 184 * @param provider JCA provider 185 * @return randomly generated symmetric key 186 * @throws NoSuchAlgorithmException algorithm not found 187 * @throws NoSuchProviderException provider not found 188 */ 189 public static SecretKey generateKey(String algo, int keyLength, String provider) 190 throws NoSuchAlgorithmException, NoSuchProviderException { 191 return SecurityHelper.generateKey(algo, keyLength, provider); 192 } 193 194 /** 195 * @deprecated 196 * Generate a random asymmetric key pair. 197 * 198 * @param algo key algorithm 199 * @param keyLength key length 200 * @param provider JCA provider 201 * @return randomly generated key 202 * @throws NoSuchAlgorithmException algorithm not found 203 * @throws NoSuchProviderException provider not found 204 */ 205 public static KeyPair generateKeyPair(String algo, int keyLength, String provider) 206 throws NoSuchAlgorithmException, NoSuchProviderException { 207 return SecurityHelper.generateKeyPair(algo, keyLength, provider); 208 } 209 210 /** 211 * @deprecated 212 * Generate a random symmetric key and return in a BasicCredential. 213 * 214 * @param algorithmURI The XML Encryption algorithm URI 215 * @return a basic credential containing a randomly generated symmetric key 216 * @throws NoSuchAlgorithmException algorithm not found 217 * @throws NoSuchProviderException provider not found 218 */ 219 public static Credential generateKeyAndCredential(String algorithmURI) 220 throws NoSuchAlgorithmException, NoSuchProviderException { 221 return SecurityHelper.generateKeyAndCredential(algorithmURI); 222 } 223 224 /** 225 * @deprecated 226 * Generate a random asymmetric key pair and return in a BasicCredential. 227 * 228 * @param algorithmURI The XML Encryption algorithm URI 229 * @param keyLength key length 230 * @param includePrivate if true, the private key will be included as well 231 * @return a basic credential containing a randomly generated asymmetric key pair 232 * @throws NoSuchAlgorithmException algorithm not found 233 * @throws NoSuchProviderException provider not found 234 */ 235 public static Credential generateKeyPairAndCredential(String algorithmURI, int keyLength, boolean includePrivate) 236 throws NoSuchAlgorithmException, NoSuchProviderException { 237 return SecurityHelper.generateKeyPairAndCredential(algorithmURI, keyLength, includePrivate); 238 } 239 240 /** 241 * @deprecated 242 * Get a basic KeyInfo credential resolver which can process standard inline 243 * data - RSAKeyValue, DSAKeyValue, X509Data. 244 * 245 * @return a new KeyInfoCredentialResolver instance 246 */ 247 public static KeyInfoCredentialResolver buildBasicInlineKeyInfoResolver() { 248 return SecurityHelper.buildBasicInlineKeyInfoResolver(); 249 } 250 }