Search in sources :

Example 36 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project j2objc by google.

the class SubjectPublicKeyInfo method getPublicKey.

/**
     * Returns the PublicKey corresponding to this SubjectPublicKeyInfo
     * instance.
     */
public PublicKey getPublicKey() {
    if (publicKey == null) {
        final byte[] encoded = getEncoded();
        final KeySpec keySpec = new X509EncodedKeySpec(encoded);
        /* Try using the algorithm name first. */
        final String algName = algorithmID.getAlgorithmName();
        publicKey = generateKeyForAlgorithm(keySpec, algName);
        /*
             * Fall back to using the algorithm OID if it's not the same as the
             * algorithm name.
             */
        final String algOid = algorithmID.getAlgorithm();
        if (publicKey == null && !algOid.equals(algName)) {
            publicKey = generateKeyForAlgorithm(keySpec, algOid);
        }
        /*
             * Encode this as an X.509 public key since we didn't have any
             * KeyFactory that could handle this algorithm name or OID. Perhaps
             * the thing that's using this can decode it.
             */
        if (publicKey == null) {
            publicKey = new X509PublicKey(algOid, encoded, subjectPublicKey);
        }
    }
    return publicKey;
}
Also used : KeySpec(java.security.spec.KeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) ASN1BitString(org.apache.harmony.security.asn1.ASN1BitString) BitString(org.apache.harmony.security.asn1.BitString)

Example 37 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project bigbluebutton by bigbluebutton.

the class RSA_SHA1 method getPublicKeyFromPem.

private PublicKey getPublicKeyFromPem(String pem) throws GeneralSecurityException, IOException {
    InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
    PEMReader reader = new PEMReader(stream);
    byte[] bytes = reader.getDerBytes();
    PublicKey pubKey;
    if (PEMReader.PUBLIC_X509_MARKER.equals(reader.getBeginMarker())) {
        KeySpec keySpec = new X509EncodedKeySpec(bytes);
        KeyFactory fac = KeyFactory.getInstance("RSA");
        pubKey = fac.generatePublic(keySpec);
    } else if (PEMReader.CERTIFICATE_X509_MARKER.equals(reader.getBeginMarker())) {
        pubKey = getPublicKeyFromDerCert(bytes);
    } else {
        throw new IOException("Invalid PEM fileL: Unknown marker for " + " public key or cert " + reader.getBeginMarker());
    }
    return pubKey;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) PublicKey(java.security.PublicKey) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) KeyFactory(java.security.KeyFactory)

Example 38 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project bigbluebutton by bigbluebutton.

the class RSA_SHA1 method getPublicKeyFromDer.

private PublicKey getPublicKeyFromDer(byte[] publicKeyObject) throws GeneralSecurityException {
    KeyFactory fac = KeyFactory.getInstance("RSA");
    EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKeyObject);
    return fac.generatePublic(pubKeySpec);
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec)

Example 39 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project XobotOS by xamarin.

the class DSAKeyFactoryImpl method engineGetKeySpec.

/**
     * This method returns a specification for the supplied key.
     *
     * The specification will be returned in the form of an object of the type
     * specified by keySpec.
     *
     * @param key -
     *            either DSAPrivateKey or DSAPublicKey
     * @param keySpec -
     *            either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
     *
     * @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
     *
     * @throws InvalidKeySpecException
     *             if "keySpec" is not a specification for DSAPublicKey or
     *             DSAPrivateKey
     */
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
    BigInteger p, q, g, x, y;
    if (key != null) {
        if (keySpec == null) {
            throw new NullPointerException("keySpec == null");
        }
        if (key instanceof DSAPrivateKey) {
            DSAPrivateKey privateKey = (DSAPrivateKey) key;
            if (keySpec.equals(DSAPrivateKeySpec.class)) {
                x = privateKey.getX();
                DSAParams params = privateKey.getParams();
                p = params.getP();
                q = params.getQ();
                g = params.getG();
                return (T) (new DSAPrivateKeySpec(x, p, q, g));
            }
            if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
                return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
            }
            throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec");
        }
        if (key instanceof DSAPublicKey) {
            DSAPublicKey publicKey = (DSAPublicKey) key;
            if (keySpec.equals(DSAPublicKeySpec.class)) {
                y = publicKey.getY();
                DSAParams params = publicKey.getParams();
                p = params.getP();
                q = params.getQ();
                g = params.getG();
                return (T) (new DSAPublicKeySpec(y, p, q, g));
            }
            if (keySpec.equals(X509EncodedKeySpec.class)) {
                return (T) (new X509EncodedKeySpec(key.getEncoded()));
            }
            throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec");
        }
    }
    throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAParams(java.security.interfaces.DSAParams) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 40 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project platform_frameworks_base by android.

the class PackageParser method parsePublicKey.

public static final PublicKey parsePublicKey(final String encodedPublicKey) {
    if (encodedPublicKey == null) {
        Slog.w(TAG, "Could not parse null public key");
        return null;
    }
    EncodedKeySpec keySpec;
    try {
        final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
        keySpec = new X509EncodedKeySpec(encoded);
    } catch (IllegalArgumentException e) {
        Slog.w(TAG, "Could not parse verifier public key; invalid Base64");
        return null;
    }
    /* First try the key as an RSA key. */
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        Slog.wtf(TAG, "Could not parse public key: RSA KeyFactory not included in build");
    } catch (InvalidKeySpecException e) {
    // Not a RSA public key.
    }
    /* Now try it as a ECDSA key. */
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("EC");
        return keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        Slog.wtf(TAG, "Could not parse public key: EC KeyFactory not included in build");
    } catch (InvalidKeySpecException e) {
    // Not a ECDSA public key.
    }
    /* Now try it as a DSA key. */
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        return keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        Slog.wtf(TAG, "Could not parse public key: DSA KeyFactory not included in build");
    } catch (InvalidKeySpecException e) {
    // Not a DSA public key.
    }
    /* Not a supported key type */
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Aggregations

X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)343 KeyFactory (java.security.KeyFactory)228 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)154 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)142 PublicKey (java.security.PublicKey)129 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)82 PrivateKey (java.security.PrivateKey)50 RSAPublicKey (java.security.interfaces.RSAPublicKey)48 IOException (java.io.IOException)39 InvalidKeyException (java.security.InvalidKeyException)37 KeyPair (java.security.KeyPair)30 Cipher (javax.crypto.Cipher)26 Signature (java.security.Signature)25 EncodedKeySpec (java.security.spec.EncodedKeySpec)21 NoSuchProviderException (java.security.NoSuchProviderException)14 ECPublicKey (java.security.interfaces.ECPublicKey)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 SecretKey (javax.crypto.SecretKey)13 BigInteger (java.math.BigInteger)12 Key (java.security.Key)12