Search in sources :

Example 1 with X509V2AttributeCertificate

use of com.github.zhenwei.provider.x509.X509V2AttributeCertificate in project robovm by robovm.

the class CMSSignedHelper method createAttributeStore.

X509Store createAttributeStore(String type, Provider provider, Store certStore) throws NoSuchStoreException, CMSException {
    try {
        Collection certHldrs = certStore.getMatches(null);
        List certs = new ArrayList(certHldrs.size());
        for (Iterator it = certHldrs.iterator(); it.hasNext(); ) {
            certs.add(new X509V2AttributeCertificate(((X509AttributeCertificateHolder) it.next()).getEncoded()));
        }
        return X509Store.getInstance("AttributeCertificate/" + type, new X509CollectionStoreParameters(certs), provider);
    } catch (IllegalArgumentException e) {
        throw new CMSException("can't setup the X509Store", e);
    } catch (IOException e) {
        throw new CMSException("can't setup the X509Store", e);
    }
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) CertificateList(org.bouncycastle.asn1.x509.CertificateList) List(java.util.List) X509AttributeCertificateHolder(org.bouncycastle.cert.X509AttributeCertificateHolder) X509CollectionStoreParameters(org.bouncycastle.x509.X509CollectionStoreParameters) IOException(java.io.IOException) X509V2AttributeCertificate(org.bouncycastle.x509.X509V2AttributeCertificate)

Example 2 with X509V2AttributeCertificate

use of com.github.zhenwei.provider.x509.X509V2AttributeCertificate in project XobotOS by xamarin.

the class MiscPEMGenerator method createPemObject.

private PemObject createPemObject(Object o) throws IOException {
    String type;
    byte[] encoding;
    if (o instanceof PemObject) {
        return (PemObject) o;
    }
    if (o instanceof PemObjectGenerator) {
        return ((PemObjectGenerator) o).generate();
    }
    if (o instanceof X509Certificate) {
        type = "CERTIFICATE";
        try {
            encoding = ((X509Certificate) o).getEncoded();
        } catch (CertificateEncodingException e) {
            throw new PemGenerationException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof X509CRL) {
        type = "X509 CRL";
        try {
            encoding = ((X509CRL) o).getEncoded();
        } catch (CRLException e) {
            throw new PemGenerationException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof KeyPair) {
        return createPemObject(((KeyPair) o).getPrivate());
    } else if (o instanceof PrivateKey) {
        PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) ASN1Object.fromByteArray(((Key) o).getEncoded()));
        if (o instanceof RSAPrivateKey) {
            type = "RSA PRIVATE KEY";
            encoding = info.getPrivateKey().getEncoded();
        } else if (o instanceof DSAPrivateKey) {
            type = "DSA PRIVATE KEY";
            DSAParameter p = DSAParameter.getInstance(info.getAlgorithmId().getParameters());
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(new DERInteger(0));
            v.add(new DERInteger(p.getP()));
            v.add(new DERInteger(p.getQ()));
            v.add(new DERInteger(p.getG()));
            BigInteger x = ((DSAPrivateKey) o).getX();
            BigInteger y = p.getG().modPow(x, p.getP());
            v.add(new DERInteger(y));
            v.add(new DERInteger(x));
            encoding = new DERSequence(v).getEncoded();
        } else if (((PrivateKey) o).getAlgorithm().equals("ECDSA")) {
            type = "EC PRIVATE KEY";
            encoding = info.getPrivateKey().getEncoded();
        } else {
            throw new IOException("Cannot identify private key");
        }
    } else if (o instanceof PublicKey) {
        type = "PUBLIC KEY";
        encoding = ((PublicKey) o).getEncoded();
    } else if (o instanceof X509AttributeCertificate) {
        type = "ATTRIBUTE CERTIFICATE";
        encoding = ((X509V2AttributeCertificate) o).getEncoded();
    } else if (o instanceof PKCS10CertificationRequest) {
        type = "CERTIFICATE REQUEST";
        encoding = ((PKCS10CertificationRequest) o).getEncoded();
    } else if (o instanceof ContentInfo) {
        type = "PKCS7";
        encoding = ((ContentInfo) o).getEncoded();
    } else {
        throw new PemGenerationException("unknown object passed - can't encode.");
    }
    return new PemObject(type, encoding);
}
Also used : X509CRL(java.security.cert.X509CRL) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) X509AttributeCertificate(org.bouncycastle.x509.X509AttributeCertificate) DERInteger(org.bouncycastle.asn1.DERInteger) PemObjectGenerator(org.bouncycastle.util.io.pem.PemObjectGenerator) DERSequence(org.bouncycastle.asn1.DERSequence) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DSAParameter(org.bouncycastle.asn1.x509.DSAParameter) CRLException(java.security.cert.CRLException) PKCS10CertificationRequest(org.bouncycastle.jce.PKCS10CertificationRequest) KeyPair(java.security.KeyPair) PemGenerationException(org.bouncycastle.util.io.pem.PemGenerationException) PublicKey(java.security.PublicKey) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509V2AttributeCertificate(org.bouncycastle.x509.X509V2AttributeCertificate) X509Certificate(java.security.cert.X509Certificate) PemObject(org.bouncycastle.util.io.pem.PemObject) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey)

Example 3 with X509V2AttributeCertificate

