Search in sources :

Example 36 with DSAParams

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

the class SHA1withDSA_SignatureImpl method engineInitVerify.

/**
     * Initializes this signature object with PublicKey object
     * passed as argument to the method.
     *
     * @params
     *    publicKey DSAPublicKey object
     * @throws
     *    InvalidKeyException if publicKey is not DSAPublicKey object
     */
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    // parameters and public key
    BigInteger p, q, y;
    int n1;
    if (publicKey == null || !(publicKey instanceof DSAPublicKey)) {
        throw new InvalidKeyException("publicKey is not an instance of DSAPublicKey");
    }
    DSAParams params = ((DSAPublicKey) publicKey).getParams();
    p = params.getP();
    q = params.getQ();
    y = ((DSAPublicKey) publicKey).getY();
    // checks described in DSA standard
    n1 = p.bitLength();
    if (p.compareTo(BigInteger.valueOf(1)) != 1 || n1 < 512 || n1 > 1024 || (n1 & 077) != 0) {
        throw new InvalidKeyException("bad p");
    }
    if (q.signum() != 1 || q.bitLength() != 160) {
        throw new InvalidKeyException("bad q");
    }
    if (y.signum() != 1) {
        throw new InvalidKeyException("y <= 0");
    }
    dsaKey = (DSAKey) publicKey;
    msgDigest.reset();
}
Also used : BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 37 with DSAParams

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

the class SHA1withDSA_SignatureImpl method engineInitSign.

/**
     * Initializes this signature object with PrivateKey object
     * passed as argument to the method.
     *
     * @params
     *    privateKey DSAPrivateKey object
     * @throws
     *    InvalidKeyException if privateKey is not DSAPrivateKey object
     */
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    DSAParams params;
    // parameters and private key
    BigInteger p, q, x;
    int n;
    if (privateKey == null || !(privateKey instanceof DSAPrivateKey)) {
        throw new InvalidKeyException();
    }
    params = ((DSAPrivateKey) privateKey).getParams();
    p = params.getP();
    q = params.getQ();
    x = ((DSAPrivateKey) privateKey).getX();
    // checks described in DSA standard
    n = p.bitLength();
    if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) {
        throw new InvalidKeyException("bad p");
    }
    if (q.signum() != 1 && q.bitLength() != 160) {
        throw new InvalidKeyException("bad q");
    }
    if (x.signum() != 1 || x.compareTo(q) != -1) {
        throw new InvalidKeyException("x <= 0 || x >= q");
    }
    dsaKey = (DSAKey) privateKey;
    msgDigest.reset();
}
Also used : BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAParams(java.security.interfaces.DSAParams) InvalidKeyException(java.security.InvalidKeyException)

Example 38 with DSAParams

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

the class OpenSSLSignature method engineInitVerify.

