From bf3a7d6ef749daa45cb1b4ff96b8f58b9f3dea18 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Mon, 17 Dec 2018 13:59:49 +0000
Subject: [PATCH] Data: More work on Encrypted LS2 (proposal 123)

---
 .../src/net/i2p/data/EncryptedLeaseSet.java   | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/core/java/src/net/i2p/data/EncryptedLeaseSet.java b/core/java/src/net/i2p/data/EncryptedLeaseSet.java
index 23de3e9453..d7bac9f2aa 100644
--- a/core/java/src/net/i2p/data/EncryptedLeaseSet.java
+++ b/core/java/src/net/i2p/data/EncryptedLeaseSet.java
@@ -4,6 +4,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import net.i2p.crypto.SHA256Generator;
 import net.i2p.crypto.SigType;
 import net.i2p.util.Clock;
 
@@ -18,6 +19,8 @@ public class EncryptedLeaseSet extends LeaseSet2 {
 
     // includes IV and MAC
     private byte[] _encryptedData;
+    private LeaseSet2 _decryptedLS2;
+    private Hash __calculatedHash;
 
     private static final int MIN_ENCRYPTED_SIZE = 8 + 16;
     private static final int MAX_ENCRYPTED_SIZE = 4096;
@@ -33,6 +36,24 @@ public class EncryptedLeaseSet extends LeaseSet2 {
         return KEY_TYPE_ENCRYPTED_LS2;
     }
 
+    /**
+     *  @return 0-16, or 0 if not decrypted.
+     */
+    @Override
+    public int getLeaseCount() {
+        // TODO attempt decryption
+        return _decryptedLS2 != null ? _decryptedLS2.getLeaseCount() : 0;
+    }
+
+    /**
+     *  @return null if not decrypted.
+     */
+    @Override
+    public Lease getLease(int index) {
+        // TODO attempt decryption
+        return _decryptedLS2 != null ? _decryptedLS2.getLease(index) : null;
+    }
+
     /**
      *  This does NOT validate the signature
      *
@@ -156,6 +177,27 @@ public class EncryptedLeaseSet extends LeaseSet2 {
         return rv;
     }
 
+    /**
+     *  Overridden because we have a blinded key, not a dest.
+     *  This is the hash of the signing public key type and the signing public key.
+     *  Throws IllegalStateException if not initialized.
+     *
+     *  @throws IllegalStateException
+     */
+    @Override
+    public Hash getHash() {
+        if (__calculatedHash == null) {
+            if (_signingKey == null)
+                throw new IllegalStateException();
+            int len = _signingKey.length();
+            byte[] b = new byte[2 + len];
+            DataHelper.toLong(b, 0, 2, _signingKey.getType().getCode());
+            System.arraycopy(_signingKey.getData(), 0, b, 2, len);
+            __calculatedHash = SHA256Generator.getInstance().calculateHash(b);
+        }
+        return __calculatedHash;
+    }
+
     // encrypt / decrypt TODO
 
     @Override
-- 
GitLab