Search in sources :

Example 21 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project android_frameworks_base by AOSPA.

the class AndroidKeyStoreProvider method getAndroidKeyStorePublicKey.

@NonNull
public static AndroidKeyStorePublicKey getAndroidKeyStorePublicKey(@NonNull String alias, int uid, @NonNull @KeyProperties.KeyAlgorithmEnum String keyAlgorithm, @NonNull byte[] x509EncodedForm) {
    PublicKey publicKey;
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(x509EncodedForm));
    } catch (NoSuchAlgorithmException e) {
        throw new ProviderException("Failed to obtain " + keyAlgorithm + " KeyFactory", e);
    } catch (InvalidKeySpecException e) {
        throw new ProviderException("Invalid X.509 encoding of public key", e);
    }
    if (KeyProperties.KEY_ALGORITHM_EC.equalsIgnoreCase(keyAlgorithm)) {
        return new AndroidKeyStoreECPublicKey(alias, uid, (ECPublicKey) publicKey);
    } else if (KeyProperties.KEY_ALGORITHM_RSA.equalsIgnoreCase(keyAlgorithm)) {
        return new AndroidKeyStoreRSAPublicKey(alias, uid, (RSAPublicKey) publicKey);
    } else {
        throw new ProviderException("Unsupported Android Keystore public key algorithm: " + keyAlgorithm);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ProviderException(java.security.ProviderException) NoSuchProviderException(java.security.NoSuchProviderException) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) NonNull(android.annotation.NonNull)

Example 22 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project OpenAM by OpenRock.

the class LibSecurityTokenProvider method createKeyInfo.

/**
     * Returns the <code>KeyInfo</code> object as a Document Element.
     */
