|
16 | 16 | * |
17 | 17 | * <p>To be implemented by the plugin. |
18 | 18 | * |
| 19 | + * <h3>Exception Handling</h3> |
| 20 | + * |
| 21 | + * <p>All methods may throw exceptions to indicate error conditions. Implementations should |
| 22 | + * distinguish between: |
| 23 | + * |
| 24 | + * <ul> |
| 25 | + * <li>{@link IllegalArgumentException} - For invalid parameters that can be validated before |
| 26 | + * hardware access |
| 27 | + * <li>{@link Exception} - For technical errors during hardware operations (communication |
| 28 | + * failures, authentication failures, etc.) |
| 29 | + * </ul> |
| 30 | + * |
| 31 | + * <p>The exception message should provide clear diagnostic information about the failure cause. |
| 32 | + * |
19 | 33 | * @since 1.0.0 |
20 | 34 | */ |
21 | 35 | public interface CommandProcessorApi { |
@@ -84,4 +98,90 @@ public interface CommandProcessorApi { |
84 | 98 | * @since 1.0.0 |
85 | 99 | */ |
86 | 100 | void writeBlock(int blockAddress, byte[] data) throws Exception; |
| 101 | + |
| 102 | + /** |
| 103 | + * Loads a card-specific authentication key into the reader's memory. |
| 104 | + * |
| 105 | + * <p>This method stores a card key in either volatile (RAM) or non-volatile (EEPROM) memory of |
| 106 | + * the card reader. The key can be subsequently used by the {@link #generalAuthenticate(int, int, |
| 107 | + * int)} method to authenticate to the card. This command can be used for all kinds of contactless |
| 108 | + * cards. |
| 109 | + * |
| 110 | + * <p>Volatile memory provides temporary storage that is cleared when the reader loses power, |
| 111 | + * while non-volatile memory persists across power cycles. The availability of non-volatile memory |
| 112 | + * depends on the specific reader hardware. |
| 113 | + * |
| 114 | + * <p>The key structure and length depend on the card type. For example: |
| 115 | + * |
| 116 | + * <ul> |
| 117 | + * <li>Mifare cards: 6-byte keys (Type A or Type B) |
| 118 | + * <li>Other contactless cards: card-specific key formats |
| 119 | + * </ul> |
| 120 | + * |
| 121 | + * <p>The key number parameter identifies the storage location in the reader's memory. The valid |
| 122 | + * range and meaning of key numbers depend on the reader implementation and whether volatile or |
| 123 | + * non-volatile memory is used. Consult the reader documentation for specific key number |
| 124 | + * assignments. |
| 125 | + * |
| 126 | + * <p>Note that keys stored in memory cannot be read back for security reasons. Once loaded, they |
| 127 | + * can only be used for authentication operations. |
| 128 | + * |
| 129 | + * @param keyStorageType The type of memory to store the key in (VOLATILE or NON_VOLATILE). |
| 130 | + * @param keyNumber The key index identifying the storage location. Valid ranges depend on the |
| 131 | + * reader implementation and memory type. |
| 132 | + * @param key A byte array containing the card-specific key value. Must not be null. The required |
| 133 | + * length depends on the card type and authentication algorithm. |
| 134 | + * @throws IllegalArgumentException if {@code keyStorageType} or {@code key} is null, if the key |
| 135 | + * length is not valid for the card type, or if {@code keyNumber} is not in the valid range |
| 136 | + * for the reader and memory type. |
| 137 | + * @throws Exception if the load operation fails, if the specified memory type is not available on |
| 138 | + * the reader hardware, or if a communication error occurs. |
| 139 | + * @see #generalAuthenticate(int, int, int) |
| 140 | + * @since 1.1.0 |
| 141 | + */ |
| 142 | + void loadKey(KeyStorageType keyStorageType, int keyNumber, byte[] key) |
| 143 | + throws Exception; // NOSONAR |
| 144 | + |
| 145 | + /** |
| 146 | + * Performs authentication to a contactless card using a previously loaded key. |
| 147 | + * |
| 148 | + * <p>This method authenticates to a specific memory location on the card using a key that was |
| 149 | + * previously loaded into the reader's memory via the {@link #loadKey(KeyStorageType, int, |
| 150 | + * byte[])} method. Successful authentication is typically required before performing read or |
| 151 | + * write operations on protected memory areas. |
| 152 | + * |
| 153 | + * <p>The authentication process establishes a secure session between the reader and the card. The |
| 154 | + * block address represents the block number or starting byte number of the card to be |
| 155 | + * authenticated, depending on the card type. |
| 156 | + * |
| 157 | + * <p>The key type parameter is card-specific and indicates which type of key to use for |
| 158 | + * authentication. Examples: |
| 159 | + * |
| 160 | + * <ul> |
| 161 | + * <li>Mifare cards: 0x60 (KEY_A) or 0x61 (KEY_B) |
| 162 | + * <li>Other contactless cards: card-specific key type values |
| 163 | + * </ul> |
| 164 | + * |
| 165 | + * <p>The key number parameter references a key previously loaded via {@link |
| 166 | + * #loadKey(KeyStorageType, int, byte[])} and identifies which stored key to use for this |
| 167 | + * authentication operation. |
| 168 | + * |
| 169 | + * @param blockAddress The block number or starting byte number on the card where authentication |
| 170 | + * is to be performed. Valid range depends on the card type and memory structure. |
| 171 | + * @param keyType The type of key to use for authentication. The valid values are card-specific |
| 172 | + * (e.g., 0x60 or 0x61 for Mifare cards). |
| 173 | + * @param keyNumber The index of the previously loaded key to use for authentication. Must |
| 174 | + * reference a key that was loaded via {@link #loadKey(KeyStorageType, int, byte[])}. |
| 175 | + * @return {@code true} if authentication succeeded, {@code false} if authentication failed (e.g., |
| 176 | + * incorrect key). |
| 177 | + * @throws IllegalArgumentException if {@code blockAddress} is out of valid range for the card |
| 178 | + * type, if {@code keyType} is not supported by the card, or if {@code keyNumber} does not |
| 179 | + * reference a valid loaded key. |
| 180 | + * @throws Exception if the referenced key was not previously loaded, if the card does not support |
| 181 | + * authentication, or if a communication error occurs. |
| 182 | + * @see #loadKey(KeyStorageType, int, byte[]) |
| 183 | + * @since 1.1.0 |
| 184 | + */ |
| 185 | + boolean generalAuthenticate(int blockAddress, int keyType, int keyNumber) |
| 186 | + throws Exception; // NOSONAR |
87 | 187 | } |
0 commit comments