Search in sources :

Example 66 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project BiglyBT by BiglySoftware.

the class PEMWriter method writeObject.

public void writeObject(Object o) throws IOException {
    String type;
    byte[] encoding;
    if (o instanceof X509Certificate) {
        type = "CERTIFICATE";
        try {
            encoding = ((X509Certificate) o).getEncoded();
        } catch (CertificateEncodingException e) {
            throw new IOException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof X509CRL) {
        type = "X509 CRL";
        try {
            encoding = ((X509CRL) o).getEncoded();
        } catch (CRLException e) {
            throw new IOException("Cannot encode object: " + e.toString());
        }
    } else if (o instanceof KeyPair) {
        writeObject(((KeyPair) o).getPrivate());
        return;
    } else if (o instanceof PrivateKey) {
        PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) ASN1Object.fromByteArray(((Key) o).getEncoded()));
        if (o instanceof RSAPrivateKey) {
            type = "RSA PRIVATE KEY";
            encoding = info.getPrivateKey().getEncoded();
        } else if (o instanceof DSAPrivateKey) {
            type = "DSA PRIVATE KEY";
            DSAParameter p = DSAParameter.getInstance(info.getAlgorithmId().getParameters());
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(new DERInteger(0));
            v.add(new DERInteger(p.getP()));
            v.add(new DERInteger(p.getQ()));
            v.add(new DERInteger(p.getG()));
            BigInteger x = ((DSAPrivateKey) o).getX();
            BigInteger y = p.getG().modPow(x, p.getP());
            v.add(new DERInteger(y));
            v.add(new DERInteger(x));
            encoding = new DERSequence(v).getEncoded();
        } else {
            throw new IOException("Cannot identify private key");
        }
    } else if (o instanceof PublicKey) {
        type = "PUBLIC KEY";
        encoding = ((PublicKey) o).getEncoded();
    } else if (o instanceof X509AttributeCertificate) {
        type = "ATTRIBUTE CERTIFICATE";
        encoding = ((X509V2AttributeCertificate) o).getEncoded();
    } else if (o instanceof PKCS10CertificationRequest) {
        type = "CERTIFICATE REQUEST";
        encoding = ((PKCS10CertificationRequest) o).getEncoded();
    } else if (o instanceof ContentInfo) {
        type = "PKCS7";
        encoding = ((ContentInfo) o).getEncoded();
    } else {
        throw new IOException("unknown object passed - can't encode.");
    }
    writeHeader(type);
    writeEncoded(encoding);
    writeFooter(type);
}
Also used : PKCS10CertificationRequest(org.gudy.bouncycastle.jce.PKCS10CertificationRequest) X509CRL(java.security.cert.X509CRL) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) X509AttributeCertificate(org.gudy.bouncycastle.x509.X509AttributeCertificate) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509V2AttributeCertificate(org.gudy.bouncycastle.x509.X509V2AttributeCertificate) X509Certificate(java.security.cert.X509Certificate) ContentInfo(org.gudy.bouncycastle.asn1.cms.ContentInfo) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) DSAParameter(org.gudy.bouncycastle.asn1.x509.DSAParameter) CRLException(java.security.cert.CRLException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKeyInfo(org.gudy.bouncycastle.asn1.pkcs.PrivateKeyInfo) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey)

Example 67 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project churchkey by tomitribe.

the class OpenSSHPrivateKeyTest method dsa.

@Test
public void dsa() throws Exception {
    final Decoder decoder = Keys::decode;
    final Resource resource = Resource.resource("opensshdsa");
    final byte[] bytes = resource.bytes("private.openssh");
    final Key key = decoder.decode(bytes);
    assertEquals(Key.Algorithm.DSA, key.getAlgorithm());
    assertEquals(Key.Format.OPENSSH, key.getFormat());
    assertEquals(Key.Type.PRIVATE, key.getType());
    final DSAPrivateKey expected = (DSAPrivateKey) key.getKey();
    final byte[] encode = key.encode(Key.Format.OPENSSH);
    final Key key2 = Keys.decode(encode);
    final DSAPrivateKey actual = (DSAPrivateKey) key2.getKey();
    KeyAsserts.assertDsaPrivateKey(expected, actual);
}
Also used : Resource(io.churchkey.Resource) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Decoder(io.churchkey.Decoder) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Test(org.junit.Test)

Example 68 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project churchkey by tomitribe.

the class BeginPrivateKeyDsaTest method opensslDSAPrivateKey.

