1 //-----------------------------------------------------------------------------
2 // Frederik Möllers - August 2012
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Routines to support the German eletronic "Personalausweis" (ID card)
9 // Note that the functions which do not implement USB commands do NOT initialize
10 // the card (with iso14443a_select_card etc.). If You want to use these
11 // functions, You need to do the setup before calling them!
12 //-----------------------------------------------------------------------------
14 #include "iso14443a.h"
18 // Protocol and Parameter Selection Request
19 // use regular (1x) speed in both directions
20 // CRC is already included
21 static const uint8_t pps
[] = {0xD0, 0x11, 0x00, 0x52, 0xA6};
23 // APDUs for communication with German Identification Card
25 // General Authenticate (request encrypted nonce) WITHOUT the Le at the end
26 static const uint8_t apdu_general_authenticate_pace_get_nonce
[] = {
32 0x7C, // Type: Dynamic Authentication Data
33 0x00, // Length: 0 bytes
36 // MSE: Set AT (only CLA, INS, P1 and P2)
37 static const uint8_t apdu_mse_set_at_start
[] = {
44 // SELECT BINARY with the ID for EF.CardAccess
45 static const uint8_t apdu_select_binary_cardaccess
[] = {
56 static const uint8_t apdu_read_binary
[] = {
65 // the leading bytes of a PACE OID
66 static const uint8_t oid_pace_start
[] = {
67 0x04, // itu-t, identified-organization
70 0x00, // etsi-identified-organization
77 //-----------------------------------------------------------------------------
78 // Closes the communication channel and turns off the field
79 //-----------------------------------------------------------------------------
82 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
86 //-----------------------------------------------------------------------------
87 // Parses DER encoded data, e.g. from EF.CardAccess and fills out the given
88 // structs. If a pointer is 0, it is ignored.
89 // The function returns 0 on success and if an error occured, it returns the
90 // offset where it occured.
92 // TODO: This function can access memory outside of the given data if the DER
94 // TODO: Support skipping elements with a length > 0x7F
95 // TODO: Support OIDs with a length > 7F
96 // TODO: Support elements with long tags (tag is longer than 1 byte)
97 // TODO: Support proprietary PACE domain parameters
98 //-----------------------------------------------------------------------------
99 size_t EPA_Parse_CardAccess(uint8_t *data
,
101 pace_version_info_t
*pace_info
)
105 while (index
<= length
- 2) {
106 // determine type of element
108 if (data
[index
] == 0x31 || data
[index
] == 0x30) {
109 // enter the set (skip tag + length)
111 // check for extended length
112 if ((data
[index
- 1] & 0x80) != 0) {
113 index
+= (data
[index
-1] & 0x7F);
117 else if (data
[index
] == 0x06) {
118 // is this a PACE OID?
119 if (data
[index
+ 1] == 0x0A // length matches
120 && memcmp(data
+ index
+ 2,
122 sizeof(oid_pace_start
)) == 0 // content matches
123 && pace_info
!= NULL
)
125 // first, clear the pace_info struct
126 memset(pace_info
, 0, sizeof(pace_version_info_t
));
127 memcpy(pace_info
->oid
, data
+ index
+ 2, sizeof(pace_info
->oid
));
128 // a PACE OID is followed by the version
129 index
+= data
[index
+ 1] + 2;
130 if (data
[index
] == 02 && data
[index
+ 1] == 01) {
131 pace_info
->version
= data
[index
+ 2];
137 // after that there might(!) be the parameter ID
138 if (data
[index
] == 02 && data
[index
+ 1] == 01) {
139 pace_info
->parameter_id
= data
[index
+ 2];
145 index
+= 2 + data
[index
+ 1];
148 // if the length is 0, something is wrong
149 // TODO: This needs to be extended to support long tags
150 else if (data
[index
+ 1] == 0) {
155 // TODO: This needs to be extended to support long tags
156 // TODO: This needs to be extended to support unknown elements with
158 index
+= 2 + data
[index
+ 1];
162 // TODO: We should check whether we reached the end in error, but for that
163 // we need a better parser (e.g. with states like IN_SET or IN_PACE_INFO)
167 //-----------------------------------------------------------------------------
168 // Read the file EF.CardAccess and save it into a buffer (at most max_length bytes)
169 // Returns -1 on failure or the length of the data on success
170 // TODO: for the moment this sends only 1 APDU regardless of the requested length
171 //-----------------------------------------------------------------------------
172 int EPA_Read_CardAccess(uint8_t *buffer
, size_t max_length
)
174 // the response APDU of the card
175 // since the card doesn't always care for the expected length we send it,
176 // we reserve 262 bytes here just to be safe (256-byte APDU + SW + ISO frame)
177 uint8_t response_apdu
[262];
178 int rapdu_length
= 0;
180 // select the file EF.CardAccess
181 rapdu_length
= iso14_apdu((uint8_t *)apdu_select_binary_cardaccess
,
182 sizeof(apdu_select_binary_cardaccess
),
184 if (rapdu_length
!= 6
185 || response_apdu
[rapdu_length
- 4] != 0x90
186 || response_apdu
[rapdu_length
- 3] != 0x00)
188 Dbprintf("epa - no select cardaccess");
193 rapdu_length
= iso14_apdu((uint8_t *)apdu_read_binary
,
194 sizeof(apdu_read_binary
),
196 if (rapdu_length
<= 6
197 || response_apdu
[rapdu_length
- 4] != 0x90
198 || response_apdu
[rapdu_length
- 3] != 0x00)
200 Dbprintf("epa - no read cardaccess");
204 // copy the content into the buffer
205 // length of data available: apdu_length - 4 (ISO frame) - 2 (SW)
206 size_t to_copy
= rapdu_length
- 6;
207 to_copy
= to_copy
< max_length
? to_copy
: max_length
;
208 memcpy(buffer
, response_apdu
+2, to_copy
);
212 //-----------------------------------------------------------------------------
213 // Abort helper function for EPA_PACE_Collect_Nonce
214 // sets relevant data in ack, sends the response
215 //-----------------------------------------------------------------------------
216 static void EPA_PACE_Collect_Nonce_Abort(uint8_t step
, int func_return
)
218 // // step in which the failure occured
219 // ack->arg[0] = step;
220 // // last return code
221 // ack->arg[1] = func_return;
223 // power down the field
226 // send the USB packet
227 cmd_send(CMD_ACK
,step
,func_return
,0,0,0);
230 //-----------------------------------------------------------------------------
231 // Acquire one encrypted PACE nonce
232 //-----------------------------------------------------------------------------
233 void EPA_PACE_Collect_Nonce(UsbCommand
*c
)
239 * step where the error occured or 0 if no error occured
241 * return code of the last executed function
246 // return value of a function
249 // // initialize ack with 0s
250 // memset(ack->arg, 0, 12);
251 // memset(ack->d.asBytes, 0, 48);
253 // set up communication
254 func_return
= EPA_Setup();
255 if (func_return
!= 0) {
256 EPA_PACE_Collect_Nonce_Abort(1, func_return
);
260 // read the CardAccess file
261 // this array will hold the CardAccess file
262 uint8_t card_access
[256] = {0};
263 int card_access_length
= EPA_Read_CardAccess(card_access
, 256);
264 // the response has to be at least this big to hold the OID
265 if (card_access_length
< 18) {
266 EPA_PACE_Collect_Nonce_Abort(2, card_access_length
);
270 // this will hold the PACE info of the card
271 pace_version_info_t pace_version_info
;
272 // search for the PACE OID
273 func_return
= EPA_Parse_CardAccess(card_access
,
276 if (func_return
!= 0 || pace_version_info
.version
== 0) {
277 EPA_PACE_Collect_Nonce_Abort(3, func_return
);
281 // initiate the PACE protocol
282 // use the CAN for the password since that doesn't change
283 func_return
= EPA_PACE_MSE_Set_AT(pace_version_info
, 2);
286 uint8_t nonce
[256] = {0};
287 uint8_t requested_size
= (uint8_t)c
->arg
[0];
288 func_return
= EPA_PACE_Get_Nonce(requested_size
, nonce
);
289 // check if the command succeeded
292 EPA_PACE_Collect_Nonce_Abort(4, func_return
);
299 // save received information
300 // ack->arg[1] = func_return;
301 // memcpy(ack->d.asBytes, nonce, func_return);
302 cmd_send(CMD_ACK
,0,func_return
,0,nonce
,func_return
);
305 //-----------------------------------------------------------------------------
306 // Performs the "Get Nonce" step of the PACE protocol and saves the returned
307 // nonce. The caller is responsible for allocating enough memory to store the
308 // nonce. Note that the returned size might be less or than or greater than the
310 // Returns the actual size of the nonce on success or a less-than-zero error
312 //-----------------------------------------------------------------------------
313 int EPA_PACE_Get_Nonce(uint8_t requested_length
, uint8_t *nonce
)
316 uint8_t apdu
[sizeof(apdu_general_authenticate_pace_get_nonce
) + 1];
317 // copy the constant part
319 apdu_general_authenticate_pace_get_nonce
,
320 sizeof(apdu_general_authenticate_pace_get_nonce
));
321 // append Le (requested length + 2 due to tag/length taking 2 bytes) in RAPDU
322 apdu
[sizeof(apdu_general_authenticate_pace_get_nonce
)] = requested_length
+ 4;
325 uint8_t response_apdu
[262];
326 int send_return
= iso14_apdu(apdu
,
329 // check if the command succeeded
331 || response_apdu
[send_return
- 4] != 0x90
332 || response_apdu
[send_return
- 3] != 0x00)
337 // if there is no nonce in the RAPDU, return here
338 if (send_return
< 10)
343 // get the actual length of the nonce
344 uint8_t nonce_length
= response_apdu
[5];
345 if (nonce_length
> send_return
- 10)
347 nonce_length
= send_return
- 10;
350 memcpy(nonce
, response_apdu
+ 6, nonce_length
);
355 //-----------------------------------------------------------------------------
356 // Initializes the PACE protocol by performing the "MSE: Set AT" step
357 // Returns 0 on success or a non-zero error code on failure
358 //-----------------------------------------------------------------------------
359 int EPA_PACE_MSE_Set_AT(pace_version_info_t pace_version_info
, uint8_t password
)
361 // create the MSE: Set AT APDU
363 // the minimum length (will be increased as more data is added)
364 size_t apdu_length
= 20;
365 // copy the constant part
367 apdu_mse_set_at_start
,
368 sizeof(apdu_mse_set_at_start
));
372 apdu
[6] = sizeof(pace_version_info
.oid
);
375 pace_version_info
.oid
,
376 sizeof(pace_version_info
.oid
));
383 // if standardized domain parameters are used, copy the ID
384 if (pace_version_info
.parameter_id
!= 0) {
386 // type: domain parameter
390 // copy the parameter ID
391 apdu
[22] = pace_version_info
.parameter_id
;
393 // now set Lc to the actual length
394 apdu
[4] = apdu_length
- 5;
396 uint8_t response_apdu
[6];
397 int send_return
= iso14_apdu(apdu
,
400 // check if the command succeeded
402 || response_apdu
[send_return
- 4] != 0x90
403 || response_apdu
[send_return
- 3] != 0x00)
410 //-----------------------------------------------------------------------------
411 // Set up a communication channel (Card Select, PPS)
412 // Returns 0 on success or a non-zero error code on failure
413 //-----------------------------------------------------------------------------
419 uint8_t pps_response
[3];
420 uint8_t pps_response_par
[1];
421 iso14a_card_select_t card_select_info
;
423 // power up the field
424 iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD
);
427 return_code
= iso14443a_select_card(uid
, &card_select_info
, NULL
);
428 if (return_code
!= 1) {
429 Dbprintf("Epa: Can't select card");
433 // send the PPS request
434 ReaderTransmit((uint8_t *)pps
, sizeof(pps
), NULL
);
435 return_code
= ReaderReceive(pps_response
, pps_response_par
);
436 if (return_code
!= 3 || pps_response
[0] != 0xD0) {
437 return return_code
== 0 ? 2 : return_code
;