|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.annotation.JsonCreator;
|
4 | 4 | import com.fasterxml.jackson.annotation.JsonValue;
|
| 5 | +import java.security.Key; |
5 | 6 | import java.util.stream.Stream;
|
6 |
| -import lombok.EqualsAndHashCode; |
| 7 | +import lombok.Getter; |
7 | 8 |
|
8 | 9 | /**
|
9 |
| - * Enum-like collection of known <code>KEY_PROTECTION</code> values. |
| 10 | + * The KEY_PROTECTION constants are flags in a bit field represented as a 16 bit long integer. They |
| 11 | + * describe the method an authenticator uses to protect the private key material for FIDO |
| 12 | + * registrations. Refer to [UAFAuthnrCommands] for more details on the relevance of keys and key |
| 13 | + * protection. These constants are reported and queried through the UAF Discovery APIs and used to |
| 14 | + * form authenticator policies in UAF protocol messages. Each constant has a case-sensitive string |
| 15 | + * representation (in quotes), which is used in the authoritative metadata for FIDO authenticators. |
10 | 16 | *
|
11 |
| - * <p>Constants in this class behave like enum constants. Use {@link #of(short)} to parse raw <code> |
12 |
| - * int</code> values. |
13 |
| - * |
14 |
| - * @see #of(short) |
| 17 | + * @see #fromValue(short) |
| 18 | + * @see #fromName(String) |
15 | 19 | * @see <a
|
16 |
| - * href="https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#key-protection-types">FIDO |
| 20 | + * href="https://fidoalliance.org/specs/common-specs/fido-registry-v2.1-ps-20191217.html#key-protection-types">FIDO |
17 | 21 | * Registry of Predefined Values §3.2 Key Protection Types</a>
|
18 | 22 | */
|
19 |
| -@EqualsAndHashCode |
20 |
| -public class KeyProtectionType { |
| 23 | +@Getter |
| 24 | +public enum KeyProtectionType { |
21 | 25 |
|
22 | 26 | /**
|
23 |
| - * This flag must be set if the authenticator uses software-based key management. Mutually |
24 |
| - * exclusive with {@link #KEY_PROTECTION_HARDWARE}, {@link #KEY_PROTECTION_TEE}, {@link |
25 |
| - * #KEY_PROTECTION_SECURE_ELEMENT}. |
| 27 | + * This flag MUST be set if the authenticator uses software-based key management. Exclusive in |
| 28 | + * authenticator metadata with {@link #KEY_PROTECTION_HARDWARE}, {@link #KEY_PROTECTION_TEE}, |
| 29 | + * {@link #KEY_PROTECTION_SECURE_ELEMENT}. |
26 | 30 | *
|
27 | 31 | * <p>NOTE: The above requirements apply to authenticators; this library DOES NOT enforce them.
|
28 | 32 | *
|
29 | 33 | * @see <a
|
30 |
| - * href="https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#key-protection-types">FIDO |
| 34 | + * href="https://fidoalliance.org/specs/common-specs/fido-registry-v2.1-ps-20191217.html#key-protection-types">FIDO |
31 | 35 | * Registry of Predefined Values §3.2 Key Protection Types</a>
|
32 | 36 | */
|
33 |
| - public static final KeyProtectionType KEY_PROTECTION_SOFTWARE = |
34 |
| - new KeyProtectionType((short) 0x0001, "SOFTWARE"); |
| 37 | + KEY_PROTECTION_SOFTWARE((short) 0x0001, "software"), |
35 | 38 |
|
36 | 39 | /**
|
37 |
| - * This flag should be set if the authenticator uses hardware-based key management. Mutually |
38 |
| - * exclusive with {@link #KEY_PROTECTION_SOFTWARE}. |
| 40 | + * This flag SHOULD be set if the authenticator uses hardware-based key management. Exclusive in |
| 41 | + * authenticator metadata with {@link #KEY_PROTECTION_SOFTWARE}. |
39 | 42 | *
|
40 | 43 | * <p>NOTE: The above requirements apply to authenticators; this library DOES NOT enforce them.
|
41 | 44 | *
|
42 | 45 | * @see <a
|
43 |
| - * href="https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#key-protection-types">FIDO |
| 46 | + * href="https://fidoalliance.org/specs/common-specs/fido-registry-v2.1-ps-20191217.html#key-protection-types">FIDO |
44 | 47 | * Registry of Predefined Values §3.2 Key Protection Types</a>
|
45 | 48 | */
|
46 |
| - public static final KeyProtectionType KEY_PROTECTION_HARDWARE = |
47 |
| - new KeyProtectionType((short) 0x0002, "HARDWARE"); |
| 49 | + KEY_PROTECTION_HARDWARE((short) 0x0002, "hardware"), |
48 | 50 |
|
49 | 51 | /**
|
50 |
| - * This flag should be set if the authenticator uses the Trusted Execution Environment for key |
51 |
| - * management. In authenticator metadata, this flag should be set in conjunction with {@link |
52 |
| - * #KEY_PROTECTION_HARDWARE}. Mutually exclusive with {@link #KEY_PROTECTION_SOFTWARE}, {@link |
53 |
| - * #KEY_PROTECTION_SECURE_ELEMENT}. |
| 52 | + * This flag SHOULD be set if the authenticator uses the Trusted Execution Environment [TEE] for |
| 53 | + * key management. In authenticator metadata, this flag should be set in conjunction with {@link |
| 54 | + * #KEY_PROTECTION_HARDWARE}. Mutually exclusive in authenticator metadata with {@link |
| 55 | + * #KEY_PROTECTION_SOFTWARE}, {@link #KEY_PROTECTION_SECURE_ELEMENT}. |
54 | 56 | *
|
55 | 57 | * <p>NOTE: The above requirements apply to authenticators; this library DOES NOT enforce them.
|
56 | 58 | *
|
57 | 59 | * @see <a
|
58 |
| - * href="https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#key-protection-types">FIDO |
| 60 | + * href="https://fidoalliance.org/specs/common-specs/fido-registry-v2.1-ps-20191217.html#key-protection-types">FIDO |
59 | 61 | * Registry of Predefined Values §3.2 Key Protection Types</a>
|
60 | 62 | */
|
61 |
| - public static final KeyProtectionType KEY_PROTECTION_TEE = |
62 |
| - new KeyProtectionType((short) 0x0004, "TEE"); |
| 63 | + KEY_PROTECTION_TEE((short) 0x0004, "tee"), |
63 | 64 |
|
64 | 65 | /**
|
65 |
| - * This flag should be set if the authenticator uses a Secure Element for key management. In |
66 |
| - * authenticator metadata, this flag should be set in conjunction with {@link |
67 |
| - * #KEY_PROTECTION_HARDWARE}. Mutually exclusive with {@link #KEY_PROTECTION_TEE}, {@link |
68 |
| - * #KEY_PROTECTION_SOFTWARE}. |
| 66 | + * This flag SHOULD be set if the authenticator uses a Secure Element [SecureElement] for key |
| 67 | + * management. In authenticator metadata, this flag should be set in conjunction with {@link |
| 68 | + * #KEY_PROTECTION_HARDWARE}. Mutually exclusive in authenticator metadata with {@link |
| 69 | + * #KEY_PROTECTION_TEE}, {@link #KEY_PROTECTION_SOFTWARE}. |
69 | 70 | *
|
70 | 71 | * <p>NOTE: The above requirements apply to authenticators; this library DOES NOT enforce them.
|
71 | 72 | *
|
72 | 73 | * @see <a
|
73 |
| - * href="https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#key-protection-types">FIDO |
| 74 | + * href="https://fidoalliance.org/specs/common-specs/fido-registry-v2.1-ps-20191217.html#key-protection-types">FIDO |
74 | 75 | * Registry of Predefined Values §3.2 Key Protection Types</a>
|
75 | 76 | */
|
76 |
| - public static final KeyProtectionType KEY_PROTECTION_SECURE_ELEMENT = |
77 |
| - new KeyProtectionType((short) 0x0008, "SECURE_ELEMENT"); |
| 77 | + KEY_PROTECTION_SECURE_ELEMENT((short) 0x0008, "secure_element"), |
78 | 78 |
|
79 | 79 | /**
|
80 |
| - * This flag must be set if the authenticator does not store (wrapped) UAuth keys at the client, |
81 |
| - * but relies on a server-provided key handle. This flag must be set in conjunction with one of |
| 80 | + * This flag MUST be set if the authenticator does not store (wrapped) UAuth keys at the client, |
| 81 | + * but relies on a server-provided key handle. This flag MUST be set in conjunction with one of |
82 | 82 | * the other KEY_PROTECTION flags to indicate how the local key handle wrapping key and operations
|
83 |
| - * are protected. Servers may unset this flag in authenticator policy if they are not prepared to |
| 83 | + * are protected. Servers MAY unset this flag in authenticator policy if they are not prepared to |
84 | 84 | * store and return key handles, for example, if they have a requirement to respond
|
85 | 85 | * indistinguishably to authentication attempts against userIDs that do and do not exist. Refer to
|
86 |
| - * [UAFProtocol] for more details. |
| 86 | + * [<a |
| 87 | + * href="https://fidoalliance.org/specs/fido-uaf-v1.2-rd-20171128/fido-uaf-protocol-v1.2-rd-20171128.html">UAFProtocol</a>] |
| 88 | + * for more details. |
87 | 89 | *
|
88 | 90 | * <p>NOTE: The above requirements apply to authenticators; this library DOES NOT enforce them.
|
89 | 91 | *
|
90 | 92 | * @see <a
|
91 |
| - * href="https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#key-protection-types">FIDO |
| 93 | + * href="https://fidoalliance.org/specs/common-specs/fido-registry-v2.1-ps-20191217.html#key-protection-types">FIDO |
92 | 94 | * Registry of Predefined Values §3.2 Key Protection Types</a>
|
93 | 95 | * @see <a
|
94 | 96 | * href="https://fidoalliance.org/specs/fido-uaf-v1.2-rd-20171128/fido-uaf-protocol-v1.2-rd-20171128.html">FIDO
|
95 | 97 | * UAF Protocol Specification [UAFProtocol]</a>
|
96 | 98 | */
|
97 |
| - public static final KeyProtectionType KEY_PROTECTION_REMOTE_HANDLE = |
98 |
| - new KeyProtectionType((short) 0x0010, "REMOTE_HANDLE"); |
| 99 | + KEY_PROTECTION_REMOTE_HANDLE((short) 0x0010, "remote_handle"); |
99 | 100 |
|
100 |
| - @JsonValue public final short value; |
| 101 | + private final short value; |
101 | 102 |
|
102 |
| - @EqualsAndHashCode.Exclude private final transient String name; |
| 103 | + @JsonValue private final String name; |
103 | 104 |
|
104 |
| - private KeyProtectionType(short value, String name) { |
| 105 | + KeyProtectionType(short value, String name) { |
105 | 106 | this.value = value;
|
106 | 107 | this.name = name;
|
107 | 108 | }
|
108 | 109 |
|
109 | 110 | /**
|
110 |
| - * @return An array containing all predefined values of {@link KeyProtectionType} known by this |
111 |
| - * implementation. |
| 111 | + * @return If <code>value</code> matches any {@link KeyProtectionType} constant, returns that |
| 112 | + * constant instance. Otherwise throws {@link IllegalArgumentException}. |
112 | 113 | */
|
113 |
| - public static KeyProtectionType[] values() { |
114 |
| - return new KeyProtectionType[] { |
115 |
| - KEY_PROTECTION_SOFTWARE, |
116 |
| - KEY_PROTECTION_HARDWARE, |
117 |
| - KEY_PROTECTION_TEE, |
118 |
| - KEY_PROTECTION_SECURE_ELEMENT, |
119 |
| - KEY_PROTECTION_REMOTE_HANDLE |
120 |
| - }; |
| 114 | + public static KeyProtectionType fromValue(short value) { |
| 115 | + return Stream.of(values()) |
| 116 | + .filter(v -> v.value == value) |
| 117 | + .findAny() |
| 118 | + .orElseThrow( |
| 119 | + () -> |
| 120 | + new IllegalArgumentException( |
| 121 | + String.format("Unknown %s value: 0x%04x", KeyProtectionType.class, value))); |
121 | 122 | }
|
122 | 123 |
|
123 | 124 | /**
|
124 |
| - * @return If <code>value</code> is the same as that of any of the constants in {@link |
125 |
| - * KeyProtectionType}, returns that constant instance. Otherwise returns a new instance |
126 |
| - * containing <code>value</code>. |
| 125 | + * @return If <code>name</code> matches any {@link Key} constant, returns that constant instance. |
| 126 | + * Otherwise throws {@link IllegalArgumentException}. |
127 | 127 | */
|
128 | 128 | @JsonCreator
|
129 |
| - public static KeyProtectionType of(short value) { |
| 129 | + public static KeyProtectionType fromName(String name) { |
130 | 130 | return Stream.of(values())
|
131 |
| - .filter(v -> v.value == value) |
| 131 | + .filter(v -> v.name.equals(name)) |
132 | 132 | .findAny()
|
133 |
| - .orElseGet(() -> new KeyProtectionType(value, null)); |
134 |
| - } |
135 |
| - |
136 |
| - @Override |
137 |
| - public String toString() { |
138 |
| - if (name == null) { |
139 |
| - return String.format("%s(%04x)", KeyProtectionType.class.getSimpleName(), value); |
140 |
| - } else { |
141 |
| - return "KEY_PROTECTION_" + name; |
142 |
| - } |
| 133 | + .orElseThrow( |
| 134 | + () -> |
| 135 | + new IllegalArgumentException( |
| 136 | + String.format("Unknown %s name: %s", KeyProtectionType.class, name))); |
143 | 137 | }
|
144 | 138 | }
|
0 commit comments