Search in sources :

Example 21 with DataFormatException

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

the class ElGamalTest method testRoundTrip.

public void testRoundTrip() {
    Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
    PublicKey pubKey = (PublicKey) keys[0];
    PrivateKey privKey = (PrivateKey) keys[1];
    String msg = "Hello world";
    Set toBeDelivered = new HashSet();
    SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
    if (key == null)
        key = _context.sessionKeyManager().createSession(pubKey);
    byte[] encrypted = _context.elGamalAESEngine().encrypt(DataHelper.getASCII(msg), pubKey, key, null, null, 64);
    byte[] decrypted = null;
    try {
        decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
    } catch (DataFormatException dfe) {
        dfe.printStackTrace();
        fail();
    }
    assertNotNull(decrypted);
    String read = new String(decrypted);
    assertEquals(msg, read);
}
Also used : PrivateKey(net.i2p.data.PrivateKey) Set(java.util.Set) HashSet(java.util.HashSet) DataFormatException(net.i2p.data.DataFormatException) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) HashSet(java.util.HashSet)

Example 22 with DataFormatException

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

the class ElGamalTest method testVerifyCompatability.

public void testVerifyCompatability() {
    PublicKey pub = new PublicKey();
    PrivateKey priv = new PrivateKey();
    try {
        pub.fromBase64(PUBLIC_KEY);
        priv.fromBase64(PRIVATE_KEY);
    } catch (DataFormatException dfe) {
        dfe.printStackTrace();
        fail();
    }
    for (int i = 0; i < ENCRYPTED.length; i++) {
        byte[] enc = Base64.decode(ENCRYPTED[i]);
        byte[] decrypted = _context.elGamalEngine().decrypt(enc, priv);
        assertTrue(DataHelper.eq(decrypted, DataHelper.getASCII(UNENCRYPTED[i])));
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException) PublicKey(net.i2p.data.PublicKey)

Example 23 with DataFormatException

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

the class TrustedUpdate method verify.

/**
 * Verifies the DSA signature of a signed update file.
 *
 * @param signedFile    The signed update file to check.
 * @param publicKeyFile A file containing the public key to use for
 *                      verification.
 *
 * @return <code>true</code> if the file has a valid signature, otherwise
 *         <code>false</code>.
 */
public boolean verify(String signedFile, String publicKeyFile) {
    SigningPublicKey signingPublicKey = new SigningPublicKey();
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(signedFile);
        signingPublicKey.readBytes(fileInputStream);
    } catch (IOException ioe) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Unable to load the signature", ioe);
        return false;
    } catch (DataFormatException dfe) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Unable to load the signature", dfe);
        return false;
    } finally {
        if (fileInputStream != null)
            try {
                fileInputStream.close();
            } catch (IOException ioe) {
            }
    }
    return verify(new File(signedFile), signingPublicKey);
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 24 with DataFormatException

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

the class TrustedUpdate method sign.

/**
 * Uses the given private key to sign the given input file along with its
 * version string using DSA. The output will be a signed update file where
 * the first 40 bytes are the resulting DSA signature, the next 16 bytes are
 * the input file's version string encoded in UTF-8 (padded with trailing
 * <code>0h</code> characters if necessary), and the remaining bytes are the
 * raw bytes of the input file.
 *
 * @param inputFile      The file to be signed.
 * @param signedFile     The signed update file to write.
 * @param privateKeyFile The name of the file containing the private key to
 *                       sign <code>inputFile</code> with.
 * @param version        The version string of the input file. If this is
 *                       longer than 16 characters it will be truncated.
 *
 * @return An instance of {@link net.i2p.data.Signature}, or
 *         <code>null</code> if there was an error.
 */
public Signature sign(String inputFile, String signedFile, String privateKeyFile, String version) {
    FileInputStream fileInputStream = null;
    SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
    try {
        fileInputStream = new FileInputStream(privateKeyFile);
        signingPrivateKey.readBytes(fileInputStream);
    } catch (IOException ioe) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Unable to load the signing key", ioe);
        return null;
    } catch (DataFormatException dfe) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Unable to load the signing key", dfe);
        return null;
    } finally {
        if (fileInputStream != null)
            try {
                fileInputStream.close();
            } catch (IOException ioe) {
            }
    }
    return sign(inputFile, signedFile, signingPrivateKey, version);
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 25 with DataFormatException

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

the class TrustedUpdate method addKey.

/**
 *  Duplicate keys or names rejected,
 *  except that duplicate empty names are allowed
 *  @param key 172 character base64 string
 *  @param name non-null but "" ok
 *  @since 0.7.12
 *  @return true if successful
 */
public boolean addKey(String key, String name) {
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Adding " + name + ": " + key);
    SigningPublicKey signingPublicKey = new SigningPublicKey();
    try {
        // fromBase64() will throw a DFE if length is not right
        signingPublicKey.fromBase64(key);
    } catch (DataFormatException dfe) {
        _log.error("Invalid signing key for " + name + " : " + key, dfe);
        return false;
    }
    String oldName = _trustedKeys.get(signingPublicKey);
    // already there?
    if (name.equals(oldName))
        return true;
    if (oldName != null && !oldName.equals("")) {
        _log.error("Key for " + name + " already stored for different name " + oldName + " : " + key);
        return false;
    }
    if ((!name.equals("")) && _trustedKeys.containsValue(name)) {
        _log.error("Key mismatch for " + name + ", spoof attempt? : " + key);
        return false;
    }
    _trustedKeys.put(signingPublicKey, name);
    return true;
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) DataFormatException(net.i2p.data.DataFormatException)

Aggregations

DataFormatException (net.i2p.data.DataFormatException)112 IOException (java.io.IOException)53 Destination (net.i2p.data.Destination)32 Properties (java.util.Properties)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 FileInputStream (java.io.FileInputStream)16 Hash (net.i2p.data.Hash)14 File (java.io.File)13 SigType (net.i2p.crypto.SigType)13 I2PSessionException (net.i2p.client.I2PSessionException)12 InputStream (java.io.InputStream)11 PrivateKey (net.i2p.data.PrivateKey)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SigningPublicKey (net.i2p.data.SigningPublicKey)11 RouterInfo (net.i2p.data.router.RouterInfo)11 Signature (net.i2p.data.Signature)10 FileOutputStream (java.io.FileOutputStream)8 InterruptedIOException (java.io.InterruptedIOException)8 HashMap (java.util.HashMap)8 PublicKey (net.i2p.data.PublicKey)8