@Override
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    if (publicKey instanceof DSAPublicKey) {
        try {
            DSAPublicKey dsaPublicKey = (DSAPublicKey) publicKey;
            DSAParams dsaParams = dsaPublicKey.getParams();
            dsa = NativeCrypto.EVP_PKEY_new_DSA(dsaParams.getP().toByteArray(), dsaParams.getQ().toByteArray(), dsaParams.getG().toByteArray(), dsaPublicKey.getY().toByteArray(), null);
        } catch (Exception e) {
            throw new InvalidKeyException(e);
        }
    } else if (publicKey instanceof RSAPublicKey) {
        try {
            RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
            rsa = NativeCrypto.EVP_PKEY_new_RSA(rsaPublicKey.getModulus().toByteArray(), rsaPublicKey.getPublicExponent().toByteArray(), null, null, null);
        } catch (Exception e) {
            throw new InvalidKeyException(e);
        }
    } else {
        throw new InvalidKeyException("Need DSA or RSA public key");
    }
    try {
        ctx = NativeCrypto.EVP_VerifyInit(evpAlgorithm);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) DSAParams(java.security.interfaces.DSAParams) InvalidKeyException(java.security.InvalidKeyException) InvalidParameterException(java.security.InvalidParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 39 with DSAParams

use of java.security.interfaces.DSAParams in project jdk8u_jdk by JetBrains.

the class AlgorithmChecker method check.

@Override
public void check(Certificate cert, Collection<String> unresolvedCritExts) throws CertPathValidatorException {
    if (!(cert instanceof X509Certificate) || constraints == null) {
        // ignore the check for non-x.509 certificate or null constraints
        return;
    }
    // check the key usage and key size
    boolean[] keyUsage = ((X509Certificate) cert).getKeyUsage();
    if (keyUsage != null && keyUsage.length < 9) {
        throw new CertPathValidatorException("incorrect KeyUsage extension", null, null, -1, PKIXReason.INVALID_KEY_USAGE);
    }
    X509CertImpl x509Cert;
    AlgorithmId algorithmId;
    try {
        x509Cert = X509CertImpl.toImpl((X509Certificate) cert);
        algorithmId = (AlgorithmId) x509Cert.get(X509CertImpl.SIG_ALG);
    } catch (CertificateException ce) {
        throw new CertPathValidatorException(ce);
    }
    AlgorithmParameters currSigAlgParams = algorithmId.getParameters();
    PublicKey currPubKey = cert.getPublicKey();
    String currSigAlg = ((X509Certificate) cert).getSigAlgName();
    // Check the signature algorithm and parameters against constraints.
    if (!constraints.permits(SIGNATURE_PRIMITIVE_SET, currSigAlg, currSigAlgParams)) {
        throw new CertPathValidatorException("Algorithm constraints check failed on signature " + "algorithm: " + currSigAlg, null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
    }
    // Assume all key usage bits are set if key usage is not present
    Set<CryptoPrimitive> primitives = KU_PRIMITIVE_SET;
    if (keyUsage != null) {
        primitives = EnumSet.noneOf(CryptoPrimitive.class);
        if (keyUsage[0] || keyUsage[1] || keyUsage[5] || keyUsage[6]) {
            // keyUsage[0]: KeyUsage.digitalSignature
            // keyUsage[1]: KeyUsage.nonRepudiation
            // keyUsage[5]: KeyUsage.keyCertSign
            // keyUsage[6]: KeyUsage.cRLSign
            primitives.add(CryptoPrimitive.SIGNATURE);
        }
        if (keyUsage[2]) {
            // KeyUsage.keyEncipherment
            primitives.add(CryptoPrimitive.KEY_ENCAPSULATION);
        }
        if (keyUsage[3]) {
            // KeyUsage.dataEncipherment
            primitives.add(CryptoPrimitive.PUBLIC_KEY_ENCRYPTION);
        }
        if (keyUsage[4]) {
            // KeyUsage.keyAgreement
            primitives.add(CryptoPrimitive.KEY_AGREEMENT);
        }
        if (primitives.isEmpty()) {
            throw new CertPathValidatorException("incorrect KeyUsage extension bits", null, null, -1, PKIXReason.INVALID_KEY_USAGE);
        }
    }
    ConstraintsParameters cp = new ConstraintsParameters((X509Certificate) cert, trustedMatch, pkixdate, jarTimestamp, variant);
    // Check against local constraints if it is DisabledAlgorithmConstraints
    if (constraints instanceof DisabledAlgorithmConstraints) {
        ((DisabledAlgorithmConstraints) constraints).permits(currSigAlg, cp);
    // DisabledAlgorithmsConstraints does not check primitives, so key
    // additional key check.
    } else {
        // Perform the default constraints checking anyway.
        certPathDefaultConstraints.permits(currSigAlg, cp);
        // Call locally set constraints to check key with primitives.
        if (!constraints.permits(primitives, currPubKey)) {
            throw new CertPathValidatorException("Algorithm constraints check failed on key " + currPubKey.getAlgorithm() + " with size of " + sun.security.util.KeyUtil.getKeySize(currPubKey) + "bits", null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
        }
    }
    // If there is no previous key, set one and exit
    if (prevPubKey == null) {
        prevPubKey = currPubKey;
        return;
    }
    // Check with previous cert for signature algorithm and public key
    if (!constraints.permits(SIGNATURE_PRIMITIVE_SET, currSigAlg, prevPubKey, currSigAlgParams)) {
        throw new CertPathValidatorException("Algorithm constraints check failed on " + "signature algorithm: " + currSigAlg, null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
    }
    // Inherit key parameters from previous key
    if (PKIX.isDSAPublicKeyWithoutParams(currPubKey)) {
        // Inherit DSA parameters from previous key
        if (!(prevPubKey instanceof DSAPublicKey)) {
            throw new CertPathValidatorException("Input key is not " + "of a appropriate type for inheriting parameters");
        }
        DSAParams params = ((DSAPublicKey) prevPubKey).getParams();
        if (params == null) {
            throw new CertPathValidatorException("Key parameters missing from public key.");
        }
        try {
            BigInteger y = ((DSAPublicKey) currPubKey).getY();
            KeyFactory kf = KeyFactory.getInstance("DSA");
            DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
            currPubKey = kf.generatePublic(ks);
        } catch (GeneralSecurityException e) {
            throw new CertPathValidatorException("Unable to generate " + "key with inherited parameters: " + e.getMessage(), e);
        }
    }
    // reset the previous public key
    prevPubKey = currPubKey;
}
Also used : DisabledAlgorithmConstraints(sun.security.util.DisabledAlgorithmConstraints) CryptoPrimitive(java.security.CryptoPrimitive) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) GeneralSecurityException(java.security.GeneralSecurityException) CertificateException(java.security.cert.CertificateException) DSAParams(java.security.interfaces.DSAParams) ConstraintsParameters(sun.security.util.ConstraintsParameters) X509Certificate(java.security.cert.X509Certificate) DSAPublicKey(java.security.interfaces.DSAPublicKey) CertPathValidatorException(java.security.cert.CertPathValidatorException) AlgorithmId(sun.security.x509.AlgorithmId) X509CertImpl(sun.security.x509.X509CertImpl) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory) AlgorithmParameters(java.security.AlgorithmParameters) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 40 with DSAParams

use of java.security.interfaces.DSAParams in project jdk8u_jdk by JetBrains.

the class BasicChecker method makeInheritedParamsKey.

/**
     * Internal method to create a new key with inherited key parameters.
     *
     * @param keyValueKey key from which to obtain key value
     * @param keyParamsKey key from which to obtain key parameters
     * @return new public key having value and parameters
     * @throws CertPathValidatorException if keys are not appropriate types
     * for this operation
     */
static PublicKey makeInheritedParamsKey(PublicKey keyValueKey, PublicKey keyParamsKey) throws CertPathValidatorException {
    if (!(keyValueKey instanceof DSAPublicKey) || !(keyParamsKey instanceof DSAPublicKey))
        throw new CertPathValidatorException("Input key is not " + "appropriate type for " + "inheriting parameters");
    DSAParams params = ((DSAPublicKey) keyParamsKey).getParams();
    if (params == null)
        throw new CertPathValidatorException("Key parameters missing");
    try {
        BigInteger y = ((DSAPublicKey) keyValueKey).getY();
        KeyFactory kf = KeyFactory.getInstance("DSA");
        DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
        return kf.generatePublic(ks);
    } catch (GeneralSecurityException e) {
        throw new CertPathValidatorException("Unable to generate key with" + " inherited parameters: " + e.getMessage(), e);
    }
}
Also used : CertPathValidatorException(java.security.cert.CertPathValidatorException) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) KeyFactory(java.security.KeyFactory) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAParams (java.security.interfaces.DSAParams)40 DSAPublicKey (java.security.interfaces.DSAPublicKey)19 BigInteger (java.math.BigInteger)16 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)13 DSAParameterSpec (java.security.spec.DSAParameterSpec)11 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)11 InvalidKeyException (java.security.InvalidKeyException)8 PublicKey (java.security.PublicKey)7 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 KeyPairGenerator (java.security.KeyPairGenerator)6 SecureRandom (java.security.SecureRandom)5 X509Certificate (java.security.cert.X509Certificate)5 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 GeneralSecurityException (java.security.GeneralSecurityException)4 KeyFactory (java.security.KeyFactory)4 KeyPair (java.security.KeyPair)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)4 IOException (java.io.IOException)3