|
| 1 | +package com.auth0.jwt.oicmsg; |
| 2 | + |
| 3 | +import com.auth0.jwt.exceptions.oicmsg_exceptions.HeaderError; |
| 4 | +import com.auth0.jwt.exceptions.oicmsg_exceptions.SerializationNotPossible; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import java.security.spec.EllipticCurve; |
| 9 | +import java.text.ParseException; |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +public class ECKey extends Key{ |
| 13 | + |
| 14 | + private String crv; |
| 15 | + private Object x; |
| 16 | + private Object y; |
| 17 | + private Object d; |
| 18 | + private Object curve; |
| 19 | + final private static Logger logger = LoggerFactory.getLogger(ECKey.class); |
| 20 | + private static Set<String> longs = new HashSet<String>(Arrays.asList("x", "y", "d")); |
| 21 | + protected static Set<String> members = new HashSet<>(Arrays.asList("kty", "alg", "use", "kid", "crv", "x", "y", "d")); |
| 22 | + public static Set<String> publicMembers = new HashSet<>(Arrays.asList("kty", "alg", "use", "kid", "crv", "x", "y")); |
| 23 | + protected static Set<String> required = new HashSet<>(Arrays.asList("crv", "key", "x", "y")); |
| 24 | + |
| 25 | + public ECKey(String kty, String alg, String use, String kid, Key key, String crv, Object x, Object y, Object d, |
| 26 | + Object curve, Map<String,String> args) { |
| 27 | + super(kty, alg, use, kid, "", "", "", key, args); |
| 28 | + this.crv = crv; |
| 29 | + this.x = x; |
| 30 | + this.y = y; |
| 31 | + this.d = d; |
| 32 | + this.curve = curve; |
| 33 | + |
| 34 | + if(this.crv != null && this.curve == null) { |
| 35 | + try { |
| 36 | + this.verify(); |
| 37 | + } catch (HeaderError headerError) { |
| 38 | + headerError.printStackTrace(); |
| 39 | + } |
| 40 | + this.deserialize(); |
| 41 | + } else if(this.getKey() != null && (this.crv == null && this.curve == null)) { |
| 42 | + this.loadKey(key); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public ECKey() { |
| 47 | + this("EC", "", "", "", null, "", null, null, null, null, null); |
| 48 | + } |
| 49 | + |
| 50 | + public void deserialize() { |
| 51 | + try { |
| 52 | + if(!(this.x instanceof Number)) { |
| 53 | + this.x = deser(this.x); |
| 54 | + } |
| 55 | + if(!(this.y instanceof Number)) { |
| 56 | + this.y = deser(this.y); |
| 57 | + } |
| 58 | + } catch (ParseException e) { |
| 59 | + logger.error("Couldn't parse value"); |
| 60 | + } |
| 61 | + |
| 62 | + this.curve = byName(this.crv); |
| 63 | + if(this.d != null) { |
| 64 | + if(this.d instanceof String) { |
| 65 | + this.d = deser(this.d); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private EllipticCurve byName(String name) { |
| 71 | + if(name.equals("P-256")) { |
| 72 | + return EllipticCurve(); |
| 73 | + } else if(name.equals("P-384")) { |
| 74 | + return EllipticCurve(); |
| 75 | + } else if(name.equals("P-521")) { |
| 76 | + return EllipticCurve(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public List<Object> getKey(boolean isPrivate) { |
| 81 | + if(isPrivate) { |
| 82 | + return new ArrayList<>(Arrays.asList(this.d)); |
| 83 | + } else { |
| 84 | + return new ArrayList<>(Arrays.asList(this.x, this.y)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public Object serialize(boolean isPrivate) throws SerializationNotPossible { |
| 89 | + if(this.crv == null && this.curve == null) { |
| 90 | + throw new SerializationNotPossible(); |
| 91 | + } |
| 92 | + |
| 93 | + Map<String, String> args = common(); |
| 94 | + args.put("crv", this.curve.getClass().getName()); |
| 95 | + args.put("x", longToBase64(this.x)); |
| 96 | + args.put("y", longToBase64(this.y)); |
| 97 | + |
| 98 | + if(isPrivate && this.d != null) { |
| 99 | + args.put("d", longToBase64(this.d)); |
| 100 | + } |
| 101 | + |
| 102 | + return args; |
| 103 | + } |
| 104 | + |
| 105 | + public ECKey loadKey(Key key) { |
| 106 | + this.curve = key; |
| 107 | + //how to return multiple objects in Java? |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public List<Object> getDecryptionKey() { |
| 112 | + return this.getKey(true); |
| 113 | + } |
| 114 | + |
| 115 | + public List<Object> getEncryptionKey(boolean isPrivate) { |
| 116 | + return this.getKey(isPrivate); |
| 117 | + } |
| 118 | +} |
0 commit comments