Example usage for org.apache.commons.ssl PEMItem getDerBytes

List of usage examples for org.apache.commons.ssl PEMItem getDerBytes

Introduction

In this page you can find the example usage for org.apache.commons.ssl PEMItem getDerBytes.

Prototype

public byte[] getDerBytes() 

Source Link

Usage

From source file:cl.nic.dte.extension.AutorizacionTypeExtensionHandler.java

@SuppressWarnings("unchecked")
public static PrivateKey getPrivateKey(AutorizacionType auth, char[] password)
        throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
        InvalidAlgorithmParameterException, IOException {

    List<PEMItem> items = PEMUtil.decode(auth.getRSASK().getBytes());

    for (PEMItem item : items) {
        if ("RSA PRIVATE KEY".equals(item.pemType)) {
            try {
                PKCS8Key pkcs8 = new PKCS8Key(item.getDerBytes(), password);

                return Utilities.readPrivateKey(pkcs8.getDecryptedBytes(), "RSA", password);
            } catch (GeneralSecurityException e) {
                throw new InvalidKeySpecException(e);
            }//from w w  w  .ja  v  a  2  s  . com
        }
    }

    return null;
}

From source file:cl.nic.dte.extension.AutorizacionTypeExtensionHandler.java

@SuppressWarnings("unchecked")
public static PublicKey getPublicKey(AutorizacionType auth)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    List<PEMItem> items = PEMUtil.decode(auth.getRSAPUBK().getBytes());

    for (PEMItem item : items) {
        if ("PUBLIC KEY".equals(item.pemType)) {
            X509EncodedKeySpec enc;
            try {
                enc = new X509EncodedKeySpec(item.getDerBytes());
                KeyFactory rsaKeyFac;
                rsaKeyFac = KeyFactory.getInstance("RSA");
                return (PublicKey) rsaKeyFac.generatePublic((enc));
            } catch (GeneralSecurityException e) {
                throw new InvalidKeySpecException(e);
            }//from w w w  . j a v  a  2  s  .co  m
        }
    }
    return null;
}

From source file:com.ah.be.cloudauth.HmCloudAuthCertMgmtImpl.java

@SuppressWarnings("rawtypes")
private void verifyCSRContent(BeRadSecCertCreationResultEvent result, String commonName)
        throws HmCloudAuthException {
    String methodName = "verifyCSRContent";
    if (result.isCreateError()) {
        throw new HmCloudAuthException(methodName, UpdateCAStatus.CSR_CREATE_ERR);
    }/* w  ww  . j av a2  s.  com*/
    if (result.isNeedCreate()) {
        byte[] csrContent = result.getCsrContent();
        final List pemItems = org.apache.commons.ssl.PEMUtil.decode(csrContent);
        if (pemItems.isEmpty()) {
            throw new HmCloudAuthException(methodName, UpdateCAStatus.CSR_DECODE_ERR);
        }

        final PEMItem csrPemItem = (PEMItem) pemItems.get(0);
        if (csrPemItem.pemType.startsWith(CERTIFICATE_REQUEST)) {
            final PKCS10CertificationRequest csr = new PKCS10CertificationRequest(csrPemItem.getDerBytes());
            CertificationRequestInfo requestInfo = csr.getCertificationRequestInfo();
            X509Name subject = requestInfo.getSubject();

            Vector commondNameVector = subject.getValues(X509Name.CN);
            Vector countryVector = subject.getValues(X509Name.C);
            Vector organizationVector = subject.getValues(X509Name.O);
            if (commondNameVector.isEmpty() || countryVector.isEmpty() || organizationVector.isEmpty()) {
                throw new HmCloudAuthException(methodName, UpdateCAStatus.CSR_FORMAT_ERR);
            }
            if (!commonName.equalsIgnoreCase(commondNameVector.get(0).toString())
                    || !ORGANIZATION.equals(organizationVector.get(0).toString())
                    || !COUNTRY.equals(countryVector.get(0).toString())) {
                throw new HmCloudAuthException(methodName, UpdateCAStatus.CSR_VERIFY_ERR);
            }
        } else {
            throw new HmCloudAuthException(methodName, UpdateCAStatus.CSR_DECODE_ERR);
        }
    } else {
        throw new HmCloudAuthException(methodName, UpdateCAStatus.CSR_STATUS_ERR);
    }
    return;
}