Data: Add LeaseSet methods to get encryption key by type

This commit is contained in:
zzz
2019-10-23 13:13:13 +00:00
parent 2c2f90089b
commit d84fc4f0c8
3 changed files with 58 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.crypto.Blinding; import net.i2p.crypto.Blinding;
@ -132,6 +133,20 @@ public class EncryptedLeaseSet extends LeaseSet2 {
return super.getEncryptionKeys(); return super.getEncryptionKeys();
} }
/**
* If more than one key, return the first supported one.
* If none supported, return null.
*
* @return first supported key or null
* @since 0.9.44
*/
@Override
public PublicKey getEncryptionKey(Set<EncType> supported) {
if (_decryptedLS2 != null)
return _decryptedLS2.getEncryptionKey(supported);
return super.getEncryptionKey(supported);
}
/** /**
* Overridden to set the blinded key. * Overridden to set the blinded key.
* setSecret() MUST be called before this for non-null secret, or alpha will be wrong. * setSecret() MUST be called before this for non-null secret, or alpha will be wrong.

View File

@ -16,9 +16,11 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.crypto.DSAEngine; import net.i2p.crypto.DSAEngine;
import net.i2p.crypto.EncType;
import net.i2p.crypto.SigType; import net.i2p.crypto.SigType;
import net.i2p.util.Clock; import net.i2p.util.Clock;
import net.i2p.util.Log; import net.i2p.util.Log;
@ -131,12 +133,29 @@ public class LeaseSet extends DatabaseEntry {
return _encryptionKey; return _encryptionKey;
} }
/**
* If more than one key, return the first supported one.
* If none supported, return null.
*
* @param supported what return types are allowed
* @return ElGamal key or null if ElGamal not in supported
* @since 0.9.44
*/
public PublicKey getEncryptionKey(Set<EncType> supported) {
if (supported.contains(EncType.ELGAMAL_2048))
return _encryptionKey;
return null;
}
/** /**
* @throws IllegalStateException if already signed * @throws IllegalStateException if already signed
*/ */
public void setEncryptionKey(PublicKey encryptionKey) { public void setEncryptionKey(PublicKey encryptionKey) {
if (_signature != null) if (_signature != null)
throw new IllegalStateException(); throw new IllegalStateException();
// subclasses may set an ECIES key
//if (encryptionKey.getType() != EncType.ELGAMAL_2048)
// throw new IllegalArgumentException();
_encryptionKey = encryptionKey; _encryptionKey = encryptionKey;
} }

View File

@ -9,6 +9,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.crypto.DSAEngine; import net.i2p.crypto.DSAEngine;
@ -140,8 +141,26 @@ public class LeaseSet2 extends LeaseSet {
return _encryptionKey; return _encryptionKey;
} }
/**
* If more than one key, return the first supported one.
* If none supported, return null.
*
* @return first supported key or null
* @since 0.9.44
*/
@Override
public PublicKey getEncryptionKey(Set<EncType> supported) {
for (PublicKey pk : getEncryptionKeys()) {
if (supported.contains(pk.getType()))
return pk;
}
return null;
}
/** /**
* Add an encryption key. * Add an encryption key.
*
* Encryption keys should be added in order of server preference, most-preferred first.
*/ */
public void addEncryptionKey(PublicKey key) { public void addEncryptionKey(PublicKey key) {
if (_encryptionKey == null) { if (_encryptionKey == null) {
@ -160,6 +179,11 @@ public class LeaseSet2 extends LeaseSet {
/** /**
* This returns all the keys. getEncryptionKey() returns the first one. * This returns all the keys. getEncryptionKey() returns the first one.
*
* Encryption keys should be in order of server preference, most-preferred first.
* Client behavior should be to select the first key with a supported encryption type.
* Clients may use other selection algorithms based on encryption support, relative performance, and other factors.
*
* @return not a copy, do not modify, null if none * @return not a copy, do not modify, null if none
*/ */
public List<PublicKey> getEncryptionKeys() { public List<PublicKey> getEncryptionKeys() {