~i2p.packages/i2p/trunk

« back to all changes in this revision

Viewing changes to router/java/src/net/i2p/router/message/GarlicMessageBuilder.java

  • Committer: zzz
  • Date: 2020-10-15 11:50:11 UTC
  • Revision ID: git-v1:e54950e02e4276fe69355d3888bdf4299a673401
Router: MessageWrapper.wrap() and GMB support for ECIES (prop. #156 WIP)
NetDB parts still TODO
Remove PK param from GMB.buildECIESMessage(), already in config

Show diffs side-by-side

added added

removed removed

Lines of Context:
275
275
     * Called by OCMJH only.
276
276
     *
277
277
     * @param ctx scope
278
 
     * @param config how/what to wrap
279
 
     * @param target public key of the location being garlic routed to (may be null if we 
280
 
     *               know the encryptKey and encryptTag)
 
278
     * @param config how/what to wrap, must have key set with setRecipientPublicKey()
281
279
     * @param callback may be null
282
280
     * @return null if expired or on other errors
283
281
     * @throws IllegalArgumentException on error
284
282
     * @since 0.9.44
285
283
     */
286
284
    static GarlicMessage buildECIESMessage(RouterContext ctx, GarlicConfig config,
287
 
                                           PublicKey target, Hash from, Destination to, SessionKeyManager skm,
 
285
                                           Hash from, Destination to, SessionKeyManager skm,
288
286
                                           ReplyCallback callback) {
289
287
        PublicKey key = config.getRecipientPublicKey();
290
288
        if (key.getType() != EncType.ECIES_X25519)
315
313
                log.warn("No SKM for " + from.toBase32());
316
314
            return null;
317
315
        }
318
 
        byte encData[] = ctx.eciesEngine().encrypt(cloveSet, target, to, priv, rskm, callback);
 
316
        byte encData[] = ctx.eciesEngine().encrypt(cloveSet, key, to, priv, rskm, callback);
319
317
        if (encData == null) {
320
318
            if (log.shouldWarn())
321
319
                log.warn("Encrypt fail for " + from.toBase32());
334
332
        return msg;
335
333
    }
336
334
    
 
335
    /**
 
336
     * Encrypt from an anonymous source.
 
337
     * ECIES_X25519 only.
 
338
     * Called by MessageWrapper only.
 
339
     *
 
340
     * @param ctx scope
 
341
     * @param config how/what to wrap, must have key set with setRecipientPublicKey()
 
342
     * @throws IllegalArgumentException on error
 
343
     * @since 0.9.48
 
344
     */
 
345
    public static GarlicMessage buildECIESMessage(RouterContext ctx, GarlicConfig config) {
 
346
        PublicKey key = config.getRecipientPublicKey();
 
347
        if (key.getType() != EncType.ECIES_X25519)
 
348
            throw new IllegalArgumentException();
 
349
        Log log = ctx.logManager().getLog(GarlicMessageBuilder.class);
 
350
        GarlicMessage msg = new GarlicMessage(ctx);
 
351
        CloveSet cloveSet = buildECIESCloveSet(ctx, config);
 
352
        byte encData[] = ctx.eciesEngine().encrypt(cloveSet, key);
 
353
        if (encData == null) {
 
354
            if (log.shouldWarn())
 
355
                log.warn("Encrypt fail for " + config);
 
356
            return null;
 
357
        }
 
358
        msg.setData(encData);
 
359
        msg.setMessageExpiration(config.getExpiration());
 
360
        long timeFromNow = config.getExpiration() - ctx.clock().now();
 
361
        if (timeFromNow < 1*1000) {
 
362
            if (log.shouldDebug())
 
363
                log.debug("Building a message expiring in " + timeFromNow + "ms: " + config, new Exception("created by"));
 
364
            return null;
 
365
        }
 
366
        if (log.shouldDebug())
 
367
            log.debug("Built ECIES CloveSet (" + config.getCloveCount() + " cloves) in " + msg);
 
368
        return msg;
 
369
    }
 
370
    
337
371
/****
338
372
    private static void noteWrap(RouterContext ctx, GarlicMessage wrapper, GarlicConfig contained) {
339
373
        for (int i = 0; i < contained.getCloveCount(); i++) {