| 166 | | { 0x6300, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 167 | | { 0x63C1, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 168 | | { 0x63C2, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 169 | | { 0x63C3, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 170 | | { 0x63C4, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 171 | | { 0x63C5, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 172 | | { 0x63C6, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 173 | | { 0x63C7, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 174 | | { 0x63C8, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 175 | | { 0x63C9, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 176 | | { 0x63Ca, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 177 | | { 0x63Cb, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 178 | | { 0x63Cc, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 179 | | { 0x63Cd, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 180 | | { 0x63Ce, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 181 | | { 0x63Cf, SC_ERROR_PIN_CODE_INCORRECT,"authentication failed"}, |
| 182 | | |
| 183 | | { 0x6400, SC_ERROR_CARD_CMD_FAILED,"Aborting"}, |
| 184 | | |
| 185 | | { 0x6500, SC_ERROR_MEMORY_FAILURE, "Memory failure"}, |
| 186 | | { 0x6581, SC_ERROR_MEMORY_FAILURE, "Memory failure"}, |
| 187 | | |
| 188 | | { 0x6700, SC_ERROR_WRONG_LENGTH, "Lc or Le invalid"}, |
| 189 | | |
| 190 | | { 0x6883, SC_ERROR_CARD_CMD_FAILED, "The finishing command of a chain is expected"}, |
| 191 | | |
| 192 | | { 0x6982, SC_ERROR_SECURITY_STATUS_NOT_SATISFIED,"required access right not granted"}, |
| 193 | | { 0x6983, SC_ERROR_AUTH_METHOD_BLOCKED, "bs object blocked"}, |
| 194 | | { 0x6985, SC_ERROR_CARD_CMD_FAILED, "command not allowed (unsuitable conditions)"}, |
| 195 | | { 0x6986, SC_ERROR_INCORRECT_PARAMETERS,"no current ef selected"}, |
| 196 | | |
| 197 | | { 0x6a80, SC_ERROR_INCORRECT_PARAMETERS,"invalid parameters in data field"}, |
| 198 | | { 0x6a81, SC_ERROR_NOT_SUPPORTED, "function/mode not supported"}, |
| 199 | | { 0x6a82, SC_ERROR_FILE_NOT_FOUND, "file (DO) not found"}, |
| 200 | | { 0x6a84, SC_ERROR_CARD_CMD_FAILED, "not enough memory"}, |
| 201 | | { 0x6a86, SC_ERROR_INCORRECT_PARAMETERS,"p1/p2 invalid"}, |
| 202 | | { 0x6a89, SC_ERROR_FILE_ALREADY_EXISTS,"file (DO) already exists"}, |
| 203 | | |
| 204 | | { 0x6b00, SC_ERROR_INCORRECT_PARAMETERS,"Out of file length"}, |
| 205 | | |
| 206 | | { 0x6c00, SC_ERROR_WRONG_LENGTH, "le does not fit the data to be sent"}, |
| 207 | | |
| 208 | | { 0x6d00, SC_ERROR_INS_NOT_SUPPORTED, "ins invalid (not supported)"}, |
| | 157 | { 0x6300, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 158 | { 0x63C1, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed. One tries left"}, |
| | 159 | { 0x63C2, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed. Two tries left"}, |
| | 160 | { 0x63C3, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 161 | { 0x63C4, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 162 | { 0x63C5, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 163 | { 0x63C6, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 164 | { 0x63C7, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 165 | { 0x63C8, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 166 | { 0x63C9, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 167 | { 0x63CA, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 168 | { 0x63CB, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 169 | { 0x63CC, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 170 | { 0x63CD, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 171 | { 0x63CE, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 172 | { 0x63CF, SC_ERROR_PIN_CODE_INCORRECT, "Authentication failed"}, |
| | 173 | |
| | 174 | { 0x6400, SC_ERROR_CARD_CMD_FAILED, "Aborting"}, |
| | 175 | |
| | 176 | { 0x6500, SC_ERROR_MEMORY_FAILURE, "Memory failure"}, |
| | 177 | { 0x6581, SC_ERROR_MEMORY_FAILURE, "Memory failure"}, |
| | 178 | |
| | 179 | { 0x6700, SC_ERROR_WRONG_LENGTH, "Lc or Le invalid"}, |
| | 180 | |
| | 181 | { 0x6883, SC_ERROR_CARD_CMD_FAILED, "The finishing command of a chain is expected"}, |
| | 182 | |
| | 183 | { 0x6982, SC_ERROR_SECURITY_STATUS_NOT_SATISFIED, "Required access right not granted"}, |
| | 184 | { 0x6983, SC_ERROR_AUTH_METHOD_BLOCKED, "DO blocked"}, |
| | 185 | { 0x6985, SC_ERROR_CARD_CMD_FAILED, "Command not allowed (unsuitable conditions)"}, |
| | 186 | { 0x6986, SC_ERROR_INCORRECT_PARAMETERS,"No current EF selected"}, |
| | 187 | |
| | 188 | { 0x6A80, SC_ERROR_INCORRECT_PARAMETERS,"Invalid parameters in data field"}, |
| | 189 | { 0x6A81, SC_ERROR_NOT_SUPPORTED, "Function/mode not supported"}, |
| | 190 | { 0x6A82, SC_ERROR_FILE_NOT_FOUND, "File (DO) not found"}, |
| | 191 | { 0x6A84, SC_ERROR_CARD_CMD_FAILED, "Not enough memory space in the token"}, |
| | 192 | { 0x6A86, SC_ERROR_INCORRECT_PARAMETERS,"P1 or P2 invalid"}, |
| | 193 | { 0x6A89, SC_ERROR_FILE_ALREADY_EXISTS, "File (DO) already exists"}, |
| | 194 | |
| | 195 | { 0x6B00, SC_ERROR_INCORRECT_PARAMETERS,"Out of maximum file length"}, |
| | 196 | |
| | 197 | { 0x6C00, SC_ERROR_WRONG_LENGTH, "Le does not fit the data to be sent"}, |
| | 198 | |
| | 199 | { 0x6D00, SC_ERROR_INS_NOT_SUPPORTED, "Ins invalid (not supported)"}, |
| 242 | | u8 rbuf[256]; |
| 243 | | int r = 0; |
| 244 | | sc_apdu_t apdu; |
| 245 | | SC_FUNC_CALLED(card->ctx, 1); |
| 246 | | /*sc_debug(card->ctx, "\n\tpath = %s\n\ttype = %d", hexdump(path, pathlen), in_path->type); |
| 247 | | prepare & transmit APDU |
| 248 | | 00 a4 00 04 20 - first*/ |
| 249 | | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xA4, 0x03, 0x00); |
| 250 | | apdu.cla = 0x00; |
| 251 | | apdu.resplen = 256; |
| 252 | | apdu.resp = rbuf; |
| 253 | | apdu.le = 256; |
| 254 | | r = sc_transmit_apdu(card, &apdu); |
| 255 | | SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
| 256 | | sc_debug(card->ctx, "rbuf = %s len %d", hexdump(apdu.resp, apdu.resplen), apdu.resplen); |
| 257 | | sc_debug(card->ctx, "sw1 = %x, sw2 = %x", apdu.sw1, apdu.sw2); |
| 258 | | return 0; |
| 259 | | } |
| 260 | | |
| 261 | | /* make little endian path from normal path. |
| 262 | | return 1 if right len, otherwise 0 */ |
| 263 | | static int make_le_path(u8 *hPath, size_t len) |
| 264 | | { |
| 265 | | #ifdef BIG_ENDIAN_RUTOKEN |
| 266 | | /* we don't need it any more */ |
| 267 | | return 1; |
| 268 | | #else |
| 269 | | int i, ret = (len > 1) && !(len & 1); /* && (len <= SC_MAX_PATH_SIZE); */ |
| 270 | | if (ret) |
| 271 | | { |
| 272 | | for(i = 0; i < len; i += 2) |
| 273 | | { |
| 274 | | u8 b = hPath[i]; |
| 275 | | hPath[i] = hPath[i+1]; |
| 276 | | hPath[i+1] = b; |
| 277 | | } |
| 278 | | } |
| 279 | | return ret; |
| 280 | | #endif |
| | 229 | u8 rbuf[256]; |
| | 230 | sc_apdu_t apdu; |
| | 231 | int ret; |
| | 232 | |
| | 233 | SC_FUNC_CALLED(card->ctx, 3); |
| | 234 | |
| | 235 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xA4, 0x03, 0x00); |
| | 236 | apdu.cla = 0x00; |
| | 237 | apdu.resplen = sizeof(rbuf); |
| | 238 | apdu.resp = rbuf; |
| | 239 | apdu.le = sizeof(rbuf); |
| | 240 | |
| | 241 | ret = sc_transmit_apdu(card, &apdu); |
| | 242 | SC_TEST_RET(card->ctx, ret, "APDU transmit failed"); |
| | 243 | ret = sc_check_sw(card, apdu.sw1, apdu.sw2); |
| | 244 | SC_FUNC_RETURN(card->ctx, 3, ret); |
| 287 | | int r = 0, len=0; |
| 288 | | sc_apdu_t apdu; |
| 289 | | |
| 290 | | SC_FUNC_CALLED(card->ctx, 1); |
| 291 | | /* sc_debug(card->ctx, "\n\tpath = %s\n\ttype = %d", hexdump(path, pathlen), in_path->type); */ |
| 292 | | /* prepare & transmit APDU */ |
| 293 | | /* 00 a4 00 04 20 - first */ |
| 294 | | |
| 295 | | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xA4, 0x00, 0x00); |
| 296 | | apdu.cla = 0x00; |
| 297 | | apdu.resplen = 256; |
| 298 | | apdu.resp = rbuf; |
| 299 | | apdu.le = 256; |
| 300 | | r = sc_transmit_apdu(card, &apdu); |
| 301 | | SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
| 302 | | sc_debug(card->ctx, "rbuf = %s len %d", hexdump(apdu.resp, apdu.resplen), apdu.resplen); |
| 303 | | sc_debug(card->ctx, "sw1 = %x, sw2 = %x", apdu.sw1, apdu.sw2); |
| 304 | | if((apdu.sw1 == 0x6a) ) |
| 305 | | { |
| 306 | | /* empty dir */ |
| 307 | | return 0; |
| 308 | | } |
| 309 | | /* todo: add check buflen */ |
| 310 | | /* save first file(dir) ID */ |
| 311 | | memcpy(buf+len, rbuf+6, 2); |
| 312 | | memcpy(previd, rbuf+6, 2); |
| 313 | | len += 2; |
| 314 | | if(rbuf[4] == FDESCR_DF) |
| 315 | | rutoken_dir_up(card); |
| 316 | | |
| 317 | | /* 00 a4 00 02 02 prev id - next */ |
| 318 | | while(1) |
| 319 | | { |
| 320 | | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0x00, 0x06); |
| 321 | | apdu.cla = 0x00; |
| 322 | | apdu.lc = 2; |
| 323 | | apdu.data = previd; |
| 324 | | apdu.datalen = 2; |
| 325 | | apdu.resplen = 256; |
| 326 | | apdu.resp = rbuf; |
| 327 | | apdu.le = 256; |
| 328 | | r = sc_transmit_apdu(card, &apdu); |
| 329 | | SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
| 330 | | sc_debug(card->ctx, "rbuf = %s len %d", hexdump(apdu.resp, apdu.resplen), apdu.resplen); |
| 331 | | sc_debug(card->ctx, "sw1 = %x, sw2 = %x", apdu.sw1, apdu.sw2); |
| 332 | | if((apdu.sw1 == 0x6a) ) |
| 333 | | { |
| 334 | | /* end list */ |
| 335 | | break; |
| 336 | | } |
| 337 | | /* todo: add check buflen */ |
| 338 | | /* save first file(dir) ID */ |
| 339 | | memcpy(buf+len, rbuf+6, 2); |
| 340 | | memcpy(previd, rbuf+6, 2); |
| 341 | | len += 2; |
| 342 | | if(rbuf[4] == FDESCR_DF) |
| | 251 | sc_apdu_t apdu; |
| | 252 | size_t len = 0; |
| | 253 | int ret, first = 1; |
| | 254 | |
| | 255 | SC_FUNC_CALLED(card->ctx, 1); |
| | 256 | while (1) |
| | 257 | { |
| | 258 | if (first) |
| | 259 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xA4, 0x00, 0x00); |
| | 260 | else |
| | 261 | { |
| | 262 | /* 00 a4 00 02 02 prev id - next */ |
| | 263 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0x00, 0x02); |
| | 264 | apdu.lc = sizeof(previd); |
| | 265 | apdu.data = previd; |
| | 266 | apdu.datalen = sizeof(previd); |
| | 267 | } |
| | 268 | apdu.cla = 0x00; |
| | 269 | apdu.resplen = sizeof(rbuf); |
| | 270 | apdu.resp = rbuf; |
| | 271 | apdu.le = sizeof(rbuf); |
| | 272 | ret = sc_transmit_apdu(card, &apdu); |
| | 273 | SC_TEST_RET(card->ctx, ret, "APDU transmit failed"); |
| | 274 | |
| | 275 | if (apdu.sw1 == 0x6A && apdu.sw2 == 0x82) |
| | 276 | break; /* if (first) "end list" else "empty dir" */ |
| | 277 | |
| | 278 | ret = sc_check_sw(card, apdu.sw1, apdu.sw2); |
| | 279 | SC_TEST_RET(card->ctx, ret, "Get list files failed"); |
| | 280 | |
| | 281 | /* save first file(dir) ID */ |
| | 282 | if (len + 2 <= buflen) |
| | 283 | { |
| | 284 | buf[len++] = rbuf[6]; |
| | 285 | buf[len++] = rbuf[7]; |
| | 286 | } |
| | 287 | memcpy(previd, rbuf+6, sizeof(previd)); |
| | 288 | if (rbuf[4] == FDESCR_DF) |
| | 310 | |
| | 311 | if (file->sec_attr && file->sec_attr_len == SEC_ATTR_SIZE |
| | 312 | #ifndef SET_ACL_FOR_EF_LENLESS8_RUTOKEN |
| | 313 | /* |
| | 314 | * No set ACL for EF: |
| | 315 | * PrKDF_path: "3F00FF000001" - SC_PKCS15_PRKDF |
| | 316 | * PuKDF_path: "3F00FF000002" - SC_PKCS15_PUKDF |
| | 317 | * CDF_path: "3F00FF000003" - SC_PKCS15_CDF |
| | 318 | * DODF_path: "3F00FF000004" - SC_PKCS15_DODF |
| | 319 | * (see files: src/libopensc/pkcs15-rutoken.c, src/pkcs15init/rutoken.profile) |
| | 320 | */ |
| | 321 | && (file->type != SC_FILE_TYPE_WORKING_EF || file->path.len >= 8) |
| | 322 | #endif |
| | 323 | ) |
| | 324 | { |
| | 325 | sc_file_add_acl_entry(file, SC_AC_OP_SELECT, |
| | 326 | SC_AC_NONE, SC_AC_KEY_REF_NONE); |
| | 327 | if (file->sec_attr[0] & 0x40) /* if AccessMode.6 */ |
| | 328 | { |
| | 329 | sc_debug(card->ctx, "SC_AC_OP_DELETE %i %i", |
| | 330 | (int)(*(int8_t*)&file->sec_attr[1 +6]), |
| | 331 | file->sec_attr[1+7 +6]); |
| | 332 | sc_file_add_acl_entry(file, SC_AC_OP_DELETE, |
| | 333 | (int)(*(int8_t*)&file->sec_attr[1 +6]), |
| | 334 | file->sec_attr[1+7 +6]); |
| | 335 | } |
| | 336 | if (file->sec_attr[0] & 0x01) /* if AccessMode.0 */ |
| | 337 | { |
| | 338 | sc_debug(card->ctx, (file->type == SC_FILE_TYPE_DF) ? |
| | 339 | "SC_AC_OP_CREATE %i %i" : "SC_AC_OP_READ %i %i", |
| | 340 | (int)(*(int8_t*)&file->sec_attr[1 +0]), |
| | 341 | file->sec_attr[1+7 +0]); |
| | 342 | sc_file_add_acl_entry(file, |
| | 343 | (file->type == SC_FILE_TYPE_DF) ? |
| | 344 | SC_AC_OP_CREATE : SC_AC_OP_READ, |
| | 345 | (int)(*(int8_t*)&file->sec_attr[1 +0]), |
| | 346 | file->sec_attr[1+7 +0]); |
| | 347 | } |
| | 348 | if (file->type == SC_FILE_TYPE_DF) |
| | 349 | { |
| | 350 | sc_file_add_acl_entry(file, SC_AC_OP_LIST_FILES, |
| | 351 | SC_AC_NONE, SC_AC_KEY_REF_NONE); |
| | 352 | } |
| | 353 | else |
| | 354 | if (file->sec_attr[0] & 0x02) /* if AccessMode.1 */ |
| | 355 | { |
| | 356 | sc_debug(card->ctx, "SC_AC_OP_UPDATE %i %i", |
| | 357 | (int)(*(int8_t*)&file->sec_attr[1 +1]), |
| | 358 | file->sec_attr[1+7 +1]); |
| | 359 | sc_file_add_acl_entry(file, SC_AC_OP_UPDATE, |
| | 360 | (int)(*(int8_t*)&file->sec_attr[1 +1]), |
| | 361 | file->sec_attr[1+7 +1]); |
| | 362 | sc_debug(card->ctx, "SC_AC_OP_WRITE %i %i", |
| | 363 | (int)(*(int8_t*)&file->sec_attr[1 +1]), |
| | 364 | file->sec_attr[1+7 +1]); |
| | 365 | sc_file_add_acl_entry(file, SC_AC_OP_WRITE, |
| | 366 | (int)(*(int8_t*)&file->sec_attr[1 +1]), |
| | 367 | file->sec_attr[1+7 +1]); |
| | 368 | } |
| | 369 | } |