use of com.github.zhenwei.provider.x509.X509V2AttributeCertificate in project LinLong-Java by zhenwei1108.

the class X509AttrCertParser method readDERCertificate.

private X509AttributeCertificate readDERCertificate(InputStream in) throws IOException {
    ASN1InputStream dIn = new ASN1InputStream(in);
    ASN1Sequence seq = ASN1Sequence.getInstance(dIn.readObject());
    if (seq.size() > 1 && seq.getObjectAt(0) instanceof ASN1ObjectIdentifier) {
        if (seq.getObjectAt(0).equals(PKCSObjectIdentifiers.signedData)) {
            sData = new SignedData(ASN1Sequence.getInstance((ASN1TaggedObject) seq.getObjectAt(1), true)).getCertificates();
            return getCertificate();
        }
    }
    return new X509V2AttributeCertificate(seq.getEncoded());
}
Also used : ASN1InputStream(com.github.zhenwei.core.asn1.ASN1InputStream) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) SignedData(com.github.zhenwei.core.asn1.pkcs.SignedData) ASN1TaggedObject(com.github.zhenwei.core.asn1.ASN1TaggedObject) X509V2AttributeCertificate(com.github.zhenwei.provider.x509.X509V2AttributeCertificate) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 4 with X509V2AttributeCertificate

use of com.github.zhenwei.provider.x509.X509V2AttributeCertificate in project BiglyBT by BiglySoftware.

the class PEMWriter method writeObject.

public void writeObject(Object o) throws IOException {
    String type;
    byte[] encoding;
    if (o instanceof X509Certificate) {
        type = "CERTIFICATE";
        try {
            encoding = ((X509Certificate) o).getEncoded();
        } catch (CertificateEncodingException e) {
            throw new IOException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof X509CRL) {
        type = "X509 CRL";
        try {
            encoding = ((X509CRL) o).getEncoded();
        } catch (CRLException e) {
            throw new IOException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof KeyPair) {
        writeObject(((KeyPair) o).getPrivate());
        return;
    } else if (o instanceof PrivateKey) {
        PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) ASN1Object.fromByteArray(((Key) o).getEncoded()));
        if (o instanceof RSAPrivateKey) {
            type = "RSA PRIVATE KEY";
            encoding = info.getPrivateKey().getEncoded();
        } else if (o instanceof DSAPrivateKey) {
            type = "DSA PRIVATE KEY";
            DSAParameter p = DSAParameter.getInstance(info.getAlgorithmId().getParameters());
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(new DERInteger(0));
            v.add(new DERInteger(p.getP()));
            v.add(new DERInteger(p.getQ()));
            v.add(new DERInteger(p.getG()));
            BigInteger x = ((DSAPrivateKey) o).getX();
            BigInteger y = p.getG().modPow(x, p.getP());
            v.add(new DERInteger(y));
            v.add(new DERInteger(x));
            encoding = new DERSequence(v).getEncoded();
        } else {
            throw new IOException("Cannot identify private key");
        }
    } else if (o instanceof PublicKey) {
        type = "PUBLIC KEY";
        encoding = ((PublicKey) o).getEncoded();
    } else if (o instanceof X509AttributeCertificate) {
        type = "ATTRIBUTE CERTIFICATE";
        encoding = ((X509V2AttributeCertificate) o).getEncoded();
    } else if (o instanceof PKCS10CertificationRequest) {
        type = "CERTIFICATE REQUEST";
        encoding = ((PKCS10CertificationRequest) o).getEncoded();
    } else if (o instanceof ContentInfo) {
        type = "PKCS7";
        encoding = ((ContentInfo) o).getEncoded();
    } else {
        throw new IOException("unknown object passed - can't encode.");
    }
    writeHeader(type);
    writeEncoded(encoding);
    writeFooter(type);
}
Also used : PKCS10CertificationRequest(org.gudy.bouncycastle.jce.PKCS10CertificationRequest) X509CRL(java.security.cert.X509CRL) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) X509AttributeCertificate(org.gudy.bouncycastle.x509.X509AttributeCertificate) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509V2AttributeCertificate(org.gudy.bouncycastle.x509.X509V2AttributeCertificate) X509Certificate(java.security.cert.X509Certificate) ContentInfo(org.gudy.bouncycastle.asn1.cms.ContentInfo) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) DSAParameter(org.gudy.bouncycastle.asn1.x509.DSAParameter) CRLException(java.security.cert.CRLException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKeyInfo(org.gudy.bouncycastle.asn1.pkcs.PrivateKeyInfo) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey)

Aggregations

IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)2 CRLException (java.security.cert.CRLException)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 X509CRL (java.security.cert.X509CRL)2 X509Certificate (java.security.cert.X509Certificate)2 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)2 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 X509V2AttributeCertificate (org.bouncycastle.x509.X509V2AttributeCertificate)2 ASN1InputStream (com.github.zhenwei.core.asn1.ASN1InputStream)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)1 ASN1TaggedObject (com.github.zhenwei.core.asn1.ASN1TaggedObject)1 SignedData (com.github.zhenwei.core.asn1.pkcs.SignedData)1 X509V2AttributeCertificate (com.github.zhenwei.provider.x509.X509V2AttributeCertificate)1 Key (java.security.Key)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1