use of net.i2p.data.PublicKey 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);
}
use of net.i2p.data.PublicKey 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])));
}
}
use of net.i2p.data.PublicKey in project i2p.i2p by i2p.
the class RouterIdentityTest method testNullCert.
@Test
public void testNullCert() throws Exception {
RouterIdentity ident = new RouterIdentity();
ident.setCertificate(null);
PublicKey pk = (PublicKey) (new PublicKeyTest()).createDataStructure();
ident.setPublicKey(pk);
SigningPublicKey k = (SigningPublicKey) (new SigningPublicKeyTest()).createDataStructure();
ident.setSigningPublicKey(k);
exception.expect(DataFormatException.class);
exception.expectMessage("Not enough data to format the router identity");
ident.writeBytes(new ByteArrayOutputStream());
}
use of net.i2p.data.PublicKey in project i2p.i2p by i2p.
the class RouterIdentityTest method testNullSigningKey.
@Test
public void testNullSigningKey() throws Exception {
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate) (new CertificateTest()).createDataStructure();
ident.setCertificate(cert);
PublicKey pk = (PublicKey) (new PublicKeyTest()).createDataStructure();
ident.setPublicKey(pk);
ident.setSigningPublicKey(null);
exception.expect(DataFormatException.class);
exception.expectMessage("Not enough data to format the router identity");
ident.writeBytes(new ByteArrayOutputStream());
}
use of net.i2p.data.PublicKey in project i2p.i2p by i2p.
the class BuildRequestor method createTunnelBuildMessage.
/**
* @since 0.7.12
*/
/**
**
*we can assume everybody supports variable now...
*keep this here for the next time we change the build protocol
* private static boolean supportsVariable(RouterContext ctx, Hash h) {
* RouterInfo ri = ctx.netDb().lookupRouterInfoLocally(h);
* if (ri == null)
* return false;
* String v = ri.getVersion();
* return VersionComparator.comp(v, MIN_VARIABLE_VERSION) >= 0;
* }
***
*/
/**
* If the tunnel is short enough, and everybody in the tunnel, and the
* OBEP or IBGW for the paired tunnel, all support the new variable-sized tunnel build message,
* then use that, otherwise the old 8-entry version.
* @return null on error
*/
private static TunnelBuildMessage createTunnelBuildMessage(RouterContext ctx, TunnelPool pool, PooledTunnelCreatorConfig cfg, TunnelInfo pairedTunnel, BuildExecutor exec) {
Log log = ctx.logManager().getLog(BuildRequestor.class);
long replyTunnel = 0;
Hash replyRouter;
boolean useVariable = SEND_VARIABLE && cfg.getLength() <= MEDIUM_RECORDS;
if (cfg.isInbound()) {
// replyTunnel = 0; // as above
replyRouter = ctx.routerHash();
/**
**
*we can assume everybody supports variable now...
*keep this here for the next time we change the build protocol
* if (useVariable) {
* // check the reply OBEP and all the tunnel peers except ourselves
* if (!supportsVariable(ctx, pairedTunnel.getPeer(pairedTunnel.getLength() - 1))) {
* useVariable = false;
* } else {
* for (int i = 0; i < cfg.getLength() - 1; i++) {
* if (!supportsVariable(ctx, cfg.getPeer(i))) {
* useVariable = false;
* break;
* }
* }
* }
* }
***
*/
} else {
replyTunnel = pairedTunnel.getReceiveTunnelId(0).getTunnelId();
replyRouter = pairedTunnel.getPeer(0);
/**
**
*we can assume everybody supports variable now
*keep this here for the next time we change the build protocol
* if (useVariable) {
* // check the reply IBGW and all the tunnel peers except ourselves
* if (!supportsVariable(ctx, replyRouter)) {
* useVariable = false;
* } else {
* for (int i = 1; i < cfg.getLength() - 1; i++) {
* if (!supportsVariable(ctx, cfg.getPeer(i))) {
* useVariable = false;
* break;
* }
* }
* }
* }
***
*/
}
// populate and encrypt the message
TunnelBuildMessage msg;
List<Integer> order;
if (useVariable) {
if (cfg.getLength() <= SHORT_RECORDS) {
msg = new VariableTunnelBuildMessage(ctx, SHORT_RECORDS);
order = new ArrayList<Integer>(SHORT_ORDER);
} else {
msg = new VariableTunnelBuildMessage(ctx, MEDIUM_RECORDS);
order = new ArrayList<Integer>(MEDIUM_ORDER);
}
} else {
msg = new TunnelBuildMessage(ctx);
order = new ArrayList<Integer>(ORDER);
}
// This is in BuildExecutor.buildTunnel() now
// long replyMessageId = ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE);
// cfg.setReplyMessageId(replyMessageId);
// randomized placement within the message
Collections.shuffle(order, ctx.random());
cfg.setReplyOrder(order);
if (log.shouldLog(Log.DEBUG))
log.debug("Build order: " + order + " for " + cfg);
for (int i = 0; i < msg.getRecordCount(); i++) {
int hop = order.get(i).intValue();
PublicKey key = null;
if (BuildMessageGenerator.isBlank(cfg, hop)) {
// erm, blank
} else {
Hash peer = cfg.getPeer(hop);
RouterInfo peerInfo = ctx.netDb().lookupRouterInfoLocally(peer);
if (peerInfo == null) {
if (log.shouldLog(Log.WARN))
log.warn("Peer selected for hop " + i + "/" + hop + " was not found locally: " + peer + " for " + cfg);
return null;
} else {
key = peerInfo.getIdentity().getPublicKey();
}
}
if (log.shouldLog(Log.DEBUG))
log.debug(cfg.getReplyMessageId() + ": record " + i + "/" + hop + " has key " + key);
BuildMessageGenerator.createRecord(i, hop, msg, cfg, replyRouter, replyTunnel, ctx, key);
}
BuildMessageGenerator.layeredEncrypt(ctx, msg, cfg, order);
return msg;
}
Aggregations