Search in sources :

Example 61 with DSAParams

use of java.security.interfaces.DSAParams in project keystore-explorer by kaikramer.

the class MsPvkUtil method dsaPrivateKeyToBlob.

private static byte[] dsaPrivateKeyToBlob(DSAPrivateKey dsaPrivKey) throws CryptoException {
    try {
        DSAParams dsaParams = dsaPrivKey.getParams();
        // 328 sufficient for a 1024 bit DSA key
        ByteBuffer bb = ByteBuffer.wrap(new byte[512]);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        // Write out the blob fields
        // dsspubkey.magic
        UnsignedUtil.putInt(bb, DSS_PRIV_MAGIC);
        BigInteger prime = dsaParams.getP();
        int bitLength = prime.toString(2).length();
        // dsspubkey.bitlen
        UnsignedUtil.putInt(bb, bitLength);
        /*
			 * Unlike RSA there are no bit length remainders (ie DSA bit length
			 * always divisible by 8 as they are multiples of 64)
			 */
        // modulus
        writeBigInteger(bb, dsaParams.getP(), (bitLength / 8));
        // prime
        writeBigInteger(bb, dsaParams.getQ(), 20);
        // generator
        writeBigInteger(bb, dsaParams.getG(), (bitLength / 8));
        // secret exponent
        writeBigInteger(bb, dsaPrivKey.getX(), 20);
        // dssseed.counter - none, fill 0xff
        UnsignedUtil.putInt(bb, 0xffffffff);
        for (// dssseed.seed - none, fill 0xff
        int i = 0; // dssseed.seed - none, fill 0xff
        i < 20; // dssseed.seed - none, fill 0xff
        i++) {
            bb.put((byte) 0xff);
        }
        return getBufferBytes(bb);
    } catch (IOException ex) {
        throw new CryptoException(res.getString("NoConvertKeyToBlob.exception.message"), ex);
    }
}
Also used : BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) IOException(java.io.IOException) CryptoException(org.kse.crypto.CryptoException) ByteBuffer(java.nio.ByteBuffer)

Example 62 with DSAParams

use of java.security.interfaces.DSAParams in project keystore-explorer by kaikramer.

the class Spkac method createPublicKeyAndChallenge.

private ASN1Sequence createPublicKeyAndChallenge() throws SpkacException {
    ASN1EncodableVector publicKeyAlgorithm = new ASN1EncodableVector();
    publicKeyAlgorithm.add(new ASN1ObjectIdentifier(getPublicKeyAlg().oid()));
    if (getPublicKey() instanceof RSAPublicKey) {
        publicKeyAlgorithm.add(DERNull.INSTANCE);
    } else {
        DSAParams dsaParams = ((DSAPublicKey) getPublicKey()).getParams();
        ASN1EncodableVector dssParams = new ASN1EncodableVector();
        dssParams.add(new ASN1Integer(dsaParams.getP()));
        dssParams.add(new ASN1Integer(dsaParams.getQ()));
        dssParams.add(new ASN1Integer(dsaParams.getG()));
        publicKeyAlgorithm.add(new DERSequence(dssParams));
    }
    ASN1EncodableVector spki = new ASN1EncodableVector();
    spki.add(new DERSequence(publicKeyAlgorithm));
    spki.add(encodePublicKeyAsBitString(getPublicKey()));
    ASN1EncodableVector publicKeyAndChallenge = new ASN1EncodableVector();
    publicKeyAndChallenge.add(new DERSequence(spki));
    publicKeyAndChallenge.add(new DERIA5String(getChallenge()));
    return new DERSequence(publicKeyAndChallenge);
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) DERIA5String(org.bouncycastle.asn1.DERIA5String) RSAPublicKey(java.security.interfaces.RSAPublicKey) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DSAParams(java.security.interfaces.DSAParams) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 63 with DSAParams

use of java.security.interfaces.DSAParams in project keystore-explorer by kaikramer.

the class DProperties method createPrivateKeyNodes.

