| 58 | | java.security.spec.InvalidKeySpecException, java.io.FileNotFoundException, java.io.IOException, org.opensaml.xml.io.MarshallingException { |
|---|
| 59 | | |
|---|
| 60 | | Security.addProvider(new BouncyCastleProvider()); |
|---|
| | 61 | java.security.spec.InvalidKeySpecException, java.io.FileNotFoundException, java.io.IOException, org.opensaml.xml.io.MarshallingException, |
|---|
| | 62 | java.security.NoSuchProviderException, java.security.cert.CertificateException |
|---|
| | 63 | { |
|---|
| | 64 | |
|---|
| | 65 | //Security.addProvider(new BouncyCastleProvider()); |
|---|
| | 66 | Security.insertProviderAt(new BouncyCastleProvider(), 2); |
|---|
| 199 | | byte[] x509KeyBytes = Base64.decode(encodedPublicKey); |
|---|
| | 208 | encodedPublicKey = encodedPublicKey.replaceAll("\n",""); |
|---|
| | 209 | int end = 64; |
|---|
| | 210 | int begin = 0; |
|---|
| | 211 | String epk = ""; |
|---|
| | 212 | do { |
|---|
| | 213 | System.out.println(encodedPublicKey.substring(begin, end)); |
|---|
| | 214 | |
|---|
| | 215 | epk += encodedPublicKey.substring(begin, end) + "\r\n"; |
|---|
| | 216 | end += 64; |
|---|
| | 217 | begin += 64; |
|---|
| | 218 | } while (end < encodedPublicKey.length()); |
|---|
| | 219 | epk += encodedPublicKey.substring(begin) + "\r\n"; |
|---|
| | 220 | System.out.println(encodedPublicKey.substring(begin)); |
|---|
| | 221 | |
|---|
| | 222 | // System.out.println("----- begin -----"); |
|---|
| | 223 | // System.out.print(epk); |
|---|
| | 224 | System.out.println("----- end -----"); |
|---|
| | 225 | |
|---|
| | 226 | byte[] x509KeyBytes = Base64.decode(epk); |
|---|
| | 228 | */ |
|---|
| | 229 | /* |
|---|
| | 230 | java.security.cert.X509Certificate jX509Cert; |
|---|
| | 231 | jX509Cert = KeyInfoHelper.getCertificate(x509Cert); |
|---|
| | 232 | X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec( jX509Cert.getEncoded() ); |
|---|
| | 233 | */ |
|---|
| | 234 | |
|---|
| | 235 | // ----- google code sample ----- |
|---|
| | 236 | /* |
|---|
| | 237 | String certFilename = "C:\\Business\\Clareity\\Dev\\SAML\\MetaTest\\src\\devcert.pem"; |
|---|
| | 238 | String pubKeyFilename = "C:\\Business\\Clareity\\Dev\\SAML\\MetaTest\\src\\devpubkey-google.pem"; |
|---|
| | 239 | try { |
|---|
| | 240 | |
|---|
| | 241 | InputStream certFileIs = new FileInputStream(certFilename); |
|---|
| | 242 | CertificateFactory cf = CertificateFactory.getInstance("X509"); |
|---|
| | 243 | java.security.cert.X509Certificate cert = |
|---|
| | 244 | (java.security.cert.X509Certificate) cf.generateCertificate(certFileIs); |
|---|
| | 245 | OutputStream publicKyFileOs = new FileOutputStream(pubKeyFilename); |
|---|
| | 246 | publicKyFileOs.write(cert.getPublicKey().getEncoded()); |
|---|
| | 247 | publicKyFileOs.close(); |
|---|
| | 248 | |
|---|
| | 249 | } catch (IOException e) { |
|---|
| | 250 | e.printStackTrace(); |
|---|
| | 251 | } catch (java.security.cert.CertificateException e) { |
|---|
| | 252 | e.printStackTrace(); |
|---|
| | 253 | } |
|---|
| | 254 | */ |
|---|
| | 255 | // ----- end google code sample ----- |
|---|
| | 256 | |
|---|
| | 257 | /* |
|---|
| | 258 | // This section reads the certficate file directly |
|---|
| | 259 | FileInputStream fis = new FileInputStream("C:\\Business\\Clareity\\Dev\\SAML\\MetaTest\\src\\devcert.pem"); |
|---|
| | 260 | BufferedInputStream bis = new BufferedInputStream(fis); |
|---|
| | 261 | |
|---|
| | 262 | CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
|---|
| | 263 | Certificate cert = null; |
|---|
| | 264 | while (bis.available() > 0) { |
|---|
| | 265 | cert = cf.generateCertificate(bis); |
|---|
| | 266 | System.out.println(cert.toString()); |
|---|
| | 267 | } |
|---|
| | 268 | // now that we havea cert object, pull out the public key to make a keyspec |
|---|
| | 269 | if (cert != null) pubKeySpec = new X509EncodedKeySpec(cert.getPublicKey().getEncoded()); |
|---|
| | 270 | if (cert == null) { |
|---|
| | 271 | System.out.println("cert is null"); |
|---|
| | 272 | return; |
|---|
| | 273 | } |
|---|
| | 274 | */ |
|---|
| | 275 | // We need a Java X509Certificate object first |
|---|
| | 276 | java.security.cert.X509Certificate jX509Cert; |
|---|
| | 277 | // Now create it based on the OpenSAML X509Certificate object |
|---|
| | 278 | jX509Cert = KeyInfoHelper.getCertificate(x509Cert); |
|---|
| | 279 | // Now we can pull out the public key part of the certificate into a KeySpec |
|---|
| | 280 | X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec( jX509Cert.getPublicKey().getEncoded() ); |
|---|
| | 281 | |
|---|
| | 282 | // Get our KeyFactory object that creates key objects for us specifying RSA |
|---|