Search in sources :

Example 31 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class KeyGenerator method getSigningPublicKey.

/**
 * Convert a SigningPrivateKey to a SigningPublicKey.
 *  As of 0.9.16, supports all key types.
 *
 * @param priv a SigningPrivateKey object
 * @return a SigningPublicKey object
 * @throws IllegalArgumentException on bad key or unknown type
 */
public static SigningPublicKey getSigningPublicKey(SigningPrivateKey priv) {
    SigType type = priv.getType();
    if (type == null)
        throw new IllegalArgumentException("Unknown type");
    try {
        switch(type.getBaseAlgorithm()) {
            case DSA:
                BigInteger x = new NativeBigInteger(1, priv.toByteArray());
                BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
                SigningPublicKey pub = new SigningPublicKey();
                pub.setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
                return pub;
            case EC:
                ECPrivateKey ecpriv = SigUtil.toJavaECKey(priv);
                BigInteger s = ecpriv.getS();
                ECParameterSpec spec = (ECParameterSpec) type.getParams();
                EllipticCurve curve = spec.getCurve();
                ECPoint g = spec.getGenerator();
                ECPoint w = ECUtil.scalarMult(g, s, curve);
                ECPublicKeySpec ecks = new ECPublicKeySpec(w, ecpriv.getParams());
                KeyFactory eckf = KeyFactory.getInstance("EC");
                ECPublicKey ecpub = (ECPublicKey) eckf.generatePublic(ecks);
                return SigUtil.fromJavaKey(ecpub, type);
            case RSA:
                RSAPrivateKey rsapriv = SigUtil.toJavaRSAKey(priv);
                BigInteger exp = ((RSAKeyGenParameterSpec) type.getParams()).getPublicExponent();
                RSAPublicKeySpec rsaks = new RSAPublicKeySpec(rsapriv.getModulus(), exp);
                KeyFactory rsakf = KeyFactory.getInstance("RSA");
                RSAPublicKey rsapub = (RSAPublicKey) rsakf.generatePublic(rsaks);
                return SigUtil.fromJavaKey(rsapub, type);
            case EdDSA:
                EdDSAPrivateKey epriv = SigUtil.toJavaEdDSAKey(priv);
                EdDSAPublicKey epub = new EdDSAPublicKey(new EdDSAPublicKeySpec(epriv.getA(), epriv.getParams()));
                return SigUtil.fromJavaKey(epub, type);
            default:
                throw new IllegalArgumentException("Unsupported algorithm");
        }
    } catch (GeneralSecurityException gse) {
        throw new IllegalArgumentException("Conversion failed", gse);
    }
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) NativeBigInteger(net.i2p.util.NativeBigInteger) GeneralSecurityException(java.security.GeneralSecurityException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) EllipticCurve(java.security.spec.EllipticCurve) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 32 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class DSAEngine method altVerifySigSHA1.

/**
 *  Alternate to verifySignature() using java.security libraries.
 *  @throws GeneralSecurityException if algorithm unvailable or on other errors
 *  @since 0.8.7 added off/len 0.9.12
 */
private boolean altVerifySigSHA1(Signature signature, byte[] data, int offset, int len, SigningPublicKey verifyingKey) throws GeneralSecurityException {
    java.security.Signature jsig = java.security.Signature.getInstance("SHA1withDSA");
    PublicKey pubKey = SigUtil.toJavaDSAKey(verifyingKey);
    jsig.initVerify(pubKey);
    jsig.update(data, offset, len);
    boolean rv = jsig.verify(SigUtil.toJavaSig(signature));
    // }
    return rv;
}
Also used : PublicKey(java.security.PublicKey) SigningPublicKey(net.i2p.data.SigningPublicKey)

Example 33 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class DSAEngine method altVerifySigRaw.

/**
 *  Generic raw verify any type
 *
 *  Warning, nonstandard for EdDSA, double-hashes, not recommended.
 *
 *  @throws GeneralSecurityException if algorithm unvailable or on other errors
 *  @since 0.9.9
 */
private boolean altVerifySigRaw(Signature signature, SimpleDataStructure hash, SigningPublicKey verifyingKey) throws GeneralSecurityException {
    SigType type = signature.getType();
    if (type != verifyingKey.getType())
        throw new IllegalArgumentException("type mismatch sig=" + type + " key=" + verifyingKey.getType());
    PublicKey pubKey = SigUtil.toJavaKey(verifyingKey);
    return verifySignature(signature, hash, pubKey);
}
Also used : PublicKey(java.security.PublicKey) SigningPublicKey(net.i2p.data.SigningPublicKey)

