Search in sources :

Example 1 with PemObject

use of com.github.zhenwei.core.util.io.pem.PemObject in project LinLong-Java by zhenwei1108.

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 X509CertificateHolder) {
        type = "CERTIFICATE";
        encoding = ((X509CertificateHolder) o).getEncoded();
    } else if (o instanceof X509CRLHolder) {
        type = "X509 CRL";
        encoding = ((X509CRLHolder) o).getEncoded();
    } else if (o instanceof X509TrustedCertificateBlock) {
        type = "TRUSTED CERTIFICATE";
        encoding = ((X509TrustedCertificateBlock) o).getEncoded();
    } else if (o instanceof PrivateKeyInfo) {
        PrivateKeyInfo info = (PrivateKeyInfo) o;
        ASN1ObjectIdentifier algOID = info.getPrivateKeyAlgorithm().getAlgorithm();
        if (algOID.equals(PKCSObjectIdentifiers.rsaEncryption)) {
            type = "RSA PRIVATE KEY";
            encoding = info.parsePrivateKey().toASN1Primitive().getEncoded();
        } else if (algOID.equals(dsaOids[0]) || algOID.equals(dsaOids[1])) {
            type = "DSA PRIVATE KEY";
            DSAParameter p = DSAParameter.getInstance(info.getPrivateKeyAlgorithm().getParameters());
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(new ASN1Integer(0));
            v.add(new ASN1Integer(p.getP()));
            v.add(new ASN1Integer(p.getQ()));
            v.add(new ASN1Integer(p.getG()));
            BigInteger x = ASN1Integer.getInstance(info.parsePrivateKey()).getValue();
            BigInteger y = p.getG().modPow(x, p.getP());
            v.add(new ASN1Integer(y));
            v.add(new ASN1Integer(x));
            encoding = new DERSequence(v).getEncoded();
        } else if (algOID.equals(X9ObjectIdentifiers.id_ecPublicKey)) {
            type = "EC PRIVATE KEY";
            encoding = info.parsePrivateKey().toASN1Primitive().getEncoded();
        } else {
            type = "PRIVATE KEY";
            encoding = info.getEncoded();
        }
    } else if (o instanceof SubjectPublicKeyInfo) {
        type = "PUBLIC KEY";
        encoding = ((SubjectPublicKeyInfo) o).getEncoded();
    } else if (o instanceof X509AttributeCertificateHolder) {
        type = "ATTRIBUTE CERTIFICATE";
        encoding = ((X509AttributeCertificateHolder) o).getEncoded();
    } else if (o instanceof com.github.zhenwei.pkix.pkcs.PKCS10CertificationRequest) {
        type = "CERTIFICATE REQUEST";
        encoding = ((PKCS10CertificationRequest) o).getEncoded();
    } else if (o instanceof PKCS8EncryptedPrivateKeyInfo) {
        type = "ENCRYPTED PRIVATE KEY";
        encoding = ((PKCS8EncryptedPrivateKeyInfo) o).getEncoded();
    } else if (o instanceof ContentInfo) {
        type = "PKCS7";
        encoding = ((ContentInfo) o).getEncoded();
    } else {
        throw new PemGenerationException("unknown object passed - can't encode.");
    }
    if (encryptor != null) {
        String dekAlgName = Strings.toUpperCase(encryptor.getAlgorithm());
        // Note: For backward compatibility
        if (dekAlgName.equals("DESEDE")) {
            dekAlgName = "DES-EDE3-CBC";
        }
        byte[] iv = encryptor.getIV();
        byte[] encData = encryptor.encrypt(encoding);
        List headers = new ArrayList(2);
        headers.add(new PemHeader("Proc-Type", "4,ENCRYPTED"));
        headers.add(new PemHeader("DEK-Info", dekAlgName + "," + getHexEncoded(iv)));
        return new PemObject(type, headers, encData);
    }
    return new PemObject(type, encoding);
}
Also used : ArrayList(java.util.ArrayList) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) PemObjectGenerator(com.github.zhenwei.core.util.io.pem.PemObjectGenerator) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) ArrayList(java.util.ArrayList) List(java.util.List) DSAParameter(com.github.zhenwei.core.asn1.x509.DSAParameter) PKCS10CertificationRequest(com.github.zhenwei.pkix.pkcs.PKCS10CertificationRequest) PemGenerationException(com.github.zhenwei.core.util.io.pem.PemGenerationException) X509AttributeCertificateHolder(com.github.zhenwei.pkix.cert.X509AttributeCertificateHolder) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) PKCS8EncryptedPrivateKeyInfo(com.github.zhenwei.pkix.pkcs.PKCS8EncryptedPrivateKeyInfo) PemObject(com.github.zhenwei.core.util.io.pem.PemObject) X509CertificateHolder(com.github.zhenwei.pkix.cert.X509CertificateHolder) X509CRLHolder(com.github.zhenwei.pkix.cert.X509CRLHolder) BigInteger(java.math.BigInteger) PKCS8EncryptedPrivateKeyInfo(com.github.zhenwei.pkix.pkcs.PKCS8EncryptedPrivateKeyInfo) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) PemHeader(com.github.zhenwei.core.util.io.pem.PemHeader)

Example 2 with PemObject

use of com.github.zhenwei.core.util.io.pem.PemObject in project LinLong-Java by zhenwei1108.

the class PEMParser method readObject.

/**
 * Read the next PEM object attempting to interpret the header and create a higher level object
 * from the content.
 *
 * @return the next object in the stream, null if no objects left.
 * @throws IOException in case of a parse error.
 */
