Data: Fix LS2 support for unknown enc. types (proposal 123)

This commit is contained in:
zzz
2019-01-01 17:48:31 +00:00
parent d95d81aea7
commit 4ae57f4f01
2 changed files with 45 additions and 9 deletions

View File

@ -37,8 +37,6 @@ public class LeaseSet2 extends LeaseSet {
protected Signature _offlineSignature; protected Signature _offlineSignature;
// may be null // may be null
protected Properties _options; protected Properties _options;
// only used for unknown types; else use _encryptionKey.getType()
private int _encType;
// only used if more than one key, otherwise null // only used if more than one key, otherwise null
private List<PublicKey> _encryptionKeys; private List<PublicKey> _encryptionKeys;
@ -236,18 +234,21 @@ public class LeaseSet2 extends LeaseSet {
if (numKeys > 1) if (numKeys > 1)
_encryptionKeys = new ArrayList<PublicKey>(numKeys); _encryptionKeys = new ArrayList<PublicKey>(numKeys);
for (int i = 0; i < numKeys; i++) { for (int i = 0; i < numKeys; i++) {
_encType = (int) DataHelper.readLong(in, 2); int encType = (int) DataHelper.readLong(in, 2);
int encLen = (int) DataHelper.readLong(in, 2); int encLen = (int) DataHelper.readLong(in, 2);
// TODO // TODO
if (_encType == 0) { if (encType == 0) {
_encryptionKey = PublicKey.create(in); _encryptionKey = PublicKey.create(in);
} else { } else {
EncType type = EncType.getByCode(_encType); EncType type = EncType.getByCode(encType);
// type will be null if unknown // type will be null if unknown
byte[] encKey = new byte[encLen]; byte[] encKey = new byte[encLen];
DataHelper.read(in, encKey); DataHelper.read(in, encKey);
// this will throw IAE if type is non-null and length is wrong // this will throw IAE if type is non-null and length is wrong
_encryptionKey = new PublicKey(type, encKey); if (type != null)
_encryptionKey = new PublicKey(type, encKey);
else
_encryptionKey = new PublicKey(encType, encKey);
} }
if (numKeys > 1) if (numKeys > 1)
_encryptionKeys.add(_encryptionKey); _encryptionKeys.add(_encryptionKey);
@ -299,7 +300,7 @@ public class LeaseSet2 extends LeaseSet {
if (type != null) { if (type != null) {
DataHelper.writeLong(out, 2, type.getCode()); DataHelper.writeLong(out, 2, type.getCode());
} else { } else {
DataHelper.writeLong(out, 2, _encType); DataHelper.writeLong(out, 2, key.getUnknownTypeCode());
} }
DataHelper.writeLong(out, 2, key.length()); DataHelper.writeLong(out, 2, key.length());
key.writeBytes(out); key.writeBytes(out);
@ -560,6 +561,14 @@ public class LeaseSet2 extends LeaseSet {
net.i2p.crypto.KeyPair encKeys2 = net.i2p.crypto.KeyGenerator.getInstance().generatePKIKeys(net.i2p.crypto.EncType.ECIES_X25519); net.i2p.crypto.KeyPair encKeys2 = net.i2p.crypto.KeyGenerator.getInstance().generatePKIKeys(net.i2p.crypto.EncType.ECIES_X25519);
pubKey = encKeys2.getPublic(); pubKey = encKeys2.getPublic();
ls2.addEncryptionKey(pubKey); ls2.addEncryptionKey(pubKey);
byte[] b = new byte[99];
rand.nextBytes(b);
pubKey = new PublicKey(77, b);
ls2.addEncryptionKey(pubKey);
b = new byte[55];
rand.nextBytes(b);
pubKey = new PublicKey(177, b);
ls2.addEncryptionKey(pubKey);
SigningPrivateKey spk = pkf.getSigningPrivKey(); SigningPrivateKey spk = pkf.getSigningPrivKey();
if (offline) { if (offline) {
now += 365*24*60*60*1000L; now += 365*24*60*60*1000L;

View File

@ -32,6 +32,7 @@ public class PublicKey extends SimpleDataStructure {
private static final SDSCache<PublicKey> _cache = new SDSCache<PublicKey>(PublicKey.class, KEYSIZE_BYTES, CACHE_SIZE); private static final SDSCache<PublicKey> _cache = new SDSCache<PublicKey>(PublicKey.class, KEYSIZE_BYTES, CACHE_SIZE);
private final EncType _type; private final EncType _type;
private final int _unknownTypeCode;
/** /**
* Pull from cache or return new. * Pull from cache or return new.
@ -63,6 +64,7 @@ public class PublicKey extends SimpleDataStructure {
public PublicKey(EncType type) { public PublicKey(EncType type) {
super(); super();
_type = type; _type = type;
_unknownTypeCode = (type != null) ? type.getCode() : -1;
} }
/** @param data must be non-null */ /** @param data must be non-null */
@ -82,7 +84,24 @@ public class PublicKey extends SimpleDataStructure {
setData(data); setData(data);
} }
/** constructs from base64 /**
* Unknown type only.
* @param typeCode must not match a known type. 1-255
* @param data must be non-null
* @since 0.9.38
*/
public PublicKey(int typeCode, byte data[]) {
_type = null;
if (data == null)
throw new IllegalArgumentException("Data must be specified");
_data = data;
if (typeCode <= 0 || typeCode > 255)
throw new IllegalArgumentException();
_unknownTypeCode = typeCode;
}
/**
* Constructs from base64. ElGamal only.
* @param base64Data a string of base64 data (the output of .toBase64() called * @param base64Data a string of base64 data (the output of .toBase64() called
* on a prior instance of PublicKey * on a prior instance of PublicKey
*/ */
@ -107,6 +126,14 @@ public class PublicKey extends SimpleDataStructure {
return _type; return _type;
} }
/**
* Only valid if getType() returns null
* @since 0.9.38
*/
public int getUnknownTypeCode() {
return _unknownTypeCode;
}
/** /**
* @since 0.9.17 * @since 0.9.17
*/ */
@ -120,7 +147,7 @@ public class PublicKey extends SimpleDataStructure {
@Override @Override
public String toString() { public String toString() {
StringBuilder buf = new StringBuilder(64); StringBuilder buf = new StringBuilder(64);
buf.append("[PublicKey ").append((_type != null) ? _type.toString() : "unknown type").append(' '); buf.append("[PublicKey ").append((_type != null) ? _type.toString() : "unknown type: " + _unknownTypeCode).append(' ');
if (_data == null) { if (_data == null) {
buf.append("null"); buf.append("null");
} else { } else {