Example 34 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class HostTxtEntry method hasValidSig.

/**
 * Verify with the dest public key using the "sig" property
 */
public boolean hasValidSig() {
    if (props == null || name == null || dest == null)
        return false;
    if (!isValidated) {
        isValidated = true;
        StringWriter buf = new StringWriter(1024);
        String sig = props.getProperty(PROP_SIG);
        if (sig == null)
            return false;
        buf.append(name);
        buf.append(KV_SEPARATOR);
        buf.append(dest);
        try {
            writeProps(buf, true, false);
        } catch (IOException ioe) {
            // won't happen
            return false;
        }
        byte[] sdata = Base64.decode(sig);
        if (sdata == null)
            return false;
        Destination d;
        try {
            d = new Destination(dest);
        } catch (DataFormatException dfe) {
            return false;
        }
        SigningPublicKey spk = d.getSigningPublicKey();
        SigType type = spk.getType();
        if (type == null)
            return false;
        Signature s;
        try {
            s = new Signature(type, sdata);
        } catch (IllegalArgumentException iae) {
            return false;
        }
        isValid = DSAEngine.getInstance().verifySignature(s, DataHelper.getUTF8(buf.toString()), spk);
    }
    return isValid;
}
Also used : Destination(net.i2p.data.Destination) SigningPublicKey(net.i2p.data.SigningPublicKey) DataFormatException(net.i2p.data.DataFormatException) StringWriter(java.io.StringWriter) Signature(net.i2p.data.Signature) IOException(java.io.IOException) SigType(net.i2p.crypto.SigType)

Example 35 with SigningPublicKey

use of net.i2p.data.SigningPublicKey in project i2p.i2p by i2p.

the class HostTxtEntry method hasValidInnerSig.

/**
 * Verify with the "olddest" property's public key using the "oldsig" property
 */
public boolean hasValidInnerSig() {
    if (props == null || name == null || dest == null)
        return false;
    boolean rv = false;
    // don't cache result
    if (true) {
        StringWriter buf = new StringWriter(1024);
        String sig = props.getProperty(PROP_OLDSIG);
        String olddest = props.getProperty(PROP_OLDDEST);
        if (sig == null || olddest == null)
            return false;
        buf.append(name);
        buf.append(KV_SEPARATOR);
        buf.append(dest);
        try {
            writeProps(buf, true, true);
        } catch (IOException ioe) {
            // won't happen
            return false;
        }
        byte[] sdata = Base64.decode(sig);
        if (sdata == null)
            return false;
        Destination d;
        try {
            d = new Destination(olddest);
        } catch (DataFormatException dfe) {
            return false;
        }
        SigningPublicKey spk = d.getSigningPublicKey();
        SigType type = spk.getType();
        if (type == null)
            return false;
        Signature s;
        try {
            s = new Signature(type, sdata);
        } catch (IllegalArgumentException iae) {
            return false;
        }
        rv = DSAEngine.getInstance().verifySignature(s, DataHelper.getUTF8(buf.toString()), spk);
    }
    return rv;
}
Also used : Destination(net.i2p.data.Destination) SigningPublicKey(net.i2p.data.SigningPublicKey) DataFormatException(net.i2p.data.DataFormatException) StringWriter(java.io.StringWriter) Signature(net.i2p.data.Signature) IOException(java.io.IOException) SigType(net.i2p.crypto.SigType)

Aggregations

SigningPublicKey (net.i2p.data.SigningPublicKey)36 SigningPrivateKey (net.i2p.data.SigningPrivateKey)13 IOException (java.io.IOException)12 DataFormatException (net.i2p.data.DataFormatException)11 SigType (net.i2p.crypto.SigType)10 Signature (net.i2p.data.Signature)10 PublicKey (net.i2p.data.PublicKey)9 File (java.io.File)8 GeneralSecurityException (java.security.GeneralSecurityException)8 PublicKey (java.security.PublicKey)7 PrivateKey (net.i2p.data.PrivateKey)6 SimpleDataStructure (net.i2p.data.SimpleDataStructure)6 BigInteger (java.math.BigInteger)5 ECPoint (java.security.spec.ECPoint)5 Certificate (net.i2p.data.Certificate)5 Destination (net.i2p.data.Destination)5 NativeBigInteger (net.i2p.util.NativeBigInteger)5 FileInputStream (java.io.FileInputStream)3 StringWriter (java.io.StringWriter)3 CertificateTest (net.i2p.data.CertificateTest)3