move toBytes into RSAKeyFormat

This commit is contained in:
Dominik Schürmann
2021-02-23 18:18:43 +01:00
parent 2a0625e615
commit 0ec0c34cb9
3 changed files with 23 additions and 20 deletions

View File

@@ -17,7 +17,6 @@
package org.sufficientlysecure.keychain.securitytoken; package org.sufficientlysecure.keychain.securitytoken;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.nist.NISTNamedCurves; import org.bouncycastle.asn1.nist.NISTNamedCurves;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags; import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
@@ -44,13 +43,7 @@ public abstract class KeyFormat {
public static KeyFormat fromBytes(byte[] bytes) { public static KeyFormat fromBytes(byte[] bytes) {
switch (bytes[0]) { switch (bytes[0]) {
case PublicKeyAlgorithmTags.RSA_GENERAL: case PublicKeyAlgorithmTags.RSA_GENERAL:
if (bytes.length < 6) { return RSAKeyFormat.fromBytes(bytes);
throw new IllegalArgumentException("Bad length for RSA attributes");
}
return new RSAKeyFormat(bytes[1] << 8 | bytes[2],
bytes[3] << 8 | bytes[4],
RSAKeyFormat.RSAAlgorithmFormat.from(bytes[5]));
case PublicKeyAlgorithmTags.ECDH: case PublicKeyAlgorithmTags.ECDH:
case PublicKeyAlgorithmTags.ECDSA: case PublicKeyAlgorithmTags.ECDSA:
return ECKeyFormat.getInstanceFromBytes(bytes); return ECKeyFormat.getInstanceFromBytes(bytes);

View File

@@ -17,6 +17,7 @@
package org.sufficientlysecure.keychain.securitytoken; package org.sufficientlysecure.keychain.securitytoken;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd; import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd;
@@ -44,37 +45,46 @@ public class RSAKeyFormat extends KeyFormat {
return mExponentLength; return mExponentLength;
} }
public static KeyFormat fromBytes(byte[] bytes) {
if (bytes.length < 6) {
throw new IllegalArgumentException("Bad length for RSA attributes");
}
return new RSAKeyFormat(bytes[1] << 8 | bytes[2],
bytes[3] << 8 | bytes[4],
RSAKeyFormat.RSAAlgorithmFormat.from(bytes[5]));
}
public RSAAlgorithmFormat getAlgorithmFormat() { public RSAAlgorithmFormat getAlgorithmFormat() {
return mRSAAlgorithmFormat; return mRSAAlgorithmFormat;
} }
public enum RSAAlgorithmFormat { public enum RSAAlgorithmFormat {
STANDARD((byte) 0, false, false), STANDARD((byte) 0x00, false, false),
STANDARD_WITH_MODULUS((byte) 1, false, true), STANDARD_WITH_MODULUS((byte) 0x01, false, true),
CRT((byte) 2, true, false), CRT((byte) 0x02, true, false),
CRT_WITH_MODULUS((byte) 3, true, true); CRT_WITH_MODULUS((byte) 0x03, true, true);
private byte mValue; private byte mImportFormat;
private boolean mIncludeModulus; private boolean mIncludeModulus;
private boolean mIncludeCrt; private boolean mIncludeCrt;
RSAAlgorithmFormat(byte value, boolean includeCrt, boolean includeModulus) { RSAAlgorithmFormat(byte importFormat, boolean includeCrt, boolean includeModulus) {
mValue = value; mImportFormat = importFormat;
mIncludeModulus = includeModulus; mIncludeModulus = includeModulus;
mIncludeCrt = includeCrt; mIncludeCrt = includeCrt;
} }
public static RSAAlgorithmFormat from(byte b) { public static RSAAlgorithmFormat from(byte importFormatByte) {
for (RSAAlgorithmFormat format : values()) { for (RSAAlgorithmFormat format : values()) {
if (format.mValue == b) { if (format.mImportFormat == importFormatByte) {
return format; return format;
} }
} }
return null; return null;
} }
public byte getValue() { public byte getImportFormat() {
return mValue; return mImportFormat;
} }
public boolean isIncludeModulus() { public boolean isIncludeModulus() {

View File

@@ -69,7 +69,7 @@ public class SecurityTokenUtils {
attrs[i++] = (byte) (modulusLength & 0xff); attrs[i++] = (byte) (modulusLength & 0xff);
attrs[i++] = (byte) ((exponentLength >> 8) & 0xff); attrs[i++] = (byte) ((exponentLength >> 8) & 0xff);
attrs[i++] = (byte) (exponentLength & 0xff); attrs[i++] = (byte) (exponentLength & 0xff);
attrs[i] = algorithmFormat.getValue(); attrs[i] = algorithmFormat.getImportFormat();
return attrs; return attrs;
} }