public String generateText() { StringBuilder str = new StringBuilder(); String type = cert.getType(); if (type.equals(CERTIFICATE_TYPE_NAME)) { str.append(generateTextForX509()); } else { str.append(cert.toString()); } return str.toString(); }
/** * Returns a string containing a concise, human-readable description of * this {@code PrivateKeyEntry}. * * @return a printable representation for this {@code PrivateKeyEntry}. */ @Override public String toString() { StringBuilder sb = new StringBuilder( "PrivateKeyEntry: number of elements in certificate chain is "); sb.append(Integer.toString(chain.length)); sb.append("\n"); for (int i = 0; i < chain.length; i++) { sb.append(chain[i].toString()); sb.append("\n"); } return sb.toString(); } }
System.out.println("alias name: " + alias); Certificate certificate = keystore.getCertificate(alias); System.out.println(certificate.toString());
/** * Returns a {@code String} representation of this {@code CertPath} * instance. It is the result of calling {@code toString} on all {@code * Certificate}s in the {@code List}. * * @return a string representation of this instance. */ public String toString() { StringBuilder sb = new StringBuilder(getType()); sb.append(" Cert Path, len="); sb.append(getCertificates().size()); sb.append(": [\n"); int n=1; for (Iterator<? extends Certificate> i=getCertificates().iterator(); i.hasNext(); n++) { sb.append("---------------certificate "); sb.append(n); sb.append("---------------\n"); sb.append(((Certificate)i.next()).toString()); } sb.append("\n]"); return sb.toString(); }
boolean validateCertificate(Certificate cert) { boolean isValidated; if (cert == null) { return false; } try { KeyStore keyStore = getTrustStore(); PKIXParameters parms = new PKIXParameters(keyStore); parms.setRevocationEnabled(false); CertPathValidator certValidator = CertPathValidator.getInstance(CertPathValidator.getDefaultType()); // PKIX ArrayList<Certificate> start = new ArrayList<>(); start.add(cert); CertificateFactory certFactory = CertificateFactory.getInstance(CERTIFICATE_FORMAT); CertPath certPath = certFactory.generateCertPath(start); certValidator.validate(certPath, parms); isValidated = true; } catch (KeyStoreException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | CertificateException | CertPathValidatorException | CryptoException e) { logger.error("Cannot validate certificate. Error is: " + e.getMessage() + "\r\nCertificate" + cert.toString()); isValidated = false; } return isValidated; } // validateCertificate
if (config.extraLogLevel(10)) { if (null != diagnosticFileDumper) { logger.trace(format("Adding cert to trust store. alias: %s. certificate:", alias) + diagnosticFileDumper.createDiagnosticFile(alias + "cert: " + caCert.toString()));
import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; ..... private static String loadCertificate(String name) throws Exception { CertificateFactory cf = CertificateFactory.getInstance("X509"); InputStream in = new FileInputStream(new File( name)); X509Certificate cert =(X509Certificate ) cf.generateCertificate(in); in.close(); return cert.toString(); }
/** * Returns a string containing a concise, human-readable description of * this {@code PrivateKeyEntry}. * * @return a printable representation for this {@code PrivateKeyEntry}. */ @Override public String toString() { StringBuilder sb = new StringBuilder( "PrivateKeyEntry: number of elements in certificate chain is "); sb.append(Integer.toString(chain.length)); sb.append("\n"); for (int i = 0; i < chain.length; i++) { sb.append(chain[i].toString()); sb.append("\n"); } return sb.toString(); } }
/** * Shows a second {@link JFrame} displaying the content of the certificate * * @param cert */ @SuppressWarnings("unused") private void showCertificate(Certificate cert) { if (cert != null) { new CertificateView(cert.toString()); } }
try { KeyStore ks = KeyStore.getInstance("AndroidCAStore"); ks.load(null); try { Enumeration<String> aliases = ks.aliases(); while (aliases.hasMoreElements()) { Certificate cert = ks.getCertificate(aliases.nextElement()); Log.e(TAG, "Certificate: " + cert.toString()); } } catch(Exception e) { e.printStackTrace(); } } catch (IOException|NoSuchAlgorithmException|KeyStoreException|CertificateException e) { e.printStackTrace(); }
private String getCertificateChainDescription() { StringBuilder buf = new StringBuilder(); try { SSLSession session = socket.getSession(); for (Certificate certificate : session.getLocalCertificates()) buf.append(certificate.toString()); buf.append("\n--------------------------------------------------------------------------\n"); for (X509Certificate certificate : session.getPeerCertificateChain()) buf.append(certificate.toString()); } catch (Exception e) { buf.append(e); } return buf.toString(); }
private String getCertificateChainDescription() { final StringBuilder buf = new StringBuilder(); try { final SSLSession session = socket.getSession(); for (final Certificate certificate : session.getLocalCertificates()) { buf.append(certificate.toString()); } buf.append("\n--------------------------------------------------------------------------\n"); for (final X509Certificate certificate : session.getPeerCertificateChain()) { buf.append(certificate.toString()); } } catch (final Exception e) { buf.append(e); } return buf.toString(); }
private Exception createExceptionForLog(Exception e, int certNum, Certificate cert) { return new Exception(e + ": certNum:" + certNum + " : " + cert.toString()); }
@Nullable public String getAlias (@Nullable final Certificate aCert) throws CertificateException { if (aCert == null) return null; try { return getKeyStore ().getCertificateAlias (aCert); } catch (final KeyStoreException kse) { throw new CertificateException ("Error getting alias for certificate: " + aCert.toString (), kse); } }
protected CommandResult execute(AliasedCertificateFactory certFx, Object[] params) throws OpenAS2Exception { if (params.length < 1) { return new CommandResult(CommandResult.TYPE_INVALID_PARAM_COUNT, getUsage()); } synchronized (certFx) { String alias = params[0].toString(); Certificate cert = certFx.getCertificate(alias); return new CommandResult(CommandResult.TYPE_OK, cert.toString()); } } }
@Test public void testCreateTrustStoreFromCertificateDirectory() throws GeneralSecurityException, IOException { File certFolder = tempFolder.newFolder(); Files.copy(TestConstants.CA_DER_CERT_PATH.toFile(), certFolder.toPath().resolve("ca.der").toFile()); Files.copy(TestConstants.SERVER_CERT_PEM_PATH.toFile(), certFolder.toPath().resolve("server.crt").toFile()); Files.copy(TestConstants.CLIENT_CERT_PEM_PATH.toFile(), certFolder.toPath().resolve("client.cer").toFile()); KeyStore trustStore = KeyStores.createTrustStoreFromCertificates(certFolder.toPath()); assertThat(trustStore.size(), is(3)); assertThat(trustStore.getCertificate("ca.der").toString(), containsString("CN=testCA")); assertThat(trustStore.getCertificate("server.crt").toString(), containsString("CN=localhost")); assertThat(trustStore.getCertificate("client.cer").toString(), containsString("CN=client")); }
@Test public void createTrustStoreFromCertificatesFromCertificatesByAlias() throws Exception { String cert = Files.toString(TestConstants.SERVER_CERT_PEM_PATH.toFile(), StandardCharsets.UTF_8); KeyStore trustStore = KeyStores.createTrustStoreFromCertificates( ImmutableMap.of("server.crt", PemX509Certificate.of(cert))); assertThat(trustStore.getCertificate("server.crt").toString(), containsString("CN=localhost")); }
@Test public void testCreateTrustStoreFromCertificateFile() throws GeneralSecurityException, IOException { KeyStore trustStore = KeyStores.createTrustStoreFromCertificates(TestConstants.CA_DER_CERT_PATH); assertThat(trustStore.size(), is(1)); assertThat(trustStore.getCertificate(TestConstants.CA_DER_CERT_PATH.getFileName().toString()).toString(), containsString("CN=testCA")); }
@Test public void testCreateKeyStoreFromPemFile() throws GeneralSecurityException, IOException { TestConstants.assumePkcs1ReaderExists(); String password = "changeit"; KeyStore keyStore = KeyStores.createKeyStoreFromCombinedPems(TestConstants.SERVER_KEY_CERT_COMBINED_PEM_PATH, password); assertThat(keyStore.size(), is(1)); assertThat(keyStore.getCertificate( TestConstants.SERVER_KEY_CERT_COMBINED_PEM_PATH.getFileName().toString()).toString(), containsString("CN=testCA")); assertThat(keyStore.getKey(TestConstants.SERVER_KEY_CERT_COMBINED_PEM_PATH.getFileName().toString(), password.toCharArray()).getFormat(), is("PKCS#8")); }
@Override protected String renderStatus() throws Exception { return render(IdentityCapabilityDescriptor.TYPE_ID + "-status.vm", new TemplateParameters() .set("nodeId", nodeAccess.getId()) .set("fingerprint", nodeAccess.getFingerprint()) .set("pem", CertificateUtil.serializeCertificateInPEM(nodeAccess.getCertificate())) .set("detail", nodeAccess.getCertificate().toString()) ); } }