Search in sources :

Example 56 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project jruby-openssl by jruby.

the class PKCS10Request method generatePublicKey.

public PublicKey generatePublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    AsymmetricKeyParameter keyParams = PublicKeyFactory.createKey(publicKeyInfo);
    final KeySpec keySpec;
    final KeyFactory keyFactory;
    if (keyParams instanceof RSAKeyParameters) {
        RSAKeyParameters rsa = (RSAKeyParameters) keyParams;
        keySpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
        keyFactory = SecurityHelper.getKeyFactory("RSA");
        return keyFactory.generatePublic(keySpec);
    } else if (keyParams instanceof DSAPublicKeyParameters) {
        DSAPublicKeyParameters dsa = (DSAPublicKeyParameters) keyParams;
        DSAParameters params = dsa.getParameters();
        keySpec = new DSAPublicKeySpec(dsa.getY(), params.getP(), params.getQ(), params.getG());
        keyFactory = SecurityHelper.getKeyFactory("DSA");
        return keyFactory.generatePublic(keySpec);
    } else if (keyParams instanceof ECPublicKeyParameters) {
        ECPublicKeyParameters ec = (ECPublicKeyParameters) keyParams;
        ECDomainParameters ecParams = ec.getParameters();
        ECParameterSpec params = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN(), ecParams.getH(), ecParams.getSeed());
        // NOTE: likely to fail if non BC factory picked up :
        keySpec = new ECPublicKeySpec(ec.getQ(), params);
        keyFactory = SecurityHelper.getKeyFactory("EC");
        return keyFactory.generatePublic(keySpec);
    } else {
        throw new IllegalStateException("could not generate public key for request, params type: " + keyParams);
    }
}
Also used : DSAPublicKeyParameters(org.bouncycastle.crypto.params.DSAPublicKeyParameters) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) RSAKeyParameters(org.bouncycastle.crypto.params.RSAKeyParameters) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) PublicKeyFactory(org.bouncycastle.crypto.util.PublicKeyFactory) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 57 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project jruby-openssl by jruby.

the class PKey method readDSAPrivateKey.

public static KeyPair readDSAPrivateKey(final KeyFactory dsaFactory, final byte[] input) throws IOException, InvalidKeySpecException {
    ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
    if (seq.size() == 6) {
        BigInteger p = ((ASN1Integer) seq.getObjectAt(1)).getValue();
        BigInteger q = ((ASN1Integer) seq.getObjectAt(2)).getValue();
        BigInteger g = ((ASN1Integer) seq.getObjectAt(3)).getValue();
        BigInteger y = ((ASN1Integer) seq.getObjectAt(4)).getValue();
        BigInteger x = ((ASN1Integer) seq.getObjectAt(5)).getValue();
        PrivateKey priv = dsaFactory.generatePrivate(new DSAPrivateKeySpec(x, p, q, g));
        PublicKey pub = dsaFactory.generatePublic(new DSAPublicKeySpec(y, p, q, g));
        return new KeyPair(pub, priv);
    }
    return null;
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) KeyPair(java.security.KeyPair) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) BigInteger(java.math.BigInteger) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 58 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project jruby-openssl by jruby.

the class PKey method readDSAPublicKey.

public static PublicKey readDSAPublicKey(final KeyFactory dsaFactory, final byte[] input) throws IOException, InvalidKeySpecException {
    ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
    if (seq.size() == 4) {
        BigInteger y = ((ASN1Integer) seq.getObjectAt(0)).getValue();
        BigInteger p = ((ASN1Integer) seq.getObjectAt(1)).getValue();
        BigInteger q = ((ASN1Integer) seq.getObjectAt(2)).getValue();
        BigInteger g = ((ASN1Integer) seq.getObjectAt(3)).getValue();
        return dsaFactory.generatePublic(new DSAPublicKeySpec(y, p, q, g));
    }
    return null;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) BigInteger(java.math.BigInteger) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 59 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project keycloak by keycloak.

the class DSAKeyValueType method convertToPublicKey.

/**
 * Convert to the JDK representation of a DSA Public Key
 *
 * @return
 *
 * @throws org.keycloak.saml.common.exceptions.ProcessingException
 */
public DSAPublicKey convertToPublicKey() throws ProcessingException {
    try {
        BigInteger BigY = new BigInteger(1, massage(Base64.decode(new String(y))));
        BigInteger BigP = new BigInteger(1, massage(Base64.decode(new String(p))));
        BigInteger BigQ = new BigInteger(1, massage(Base64.decode(new String(q))));
        BigInteger BigG = new BigInteger(1, massage(Base64.decode(new String(g))));
        KeyFactory dsaKeyFactory = KeyFactory.getInstance("dsa");
        DSAPublicKeySpec kspec = new DSAPublicKeySpec(BigY, BigP, BigQ, BigG);
        return (DSAPublicKey) dsaKeyFactory.generatePublic(kspec);
    } catch (Exception e) {
        throw new ProcessingException(e);
    }
}
Also used : BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) DSAPublicKey(java.security.interfaces.DSAPublicKey) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException)

Example 60 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project karaf by apache.

the class PublicKeyEncodingTest method testDSA.