public Object readObject() throws IOException {
    PemObject obj = readPemObject();
    if (obj != null) {
        String type = obj.getType();
        Object pemObjectParser = parsers.get(type);
        if (pemObjectParser != null) {
            return ((PemObjectParser) pemObjectParser).parseObject(obj);
        } else {
            throw new IOException("unrecognised object: " + type);
        }
    }
    return null;
}
Also used : PemObject(com.github.zhenwei.core.util.io.pem.PemObject) PemObject(com.github.zhenwei.core.util.io.pem.PemObject) IOException(java.io.IOException) PemObjectParser(com.github.zhenwei.core.util.io.pem.PemObjectParser)

Example 3 with PemObject

use of com.github.zhenwei.core.util.io.pem.PemObject in project LinLong-Java by zhenwei1108.

the class PKIXCertPath method getEncoded.

/**
 * Returns the encoded form of this certification path, using the specified encoding.
 *
 * @param encoding the name of the encoding to use
 * @return the encoded bytes
 * @throws CertificateEncodingException if an encoding error occurs or the encoding requested is
 *                                      not supported
 */
public byte[] getEncoded(String encoding) throws CertificateEncodingException {
    if (encoding.equalsIgnoreCase("PkiPath")) {
        ASN1EncodableVector v = new ASN1EncodableVector();
        ListIterator iter = certificates.listIterator(certificates.size());
        while (iter.hasPrevious()) {
            v.add(toASN1Object((X509Certificate) iter.previous()));
        }
        return toDEREncoded(new DERSequence(v));
    } else if (encoding.equalsIgnoreCase("PKCS7")) {
        ContentInfo encInfo = new ContentInfo(PKCSObjectIdentifiers.data, null);
        ASN1EncodableVector v = new ASN1EncodableVector();
        for (int i = 0; i != certificates.size(); i++) {
            v.add(toASN1Object((X509Certificate) certificates.get(i)));
        }
        SignedData sd = new SignedData(new ASN1Integer(1), new DERSet(), encInfo, new DERSet(v), null, new DERSet());
        return toDEREncoded(new ContentInfo(PKCSObjectIdentifiers.signedData, sd));
    } else if (encoding.equalsIgnoreCase("PEM")) {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut));
        try {
            for (int i = 0; i != certificates.size(); i++) {
                pWrt.writeObject(new PemObject("CERTIFICATE", ((X509Certificate) certificates.get(i)).getEncoded()));
            }
            pWrt.close();
        } catch (Exception e) {
            throw new CertificateEncodingException("can't encode certificate for PEM encoded path");
        }
        return bOut.toByteArray();
    } else {
        throw new CertificateEncodingException("unsupported encoding: " + encoding);
    }
}
Also used : SignedData(com.github.zhenwei.core.asn1.pkcs.SignedData) PemWriter(com.github.zhenwei.core.util.io.pem.PemWriter) CertificateEncodingException(java.security.cert.CertificateEncodingException) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ListIterator(java.util.ListIterator) DERSet(com.github.zhenwei.core.asn1.DERSet) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException) PemObject(com.github.zhenwei.core.util.io.pem.PemObject) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ContentInfo(com.github.zhenwei.core.asn1.pkcs.ContentInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) OutputStreamWriter(java.io.OutputStreamWriter)

Example 4 with PemObject

use of com.github.zhenwei.core.util.io.pem.PemObject in project LinLong-Java by zhenwei1108.

the class PKCS8Generator method generate.

private PemObject generate(PrivateKeyInfo key, OutputEncryptor encryptor) throws PemGenerationException {
    try {
        byte[] keyData = key.getEncoded();
        if (encryptor == null) {
            return new PemObject("PRIVATE KEY", keyData);
        }
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        OutputStream cOut = encryptor.getOutputStream(bOut);
        cOut.write(key.getEncoded());
        cOut.close();
        EncryptedPrivateKeyInfo info = new EncryptedPrivateKeyInfo(encryptor.getAlgorithmIdentifier(), bOut.toByteArray());
        return new PemObject("ENCRYPTED PRIVATE KEY", info.getEncoded());
    } catch (IOException e) {
        throw new PemGenerationException("unable to process encoded key data: " + e.getMessage(), e);
    }
}
Also used : PemObject(com.github.zhenwei.core.util.io.pem.PemObject) PemGenerationException(com.github.zhenwei.core.util.io.pem.PemGenerationException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EncryptedPrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

PemObject (com.github.zhenwei.core.util.io.pem.PemObject)4 IOException (java.io.IOException)3 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)2 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)2 DERSequence (com.github.zhenwei.core.asn1.DERSequence)2 PemGenerationException (com.github.zhenwei.core.util.io.pem.PemGenerationException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 DERSet (com.github.zhenwei.core.asn1.DERSet)1 ContentInfo (com.github.zhenwei.core.asn1.pkcs.ContentInfo)1 EncryptedPrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo)1 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)1 SignedData (com.github.zhenwei.core.asn1.pkcs.SignedData)1 DSAParameter (com.github.zhenwei.core.asn1.x509.DSAParameter)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 PemHeader (com.github.zhenwei.core.util.io.pem.PemHeader)1 PemObjectGenerator (com.github.zhenwei.core.util.io.pem.PemObjectGenerator)1 PemObjectParser (com.github.zhenwei.core.util.io.pem.PemObjectParser)1 PemWriter (com.github.zhenwei.core.util.io.pem.PemWriter)1 X509AttributeCertificateHolder (com.github.zhenwei.pkix.cert.X509AttributeCertificateHolder)1