Search in sources :

Example 1 with DSAPublicKey

use of com.mindbright.security.pkcs1.DSAPublicKey in project SpringRemote by HaleyWang.

the class X509Certificate method getPublicKey.

public PublicKey getPublicKey() {
    SubjectPublicKeyInfo spki = certificate.tbsCertificate.subjectPublicKeyInfo;
    String alg = spki.algorithm.algorithmName().toUpperCase();
    ASN1DER der = new ASN1DER();
    if (alg.startsWith("RSA")) {
        RSAPublicKey rsa = new RSAPublicKey();
        ByteArrayInputStream ba = new ByteArrayInputStream(spki.subjectPublicKey.getBitArray());
        try {
            der.decode(ba, rsa);
        } catch (Exception e) {
            throw new Error("Internal error decoding SubjectPublicKeyInfo.subjectPublicKey: " + e.getMessage());
        }
        try {
            KeyFactory keyFact = KeyFactory.getInstance("RSA");
            RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(rsa.modulus.getValue(), rsa.publicExponent.getValue());
            return keyFact.generatePublic(pubSpec);
        } catch (Exception e) {
            throw new Error("Error creating RSA key: " + e.getMessage());
        }
    } else if (alg.startsWith("DSA")) {
        DSAPublicKey dsa = new DSAPublicKey();
        ByteArrayInputStream ba = new ByteArrayInputStream(spki.subjectPublicKey.getBitArray());
        try {
            der.decode(ba, dsa);
        } catch (Exception e) {
            throw new Error("Internal error decoding SubjectPublicKeyInfo.subjectPublicKey: " + e.getMessage());
        }
        BigInteger y = dsa.getValue();
        DSAParams dsaParams = (DSAParams) spki.algorithm.parameters.getValue();
        BigInteger p = dsaParams.p.getValue();
        BigInteger q = dsaParams.q.getValue();
        BigInteger g = dsaParams.g.getValue();
        try {
            KeyFactory dsaKeyFact = KeyFactory.getInstance("DSA");
            DSAPublicKeySpec dsaPubSpec = new DSAPublicKeySpec(y, p, q, g);
            return dsaKeyFact.generatePublic(dsaPubSpec);
        } catch (Exception e) {
            throw new Error("Error creating DSA key: " + e.getMessage());
        }
    } else {
        throw new Error("Internal error decoding publicKey: unknown algorithm");
    }
}
Also used : ASN1DER(com.mindbright.asn1.ASN1DER) ASN1IA5String(com.mindbright.asn1.ASN1IA5String) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAParams(com.mindbright.security.pkcs1.DSAParams) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException) DSAPublicKey(com.mindbright.security.pkcs1.DSAPublicKey) RSAPublicKey(com.mindbright.security.pkcs1.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

ASN1DER (com.mindbright.asn1.ASN1DER)1 ASN1IA5String (com.mindbright.asn1.ASN1IA5String)1 DSAParams (com.mindbright.security.pkcs1.DSAParams)1 DSAPublicKey (com.mindbright.security.pkcs1.DSAPublicKey)1 RSAPublicKey (com.mindbright.security.pkcs1.RSAPublicKey)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 SignatureException (java.security.SignatureException)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateException (java.security.cert.CertificateException)1 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)1 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)1