private Element createKeyInfo() throws SecurityTokenException {
    X509Certificate cert = getX509Certificate();
    Document doc = null;
    try {
        doc = XMLUtils.newDocument();
    } catch (Exception e) {
        debug.error("createKeyInfo: ", e);
        throw new SecurityTokenException(e.getMessage());
    }
    String keyNameTextString = null;
    String base64CertString = null;
    PublicKey pk = null;
    try {
        pk = cert.getPublicKey();
        keyNameTextString = cert.getSubjectDN().getName();
        base64CertString = Base64.encode(cert.getEncoded());
    } catch (Exception e) {
        debug.error("createKeyInfo: ", e);
        throw new SecurityTokenException(e.getMessage());
    }
    Element keyInfo = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, SAMLConstants.TAG_KEYINFO);
    keyInfo.setAttribute("xmlns", SAMLConstants.XMLSIG_NAMESPACE_URI);
    if ((keyInfoType != null) && (keyInfoType.equalsIgnoreCase("certificate"))) {
        //put Certificate in KeyInfo
        Element x509Data = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, SAMLConstants.TAG_X509DATA);
        Element x509Certificate = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, SAMLConstants.TAG_X509CERTIFICATE);
        Text certText = doc.createTextNode(base64CertString);
        x509Certificate.appendChild(certText);
        keyInfo.appendChild(x509Data).appendChild(x509Certificate);
    } else {
        //put public key in keyinfo
        Element keyName = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, SAMLConstants.TAG_KEYNAME);
        Text keyNameText = doc.createTextNode(keyNameTextString);
        Element keyvalue = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, SAMLConstants.TAG_KEYVALUE);
        if (pk.getAlgorithm().equals("DSA")) {
            DSAPublicKey dsakey = (DSAPublicKey) pk;
            DSAParams dsaParams = dsakey.getParams();
            BigInteger _p = dsaParams.getP();
            BigInteger _q = dsaParams.getQ();
            BigInteger _g = dsaParams.getG();
            BigInteger _y = dsakey.getY();
            Element DSAKeyValue = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, "DSAKeyValue");
            Element p = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, "P");
            Text value_p = doc.createTextNode(Base64.encode(_p.toByteArray()));
            p.appendChild(value_p);
            DSAKeyValue.appendChild(p);
            Element q = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, "Q");
            Text value_q = doc.createTextNode(Base64.encode(_q.toByteArray()));
            q.appendChild(value_q);
            DSAKeyValue.appendChild(q);
            Element g = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, "G");
            Text value_g = doc.createTextNode(Base64.encode(_g.toByteArray()));
            g.appendChild(value_g);
            DSAKeyValue.appendChild(g);
            Element y = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, "Y");
            Text value_y = doc.createTextNode(Base64.encode(_y.toByteArray()));
            y.appendChild(value_y);
            DSAKeyValue.appendChild(y);
            keyvalue.appendChild(DSAKeyValue);
        } else {
            // It is RSA
            RSAPublicKey rsakey = (RSAPublicKey) pk;
            BigInteger exponent = rsakey.getPublicExponent();
            BigInteger modulus = rsakey.getModulus();
            Element RSAKeyValue = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, "RSAKeyValue");
            Element modulusNode = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, "Modulus");
            Element exponentNode = doc.createElementNS(SAMLConstants.XMLSIG_NAMESPACE_URI, "Exponent");
            RSAKeyValue.appendChild(modulusNode);
            RSAKeyValue.appendChild(exponentNode);
            Text modulusValue = doc.createTextNode(Base64.encode(modulus.toByteArray()));
            modulusNode.appendChild(modulusValue);
            Text exponentValue = doc.createTextNode(Base64.encode(exponent.toByteArray()));
            exponentNode.appendChild(exponentValue);
            keyvalue.appendChild(RSAKeyValue);
        }
        keyInfo.appendChild(keyName).appendChild(keyNameText);
        keyInfo.appendChild(keyvalue);
    }
    return keyInfo;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) Element(org.w3c.dom.Element) BigInteger(java.math.BigInteger) Text(org.w3c.dom.Text) DSAParams(java.security.interfaces.DSAParams) Document(org.w3c.dom.Document) X509Certificate(java.security.cert.X509Certificate) SessionException(com.sun.identity.plugin.session.SessionException) SAMLException(com.sun.identity.saml.common.SAMLException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 23 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project OpenAM by OpenRock.

the class OpenAMOAuth2ProviderSettings method getJWKSet.

public JsonValue getJWKSet() throws ServerException {
    synchronized (jwks) {
        if (jwks.isEmpty()) {
            PublicKey key = getServerKeyPair().getPublic();
            jwks.add(createRSAJWK((RSAPublicKey) key, KeyUse.SIG, JwsAlgorithm.RS256.name()));
        }
    }
    return new JsonValue(Collections.singletonMap("keys", jwks));
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) JsonValue(org.forgerock.json.JsonValue)

Example 24 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project OpenAM by OpenRock.

the class PEMDecoder method decodeRSAPublicKey.

/**
     * Decodes a PEM encoded Public Key.
     *
     * @param encodedKey The Base64 encoded public key bytes.
     * @return The decoded Public Key.
     * @throws NoSuchAlgorithmException If the key cannot be decoded.
     * @throws InvalidKeySpecException If the key cannot be decoded.
     */
public RSAPublicKey decodeRSAPublicKey(String encodedKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    if (encodedKey == null) {
        return null;
    }
    encodedKey = encodedKey.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").trim();
    byte[] decodedKey = Base64.decode(encodedKey);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
    return (RSAPublicKey) keyFactory.generatePublic(keySpec);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 25 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project android_frameworks_base by ResurrectionRemix.

the class AndroidKeyStoreProvider method getAndroidKeyStorePublicKey.

@NonNull
public static AndroidKeyStorePublicKey getAndroidKeyStorePublicKey(@NonNull String alias, int uid, @NonNull @KeyProperties.KeyAlgorithmEnum String keyAlgorithm, @NonNull byte[] x509EncodedForm) {
    PublicKey publicKey;
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(x509EncodedForm));
    } catch (NoSuchAlgorithmException e) {
        throw new ProviderException("Failed to obtain " + keyAlgorithm + " KeyFactory", e);
    } catch (InvalidKeySpecException e) {
        throw new ProviderException("Invalid X.509 encoding of public key", e);
    }
    if (KeyProperties.KEY_ALGORITHM_EC.equalsIgnoreCase(keyAlgorithm)) {
        return new AndroidKeyStoreECPublicKey(alias, uid, (ECPublicKey) publicKey);
    } else if (KeyProperties.KEY_ALGORITHM_RSA.equalsIgnoreCase(keyAlgorithm)) {
        return new AndroidKeyStoreRSAPublicKey(alias, uid, (RSAPublicKey) publicKey);
    } else {
        throw new ProviderException("Unsupported Android Keystore public key algorithm: " + keyAlgorithm);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ProviderException(java.security.ProviderException) NoSuchProviderException(java.security.NoSuchProviderException) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) NonNull(android.annotation.NonNull)

Aggregations

RSAPublicKey (java.security.interfaces.RSAPublicKey)83 PublicKey (java.security.PublicKey)29 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)24 BigInteger (java.math.BigInteger)17 KeyFactory (java.security.KeyFactory)17 X509Certificate (java.security.cert.X509Certificate)16 KeyPair (java.security.KeyPair)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 PrivateKey (java.security.PrivateKey)14 ECPublicKey (java.security.interfaces.ECPublicKey)14 IOException (java.io.IOException)13 InvalidKeyException (java.security.InvalidKeyException)13 KeyPairGenerator (java.security.KeyPairGenerator)13 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)13 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 CertificateFactory (java.security.cert.CertificateFactory)9 RSAKey (java.security.interfaces.RSAKey)8 DSAPublicKey (java.security.interfaces.DSAPublicKey)7