Search in sources :

Example 1 with IAliasedCertificateFactory

use of com.helger.as2lib.cert.IAliasedCertificateFactory in project as2-server by phax.

the class ImportCertInEncodedStreamCommand method _importCert.

private CommandResult _importCert(final IAliasedCertificateFactory certFx, final String alias, final String encodedCert) throws CertificateException, OpenAS2Exception {
    final NonBlockingByteArrayInputStream bais = new NonBlockingByteArrayInputStream(ByteCoder.decode(encodedCert).getBytes());
    final CertificateFactory cf = CertificateFactory.getInstance("X.509");
    while (bais.available() > 0) {
        final Certificate cert = cf.generateCertificate(bais);
        if (cert instanceof X509Certificate) {
            certFx.addCertificate(alias, (X509Certificate) cert, true);
            final CommandResult cmdRes = new CommandResult(ECommandResultType.TYPE_OK, "Certificate(s) imported successfully");
            cmdRes.addResult("Imported certificate: " + cert.toString());
            return cmdRes;
        }
    }
    return new CommandResult(ECommandResultType.TYPE_ERROR, "No valid X509 certificates found");
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) IAliasedCertificateFactory(com.helger.as2lib.cert.IAliasedCertificateFactory) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) CommandResult(com.helger.as2.cmd.CommandResult)

Example 2 with IAliasedCertificateFactory

use of com.helger.as2lib.cert.IAliasedCertificateFactory in project as2-server by phax.

the class ImportCertCommand method importCert.

@Nonnull
protected CommandResult importCert(final IAliasedCertificateFactory certFx, final String sAlias, final String sFilename) throws IOException, CertificateException, OpenAS2Exception {
    try (final FileInputStream fis = new FileInputStream(sFilename);
        final NonBlockingBufferedInputStream bis = new NonBlockingBufferedInputStream(fis)) {
        final CertificateFactory cf = CertificateFactory.getInstance("X.509");
        while (bis.available() > 0) {
            final Certificate aCert = cf.generateCertificate(bis);
            if (aCert instanceof X509Certificate) {
                certFx.addCertificate(sAlias, (X509Certificate) aCert, true);
                final CommandResult cmdRes = new CommandResult(ECommandResultType.TYPE_OK, "Certificate(s) imported successfully");
                cmdRes.addResult("Imported certificate: " + aCert.toString());
                return cmdRes;
            }
        }
        return new CommandResult(ECommandResultType.TYPE_ERROR, "No valid X509 certificates found");
    }
}
Also used : NonBlockingBufferedInputStream(com.helger.commons.io.stream.NonBlockingBufferedInputStream) IAliasedCertificateFactory(com.helger.as2lib.cert.IAliasedCertificateFactory) CertificateFactory(java.security.cert.CertificateFactory) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) CommandResult(com.helger.as2.cmd.CommandResult) Nonnull(javax.annotation.Nonnull)

Aggregations

CommandResult (com.helger.as2.cmd.CommandResult)2 IAliasedCertificateFactory (com.helger.as2lib.cert.IAliasedCertificateFactory)2 Certificate (java.security.cert.Certificate)2 CertificateFactory (java.security.cert.CertificateFactory)2 X509Certificate (java.security.cert.X509Certificate)2 NonBlockingBufferedInputStream (com.helger.commons.io.stream.NonBlockingBufferedInputStream)1 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 Nonnull (javax.annotation.Nonnull)1