forked from I2P_Developers/i2p.i2p
- Move I2P-to-Java DSA key conversion from DSAEngine to SigUtil
- Add Java-to-I2P DSA key conversion to SigUtil - Export keys from SU3File in Java encoded format instead of I2P format
This commit is contained in:
@@ -38,9 +38,6 @@ import java.security.KeyFactory;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.DSAPrivateKeySpec;
|
||||
import java.security.spec.DSAPublicKeySpec;
|
||||
import java.security.spec.KeySpec;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.Hash;
|
||||
@@ -501,13 +498,7 @@ public class DSAEngine {
|
||||
*/
|
||||
private boolean altVerifySigSHA1(Signature signature, byte[] data, SigningPublicKey verifyingKey) throws GeneralSecurityException {
|
||||
java.security.Signature jsig = java.security.Signature.getInstance("SHA1withDSA");
|
||||
KeyFactory keyFact = KeyFactory.getInstance("DSA");
|
||||
// y p q g
|
||||
KeySpec spec = new DSAPublicKeySpec(new NativeBigInteger(1, verifyingKey.getData()),
|
||||
CryptoConstants.dsap,
|
||||
CryptoConstants.dsaq,
|
||||
CryptoConstants.dsag);
|
||||
PublicKey pubKey = keyFact.generatePublic(spec);
|
||||
PublicKey pubKey = SigUtil.toJavaDSAKey(verifyingKey);
|
||||
jsig.initVerify(pubKey);
|
||||
jsig.update(data);
|
||||
boolean rv = jsig.verify(SigUtil.toJavaSig(signature));
|
||||
@@ -563,13 +554,7 @@ public class DSAEngine {
|
||||
*/
|
||||
private Signature altSignSHA1(byte[] data, SigningPrivateKey privateKey) throws GeneralSecurityException {
|
||||
java.security.Signature jsig = java.security.Signature.getInstance("SHA1withDSA");
|
||||
KeyFactory keyFact = KeyFactory.getInstance("DSA");
|
||||
// y p q g
|
||||
KeySpec spec = new DSAPrivateKeySpec(new NativeBigInteger(1, privateKey.getData()),
|
||||
CryptoConstants.dsap,
|
||||
CryptoConstants.dsaq,
|
||||
CryptoConstants.dsag);
|
||||
PrivateKey privKey = keyFact.generatePrivate(spec);
|
||||
PrivateKey privKey = SigUtil.toJavaDSAKey(privateKey);
|
||||
jsig.initSign(privKey, _context.random());
|
||||
jsig.update(data);
|
||||
return SigUtil.fromJavaSig(jsig.sign(), SigType.DSA_SHA1);
|
||||
|
@@ -12,6 +12,8 @@ import java.io.OutputStream;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.DigestOutputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
@@ -547,6 +549,7 @@ public class SU3File {
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes Java-encoded keys (X.509 for public and PKCS#8 for private)
|
||||
* @return success
|
||||
* @since 0.9.9
|
||||
*/
|
||||
@@ -564,17 +567,20 @@ public class SU3File {
|
||||
FileOutputStream fileOutputStream = null;
|
||||
I2PAppContext context = I2PAppContext.getGlobalContext();
|
||||
try {
|
||||
// inefficiently go from Java to I2P to Java formats
|
||||
SimpleDataStructure signingKeypair[] = context.keyGenerator().generateSigningKeys(type);
|
||||
SigningPublicKey signingPublicKey = (SigningPublicKey) signingKeypair[0];
|
||||
SigningPrivateKey signingPrivateKey = (SigningPrivateKey) signingKeypair[1];
|
||||
PublicKey pubkey = SigUtil.toJavaKey(signingPublicKey);
|
||||
PrivateKey privkey = SigUtil.toJavaKey(signingPrivateKey);
|
||||
|
||||
fileOutputStream = new SecureFileOutputStream(pubFile);
|
||||
signingPublicKey.writeBytes(fileOutputStream);
|
||||
fileOutputStream.write(pubkey.getEncoded());
|
||||
fileOutputStream.close();
|
||||
fileOutputStream = null;
|
||||
|
||||
fileOutputStream = new SecureFileOutputStream(privFile);
|
||||
signingPrivateKey.writeBytes(fileOutputStream);
|
||||
fileOutputStream.write(privkey.getEncoded());
|
||||
|
||||
System.out.println("\r\n" + type + " Private key written to: " + privateKeyFile);
|
||||
System.out.println(type + " Public key written to: " + publicKeyFile);
|
||||
|
@@ -7,8 +7,12 @@ import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.SignatureException;
|
||||
import java.security.interfaces.DSAPrivateKey;
|
||||
import java.security.interfaces.DSAPublicKey;
|
||||
import java.security.interfaces.ECPrivateKey;
|
||||
import java.security.interfaces.ECPublicKey;
|
||||
import java.security.spec.DSAPrivateKeySpec;
|
||||
import java.security.spec.DSAPublicKeySpec;
|
||||
import java.security.spec.ECField;
|
||||
import java.security.spec.ECFieldFp;
|
||||
import java.security.spec.ECGenParameterSpec;
|
||||
@@ -17,12 +21,14 @@ import java.security.spec.ECPrivateKeySpec;
|
||||
import java.security.spec.ECPublicKeySpec;
|
||||
import java.security.spec.ECPoint;
|
||||
import java.security.spec.EllipticCurve;
|
||||
import java.security.spec.KeySpec;
|
||||
import java.util.Map;
|
||||
|
||||
import net.i2p.data.Signature;
|
||||
import net.i2p.data.SigningPrivateKey;
|
||||
import net.i2p.data.SigningPublicKey;
|
||||
import net.i2p.util.LHMCache;
|
||||
import net.i2p.util.NativeBigInteger;
|
||||
|
||||
|
||||
/**
|
||||
@@ -43,7 +49,7 @@ class SigUtil {
|
||||
public static PublicKey toJavaKey(SigningPublicKey pk)
|
||||
throws GeneralSecurityException {
|
||||
if (pk.getType() == SigType.DSA_SHA1)
|
||||
throw new UnsupportedOperationException();
|
||||
return toJavaDSAKey(pk);
|
||||
else
|
||||
return toJavaECKey(pk);
|
||||
}
|
||||
@@ -54,7 +60,7 @@ class SigUtil {
|
||||
public static PrivateKey toJavaKey(SigningPrivateKey pk)
|
||||
throws GeneralSecurityException {
|
||||
if (pk.getType() == SigType.DSA_SHA1)
|
||||
throw new UnsupportedOperationException();
|
||||
return toJavaDSAKey(pk);
|
||||
else
|
||||
return toJavaECKey(pk);
|
||||
}
|
||||
@@ -65,7 +71,7 @@ class SigUtil {
|
||||
public static SigningPublicKey fromJavaKey(PublicKey pk, SigType type)
|
||||
throws GeneralSecurityException {
|
||||
if (type == SigType.DSA_SHA1)
|
||||
throw new UnsupportedOperationException();
|
||||
return fromJavaKey((DSAPublicKey) pk);
|
||||
else
|
||||
return fromJavaKey((ECPublicKey) pk, type);
|
||||
}
|
||||
@@ -76,7 +82,7 @@ class SigUtil {
|
||||
public static SigningPrivateKey fromJavaKey(PrivateKey pk, SigType type)
|
||||
throws GeneralSecurityException {
|
||||
if (type == SigType.DSA_SHA1)
|
||||
throw new UnsupportedOperationException();
|
||||
return fromJavaKey((DSAPrivateKey) pk);
|
||||
else
|
||||
return fromJavaKey((ECPrivateKey) pk, type);
|
||||
}
|
||||
@@ -172,6 +178,46 @@ class SigUtil {
|
||||
return new SigningPrivateKey(type, bs);
|
||||
}
|
||||
|
||||
public static DSAPublicKey toJavaDSAKey(SigningPublicKey pk)
|
||||
throws GeneralSecurityException {
|
||||
KeyFactory kf = KeyFactory.getInstance("DSA");
|
||||
// y p q g
|
||||
KeySpec ks = new DSAPublicKeySpec(new NativeBigInteger(1, pk.getData()),
|
||||
CryptoConstants.dsap,
|
||||
CryptoConstants.dsaq,
|
||||
CryptoConstants.dsag);
|
||||
return (DSAPublicKey) kf.generatePublic(ks);
|
||||
}
|
||||
|
||||
public static DSAPrivateKey toJavaDSAKey(SigningPrivateKey pk)
|
||||
throws GeneralSecurityException {
|
||||
KeyFactory kf = KeyFactory.getInstance("DSA");
|
||||
// x p q g
|
||||
KeySpec ks = new DSAPrivateKeySpec(new NativeBigInteger(1, pk.getData()),
|
||||
CryptoConstants.dsap,
|
||||
CryptoConstants.dsaq,
|
||||
CryptoConstants.dsag);
|
||||
return (DSAPrivateKey) kf.generatePrivate(ks);
|
||||
}
|
||||
|
||||
public static SigningPublicKey fromJavaKey(DSAPublicKey pk)
|
||||
throws GeneralSecurityException {
|
||||
BigInteger y = pk.getY();
|
||||
SigType type = SigType.DSA_SHA1;
|
||||
int len = type.getPubkeyLen();
|
||||
byte[] by = rectify(y, len);
|
||||
return new SigningPublicKey(type, by);
|
||||
}
|
||||
|
||||
public static SigningPrivateKey fromJavaKey(DSAPrivateKey pk)
|
||||
throws GeneralSecurityException {
|
||||
BigInteger x = pk.getX();
|
||||
SigType type = SigType.DSA_SHA1;
|
||||
int len = type.getPrivkeyLen();
|
||||
byte[] bx = rectify(x, len);
|
||||
return new SigningPrivateKey(type, bx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ASN.1 representation
|
||||
*/
|
||||
|
Reference in New Issue
Block a user