@Test
public void testDSA() throws FailedLoginException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Generated using: ssh-keygen -t dsa
    String storedKey = "AAAAB3NzaC1kc3MAAACBAJlAn/bPWpugKCLyoQpe8AbSZiIxdEJhl+VV8YEH6jfb9lLPA9JkQAf/lnG1Jx01UM65RRyKtnMAiB" + "pkhrPy3DbqJ4FgYBmc1Sdiufomilq6zSbE0esJEMyxEvSNDQLqIiUcSwVyJJj1vpV6ZPA6ihipTIaiSV+rmfKcS05i27UlAAAAFQCg3ZtIytPmG" + "ILQ7OEifIJvCSlS5QAAAIBUbgpjk7vSWVNICgKG6OrXeK0kJYRG6AaUZSiB2neoABMyGIHQ8dBCk+jtYqRMYyoc+OPi5q43VcDMxgzR/cHGjZi6" + "0w/I3M83072dAdaoi0cleL/V8NaH+SOvkkYkAG57OIa3ly9PVpPfeXRnbbjkz1EsrvXIelqb5enLhlIgXgAAAIA11rUkN/J3K7nw/BiolhpZR3M" + "VhWWIJFjJyU7ZC0yO8a+3AExuhTI6YQvsyvlY69KCwAwZsZvx9DryDE5xTfhzYa5kV4mM4AJSrE8/GtxLUVPZLwV6eoZLv1RIqP543ihZtoFyVm" + "MaTQFj45Qo8uAuVDjx5mpk/Rk1pYPUd0lc1Q==";
    String p = "1076175237625726563105954460741409330556298182412863930703571469202992312952487088821612089126846931217220" + "139938550642040962241586994856559462488140821681403960733982209827487135132210000913512532065787125116985685638" + "40437219296134522589816052156357553531846010339651017908589163855315552516201352809575855397";
    String q = "918380515194943729419256231914804453973955269349";
    String g = "5928865413019314795162062081939159959737363875586187627523617102819491716184351195073908492559564825805562" + "104476892066919492044841627907376461274343797017375757242038772707578284292374846844427026690399002493750530347" + "2378225083646830569532678306021077676137269211638266431262139218141967811197461432032698462";
    String y = "3780682190459260799543888842390974417268312111951424991203659597814001671832656608276823896973755971735795" + "130565245682634187551545737028902938478313465290457154458005480679650487421678748598551351730312164280338152996" + "0448119336850459047721615478019482431582683540283279032651976075781966545889409150149549269";
    // Generate a PublicKey using the known values
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec publicKeySpec = new DSAPublicKeySpec(new BigInteger(y), new BigInteger(p), new BigInteger(q), new BigInteger(g));
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
    assertTrue(PublickeyLoginModule.equals(publicKey, storedKey));
    // Make sure a different stored key does not work
    String differentKey = "AAAAB3NzaC1kc3MAAACBALE+qmsDN5lJYqQUmtrM7RI4vFcAQnla7Jp8Qy5ZUf63IFEA+tDzareKZOadwYbHOrIq3bDFCMH" + "HIVvFWJNhcJUBH8ZZnk7942Sxg6P5B3OQlCC6O4ADpe6NmwsxCpjpkyJizWTwvTspE6vV32VMa70UJlL1OtymgsWDef8ZQKqBAAAAFQCwiMFuOv" + "t6AZ1PgOwytbS1ra/FswAAAIAvf9b+K6eF6Mx3CnUVMHVldK4VybXjn/GwARH7BG8HJ8aGmMLvhk2qKGN5NatxgAc6IzRcwFbKvtniTTh06seuY" + "CwIvHs+7nldZ255D23as90jAqstkBGt5NmX5R/TgHQPwQILJpydaYUEf6f/KU6MZPANo8cbEi2hxgljWCQcwAAAAIEAh2S+0V+64AZy8+T03eMX" + "yBmt4xn8JPJzIHizF4VeUpTVwyA2EsiG9/YEWEGATj7mAcfAmLKl5rV1tQdXgUl2uxCDXw91c9PrYbfrHJjD1Oj6xHOjExDZI31Z8S6OKwo7df7" + "0GumGSDsg0nibs5rEwkkcT64AOMn1o4JvabsP200=";
    assertFalse(PublickeyLoginModule.equals(publicKey, differentKey));
}
Also used : PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) Test(org.junit.Test)

Aggregations

DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)63 BigInteger (java.math.BigInteger)45 KeyFactory (java.security.KeyFactory)37 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)23 PublicKey (java.security.PublicKey)22 DSAPublicKey (java.security.interfaces.DSAPublicKey)21 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)19 KeySpec (java.security.spec.KeySpec)19 DSAParams (java.security.interfaces.DSAParams)17 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)17 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 PrivateKey (java.security.PrivateKey)10 GeneralSecurityException (java.security.GeneralSecurityException)9 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)9 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)9 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)9 IOException (java.io.IOException)7 CertPathValidatorException (java.security.cert.CertPathValidatorException)7 InvalidKeyException (java.security.InvalidKeyException)5 KeyPair (java.security.KeyPair)5