Search in sources :

Example 11 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey 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 12 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project XobotOS by xamarin.

the class DSAKeyFactoryImpl method engineTranslateKey.

/**
     * The method generates a DSAPublicKey object from the provided key.
     *
     * @param
     *    key - a DSAPublicKey object or DSAPrivateKey object.
     *
     * @return
     *    object of the same type as the "key" argument
     *
     * @throws InvalidKeyException
     *     if "key" is neither DSAPublicKey nor DSAPrivateKey
     */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (key != null) {
        if (key instanceof DSAPrivateKey) {
            DSAPrivateKey privateKey = (DSAPrivateKey) key;
            DSAParams params = privateKey.getParams();
            try {
                return engineGeneratePrivate(new DSAPrivateKeySpec(privateKey.getX(), params.getP(), params.getQ(), params.getG()));
            } catch (InvalidKeySpecException e) {
                // Actually this exception shouldn't be thrown
                throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
            }
        }
        if (key instanceof DSAPublicKey) {
            DSAPublicKey publicKey = (DSAPublicKey) key;
            DSAParams params = publicKey.getParams();
            try {
                return engineGeneratePublic(new DSAPublicKeySpec(publicKey.getY(), params.getP(), params.getQ(), params.getG()));
            } catch (InvalidKeySpecException e) {
                // Actually this exception shouldn't be thrown
                throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
            }
        }
    }
    throw new InvalidKeyException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAParams(java.security.interfaces.DSAParams) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 13 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project XobotOS by xamarin.

the class CertPathValidatorUtilities method getNextWorkingKey.

/**
     * Return the next working key inheriting DSA parameters if necessary.
     * <p>
     * This methods inherits DSA parameters from the indexed certificate or
     * previous certificates in the certificate chain to the returned
     * <code>PublicKey</code>. The list is searched upwards, meaning the end
     * certificate is at position 0 and previous certificates are following.
     * </p>
     * <p>
     * If the indexed certificate does not contain a DSA key this method simply
     * returns the public key. If the DSA key already contains DSA parameters
     * the key is also only returned.
     * </p>
     * 
     * @param certs The certification path.
     * @param index The index of the certificate which contains the public key
     *            which should be extended with DSA parameters.
     * @return The public key of the certificate in list position
     *         <code>index</code> extended with DSA parameters if applicable.
     * @throws AnnotatedException if DSA parameters cannot be inherited.
     */
protected static PublicKey getNextWorkingKey(List certs, int index) throws CertPathValidatorException {
    Certificate cert = (Certificate) certs.get(index);
    PublicKey pubKey = cert.getPublicKey();
    if (!(pubKey instanceof DSAPublicKey)) {
        return pubKey;
    }
    DSAPublicKey dsaPubKey = (DSAPublicKey) pubKey;
    if (dsaPubKey.getParams() != null) {
        return dsaPubKey;
    }
    for (int i = index + 1; i < certs.size(); i++) {
        X509Certificate parentCert = (X509Certificate) certs.get(i);
        pubKey = parentCert.getPublicKey();
        if (!(pubKey instanceof DSAPublicKey)) {
            throw new CertPathValidatorException("DSA parameters cannot be inherited from previous certificate.");
        }
        DSAPublicKey prevDSAPubKey = (DSAPublicKey) pubKey;
        if (prevDSAPubKey.getParams() == null) {
            continue;
        }
        DSAParams dsaParams = prevDSAPubKey.getParams();
        DSAPublicKeySpec dsaPubKeySpec = new DSAPublicKeySpec(dsaPubKey.getY(), dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
            return keyFactory.generatePublic(dsaPubKeySpec);
        } catch (Exception exception) {
            throw new RuntimeException(exception.getMessage());
        }
    }
    throw new CertPathValidatorException("DSA parameters cannot be inherited from previous certificate.");
}
Also used : CertPathValidatorException(java.security.cert.CertPathValidatorException) ExtCertPathValidatorException(org.bouncycastle.jce.exception.ExtCertPathValidatorException) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAParams(java.security.interfaces.DSAParams) CRLDistPoint(org.bouncycastle.asn1.x509.CRLDistPoint) DistributionPoint(org.bouncycastle.asn1.x509.DistributionPoint) X509Certificate(java.security.cert.X509Certificate) KeyFactory(java.security.KeyFactory) GeneralSecurityException(java.security.GeneralSecurityException) CertPathValidatorException(java.security.cert.CertPathValidatorException) ParseException(java.text.ParseException) ExtCertPathValidatorException(org.bouncycastle.jce.exception.ExtCertPathValidatorException) CertStoreException(java.security.cert.CertStoreException) CertificateParsingException(java.security.cert.CertificateParsingException) StoreException(org.bouncycastle.util.StoreException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) X509AttributeCertificate(org.bouncycastle.x509.X509AttributeCertificate) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 14 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey 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 15 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project karaf by apache.

the class PublickeyLoginModule method getString.

private String getString(PublicKey key) throws FailedLoginException {
    try {
        if (key instanceof DSAPublicKey) {
            DSAPublicKey dsa = (DSAPublicKey) key;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            write(dos, "ssh-dss");
            write(dos, dsa.getParams().getP());
            write(dos, dsa.getParams().getQ());
            write(dos, dsa.getParams().getG());
            write(dos, dsa.getY());
            dos.close();
            return base64Encode(baos.toByteArray());
        } else if (key instanceof RSAKey) {
            RSAPublicKey rsa = (RSAPublicKey) key;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            write(dos, "ssh-rsa");
            write(dos, rsa.getPublicExponent());
            write(dos, rsa.getModulus());
            dos.close();
            return base64Encode(baos.toByteArray());
        } else {
            throw new FailedLoginException("Unsupported key type " + key.getClass().toString());
        }
    } catch (IOException e) {
        throw new FailedLoginException("Unable to check public key");
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) FailedLoginException(javax.security.auth.login.FailedLoginException) RSAPublicKey(java.security.interfaces.RSAPublicKey) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Aggregations

DSAPublicKey (java.security.interfaces.DSAPublicKey)31 DSAParams (java.security.interfaces.DSAParams)19 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)11 PublicKey (java.security.PublicKey)10 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)10 BigInteger (java.math.BigInteger)9 RSAPublicKey (java.security.interfaces.RSAPublicKey)8 InvalidKeyException (java.security.InvalidKeyException)7 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)6 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)6 IOException (java.io.IOException)5 GeneralSecurityException (java.security.GeneralSecurityException)5 KeyPairGenerator (java.security.KeyPairGenerator)5 X509Certificate (java.security.cert.X509Certificate)5 KeyFactory (java.security.KeyFactory)4 KeyPair (java.security.KeyPair)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)4 DSAParameterSpec (java.security.spec.DSAParameterSpec)4 CertificateException (java.security.cert.CertificateException)3 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)3