|
72 | 72 | import java.math.BigInteger; |
73 | 73 | import java.net.URLEncoder; |
74 | 74 | import java.security.InvalidKeyException; |
| 75 | +import java.security.KeyFactory; |
75 | 76 | import java.security.KeyPair; |
76 | 77 | import java.security.KeyPairGenerator; |
77 | 78 | import java.security.NoSuchAlgorithmException; |
78 | 79 | import java.security.NoSuchProviderException; |
79 | 80 | import java.security.PrivateKey; |
| 81 | +import java.security.PublicKey; |
80 | 82 | import java.security.SecureRandom; |
81 | 83 | import java.security.Security; |
82 | 84 | import java.security.Signature; |
83 | 85 | import java.security.SignatureException; |
84 | 86 | import java.security.cert.CertificateEncodingException; |
85 | 87 | import java.security.cert.X509Certificate; |
| 88 | +import java.security.spec.InvalidKeySpecException; |
| 89 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 90 | +import java.security.spec.X509EncodedKeySpec; |
86 | 91 | import java.util.Date; |
87 | 92 | import java.util.zip.Deflater; |
88 | 93 | import java.util.zip.DeflaterOutputStream; |
@@ -220,6 +225,70 @@ public static String generateSAMLRequestSignature(String urlEncodedString, Priva |
220 | 225 | return URLEncoder.encode(Base64.encodeBytes(signature.sign(), Base64.DONT_BREAK_LINES), HttpUtils.UTF_8); |
221 | 226 | } |
222 | 227 |
|
| 228 | + public static KeyFactory getKeyFactory() { |
| 229 | + KeyFactory keyFactory = null; |
| 230 | + try { |
| 231 | + Security.addProvider(new BouncyCastleProvider()); |
| 232 | + keyFactory = KeyFactory.getInstance("RSA", "BC"); |
| 233 | + } catch (NoSuchAlgorithmException | NoSuchProviderException e) { |
| 234 | + s_logger.error("Unable to create KeyFactory:" + e.getMessage()); |
| 235 | + } |
| 236 | + return keyFactory; |
| 237 | + } |
| 238 | + |
| 239 | + public static String savePublicKey(PublicKey key) { |
| 240 | + try { |
| 241 | + KeyFactory keyFactory = SAMLUtils.getKeyFactory(); |
| 242 | + if (keyFactory == null) return null; |
| 243 | + X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class); |
| 244 | + return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded())); |
| 245 | + } catch (InvalidKeySpecException e) { |
| 246 | + s_logger.error("Unable to create KeyFactory:" + e.getMessage()); |
| 247 | + } |
| 248 | + return null; |
| 249 | + } |
| 250 | + |
| 251 | + public static String savePrivateKey(PrivateKey key) { |
| 252 | + try { |
| 253 | + KeyFactory keyFactory = SAMLUtils.getKeyFactory(); |
| 254 | + if (keyFactory == null) return null; |
| 255 | + PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key, |
| 256 | + PKCS8EncodedKeySpec.class); |
| 257 | + return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded())); |
| 258 | + } catch (InvalidKeySpecException e) { |
| 259 | + s_logger.error("Unable to create KeyFactory:" + e.getMessage()); |
| 260 | + } |
| 261 | + return null; |
| 262 | + } |
| 263 | + |
| 264 | + public static PublicKey loadPublicKey(String publicKey) { |
| 265 | + byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(publicKey); |
| 266 | + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes); |
| 267 | + KeyFactory keyFact = SAMLUtils.getKeyFactory(); |
| 268 | + if (keyFact == null) |
| 269 | + return null; |
| 270 | + try { |
| 271 | + return keyFact.generatePublic(x509KeySpec); |
| 272 | + } catch (InvalidKeySpecException e) { |
| 273 | + s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage()); |
| 274 | + } |
| 275 | + return null; |
| 276 | + } |
| 277 | + |
| 278 | + public static PrivateKey loadPrivateKey(String privateKey) { |
| 279 | + byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(privateKey); |
| 280 | + PKCS8EncodedKeySpec pkscs8KeySpec = new PKCS8EncodedKeySpec(sigBytes); |
| 281 | + KeyFactory keyFact = SAMLUtils.getKeyFactory(); |
| 282 | + if (keyFact == null) |
| 283 | + return null; |
| 284 | + try { |
| 285 | + return keyFact.generatePrivate(pkscs8KeySpec); |
| 286 | + } catch (InvalidKeySpecException e) { |
| 287 | + s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage()); |
| 288 | + } |
| 289 | + return null; |
| 290 | + } |
| 291 | + |
223 | 292 | public static KeyPair generateRandomKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException { |
224 | 293 | Security.addProvider(new BouncyCastleProvider()); |
225 | 294 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); |
|
0 commit comments