Data: Check data length in typed constructors

Check for type match in KeyPair
Mark placeholder EncTypes 1-3 as unavailable
This commit is contained in:
zzz
2018-11-30 15:56:27 +00:00
parent 2487bca47c
commit f1689187a4
4 changed files with 13 additions and 6 deletions

View File

@ -113,6 +113,9 @@ public enum EncType {
private boolean x_isAvailable() { private boolean x_isAvailable() {
if (ELGAMAL_2048 == this) if (ELGAMAL_2048 == this)
return true; return true;
// EC types are placeholders for now
if (base == EncAlgo.EC)
return false;
try { try {
getParams(); getParams();
} catch (InvalidParameterSpecException e) { } catch (InvalidParameterSpecException e) {

View File

@ -13,9 +13,15 @@ public class KeyPair {
private final PublicKey pub; private final PublicKey pub;
private final PrivateKey priv; private final PrivateKey priv;
/**
* @param publicKey non-null, same EncType as privateKey
* @param privateKey non-null, same EncType as publicKey
*/
public KeyPair(PublicKey publicKey, PrivateKey privateKey) { public KeyPair(PublicKey publicKey, PrivateKey privateKey) {
pub = publicKey; pub = publicKey;
priv = privateKey; priv = privateKey;
if (pub.getType() != priv.getType())
throw new IllegalArgumentException();
} }
public PublicKey getPublic() { public PublicKey getPublic() {

View File

@ -53,11 +53,10 @@ public class PrivateKey extends SimpleDataStructure {
* @since 0.9.38 * @since 0.9.38
*/ */
public PrivateKey(EncType type, byte data[]) { public PrivateKey(EncType type, byte data[]) {
super(); this(type);
_type = type;
if (data == null) if (data == null)
throw new IllegalArgumentException("Data must be specified"); throw new IllegalArgumentException("Data must be specified");
_data = data; setData(data);
} }
/** constructs from base64 /** constructs from base64

View File

@ -76,11 +76,10 @@ public class PublicKey extends SimpleDataStructure {
* @since 0.9.38 * @since 0.9.38
*/ */
public PublicKey(EncType type, byte data[]) { public PublicKey(EncType type, byte data[]) {
super(); this(type);
_type = type;
if (data == null) if (data == null)
throw new IllegalArgumentException("Data must be specified"); throw new IllegalArgumentException("Data must be specified");
_data = data; setData(data);
} }
/** constructs from base64 /** constructs from base64