1212import org .slf4j .Logger ;
1313import org .slf4j .LoggerFactory ;
1414
15+ /**
16+ * JSON Web key representation of a Elliptic curve key.
17+ According to RFC 7517 a JWK representation of a EC key can look like
18+ this::
19+ {"kty":"EC",
20+ "crv":"P-256",
21+ "x":"MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
22+ "y":"4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
23+ "d":"870MB6gfuTJ4HtUnUvYMyJpr5eUZNP4Bk43bVdj3eAE"
24+ }
25+
26+ Parameters according to https://tools.ietf.org/html/rfc7518#section-6.2
27+ */
1528public class ECKey extends Key {
1629
1730 private String crv ;
@@ -20,6 +33,7 @@ public class ECKey extends Key {
2033 private Object d ;
2134 private Object curve ;
2235 final private static Logger logger = LoggerFactory .getLogger (ECKey .class );
36+ //The elliptic curve specific attributes
2337 private static Set <String > longs = new HashSet <String >(Arrays .asList ("x" , "y" , "d" ));
2438 protected static Set <String > members = new HashSet <>(Arrays .asList ("kty" , "alg" , "use" , "kid" , "crv" , "x" , "y" , "d" ));
2539 public static Set <String > publicMembers = new HashSet <>(Arrays .asList ("kty" , "alg" , "use" , "kid" , "crv" , "x" , "y" ));
@@ -50,6 +64,27 @@ public ECKey() {
5064 this ("EC" , "" , "" , "" , null , "" , null , null , null , null , null );
5165 }
5266
67+ /**
68+ * Starting with information gathered from the on-the-wire representation
69+ of an elliptic curve key (a JWK) initiate an
70+ cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey
71+ or EllipticCurvePrivateKey instance. So we have to get from having::
72+ {
73+ "kty":"EC",
74+ "crv":"P-256",
75+ "x":"MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
76+ "y":"4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
77+ "d":"870MB6gfuTJ4HtUnUvYMyJpr5eUZNP4Bk43bVdj3eAE"
78+ }
79+ to having a key that can be used for signing/verifying and/or
80+ encrypting/decrypting.
81+ If 'd' has value then we're dealing with a private key otherwise
82+ a public key. 'x' and 'y' must have values.
83+ If this.key has a value beforehand this will overwrite whatever
84+ was there to begin with.
85+
86+ x, y and d (if present) must be strings or bytes.
87+ */
5388 public void deserialize () {
5489 try {
5590 if (!(this .x instanceof Number )) {
@@ -88,6 +123,14 @@ public List<Object> getKey(boolean isPrivate) {
88123 }
89124 }
90125
126+ /**
127+ * Go from a
128+ cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
129+ or EllipticCurvePublicKey instance to a JWK representation.
130+ * @param isPrivate: Whether we should include the private parts or not.
131+ * @return A JWK as a hashmap
132+ * @throws SerializationNotPossible
133+ */
91134 public Object serialize (boolean isPrivate ) throws SerializationNotPossible {
92135 if (this .crv == null && this .curve == null ) {
93136 throw new SerializationNotPossible ();
@@ -105,6 +148,11 @@ public Object serialize(boolean isPrivate) throws SerializationNotPossible {
105148 return args ;
106149 }
107150
151+ /**
152+ * Load an Elliptic curve key
153+ * @param key: An elliptic curve key instance
154+ * @return
155+ */
108156 public ECKey loadKey (Key key ) {
109157 this .curve = key ;
110158 //how to return multiple objects in Java?
@@ -116,6 +164,7 @@ public List<Object> getDecryptionKey() {
116164 }
117165
118166 public List <Object > getEncryptionKey (boolean isPrivate ) {
167+ //both for encryption and decryption.
119168 return this .getKey (isPrivate );
120169 }
121170
0 commit comments