private void createPrivateKeyNodes(DefaultMutableTreeNode parentNode, PrivateKey privateKey) throws CryptoException {
    DefaultMutableTreeNode privateKeyNode = new DefaultMutableTreeNode(res.getString("DProperties.properties.PrivateKey"));
    parentNode.add(privateKeyNode);
    currentState.getKeyStore();
    KeyInfo keyInfo = KeyPairUtil.getKeyInfo(privateKey);
    String keyAlg = keyInfo.getAlgorithm();
    privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Algorithm"), keyAlg)));
    Integer keySize = keyInfo.getSize();
    if (keySize != null) {
        privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.KeySize"), "" + keyInfo.getSize())));
    } else {
        privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.KeySize"), "?")));
    }
    String keyFormat = privateKey.getFormat();
    privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Format"), keyFormat)));
    String keyEncoded;
    byte[] encodedKey = privateKey.getEncoded();
    if (encodedKey != null) {
        keyEncoded = "0x" + new BigInteger(1, privateKey.getEncoded()).toString(16).toUpperCase();
    } else {
        keyEncoded = "*****";
    }
    privateKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Encoded"), keyEncoded)));
    if (privateKey instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        String publicExponent = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PublicExponent"), "0x" + rsaPrivateKey.getPublicExponent().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(publicExponent));
        String modulus = MessageFormat.format(res.getString("DProperties.properties.private.rsa.Modulus"), "0x" + rsaPrivateKey.getModulus().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(modulus));
        String primeP = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrimeP"), "0x" + rsaPrivateKey.getPrimeP().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(primeP));
        String primeQ = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrimeQ"), "0x" + rsaPrivateKey.getPrimeQ().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(primeQ));
        String primeExponentP = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrimeExponentP"), "0x" + rsaPrivateKey.getPrimeExponentP().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(primeExponentP));
        String primeExponentQ = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrimeExponentQ"), "0x" + rsaPrivateKey.getPrimeExponentQ().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(primeExponentQ));
        String crtCoefficient = MessageFormat.format(res.getString("DProperties.properties.private.rsa.CrtCoefficient"), "0x" + rsaPrivateKey.getCrtCoefficient().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(crtCoefficient));
        String privateExponent = MessageFormat.format(res.getString("DProperties.properties.private.rsa.PrivateExponent"), "0x" + rsaPrivateKey.getPrivateExponent().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(privateExponent));
    } else if (privateKey instanceof DSAPrivateKey) {
        DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
        DSAParams dsaParams = dsaPrivateKey.getParams();
        String primeModulusP = MessageFormat.format(res.getString("DProperties.properties.private.dsa.PrimeModulusP"), "0x" + dsaParams.getP().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(primeModulusP));
        String primeQ = MessageFormat.format(res.getString("DProperties.properties.private.dsa.PrimeQ"), "0x" + dsaParams.getQ().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(primeQ));
        String generatorG = MessageFormat.format(res.getString("DProperties.properties.private.dsa.GeneratorG"), "0x" + dsaParams.getG().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(generatorG));
        String secretExponentX = MessageFormat.format(res.getString("DProperties.properties.private.dsa.SecretExponentX"), "0x" + dsaPrivateKey.getX().toString(16).toUpperCase());
        privateKeyNode.add(new DefaultMutableTreeNode(secretExponentX));
    }
}
Also used : BigInteger(java.math.BigInteger) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) KeyInfo(org.kse.crypto.KeyInfo) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAParams(java.security.interfaces.DSAParams)

Example 64 with DSAParams

use of java.security.interfaces.DSAParams in project j2objc by google.

the class DSAParamsTest method test_getP.

/**
 * java.security.interfaces.DSAParams
 * #getP()
 */
public void test_getP() {
    DSAParams params = new DSAParameterSpec(p, q, g);
    assertEquals("Invalid P", p, params.getP());
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) DSAParams(java.security.interfaces.DSAParams)

Example 65 with DSAParams

use of java.security.interfaces.DSAParams in project jruby-openssl by jruby.

the class PKey method toDerDSAKey.

public static byte[] toDerDSAKey(DSAPublicKey pubKey, DSAPrivateKey privKey) throws IOException {
    if (pubKey != null && privKey == null) {
        return pubKey.getEncoded();
    }
    if (privKey != null && pubKey != null) {
        ASN1EncodableVector vec = new ASN1EncodableVector();
        final DSAParams params = privKey.getParams();
        vec.add(new ASN1Integer(BigInteger.ZERO));
        vec.add(new ASN1Integer(params.getP()));
        vec.add(new ASN1Integer(params.getQ()));
        vec.add(new ASN1Integer(params.getG()));
        vec.add(new ASN1Integer(pubKey.getY()));
        vec.add(new ASN1Integer(privKey.getX()));
        return new DLSequence(vec).getEncoded();
    }
    if (privKey == null) {
        throw new IllegalArgumentException("private key as well as public key are null");
    }
    return privKey.getEncoded();
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DSAParams(java.security.interfaces.DSAParams) ASN1Integer(org.bouncycastle.asn1.ASN1Integer)

Aggregations

DSAParams (java.security.interfaces.DSAParams)66 DSAPublicKey (java.security.interfaces.DSAPublicKey)30 BigInteger (java.math.BigInteger)29 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)18 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)17 DSAParameterSpec (java.security.spec.DSAParameterSpec)16 PublicKey (java.security.PublicKey)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9 InvalidKeyException (java.security.InvalidKeyException)8 KeyFactory (java.security.KeyFactory)8 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)8 IOException (java.io.IOException)7 GeneralSecurityException (java.security.GeneralSecurityException)7 CertPathValidatorException (java.security.cert.CertPathValidatorException)7 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)7 RSAPublicKey (java.security.interfaces.RSAPublicKey)7 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)7 AlgorithmParameters (java.security.AlgorithmParameters)6 X509Certificate (java.security.cert.X509Certificate)6