Search in sources :

Example 1 with SecCertificateRef

use of ch.cyberduck.core.keychain.SecCertificateRef in project cyberduck by iterate-ch.

the class KeychainCertificateStore method toX509Certificate.

public static X509Certificate toX509Certificate(final SecIdentityRef identityRef) {
    final PointerByReference reference = new PointerByReference();
    int err;
    err = SecurityFunctions.library.SecIdentityCopyCertificate(identityRef, reference);
    if (0 != err) {
        log.error(String.format("SecIdentityCopyCertificate returning error %d", err));
        return null;
    }
    final SecCertificateRef certificateRef = new SecCertificateRef(reference.getValue());
    try {
        final CertificateFactory factory = CertificateFactory.getInstance("X.509");
        final NSData dataRef = SecurityFunctions.library.SecCertificateCopyData(certificateRef);
        final X509Certificate selected = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(Base64.decodeBase64(dataRef.base64Encoding())));
        if (log.isDebugEnabled()) {
            log.info(String.format("Selected certificate %s", selected));
        }
        FoundationKitFunctions.library.CFRelease(certificateRef);
        return selected;
    } catch (CertificateException e) {
        log.error(String.format("Error %s creating certificate from reference", e));
        return null;
    }
}
Also used : NSData(ch.cyberduck.binding.foundation.NSData) ByteArrayInputStream(java.io.ByteArrayInputStream) PointerByReference(com.sun.jna.ptr.PointerByReference) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) SecCertificateRef(ch.cyberduck.core.keychain.SecCertificateRef) X509Certificate(java.security.cert.X509Certificate)

Example 2 with SecCertificateRef

use of ch.cyberduck.core.keychain.SecCertificateRef in project cyberduck by iterate-ch.

the class KeychainCertificateStore method toDEREncodedCertificates.

public static NSArray toDEREncodedCertificates(final List<X509Certificate> certificates) {
    // Prepare the certificate chain
    try {
        final Object[] encoded = new DEREncoder().encode(certificates);
    } catch (CertificateException e) {
        log.error(String.format("Failure %s DER encoding certificates %s", e, certificates));
        return NSArray.array();
    }
    final NSMutableArray certs = NSMutableArray.arrayWithCapacity(new NSUInteger(certificates.size()));
    for (X509Certificate certificate : certificates) {
        try {
            final SecCertificateRef certificateRef = SecurityFunctions.library.SecCertificateCreateWithData(null, NSData.dataWithBase64EncodedString(Base64.encodeBase64String(certificate.getEncoded())));
            if (null == certificateRef) {
                log.error(String.format("Error creating converting from ASN.1 DER encoded certificate %s", certificate));
                continue;
            }
            certs.addObject(certificateRef);
        } catch (CertificateEncodingException e) {
            log.error(String.format("Failure %s retrieving encoded  certificate", e));
        }
    }
    return certs;
}
Also used : DEREncoder(ch.cyberduck.core.ssl.DEREncoder) NSMutableArray(ch.cyberduck.binding.foundation.NSMutableArray) CertificateException(java.security.cert.CertificateException) CertificateEncodingException(java.security.cert.CertificateEncodingException) NSUInteger(org.rococoa.cocoa.foundation.NSUInteger) X509Certificate(java.security.cert.X509Certificate) SecCertificateRef(ch.cyberduck.core.keychain.SecCertificateRef)

Aggregations

SecCertificateRef (ch.cyberduck.core.keychain.SecCertificateRef)2 CertificateException (java.security.cert.CertificateException)2 X509Certificate (java.security.cert.X509Certificate)2 NSData (ch.cyberduck.binding.foundation.NSData)1 NSMutableArray (ch.cyberduck.binding.foundation.NSMutableArray)1 DEREncoder (ch.cyberduck.core.ssl.DEREncoder)1 PointerByReference (com.sun.jna.ptr.PointerByReference)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateFactory (java.security.cert.CertificateFactory)1 NSUInteger (org.rococoa.cocoa.foundation.NSUInteger)1