use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.
the class KeyStoreState method isEntryPrivateKeyEqual.
protected boolean isEntryPrivateKeyEqual(KeyStoreState targetState, String alias, Password password) throws GeneralSecurityException {
Key currentKey = keyStore.getKey(alias, password.toCharArray());
Key targetKey = targetState.getKeyStore().getKey(alias, password.toCharArray());
// JDKDSAPrivateKey has no equals method defined
if ((currentKey instanceof JDKDSAPrivateKey) || (targetKey instanceof JDKDSAPrivateKey)) {
DSAPrivateKey currentDsaKey = (DSAPrivateKey) currentKey;
DSAPrivateKey targetDsaKey = (DSAPrivateKey) targetKey;
return currentDsaKey.getX().equals(targetDsaKey.getX()) && currentDsaKey.getParams().getG().equals(targetDsaKey.getParams().getG()) && currentDsaKey.getParams().getP().equals(targetDsaKey.getParams().getP()) && currentDsaKey.getParams().getQ().equals(targetDsaKey.getParams().getQ());
} else {
return currentKey.equals(targetKey);
}
}
use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.
the class MsPvkUtil method getEncryptedInternal.
private static byte[] getEncryptedInternal(PrivateKey privateKey, int keyType, Password password, boolean strong) throws CryptoException {
try {
// Write PVK to a byte buffer set up to write little endian
ByteBuffer bb = ByteBuffer.wrap(new byte[PVK_BUFFER_LENGTH]);
bb.order(ByteOrder.LITTLE_ENDIAN);
// Write magic number, reserved and and key type fields
writeReservedMagicKeyType(bb, keyType);
// Get password as bytes
byte[] passwordBytes = new String(password.toCharArray()).getBytes();
// Generate salt for encryption
byte[] salt = generate16ByteSalt();
// Concatenate the salt and password
byte[] saltAndPassword = new byte[salt.length + passwordBytes.length];
System.arraycopy(salt, 0, saltAndPassword, 0, salt.length);
System.arraycopy(passwordBytes, 0, saltAndPassword, salt.length, passwordBytes.length);
// Digest the salt and password to create the encryption key
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
byte[] key = messageDigest.digest(saltAndPassword);
// Get private key blob
byte[] privateKeyBlob = null;
if (privateKey instanceof RSAPrivateCrtKey) {
privateKeyBlob = rsaPrivateKeyToBlob((RSAPrivateCrtKey) privateKey);
} else {
privateKeyBlob = dsaPrivateKeyToBlob((DSAPrivateKey) privateKey);
}
// Encrypt private key blob
byte[] encryptedPrivateKeyBlob = null;
if (strong) {
// Strong version uses all 16 bytes of the key
byte[] strongKey = new byte[16];
System.arraycopy(key, 0, strongKey, 0, strongKey.length);
encryptedPrivateKeyBlob = encryptPrivateKeyBlob(privateKeyBlob, strongKey);
} else {
// The weak version uses only 5 bytes of the key followed by 11 zero bytes
byte[] weakKey = new byte[16];
System.arraycopy(key, 0, weakKey, 0, 5);
for (int i = 5; i < weakKey.length; i++) {
weakKey[i] = 0;
}
encryptedPrivateKeyBlob = encryptPrivateKeyBlob(privateKeyBlob, weakKey);
}
// Write type field - encrypted
UnsignedUtil.putInt(bb, PVK_ENCRYPTED);
// Write salt length field
UnsignedUtil.putInt(bb, salt.length);
// Write key length field - length of the blob plus length blob header
int keyLength = encryptedPrivateKeyBlob.length + BLOB_HEADER_LENGTH;
UnsignedUtil.putInt(bb, keyLength);
// Write salt
bb.put(salt);
// Write private key blob header
writePrivateKeyBlobHeader(bb, keyType, privateKey);
// Write blob
bb.put(encryptedPrivateKeyBlob);
return getBufferBytes(bb);
} catch (IOException ex) {
throw new CryptoException(res.getString("NoGetMsPvk.exception.message"), ex);
} catch (NoSuchAlgorithmException ex) {
throw new CryptoException(res.getString("NoGetMsPvk.exception.message"), ex);
}
}
use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.
the class MsPvkUtil method blobToDsaPrivateKey.
private static DSAPrivateKey blobToDsaPrivateKey(byte[] dsaPrivateKeyBlob) throws CryptoException {
try {
ByteBuffer bb = ByteBuffer.wrap(dsaPrivateKeyBlob);
bb.order(ByteOrder.LITTLE_ENDIAN);
// Get each blob field
// dsspubkey.magic
long magic = UnsignedUtil.getInt(bb);
// Check magic field is valid
if (magic != DSS_PRIV_MAGIC) {
throw new CryptoException(MessageFormat.format(res.getString("InvalidDsaMagicField.exception.message"), Long.toHexString(magic), Long.toHexString(DSS_PRIV_MAGIC)));
}
// dsspubkey.bitlen
long bitLength = UnsignedUtil.getInt(bb);
// modulus
BigInteger p = readBigInteger(bb, (int) (bitLength / 8));
// prime
BigInteger q = readBigInteger(bb, 20);
// generator
BigInteger g = readBigInteger(bb, (int) (bitLength / 8));
// secret exponent
BigInteger x = readBigInteger(bb, 20);
// Ignore 24 bytes of dssseed (only applicable to public keys)
for (int i = 0; i < 24; i++) {
bb.get();
}
DSAPrivateKeySpec dsaPrivateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return (DSAPrivateKey) keyFactory.generatePrivate(dsaPrivateKeySpec);
} catch (IOException ex) {
throw new CryptoException(res.getString("NoConvertBlobToDsaKey.exception.message"), ex);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoConvertBlobToDsaKey.exception.message"), ex);
}
}
use of java.security.interfaces.DSAPrivateKey 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));
}
}
use of java.security.interfaces.DSAPrivateKey in project BiglyBT by BiglySoftware.
the class PEMWriter method writeObject.
public void writeObject(Object obj, String algorithm, char[] password, SecureRandom random) throws IOException {
if (obj instanceof KeyPair) {
writeObject(((KeyPair) obj).getPrivate());
return;
}
String type = null;
byte[] keyData = null;
if (obj instanceof RSAPrivateCrtKey) {
type = "RSA PRIVATE KEY";
RSAPrivateCrtKey k = (RSAPrivateCrtKey) obj;
RSAPrivateKeyStructure keyStruct = new RSAPrivateKeyStructure(k.getModulus(), k.getPublicExponent(), k.getPrivateExponent(), k.getPrimeP(), k.getPrimeQ(), k.getPrimeExponentP(), k.getPrimeExponentQ(), k.getCrtCoefficient());
// convert to bytearray
keyData = keyStruct.getEncoded();
} else if (obj instanceof DSAPrivateKey) {
type = "DSA PRIVATE KEY";
DSAPrivateKey k = (DSAPrivateKey) obj;
DSAParams p = k.getParams();
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 = k.getX();
BigInteger y = p.getG().modPow(x, p.getP());
v.add(new DERInteger(y));
v.add(new DERInteger(x));
keyData = new DERSequence(v).getEncoded();
}
if (type == null || keyData == null) {
// TODO Support other types?
throw new IllegalArgumentException("Object type not supported: " + obj.getClass().getName());
}
String dekAlgName = Strings.toUpperCase(algorithm);
// Note: For backward compatibility
if (dekAlgName.equals("DESEDE")) {
dekAlgName = "DES-EDE3-CBC";
}
int ivLength = dekAlgName.startsWith("AES-") ? 16 : 8;
byte[] iv = new byte[ivLength];
random.nextBytes(iv);
byte[] encData = PEMUtilities.crypt(true, provider, keyData, password, dekAlgName, iv);
// write the data
writeHeader(type);
this.write("Proc-Type: 4,ENCRYPTED");
this.newLine();
this.write("DEK-Info: " + dekAlgName + ",");
this.writeHexEncoded(iv);
this.newLine();
this.newLine();
this.writeEncoded(encData);
writeFooter(type);
}
Aggregations