1212import org .slf4j .Logger ;
1313import org .slf4j .LoggerFactory ;
1414
15+
1516/**
1617 * Curve values from {@link ee.cyber.cdoc2.fbs.recipients.EllipticCurve} defined as enum and mapped to
1718 * known elliptic curve names and oid's
1819 */
1920public enum EllipticCurve {
21+
2022 UNKNOWN (ee .cyber .cdoc2 .fbs .recipients .EllipticCurve .UNKNOWN , null , null ),
2123 SECP384R1 (ee .cyber .cdoc2 .fbs .recipients .EllipticCurve .secp384r1 , ECKeys .SECP_384_R_1 , ECKeys .SECP_384_OID );
2224
@@ -31,6 +33,7 @@ public enum EllipticCurve {
3133 this .name = name ;
3234 this .oid = oid ;
3335 }
36+
3437 public byte getValue () {
3538 return value ;
3639 }
@@ -43,43 +46,34 @@ public String getOid() {
4346 }
4447
4548 public boolean isValidKey (ECPublicKey key ) throws GeneralSecurityException {
46- switch (this ) {
47- case SECP384R1 :
48- return ECKeys .isValidSecP384R1 (key );
49- default :
50- throw new IllegalStateException ("isValidKey not implemented for " + this );
49+ if (this == EllipticCurve .SECP384R1 ) {
50+ return ECKeys .isValidSecP384R1 (key );
5151 }
52+ throw new IllegalStateException ("isValidKey not implemented for " + this );
5253 }
5354
5455 public boolean isValidKeyPair (KeyPair keyPair ) throws GeneralSecurityException {
55- switch (this ) {
56- case SECP384R1 :
57- return ECKeys .isECSecp384r1 (keyPair );
58- default :
59- throw new IllegalStateException ("isValidKeyPair not implemented for " + this );
56+ if (this == EllipticCurve .SECP384R1 ) {
57+ return ECKeys .isECSecp384r1 (keyPair );
6058 }
59+ throw new IllegalStateException ("isValidKeyPair not implemented for " + this );
6160 }
6261
6362 /**
6463 * Key length in bytes. For secp384r1, its 384/8=48
6564 */
6665 public int getKeyLength () {
67- switch (this ) {
68- case SECP384R1 :
69- return ECKeys .SECP_384_R_1_LEN_BYTES ;
70- default :
71- throw new IllegalStateException ("getKeyLength not implemented for " + this );
66+ if (this == EllipticCurve .SECP384R1 ) {
67+ return ECKeys .SECP_384_R_1_LEN_BYTES ;
7268 }
69+ throw new IllegalStateException ("getKeyLength not implemented for " + this );
7370 }
7471
7572 public ECPublicKey decodeFromTls (ByteBuffer encoded ) throws GeneralSecurityException {
76- switch (this ) {
77- case SECP384R1 :
78- // calls also isValidSecP384R1
79- return ECKeys .decodeSecP384R1EcPublicKeyFromTls (encoded );
80- default :
81- throw new IllegalStateException ("decodeFromTls not implemented for " + this );
73+ if (this == EllipticCurve .SECP384R1 ) { // calls also isValidSecP384R1
74+ return ECKeys .decodeSecP384R1EcPublicKeyFromTls (encoded );
8275 }
76+ throw new IllegalStateException ("decodeFromTls not implemented for " + this );
8377 }
8478
8579 public KeyPair generateEcKeyPair () throws GeneralSecurityException {
@@ -101,17 +95,15 @@ public static EllipticCurve forOid(String oid) throws NoSuchAlgorithmException {
10195 }
10296
10397 public static EllipticCurve forValue (byte value ) throws NoSuchAlgorithmException {
104- switch (value ) {
105- case ee .cyber .cdoc2 .fbs .recipients .EllipticCurve .secp384r1 :
106- return SECP384R1 ;
107- default :
108- throw new NoSuchAlgorithmException ("Unknown EC curve value " + value );
98+ if (value == ee .cyber .cdoc2 .fbs .recipients .EllipticCurve .secp384r1 ) {
99+ return SECP384R1 ;
109100 }
101+ throw new NoSuchAlgorithmException ("Unknown EC curve value " + value );
110102 }
111103
112104 /**
113105 * @param publicKey ECPublicKey
114- * @return
106+ * @return EllipticCurve
115107 * @throws NoSuchAlgorithmException if publicKey EC curve is not supported
116108 * @throws InvalidParameterSpecException
117109 * @throws NoSuchProviderException
@@ -120,8 +112,7 @@ public static EllipticCurve forValue(byte value) throws NoSuchAlgorithmException
120112 public static EllipticCurve forPubKey (PublicKey publicKey ) throws NoSuchAlgorithmException ,
121113 InvalidParameterSpecException , NoSuchProviderException , InvalidKeyException {
122114
123- if (publicKey instanceof ECPublicKey ) {
124- ECPublicKey ecPublicKey = (ECPublicKey ) publicKey ;
115+ if (publicKey instanceof ECPublicKey ecPublicKey ) {
125116 return forOid (ECKeys .getCurveOid (ecPublicKey ));
126117 } else {
127118 throw new InvalidKeyException ("Unsupported key algorithm " + publicKey .getAlgorithm ());
@@ -150,4 +141,5 @@ public static boolean isSupported(PublicKey publicKey) {
150141 public static String [] names () {
151142 return ee .cyber .cdoc2 .fbs .recipients .EllipticCurve .names ;
152143 }
144+
153145}
0 commit comments