@Test
public void opensslDSAPrivateKey() throws Exception {
    final Resource resources = Resource.resource(this.getClass().getSimpleName());
    final byte[] bytes = resources.bytes("openssl-dsaprivatekey-3072.pem");
    final Key key = Keys.decode(bytes);
    final DSAPrivateKey privateKey = (DSAPrivateKey) key.getKey();
    assertBigInteger("x", privateKey.getX(), "" + "7D44BBD37D39F551F52841C332CD68E3E6EE281CEF6CA34B59D9B7CF2CA11800");
    assertBigInteger("p", privateKey.getParams().getP(), "" + "8C30CA86F8D1BE736583B28925716BCDFAD596860A678CE1498E8AC8564CD2CC" + "E5FC714F3BBC78404668E5CB3A36D1EC70914E20A5F79274225AB32E514CC14A" + "3D35B524ED6CA5FB6E5A540F637BDB66EE77C1E57E17678F47BB87471D4C2426" + "9BC6CFDB3CA947752F7A9D033F83F0D46521250E9524AAB6A98B6BF667B554B2" + "27385CCDEE3EA239ADC886A181FA04BA89CB5C01DBF28F33F76092CB204905F6" + "026B6E6AAAC2927AEB40CF5DEC55B9878E5FAC6FA9CF2E48A81B2C83342AFD9F" + "C3FD7F3929B7AC5FD4BAA47D6B071014040C5A57AD0F370E2E3B1812A2122493" + "FED62052104A1FFD07B8E35510AEE0AAC0E12C9CACA7FD476626CB8B05CB39AD" + "70E8F1C05310E56FBEE58638BE75988246F9E827F235C930B2233CB98134751B" + "224F4AAA6F28A0FB5B032AE22771470EBB77EC606609A594EE19374726FCE4AA" + "7C420CE50AD5068CA8FAA242A5EFC605B9C08E6D509CE4A4E6E0C180BBF5064C" + "FD9DB96D78F7A79A97F481C54FB82382B1FB6EF14CB35DB01D33748E537C03F3");
    assertBigInteger("q", privateKey.getParams().getQ(), "" + "F7BF79F6D58A8A90F16761755EAC0EB18C722208968004B2DDADA241263A0E99");
    assertBigInteger("g", privateKey.getParams().getG(), "" + "5E71E1126FAD52239C493188E5B6A7FFBC861E0FC33B7112983842A05DCFDEEA" + "8231F63A9DF7BC100885A55785A38F1196200D9FCC98BF2096034AF3132BDD2F" + "AD4FAA907076A4E267AB945392044D3EA13A0338F538C45D60035D97AD525478" + "58B5DA4DAEB2245921DC7179038FF2A556A303A06ADF1601780FF7D603E0F60A" + "A7EE4BAEDFB1C7D8CF183872F681C4F6FCEEF1D97C06746358B4EEB5C087AE57" + "2849BA76DCC15AE12D8FEF769E0583CDF5077B4BFBCA024ADC10A662B8C3B250" + "07EF3478E12FE1926A7883E5FEF25C0D2964B5F4D24863E6AC942FBA6BE2810A" + "7D41523048ACA39BABCC56F7B3097F94E04914E4C17786893C02D83EB8E90A7A" + "204A4C7021AAF5A53B3D48BFD33C1F441B7AB5B4B51450BC861D6B5E26DA73F0" + "8E65EC5FE9FA2A72348BAFDCD71C126D353341EE18820335DF2C19D01B9D1737" + "DEA95578D503D8C75184B777022CDD8D32328E85C70AEC21ABE71BFB75A5CFDD" + "77FC738CEA45C48A44B1A4E67394592AA955B4D8E09D81007BCBB91AD13D68DB");
}
Also used : Resource(io.churchkey.Resource) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) Test(org.junit.Test)

Example 69 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project churchkey by tomitribe.

the class BeginPrivateKeyDsaTest method javaDSAPrivateKey.

