use of org.kse.crypto.KeyInfo in project keystore-explorer by kaikramer.
the class KeyPairUtil method getKeyInfo.
/**
* Get the information about the supplied public key.
*
* @param publicKey
* The public key
* @return Key information
* @throws CryptoException
* If there is a problem getting the information
*/
public static KeyInfo getKeyInfo(PublicKey publicKey) throws CryptoException {
try {
String algorithm = publicKey.getAlgorithm();
if (algorithm.equals(RSA.jce())) {
KeyFactory keyFact = KeyFactory.getInstance(algorithm, BOUNCY_CASTLE.jce());
RSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class);
BigInteger modulus = keySpec.getModulus();
return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length());
} else if (algorithm.equals(DSA.jce())) {
KeyFactory keyFact = KeyFactory.getInstance(algorithm);
DSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, DSAPublicKeySpec.class);
BigInteger prime = keySpec.getP();
return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length());
} else if (algorithm.equals(EC.jce())) {
ECPublicKey pubk = (ECPublicKey) publicKey;
int size = pubk.getParams().getOrder().bitLength();
return new KeyInfo(ASYMMETRIC, algorithm, size);
}
// size unknown
return new KeyInfo(ASYMMETRIC, algorithm);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoPublicKeysize.exception.message"), ex);
}
}
use of org.kse.crypto.KeyInfo 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 org.kse.crypto.KeyInfo in project keystore-explorer by kaikramer.
the class DProperties method createSecretKeyNodes.
private void createSecretKeyNodes(DefaultMutableTreeNode parentNode, SecretKey secretKey) {
DefaultMutableTreeNode secretKeyNode = new DefaultMutableTreeNode(res.getString("DProperties.properties.SecretKey"));
parentNode.add(secretKeyNode);
KeyInfo keyInfo = SecretKeyUtil.getKeyInfo(secretKey);
String keyAlg = keyInfo.getAlgorithm();
// Try and get friendly algorithm name for secret key
SecretKeyType secretKeyType = SecretKeyType.resolveJce(keyAlg);
if (secretKeyType != null) {
keyAlg = secretKeyType.friendly();
}
secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Algorithm"), keyAlg)));
Integer keySize = keyInfo.getSize();
if (keySize != null) {
secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.KeySize"), "" + keyInfo.getSize())));
} else {
secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.KeySize"), "?")));
}
String keyFormat = secretKey.getFormat();
secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Format"), keyFormat)));
String keyEncoded = "0x" + new BigInteger(1, secretKey.getEncoded()).toString(16).toUpperCase();
secretKeyNode.add(new DefaultMutableTreeNode(MessageFormat.format(res.getString("DProperties.properties.Encoded"), keyEncoded)));
}
use of org.kse.crypto.KeyInfo in project keystore-explorer by kaikramer.
the class DViewCsr method populatePublicKey.
private void populatePublicKey(PublicKey csrPublicKey) throws CryptoException {
KeyInfo keyInfo = KeyPairUtil.getKeyInfo(csrPublicKey);
jtfPublicKey.setText(keyInfo.getAlgorithm());
Integer keySize = keyInfo.getSize();
if (keySize != null) {
jtfPublicKey.setText(MessageFormat.format(res.getString("DViewCsr.jtfPublicKey.text"), jtfPublicKey.getText(), "" + keySize));
} else {
jtfPublicKey.setText(MessageFormat.format(res.getString("DViewCsr.jtfPublicKey.text"), jtfPublicKey.getText(), "?"));
}
jtfPublicKey.setCaretPosition(0);
}
use of org.kse.crypto.KeyInfo in project keystore-explorer by kaikramer.
the class DialogHelper method populateSigAlgs.
/**
* Populate a JComboBox with signature algorithms depending on the key pair type.
*
* @param keyPairType
* @param privateKey
* @param jcbSignatureAlgorithm
* @throws CryptoException
*/
public static void populateSigAlgs(KeyPairType keyPairType, PrivateKey privateKey, Provider provider, JComboBox<SignatureType> jcbSignatureAlgorithm) throws CryptoException {
List<SignatureType> sigAlgs;
switch(keyPairType) {
case RSA:
KeyInfo keyInfo = KeyPairUtil.getKeyInfo(privateKey);
sigAlgs = SignatureType.rsaSignatureTypes(keyInfo.getSize());
break;
case DSA:
sigAlgs = SignatureType.dsaSignatureTypes();
break;
case EC:
default:
sigAlgs = SignatureType.ecdsaSignatureTypes();
}
jcbSignatureAlgorithm.removeAllItems();
for (SignatureType sigAlg : sigAlgs) {
jcbSignatureAlgorithm.addItem(sigAlg);
}
// pre-select modern hash algs
if (sigAlgs.contains(SignatureType.SHA256_RSA)) {
jcbSignatureAlgorithm.setSelectedItem(SignatureType.SHA256_RSA);
} else if (sigAlgs.contains(SignatureType.SHA256_ECDSA)) {
jcbSignatureAlgorithm.setSelectedItem(SignatureType.SHA256_ECDSA);
} else if (sigAlgs.contains(SignatureType.SHA256_DSA)) {
jcbSignatureAlgorithm.setSelectedItem(SignatureType.SHA256_DSA);
} else {
jcbSignatureAlgorithm.setSelectedIndex(0);
}
}
Aggregations