@Test
public void javaDSAPrivateKey() throws Exception {
    final Resource resources = Resource.resource(this.getClass().getSimpleName());
    final byte[] bytes = resources.bytes("java-dsaprivatekey-3072.pem");
    final Key key = Keys.decode(bytes);
    final DSAPrivateKey privateKey = (DSAPrivateKey) key.getKey();
    assertBigInteger("x", privateKey.getX(), "" + "4740514706213F462F244288A429E6F6BF9E5752B066C4881ADBEF2F23B06A42");
    assertBigInteger("p", privateKey.getParams().getP(), "" + "EA9CDA9F5FBDA66DD830494609405687AB7CF38538E058D1E2F68DEA95364866" + "E1C05BEACDED24227EDEE28CAD80BCECAD39913BE3B713267B3B96C8D9F0F6A0" + "3B5DFC9222D5CFE4AFCC9982F33784F760C3B759AEBE3BBE9098A6B84C96F1FD" + "E44CE11C084C2A082C7A76A0EF142928B4F328406AB9BEB24F84577DD0F46CE8" + "6FD8F08488269998BF4742D6425F7A0EC75D8660C5DD6F4E3B3D3BEE81B2C21A" + "FE8C9E8B84B87192E2CC20F961D2BCD8133AFCF3675AB80681CB374C78F33E29" + "D1011083D89F9C5728B94676FCCB1B57BC60288C15D85AE838AE1941C5A20AE2" + "B2049B3583FE30DA455DDB3E6AD9B9955CD9BB5681431622BEB0F92DA533FCAB" + "496CEBC447AA1BB5A8039522F2DA98FF416289323A64DF626AB6881870927DCE" + "E387F13B5C9D24D6CBA1D82ED375A082506EE87BC7AE30067F4A94E2EE363D99" + "2C40F2725B5DB4B3525EBDE22BBBFD0FA124A588B0F5A4ACB3A86951AFF09F8C" + "8198FB5B53DA0C931CEDC598B4F835B779D04D99026C7BA08C4B27F118AC1E3D");
    assertBigInteger("q", privateKey.getParams().getQ(), "" + "C4EEAC2BBAB79BD831946D717A56A6E687547AA8E9C5494A5A4B2F4CA13D6C11");
    assertBigInteger("g", privateKey.getParams().getG(), "" + "42E5FA7844F8FA9D8998D830D004E7B15B1D276BCBE5F12C35EC90C1A25F5832" + "018A6724BD9CDBE803B675509BED167F3D7CF8599FC865C6D5A0F79158C1BC91" + "8F00A944D0AD0F38F520FB91D85D82674D0D5F874FAA5FCDFE56CD178C1AFDC7" + "CE8795727B7DEE966ED0B3C5CEDCEF8ACA628BEFEBF2D105C7AFF8EB0DA9C961" + "0737DD64DCE1237B82C1B2BC8608D55FFDA98D7189444E65883315669C05716B" + "DE36C78B130AA3DF2E4D609914C7C8DC470F4E300187C775F81E7B1A9C0DCE40" + "5D6EAB2CBB9D9C4EF44412BA573DD403C4ED7BC2364772F56A30C48DE78F5003" + "F9371C55262D2C8AC2246ADE3B02FDCFCF5CBFDE74FBCBFE6E0E0FDF3160764F" + "84D311C179A40AF679A8F47AB13C8F706893245EB11EDCCE451FA2AB98001998" + "7F125D8DC96622D419BA0D71F16C6024DCE9D364C3B26D8EC1A3C828F6C9D14B" + "1D0333B95DB77BFDBE3C6BCE5337A1A5A7ACE10111219448447197E2A344CC42" + "3BE768BB89E27BE6CBD22085614A5A3360BE23B1BFBB6E6E6471363D32C85D31");
}
Also used : Resource(io.churchkey.Resource) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) Test(org.junit.Test)

Example 70 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project churchkey by tomitribe.

the class KeyPairTest method dsa.

@Test
@Skip({ "SSH2" })
public void dsa() throws Exception {
    final Key expected = generate(Key.Algorithm.DSA);
    final byte[] encoded = expected.encode(format);
    final Key actual = Keys.decode(encoded);
    Assert.assertEquals(Key.Type.PRIVATE, actual.getType());
    assertEquals(format, actual.getFormat());
    Assert.assertEquals(Key.Algorithm.DSA, actual.getAlgorithm());
    assertNotNull(actual.getPublicKey());
    Assert.assertEquals(Key.Type.PUBLIC, actual.getPublicKey().getType());
    assertEquals(format, actual.getPublicKey().getFormat());
    Assert.assertEquals(Key.Algorithm.DSA, actual.getPublicKey().getAlgorithm());
    KeyAsserts.assertDsaPrivateKey((DSAPrivateKey) expected.getKey(), (DSAPrivateKey) actual.getKey());
    KeyAsserts.assertDsaPublicKey((DSAPublicKey) expected.getPublicKey().getKey(), (DSAPublicKey) actual.getPublicKey().getKey());
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECPublicKey(java.security.interfaces.ECPublicKey) Test(org.junit.Test)

Aggregations

DSAPrivateKey (java.security.interfaces.DSAPrivateKey)86 BigInteger (java.math.BigInteger)35 DSAPublicKey (java.security.interfaces.DSAPublicKey)31 DSAParams (java.security.interfaces.DSAParams)26 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)25 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)25 ECPrivateKey (java.security.interfaces.ECPrivateKey)23 IOException (java.io.IOException)18 KeyPair (java.security.KeyPair)18 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)15 PrivateKey (java.security.PrivateKey)14 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)14 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)13 KeyFactory (java.security.KeyFactory)12 RSAPublicKey (java.security.interfaces.RSAPublicKey)12 PublicKey (java.security.PublicKey)11 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)10 InvalidKeyException (java.security.InvalidKeyException)9 KeyPairGenerator (java.security.KeyPairGenerator)9 ECPublicKey (java.security.interfaces.ECPublicKey)9