| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include "internal.h" |
|---|
| 22 | #include "asn1.h" |
|---|
| 23 | #include "cardctl.h" |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | |
|---|
| 27 | static struct sc_atr_table starcos_atrs[] = { |
|---|
| 28 | Â Â Â Â { "3B:B7:94:00:c0:24:31:fe:65:53:50:4b:32:33:90:00:b4", NULL, NULL, SC_CARD_TYPE_STARCOS_GENERIC, 0, NULL }, |
|---|
| 29 | Â Â Â Â { "3B:B7:94:00:81:31:fe:65:53:50:4b:32:33:90:00:d1", NULL, NULL, SC_CARD_TYPE_STARCOS_GENERIC, 0, NULL }, |
|---|
| 30 | Â Â Â Â { "3b:b7:18:00:c0:3e:31:fe:65:53:50:4b:32:34:90:00:25", NULL, NULL, SC_CARD_TYPE_STARCOS_GENERIC, 0, NULL }, |
|---|
| 31 | Â Â Â Â { NULL, NULL, NULL, 0, 0, NULL } |
|---|
| 32 | }; |
|---|
| 33 | |
|---|
| 34 | static struct sc_card_operations starcos_ops; |
|---|
| 35 | static struct sc_card_operations *iso_ops = NULL; |
|---|
| 36 | |
|---|
| 37 | static struct sc_card_driver starcos_drv = { |
|---|
| 38 | Â Â Â Â "STARCOS SPK 2.3/2.4", |
|---|
| 39 | Â Â Â Â "starcos", |
|---|
| 40 | Â Â Â Â &starcos_ops, |
|---|
| 41 | Â Â Â Â NULL, 0, NULL |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | static const struct sc_card_error starcos_errors[] = |
|---|
| 45 | { |
|---|
| 46 | Â Â Â Â { 0x6600, SC_ERROR_INCORRECT_PARAMETERS, "Error setting the security env"}, |
|---|
| 47 | Â Â Â Â { 0x66F0, SC_ERROR_INCORRECT_PARAMETERS, "No space left for padding"}, |
|---|
| 48 |     { 0x69F0, SC_ERROR_NOT_ALLOWED,     "Command not allowed"}, |
|---|
| 49 |     { 0x6A89, SC_ERROR_FILE_ALREADY_EXISTS, "Files exists"}, |
|---|
| 50 |     { 0x6A8A, SC_ERROR_FILE_ALREADY_EXISTS, "Application exists"}, |
|---|
| 51 | Â Â Â Â { 0x6F01, SC_ERROR_CARD_CMD_FAILED, "public key not complete"}, |
|---|
| 52 | Â Â Â Â { 0x6F02, SC_ERROR_CARD_CMD_FAILED, "data overflow"}, |
|---|
| 53 | Â Â Â Â { 0x6F03, SC_ERROR_CARD_CMD_FAILED, "invalid command sequence"}, |
|---|
| 54 | Â Â Â Â { 0x6F05, SC_ERROR_CARD_CMD_FAILED, "security enviroment invalid"}, |
|---|
| 55 | Â Â Â Â { 0x6F07, SC_ERROR_FILE_NOT_FOUND, "key part not found"}, |
|---|
| 56 | Â Â Â Â { 0x6F08, SC_ERROR_CARD_CMD_FAILED, "signature failed"}, |
|---|
| 57 | Â Â Â Â { 0x6F0A, SC_ERROR_INCORRECT_PARAMETERS, "key format does not match key length"}, |
|---|
| 58 | Â Â Â Â { 0x6F0B, SC_ERROR_INCORRECT_PARAMETERS, "length of key component inconsistent with algorithm"}, |
|---|
| 59 | Â Â Â Â { 0x6F81, SC_ERROR_CARD_CMD_FAILED, "system error"} |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | typedef struct starcos_ex_data_st { |
|---|
| 64 |     int  sec_ops; |
|---|
| 65 | |
|---|
| 66 |     unsigned int  fix_digestInfo; |
|---|
| 67 | } starcos_ex_data; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | static int starcos_match_card(sc_card_t *card) |
|---|
| 71 | { |
|---|
| 72 |     int i; |
|---|
| 73 | |
|---|
| 74 | Â Â Â Â i = _sc_match_atr(card, starcos_atrs, &card->type); |
|---|
| 75 |     if (i < 0) |
|---|
| 76 |         return 0; |
|---|
| 77 |     return 1; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | static int starcos_init(sc_card_t *card) |
|---|
| 81 | { |
|---|
| 82 |     unsigned int flags; |
|---|
| 83 | Â Â Â Â starcos_ex_data *ex_data; |
|---|
| 84 | |
|---|
| 85 | Â Â Â Â ex_data = (starcos_ex_data *) calloc(1, sizeof(starcos_ex_data)); |
|---|
| 86 |     if (ex_data == NULL) |
|---|
| 87 |         return SC_ERROR_OUT_OF_MEMORY; |
|---|
| 88 | |
|---|
| 89 | Â Â Â Â card->name = "STARCOS SPK 2.3"; |
|---|
| 90 |     card->cla = 0x00; |
|---|
| 91 |     card->drv_data = (void *)ex_data; |
|---|
| 92 | |
|---|
| 93 | Â Â Â Â flags = SC_ALGORITHM_RSA_PAD_PKCS1 |
|---|
| 94 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_ONBOARD_KEY_GEN |
|---|
| 95 | Â Â Â Â Â Â Â Â | SC_CARD_FLAG_RNG |
|---|
| 96 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_RSA_PAD_ISO9796 |
|---|
| 97 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_RSA_HASH_NONE |
|---|
| 98 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_RSA_HASH_SHA1 |
|---|
| 99 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_RSA_HASH_MD5 |
|---|
| 100 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_RSA_HASH_RIPEMD160 |
|---|
| 101 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_RSA_HASH_MD5_SHA1; |
|---|
| 102 | |
|---|
| 103 | Â Â Â Â _sc_card_add_rsa_alg(card, 512, flags, 0x10001); |
|---|
| 104 | Â Â Â Â _sc_card_add_rsa_alg(card, 768, flags, 0x10001); |
|---|
| 105 | Â Â Â Â _sc_card_add_rsa_alg(card,1024, flags, 0x10001); |
|---|
| 106 | |
|---|
| 107 | Â Â Â Â card->caps = SC_CARD_CAP_RNG; |
|---|
| 108 | |
|---|
| 109 | Â Â Â Â |
|---|
| 110 |     if (card->max_send_size > 128) |
|---|
| 111 | Â Â Â Â Â Â Â Â card->max_send_size = 128; |
|---|
| 112 |     if (card->max_recv_size > 128) |
|---|
| 113 | Â Â Â Â Â Â Â Â card->max_recv_size = 128; |
|---|
| 114 | |
|---|
| 115 |     return 0; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | static int starcos_finish(sc_card_t *card) |
|---|
| 119 | { |
|---|
| 120 |     if (card->drv_data) |
|---|
| 121 | Â Â Â Â Â Â Â Â free((starcos_ex_data *)card->drv_data); |
|---|
| 122 |     return 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | static int process_fci(sc_context_t *ctx, sc_file_t *file, |
|---|
| 126 |             const u8 *buf, size_t buflen) |
|---|
| 127 | { |
|---|
| 128 | Â Â Â Â |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | Â Â Â Â size_t taglen, len = buflen; |
|---|
| 135 |     const u8 *tag = NULL, *p; |
|---|
| 136 | Â |
|---|
| 137 |     if (ctx->debug >= 3) |
|---|
| 138 | Â Â Â Â Â Â Â Â sc_debug(ctx, "processing FCI bytes\n"); |
|---|
| 139 | |
|---|
| 140 |     if (buflen < 2) |
|---|
| 141 |         return SC_ERROR_INTERNAL; |
|---|
| 142 |     if (buf[0] != 0x6f) |
|---|
| 143 |         return SC_ERROR_INVALID_DATA; |
|---|
| 144 | Â Â Â Â len = (size_t)buf[1]; |
|---|
| 145 |     if (buflen - 2 < len) |
|---|
| 146 |         return SC_ERROR_INVALID_DATA; |
|---|
| 147 | Â Â Â Â p = buf + 2; |
|---|
| 148 | |
|---|
| 149 | Â Â Â Â |
|---|
| 150 | Â Â Â Â file->type = SC_FILE_TYPE_WORKING_EF; |
|---|
| 151 | Â Â Â Â file->ef_structure = SC_FILE_EF_UNKNOWN; |
|---|
| 152 | Â Â Â Â file->shareable = 0; |
|---|
| 153 | Â Â Â Â file->record_length = 0; |
|---|
| 154 | Â Â Â Â file->size = 0; |
|---|
| 155 | Â |
|---|
| 156 | Â Â Â Â tag = sc_asn1_find_tag(ctx, p, len, 0x80, &taglen); |
|---|
| 157 |     if (tag != NULL && taglen >= 2) { |
|---|
| 158 |         int bytes = (tag[0] << 8) + tag[1]; |
|---|
| 159 |         if (ctx->debug >= 3) |
|---|
| 160 | Â Â Â Â Â Â Â Â Â Â Â Â sc_debug(ctx, "Â bytes in file: %d\n", bytes); |
|---|
| 161 | Â Â Â Â Â Â Â Â file->size = bytes; |
|---|
| 162 | Â Â Â Â } |
|---|
| 163 | |
|---|
| 164 | Â Â Â Â tag = sc_asn1_find_tag(ctx, p, len, 0x82, &taglen); |
|---|
| 165 |     if (tag != NULL) { |
|---|
| 166 |         const char *type = "unknown"; |
|---|
| 167 |         const char *structure = "unknown"; |
|---|
| 168 | |
|---|
| 169 |         if (taglen == 1 && tag[0] == 0x01) { |
|---|
| 170 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 171 | Â Â Â Â Â Â Â Â Â Â Â Â type = "working EF"; |
|---|
| 172 | Â Â Â Â Â Â Â Â Â Â Â Â structure = "transparent"; |
|---|
| 173 | Â Â Â Â Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_WORKING_EF; |
|---|
| 174 | Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_TRANSPARENT; |
|---|
| 175 |         } else if (taglen == 1 && tag[0] == 0x11) { |
|---|
| 176 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 177 | Â Â Â Â Â Â Â Â Â Â Â Â type = "working EF"; |
|---|
| 178 | Â Â Â Â Â Â Â Â Â Â Â Â structure = "object"; |
|---|
| 179 | Â Â Â Â Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_WORKING_EF; |
|---|
| 180 | Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_TRANSPARENT; |
|---|
| 181 |         } else if (taglen == 3 && tag[1] == 0x21) { |
|---|
| 182 | Â Â Â Â Â Â Â Â Â Â Â Â type = "working EF"; |
|---|
| 183 | Â Â Â Â Â Â Â Â Â Â Â Â file->record_length = tag[2]; |
|---|
| 184 | Â Â Â Â Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_WORKING_EF; |
|---|
| 185 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 186 |             switch ( tag[0] ) |
|---|
| 187 | Â Â Â Â Â Â Â Â Â Â Â Â { |
|---|
| 188 |                 case 0x02: |
|---|
| 189 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â structure = "linear fixed"; |
|---|
| 190 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_LINEAR_FIXED; |
|---|
| 191 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 192 |                 case 0x07: |
|---|
| 193 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â structure = "cyclic"; |
|---|
| 194 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_CYCLIC; |
|---|
| 195 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 196 |                 case 0x17: |
|---|
| 197 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â structure = "compute"; |
|---|
| 198 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_UNKNOWN; |
|---|
| 199 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 200 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â default: |
|---|
| 201 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â structure = "unknown"; |
|---|
| 202 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_UNKNOWN; |
|---|
| 203 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->record_length = 0; |
|---|
| 204 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 205 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 206 | Â Â Â Â Â Â Â Â } |
|---|
| 207 | |
|---|
| 208 |         if (ctx->debug >= 3) { |
|---|
| 209 | Â Â Â Â Â Â Â Â Â Â Â Â sc_debug(ctx, "Â type: %s\n", type); |
|---|
| 210 | Â Â Â Â Â Â Â Â Â Â Â Â sc_debug(ctx, "Â EF structure: %s\n", structure); |
|---|
| 211 | Â Â Â Â Â Â Â Â } |
|---|
| 212 | Â Â Â Â } |
|---|
| 213 | Â Â Â Â file->magic = SC_FILE_MAGIC; |
|---|
| 214 | |
|---|
| 215 |     return SC_SUCCESS; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | static int starcos_select_aid(sc_card_t *card, |
|---|
| 219 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â u8 aid[16], size_t len, |
|---|
| 220 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_t **file_out) |
|---|
| 221 | { |
|---|
| 222 | Â Â Â Â sc_apdu_t apdu; |
|---|
| 223 |     int r; |
|---|
| 224 | Â Â Â Â size_t i = 0; |
|---|
| 225 | |
|---|
| 226 | Â Â Â Â sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xA4, 0x04, 0x0C); |
|---|
| 227 | Â Â Â Â apdu.lc = len; |
|---|
| 228 | Â Â Â Â apdu.data = (u8*)aid; |
|---|
| 229 | Â Â Â Â apdu.datalen = len; |
|---|
| 230 | Â Â Â Â apdu.resplen = 0; |
|---|
| 231 | Â Â Â Â apdu.le = 0; |
|---|
| 232 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 233 | Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 234 | |
|---|
| 235 | Â Â Â Â |
|---|
| 236 |     if (!(apdu.sw1 == 0x90 && apdu.sw2 == 0x00) && apdu.sw1 != 0x61 ) |
|---|
| 237 | Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 2, sc_check_sw(card, apdu.sw1, apdu.sw2)); |
|---|
| 238 | Â |
|---|
| 239 | Â Â Â Â |
|---|
| 240 | Â Â Â Â card->cache.current_path.type = SC_PATH_TYPE_DF_NAME; |
|---|
| 241 | Â Â Â Â card->cache.current_path.len = len; |
|---|
| 242 | Â Â Â Â memcpy(card->cache.current_path.value, aid, len); |
|---|
| 243 | |
|---|
| 244 |     if (file_out) { |
|---|
| 245 | Â Â Â Â Â Â Â Â sc_file_t *file = sc_file_new(); |
|---|
| 246 |         if (!file) |
|---|
| 247 | Â Â Â Â Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 0, SC_ERROR_OUT_OF_MEMORY); |
|---|
| 248 | Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_DF; |
|---|
| 249 | Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_UNKNOWN; |
|---|
| 250 | Â Â Â Â Â Â Â Â file->path.len = 0; |
|---|
| 251 | Â Â Â Â Â Â Â Â file->size = 0; |
|---|
| 252 | Â Â Â Â Â Â Â Â |
|---|
| 253 |         for (i = 0; i < len; i++) |
|---|
| 254 | Â Â Â Â Â Â Â Â Â Â Â Â file->name[i] = aid[i]; |
|---|
| 255 | Â Â Â Â Â Â Â Â file->namelen = len; |
|---|
| 256 | Â Â Â Â Â Â Â Â file->id = 0x0000; |
|---|
| 257 | Â Â Â Â Â Â Â Â file->magic = SC_FILE_MAGIC; |
|---|
| 258 | Â Â Â Â Â Â Â Â *file_out = file; |
|---|
| 259 | Â Â Â Â } |
|---|
| 260 | Â Â Â Â SC_FUNC_RETURN(card->ctx, 2, SC_SUCCESS); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | static int starcos_select_fid(sc_card_t *card, |
|---|
| 264 |                unsigned int id_hi, unsigned int id_lo, |
|---|
| 265 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_t **file_out) |
|---|
| 266 | { |
|---|
| 267 | Â Â Â Â sc_apdu_t apdu; |
|---|
| 268 | Â Â Â Â u8 data[] = {id_hi & 0xff, id_lo & 0xff}; |
|---|
| 269 | Â Â Â Â u8 resp[SC_MAX_APDU_BUFFER_SIZE]; |
|---|
| 270 |     int bIsDF = 0, r; |
|---|
| 271 | |
|---|
| 272 | Â Â Â Â |
|---|
| 273 | Â Â Â Â sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0x00, 0x00); |
|---|
| 274 | Â Â Â Â apdu.p2Â Â = 0x00; |
|---|
| 275 | Â Â Â Â apdu.resp = (u8*)resp; |
|---|
| 276 | Â Â Â Â apdu.resplen = SC_MAX_APDU_BUFFER_SIZE; |
|---|
| 277 | Â Â Â Â apdu.le = 256; |
|---|
| 278 | Â Â Â Â apdu.lc = 2; |
|---|
| 279 | Â Â Â Â apdu.data = (u8*)data; |
|---|
| 280 | Â Â Â Â apdu.datalen = 2; |
|---|
| 281 | |
|---|
| 282 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 283 | Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 284 | |
|---|
| 285 |     if (apdu.p2 == 0x00 && apdu.sw1 == 0x62 && apdu.sw2 == 0x84 ) { |
|---|
| 286 | Â Â Â Â Â Â Â Â |
|---|
| 287 | Â Â Â Â Â Â Â Â bIsDF = 1; |
|---|
| 288 | Â Â Â Â Â Â Â Â apdu.p2 = 0x0C; |
|---|
| 289 | Â Â Â Â Â Â Â Â apdu.cse = SC_APDU_CASE_3_SHORT; |
|---|
| 290 | Â Â Â Â Â Â Â Â apdu.resplen = 0; |
|---|
| 291 | Â Â Â Â Â Â Â Â apdu.le = 0; |
|---|
| 292 | Â Â Â Â Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 293 | Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU re-transmit failed"); |
|---|
| 294 |     } else if (apdu.sw1 == 0x61 || (apdu.sw1 == 0x90 && apdu.sw2 == 0x00)) { |
|---|
| 295 | Â Â Â Â Â Â Â Â |
|---|
| 296 | |
|---|
| 297 | Â Â Â Â Â Â Â Â sc_apdu_t apdu2; |
|---|
| 298 | Â Â Â Â Â Â Â Â u8 resp2[2]; |
|---|
| 299 | Â Â Â Â Â Â Â Â sc_format_apdu(card, &apdu2, SC_APDU_CASE_2_SHORT, 0xB0, 0, 0); |
|---|
| 300 | Â Â Â Â Â Â Â Â apdu2.resp = (u8*)resp2; |
|---|
| 301 | Â Â Â Â Â Â Â Â apdu2.resplen = 2; |
|---|
| 302 | Â Â Â Â Â Â Â Â apdu2.le = 1; |
|---|
| 303 | Â Â Â Â Â Â Â Â apdu2.lc = 0; |
|---|
| 304 | Â Â Â Â Â Â Â Â r = sc_transmit_apdu(card, &apdu2); |
|---|
| 305 | Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 306 |         if (apdu2.sw1 == 0x69 && apdu2.sw2 == 0x86) |
|---|
| 307 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 308 | Â Â Â Â Â Â Â Â Â Â Â Â bIsDF = 1; |
|---|
| 309 | Â Â Â Â } |
|---|
| 310 | |
|---|
| 311 |     if (apdu.sw1 != 0x61 && (apdu.sw1 != 0x90 || apdu.sw2 != 0x00)) |
|---|
| 312 | Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 2, sc_check_sw(card, apdu.sw1, apdu.sw2)); |
|---|
| 313 | |
|---|
| 314 | Â Â Â Â |
|---|
| 315 |     if (bIsDF) { |
|---|
| 316 | Â Â Â Â Â Â Â Â card->cache.current_path.type = SC_PATH_TYPE_PATH; |
|---|
| 317 | Â Â Â Â Â Â Â Â card->cache.current_path.value[0] = 0x3f; |
|---|
| 318 | Â Â Â Â Â Â Â Â card->cache.current_path.value[1] = 0x00; |
|---|
| 319 |         if (id_hi == 0x3f && id_lo == 0x00) |
|---|
| 320 | Â Â Â Â Â Â Â Â Â Â Â Â card->cache.current_path.len = 2; |
|---|
| 321 |         else { |
|---|
| 322 | Â Â Â Â Â Â Â Â Â Â Â Â card->cache.current_path.len = 4; |
|---|
| 323 | Â Â Â Â Â Â Â Â Â Â Â Â card->cache.current_path.value[2] = id_hi; |
|---|
| 324 | Â Â Â Â Â Â Â Â Â Â Â Â card->cache.current_path.value[3] = id_lo; |
|---|
| 325 | Â Â Â Â Â Â Â Â } |
|---|
| 326 | Â Â Â Â } |
|---|
| 327 | |
|---|
| 328 |     if (file_out) { |
|---|
| 329 | Â Â Â Â Â Â Â Â sc_file_t *file = sc_file_new(); |
|---|
| 330 |         if (!file) |
|---|
| 331 | Â Â Â Â Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 0, SC_ERROR_OUT_OF_MEMORY); |
|---|
| 332 |         file->id  = (id_hi << 8) + id_lo; |
|---|
| 333 | Â Â Â Â Â Â Â Â file->path = card->cache.current_path; |
|---|
| 334 | |
|---|
| 335 |         if (bIsDF) { |
|---|
| 336 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 337 | Â Â Â Â Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_DF; |
|---|
| 338 | Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_UNKNOWN; |
|---|
| 339 | Â Â Â Â Â Â Â Â Â Â Â Â file->size = 0; |
|---|
| 340 | Â Â Â Â Â Â Â Â Â Â Â Â file->namelen = 0; |
|---|
| 341 | Â Â Â Â Â Â Â Â Â Â Â Â file->magic = SC_FILE_MAGIC; |
|---|
| 342 | Â Â Â Â Â Â Â Â Â Â Â Â *file_out = file; |
|---|
| 343 |         } else { |
|---|
| 344 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 345 | Â Â Â Â Â Â Â Â Â Â Â Â r = process_fci(card->ctx, file, apdu.resp, |
|---|
| 346 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â apdu.resplen); |
|---|
| 347 |             if (r != SC_SUCCESS) { |
|---|
| 348 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_free(file); |
|---|
| 349 |                 return r; |
|---|
| 350 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 351 | |
|---|
| 352 | Â Â Â Â Â Â Â Â Â Â Â Â *file_out = file; |
|---|
| 353 | Â Â Â Â Â Â Â Â } |
|---|
| 354 | Â Â Â Â } |
|---|
| 355 | |
|---|
| 356 | Â Â Â Â SC_FUNC_RETURN(card->ctx, 2, SC_SUCCESS); |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | static int starcos_select_file(sc_card_t *card, |
|---|
| 360 |                 const sc_path_t *in_path, |
|---|
| 361 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_t **file_out) |
|---|
| 362 | { |
|---|
| 363 | Â Â Â Â u8 pathbuf[SC_MAX_PATH_SIZE], *path = pathbuf; |
|---|
| 364 |     int  r; |
|---|
| 365 | Â Â Â Â size_t i, pathlen; |
|---|
| 366 | |
|---|
| 367 | Â Â Â Â SC_FUNC_CALLED(card->ctx, 1); |
|---|
| 368 | |
|---|
| 369 |     if (card->ctx->debug >= 4) { |
|---|
| 370 |         char pbuf[SC_MAX_PATH_STRING_SIZE]; |
|---|
| 371 | |
|---|
| 372 | Â Â Â Â Â Â Â Â r = sc_path_print(pbuf, sizeof(pbuf), &card->cache.current_path); |
|---|
| 373 |         if (r != SC_SUCCESS) |
|---|
| 374 | Â Â Â Â Â Â Â Â Â Â Â Â pbuf[0] = '\0'; |
|---|
| 375 | |
|---|
| 376 | Â Â Â Â Â Â Â Â sc_debug(card->ctx, "current path (%s, %s): %s (len: %u)\n", |
|---|
| 377 | Â Â Â Â Â Â Â Â Â Â Â Â (card->cache.current_path.type==SC_PATH_TYPE_DF_NAME?"aid":"path"), |
|---|
| 378 | Â Â Â Â Â Â Â Â Â Â Â Â (card->cache_valid?"valid":"invalid"), pbuf, |
|---|
| 379 | Â Â Â Â Â Â Â Â Â Â Â Â card->cache.current_path.len); |
|---|
| 380 | Â Â Â Â } |
|---|
| 381 | Â |
|---|
| 382 | Â Â Â Â memcpy(path, in_path->value, in_path->len); |
|---|
| 383 | Â Â Â Â pathlen = in_path->len; |
|---|
| 384 | |
|---|
| 385 |     if (in_path->type == SC_PATH_TYPE_FILE_ID) |
|---|
| 386 | Â Â Â Â {Â Â Â Â |
|---|
| 387 | Â Â Â Â Â Â Â Â |
|---|
| 388 |         if (pathlen != 2) |
|---|
| 389 | Â Â Â Â Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx,2,SC_ERROR_INVALID_ARGUMENTS); |
|---|
| 390 |         return starcos_select_fid(card, path[0], path[1], file_out); |
|---|
| 391 | Â Â Â Â } |
|---|
| 392 |     else if (in_path->type == SC_PATH_TYPE_DF_NAME) |
|---|
| 393 | Â Â Â Â {Â Â Â Â |
|---|
| 394 | Â Â Â Â Â Â Â Â |
|---|
| 395 |         if (card->cache_valid |
|---|
| 396 | Â Â Â Â Â Â Â Â Â Â && card->cache.current_path.type == SC_PATH_TYPE_DF_NAME |
|---|
| 397 | Â Â Â Â Â Â Â Â Â Â && card->cache.current_path.len == pathlen |
|---|
| 398 | Â Â Â Â Â Â Â Â Â Â && memcmp(card->cache.current_path.value, pathbuf, pathlen) == 0 ) |
|---|
| 399 | Â Â Â Â Â Â Â Â { |
|---|
| 400 |             if (card->ctx->debug >= 4) |
|---|
| 401 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_debug(card->ctx, "cache hit\n"); |
|---|
| 402 | Â Â Â Â Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 2, SC_SUCCESS); |
|---|
| 403 | Â Â Â Â Â Â Â Â } |
|---|
| 404 | Â Â Â Â Â Â Â Â else |
|---|
| 405 |             return starcos_select_aid(card, pathbuf, pathlen, file_out); |
|---|
| 406 | Â Â Â Â } |
|---|
| 407 |     else if (in_path->type == SC_PATH_TYPE_PATH) |
|---|
| 408 | Â Â Â Â { |
|---|
| 409 | Â Â Â Â Â Â Â Â u8 n_pathbuf[SC_MAX_PATH_SIZE]; |
|---|
| 410 |         int bMatch = -1; |
|---|
| 411 | |
|---|
| 412 | Â Â Â Â Â Â Â Â |
|---|
| 413 | Â Â Â Â Â Â Â Â |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 |         if (pathlen%2 != 0 || pathlen > 6 || pathlen <= 0) |
|---|
| 419 | Â Â Â Â Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS); |
|---|
| 420 | Â Â Â Â Â Â Â Â |
|---|
| 421 |         if (pathlen == 6 && ( path[0] != 0x3f || path[1] != 0x00 )) |
|---|
| 422 | Â Â Â Â Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS); |
|---|
| 423 | |
|---|
| 424 | Â Â Â Â Â Â Â Â |
|---|
| 425 |         if (path[0] != 0x3f || path[1] != 0x00) |
|---|
| 426 | Â Â Â Â Â Â Â Â { |
|---|
| 427 | Â Â Â Â Â Â Â Â Â Â Â Â n_pathbuf[0] = 0x3f; |
|---|
| 428 | Â Â Â Â Â Â Â Â Â Â Â Â n_pathbuf[1] = 0x00; |
|---|
| 429 |             for (i=0; i< pathlen; i++) |
|---|
| 430 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â n_pathbuf[i+2] = pathbuf[i]; |
|---|
| 431 | Â Â Â Â Â Â Â Â Â Â Â Â path = n_pathbuf; |
|---|
| 432 | Â Â Â Â Â Â Â Â Â Â Â Â pathlen += 2; |
|---|
| 433 | Â Â Â Â Â Â Â Â } |
|---|
| 434 | Â Â Â Â |
|---|
| 435 | Â Â Â Â Â Â Â Â |
|---|
| 436 |         if (card->cache_valid |
|---|
| 437 | Â Â Â Â Â Â Â Â Â Â && card->cache.current_path.type == SC_PATH_TYPE_PATH |
|---|
| 438 | Â Â Â Â Â Â Â Â Â Â && card->cache.current_path.len >= 2 |
|---|
| 439 | Â Â Â Â Â Â Â Â Â Â && card->cache.current_path.len <= pathlen ) |
|---|
| 440 | Â Â Â Â Â Â Â Â { |
|---|
| 441 | Â Â Â Â Â Â Â Â Â Â Â Â bMatch = 0; |
|---|
| 442 |             for (i=0; i < card->cache.current_path.len; i+=2) |
|---|
| 443 |                 if (card->cache.current_path.value[i] == path[i] |
|---|
| 444 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â && card->cache.current_path.value[i+1] == path[i+1] ) |
|---|
| 445 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â bMatch += 2; |
|---|
| 446 | Â Â Â Â Â Â Â Â } |
|---|
| 447 | |
|---|
| 448 |         if ( card->cache_valid && bMatch >= 0 ) |
|---|
| 449 | Â Â Â Â Â Â Â Â { |
|---|
| 450 |             if ( pathlen - bMatch == 2 ) |
|---|
| 451 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 452 |                 return starcos_select_fid(card, path[bMatch], path[bMatch+1], file_out); |
|---|
| 453 |             else if ( pathlen - bMatch > 2 ) |
|---|
| 454 | Â Â Â Â Â Â Â Â Â Â Â Â { |
|---|
| 455 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 456 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_path_t new_path; |
|---|
| 457 | Â Â Â Â |
|---|
| 458 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 459 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â r = starcos_select_fid(card, path[bMatch], path[bMatch+1], NULL); |
|---|
| 460 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "SELECT FILE (DF-ID) failed"); |
|---|
| 461 | Â Â Â Â Â Â Â Â |
|---|
| 462 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â new_path.type = SC_PATH_TYPE_PATH; |
|---|
| 463 |                 new_path.len = pathlen - bMatch-2; |
|---|
| 464 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â memcpy(new_path.value, &(path[bMatch+2]), new_path.len); |
|---|
| 465 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 466 |                 return starcos_select_file(card, &new_path, file_out); |
|---|
| 467 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 468 |             else |
|---|
| 469 | Â Â Â Â Â Â Â Â Â Â Â Â { |
|---|
| 470 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 471 | |
|---|
| 472 |                 if ( card->ctx->debug >= 4 ) |
|---|
| 473 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_debug(card->ctx, "cache hit\n"); |
|---|
| 474 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 475 |                 if (file_out) { |
|---|
| 476 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_t *file = sc_file_new(); |
|---|
| 477 |                     if (!file) |
|---|
| 478 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 0, SC_ERROR_OUT_OF_MEMORY); |
|---|
| 479 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->id = (path[pathlen-2] << 8) + |
|---|
| 480 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â path[pathlen-1]; |
|---|
| 481 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->path = card->cache.current_path; |
|---|
| 482 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_DF; |
|---|
| 483 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = SC_FILE_EF_UNKNOWN; |
|---|
| 484 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->size = 0; |
|---|
| 485 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->namelen = 0; |
|---|
| 486 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->magic = SC_FILE_MAGIC; |
|---|
| 487 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â *file_out = file; |
|---|
| 488 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 489 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 490 |                 return SC_SUCCESS; |
|---|
| 491 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 492 | Â Â Â Â Â Â Â Â } |
|---|
| 493 | Â Â Â Â Â Â Â Â else |
|---|
| 494 | Â Â Â Â Â Â Â Â { |
|---|
| 495 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 496 |             for ( i=0; i<pathlen-2; i+=2 ) |
|---|
| 497 | Â Â Â Â Â Â Â Â Â Â Â Â { |
|---|
| 498 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â r = starcos_select_fid(card, path[i], path[i+1], NULL); |
|---|
| 499 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "SELECT FILE (DF-ID) failed"); |
|---|
| 500 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 501 |             return starcos_select_fid(card, path[pathlen-2], path[pathlen-1], file_out); |
|---|
| 502 | Â Â Â Â Â Â Â Â } |
|---|
| 503 | Â Â Â Â } |
|---|
| 504 | Â Â Â Â else |
|---|
| 505 | Â Â Â Â Â Â Â Â SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS); |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | #define STARCOS_AC_ALWAYS    0x9f |
|---|
| 509 | #define STARCOS_AC_NEVER    0x5f |
|---|
| 510 | #define STARCOS_PINID2STATE(a) ((((a) & 0x0f) == 0x01) ? ((a) & 0x0f) : (0x0f - ((0x0f & (a)) >> 1))) |
|---|
| 511 | |
|---|
| 512 | static u8 process_acl_entry(sc_file_t *in, unsigned int method, unsigned int in_def) |
|---|
| 513 | { |
|---|
| 514 | Â Â Â Â u8 def = (u8)in_def; |
|---|
| 515 |     const sc_acl_entry_t *entry = sc_file_get_acl_entry(in, method); |
|---|
| 516 |     if (!entry) |
|---|
| 517 |         return def; |
|---|
| 518 |     else if (entry->method & SC_AC_CHV) { |
|---|
| 519 |         unsigned int key_ref = entry->key_ref; |
|---|
| 520 |         if (key_ref == SC_AC_KEY_REF_NONE) |
|---|
| 521 |             return def; |
|---|
| 522 |         else if ((key_ref & 0x0f) == 1) |
|---|
| 523 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 524 |             return (key_ref & 0x80 ? 0x10 : 0x00) | 0x01; |
|---|
| 525 | Â Â Â Â Â Â Â Â else |
|---|
| 526 |             return (key_ref & 0x80 ? 0x10 : 0x00) | STARCOS_PINID2STATE(key_ref); |
|---|
| 527 |     } else if (entry->method & SC_AC_NEVER) |
|---|
| 528 |         return STARCOS_AC_NEVER; |
|---|
| 529 | Â Â Â Â else |
|---|
| 530 |         return def; |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | |
|---|
| 539 | |
|---|
| 540 | |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | static int starcos_process_acl(sc_card_t *card, sc_file_t *file, |
|---|
| 544 | Â Â Â Â sc_starcos_create_data *data) |
|---|
| 545 | { |
|---|
| 546 | Â Â Â Â u8Â Â Â tmp, *p; |
|---|
| 547 |     static const u8 def_key[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; |
|---|
| 548 | |
|---|
| 549 |     if (file->type == SC_FILE_TYPE_DF && file->id == 0x3f00) { |
|---|
| 550 |         p  = data->data.mf.header; |
|---|
| 551 | Â Â Â Â Â Â Â Â memcpy(p, def_key, 8); |
|---|
| 552 |         p  += 8; |
|---|
| 553 | Â Â Â Â Â Â Â Â *p++ = (file->size >> 8) & 0xff; |
|---|
| 554 | Â Â Â Â Â Â Â Â *p++ = file->size & 0xff; |
|---|
| 555 | Â Â Â Â Â Â Â Â |
|---|
| 556 | Â Â Â Â Â Â Â Â *p++ = (file->size >> 10) & 0xff; |
|---|
| 557 | Â Â Â Â Â Â Â Â *p++ = (file->size >> 2)Â & 0xff; |
|---|
| 558 | Â Â Â Â Â Â Â Â |
|---|
| 559 | Â Â Â Â Â Â Â Â *p++ = process_acl_entry(file,SC_AC_OP_CREATE,STARCOS_AC_ALWAYS); |
|---|
| 560 | Â Â Â Â Â Â Â Â |
|---|
| 561 | Â Â Â Â Â Â Â Â *p++ = process_acl_entry(file,SC_AC_OP_CREATE,STARCOS_AC_ALWAYS); |
|---|
| 562 | Â Â Â Â Â Â Â Â |
|---|
| 563 | Â Â Â Â Â Â Â Â *p++ = process_acl_entry(file,SC_AC_OP_CREATE,STARCOS_AC_ALWAYS); |
|---|
| 564 | Â Â Â Â Â Â Â Â |
|---|
| 565 | Â Â Â Â Â Â Â Â *p++ = data->data.mf.header[14]; |
|---|
| 566 | Â Â Â Â Â Â Â Â |
|---|
| 567 |         if (file->acl[SC_AC_OP_CREATE] && (sc_file_get_acl_entry(file, SC_AC_OP_CREATE))->method & SC_AC_PRO) |
|---|
| 568 | Â Â Â Â Â Â Â Â Â Â Â Â tmp = 0x03;Â Â Â |
|---|
| 569 | Â Â Â Â Â Â Â Â else |
|---|
| 570 | Â Â Â Â Â Â Â Â Â Â Â Â tmp = 0x00;Â Â Â |
|---|
| 571 | Â Â Â Â Â Â Â Â *p++ = tmp;Â Â Â |
|---|
| 572 | Â Â Â Â Â Â Â Â *p++ = tmp; |
|---|
| 573 | Â Â Â Â Â Â Â Â *p++ = tmp; |
|---|
| 574 | Â Â Â Â Â Â Â Â data->type = SC_STARCOS_MF_DATA; |
|---|
| 575 | |
|---|
| 576 |         return SC_SUCCESS; |
|---|
| 577 |     } else if (file->type == SC_FILE_TYPE_DF){ |
|---|
| 578 |         p  = data->data.df.header; |
|---|
| 579 | Â Â Â Â Â Â Â Â *p++ = (file->id >> 8) & 0xff; |
|---|
| 580 | Â Â Â Â Â Â Â Â *p++ = file->id & 0xff; |
|---|
| 581 |         if (file->namelen) { |
|---|
| 582 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 583 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = file->namelen & 0xff; |
|---|
| 584 | Â Â Â Â Â Â Â Â Â Â Â Â memset(p, 0, 16); |
|---|
| 585 | Â Â Â Â Â Â Â Â Â Â Â Â memcpy(p, file->name, (u8)file->namelen); |
|---|
| 586 |             p  += 16; |
|---|
| 587 |         } else { |
|---|
| 588 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 589 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = 2; |
|---|
| 590 | Â Â Â Â Â Â Â Â Â Â Â Â memset(p, 0, 16); |
|---|
| 591 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = (file->id >> 8) & 0xff; |
|---|
| 592 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = file->id & 0xff; |
|---|
| 593 |             p  += 14; |
|---|
| 594 | Â Â Â Â Â Â Â Â } |
|---|
| 595 | Â Â Â Â Â Â Â Â |
|---|
| 596 | Â Â Â Â Â Â Â Â *p++ = (file->size >> 10) & 0xff;Â Â Â Â |
|---|
| 597 | Â Â Â Â Â Â Â Â *p++ = (file->size >> 2)Â & 0xff;Â Â Â Â |
|---|
| 598 | Â Â Â Â Â Â Â Â |
|---|
| 599 | Â Â Â Â Â Â Â Â *p++ = process_acl_entry(file,SC_AC_OP_CREATE,STARCOS_AC_ALWAYS); |
|---|
| 600 | Â Â Â Â Â Â Â Â |
|---|
| 601 | Â Â Â Â Â Â Â Â *p++ = process_acl_entry(file,SC_AC_OP_CREATE,STARCOS_AC_ALWAYS); |
|---|
| 602 | Â Â Â Â Â Â Â Â |
|---|
| 603 |         if (file->acl[SC_AC_OP_CREATE] && |
|---|
| 604 | Â Â Â Â Â Â Â Â Â Â (sc_file_get_acl_entry(file, SC_AC_OP_CREATE)->method & |
|---|
| 605 | Â Â Â Â Â Â Â Â Â Â Â SC_AC_PRO)) |
|---|
| 606 | Â Â Â Â Â Â Â Â Â Â Â Â tmp = 0x03; |
|---|
| 607 | Â Â Â Â Â Â Â Â else |
|---|
| 608 | Â Â Â Â Â Â Â Â Â Â Â Â tmp = 0x00; |
|---|
| 609 | Â Â Â Â Â Â Â Â *p++ = tmp;Â Â Â |
|---|
| 610 | Â Â Â Â Â Â Â Â *p++ = tmp;Â Â Â |
|---|
| 611 | |
|---|
| 612 | Â Â Â Â Â Â Â Â data->data.df.size[0] = (file->size >> 8) & 0xff; |
|---|
| 613 | Â Â Â Â Â Â Â Â data->data.df.size[1] = file->size & 0xff; |
|---|
| 614 | Â Â Â Â Â Â Â Â data->type = SC_STARCOS_DF_DATA; |
|---|
| 615 | |
|---|
| 616 |         return SC_SUCCESS; |
|---|
| 617 |     } else if (file->type == SC_FILE_TYPE_WORKING_EF) { |
|---|
| 618 |         p  = data->data.ef.header; |
|---|
| 619 | Â Â Â Â Â Â Â Â *p++ = (file->id >> 8) & 0xff; |
|---|
| 620 | Â Â Â Â Â Â Â Â *p++ = file->id & 0xff; |
|---|
| 621 | Â Â Â Â Â Â Â Â |
|---|
| 622 | Â Â Â Â Â Â Â Â *p++ = process_acl_entry(file, SC_AC_OP_READ,STARCOS_AC_ALWAYS); |
|---|
| 623 | Â Â Â Â Â Â Â Â |
|---|
| 624 | Â Â Â Â Â Â Â Â *p++ = process_acl_entry(file, SC_AC_OP_WRITE,STARCOS_AC_ALWAYS); |
|---|
| 625 | Â Â Â Â Â Â Â Â |
|---|
| 626 | Â Â Â Â Â Â Â Â *p++ = process_acl_entry(file, SC_AC_OP_ERASE,STARCOS_AC_ALWAYS); |
|---|
| 627 | Â Â Â Â Â Â Â Â *p++ = STARCOS_AC_ALWAYS;Â Â Â Â |
|---|
| 628 | Â Â Â Â Â Â Â Â *p++ = STARCOS_AC_ALWAYS;Â Â Â Â |
|---|
| 629 | Â Â Â Â Â Â Â Â *p++ = STARCOS_AC_ALWAYS;Â Â Â Â |
|---|
| 630 | Â Â Â Â Â Â Â Â *p++ = STARCOS_AC_ALWAYS;Â Â Â Â |
|---|
| 631 | Â Â Â Â Â Â Â Â *p++ = 0x00;Â Â Â Â Â Â Â Â Â Â |
|---|
| 632 | Â Â Â Â Â Â Â Â *p++ = 0x00;Â Â Â Â Â Â Â Â Â Â |
|---|
| 633 | Â Â Â Â Â Â Â Â |
|---|
| 634 |         if ((file->acl[SC_AC_OP_READ]  && (sc_file_get_acl_entry(file, SC_AC_OP_READ)->method & SC_AC_PRO)) || |
|---|
| 635 | Â Â Â Â Â Â Â Â Â Â (file->acl[SC_AC_OP_UPDATE] && (sc_file_get_acl_entry(file, SC_AC_OP_UPDATE)->method & SC_AC_PRO)) || |
|---|
| 636 | Â Â Â Â Â Â Â Â Â Â (file->acl[SC_AC_OP_WRITE]Â && (sc_file_get_acl_entry(file, SC_AC_OP_WRITE)->method & SC_AC_PRO)) ) |
|---|
| 637 | Â Â Â Â Â Â Â Â Â Â Â Â tmp = 0x03; |
|---|
| 638 | Â Â Â Â Â Â Â Â else |
|---|
| 639 | Â Â Â Â Â Â Â Â Â Â Â Â tmp = 0x00; |
|---|
| 640 | Â Â Â Â Â Â Â Â *p++ = tmp;Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 641 | Â Â Â Â Â Â Â Â *p++ = 0x00;Â Â Â Â Â Â Â Â Â Â |
|---|
| 642 | |
|---|
| 643 |         switch (file->ef_structure) |
|---|
| 644 | Â Â Â Â Â Â Â Â { |
|---|
| 645 |         case SC_FILE_EF_TRANSPARENT: |
|---|
| 646 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = 0x81; |
|---|
| 647 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = (file->size >> 8) & 0xff; |
|---|
| 648 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = file->size & 0xff; |
|---|
| 649 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 650 |         case SC_FILE_EF_LINEAR_FIXED: |
|---|
| 651 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = 0x82; |
|---|
| 652 |             *p++ = file->record_count & 0xff; |
|---|
| 653 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = file->record_length & 0xff; |
|---|
| 654 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 655 |         case SC_FILE_EF_CYCLIC: |
|---|
| 656 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = 0x84; |
|---|
| 657 |             *p++ = file->record_count & 0xff; |
|---|
| 658 | Â Â Â Â Â Â Â Â Â Â Â Â *p++ = file->record_length & 0xff; |
|---|
| 659 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 660 | Â Â Â Â Â Â Â Â default: |
|---|
| 661 |             return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 662 | Â Â Â Â Â Â Â Â } |
|---|
| 663 | Â Â Â Â Â Â Â Â data->type = SC_STARCOS_EF_DATA; |
|---|
| 664 | |
|---|
| 665 |         return SC_SUCCESS; |
|---|
| 666 | Â Â Â Â } else |
|---|
| 667 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | static int starcos_create_mf(sc_card_t *card, sc_starcos_create_data *data) |
|---|
| 681 | { |
|---|
| 682 |     int  r; |
|---|
| 683 |     sc_apdu_t    apdu; |
|---|
| 684 |     sc_context_t  *ctx = card->ctx; |
|---|
| 685 | |
|---|
| 686 |     if (ctx->debug >= 3) |
|---|
| 687 | Â Â Â Â Â Â Â Â sc_debug(ctx, "creating MF \n"); |
|---|
| 688 | Â Â Â Â sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x00, 0x00); |
|---|
| 689 | Â Â Â Â apdu.cla |= 0x80; |
|---|
| 690 |     apdu.lc  = 19; |
|---|
| 691 | Â Â Â Â apdu.datalen = 19; |
|---|
| 692 | Â Â Â Â apdu.data = (u8 *) data->data.mf.header; |
|---|
| 693 | |
|---|
| 694 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 695 | Â Â Â Â SC_TEST_RET(ctx, r, "APDU transmit failed"); |
|---|
| 696 |     return sc_check_sw(card, apdu.sw1, apdu.sw2);  |
|---|
| 697 | } |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | static int starcos_create_df(sc_card_t *card, sc_starcos_create_data *data) |
|---|
| 710 | { |
|---|
| 711 |     int  r; |
|---|
| 712 | Â Â Â Â size_t len; |
|---|
| 713 |     sc_apdu_t    apdu; |
|---|
| 714 |     sc_context_t  *ctx = card->ctx; |
|---|
| 715 | |
|---|
| 716 |     if (ctx->debug >= 3) |
|---|
| 717 | Â Â Â Â Â Â Â Â sc_debug(ctx, "creating DF\n"); |
|---|
| 718 | Â Â Â Â |
|---|
| 719 |     if (ctx->debug >= 3) |
|---|
| 720 | Â Â Â Â Â Â Â Â sc_debug(ctx, "calling REGISTER DF\n"); |
|---|
| 721 | |
|---|
| 722 | Â Â Â Â sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x52, |
|---|
| 723 | Â Â Â Â Â Â Â Â Â Â Â Â data->data.df.size[0], data->data.df.size[1]); |
|---|
| 724 |     len = 3 + data->data.df.header[2]; |
|---|
| 725 | Â Â Â Â apdu.cla |= 0x80; |
|---|
| 726 |     apdu.lc  = len; |
|---|
| 727 | Â Â Â Â apdu.datalen = len; |
|---|
| 728 | Â Â Â Â apdu.data = data->data.df.header; |
|---|
| 729 | |
|---|
| 730 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 731 | Â Â Â Â SC_TEST_RET(ctx, r, "APDU transmit failed"); |
|---|
| 732 | Â Â Â Â |
|---|
| 733 |     if (ctx->debug >= 3) |
|---|
| 734 | Â Â Â Â Â Â Â Â sc_debug(ctx, "calling CREATE DF\n"); |
|---|
| 735 | |
|---|
| 736 | Â Â Â Â sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x01, 0x00); |
|---|
| 737 | Â Â Â Â apdu.cla |= 0x80; |
|---|
| 738 |     apdu.lc  = 25; |
|---|
| 739 | Â Â Â Â apdu.datalen = 25; |
|---|
| 740 | Â Â Â Â apdu.data = data->data.df.header; |
|---|
| 741 | |
|---|
| 742 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 743 | Â Â Â Â SC_TEST_RET(ctx, r, "APDU transmit failed"); |
|---|
| 744 |     return sc_check_sw(card, apdu.sw1, apdu.sw2); |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | |
|---|
| 753 | |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | static int starcos_create_ef(sc_card_t *card, sc_starcos_create_data *data) |
|---|
| 757 | {Â Â Â Â |
|---|
| 758 |     int  r; |
|---|
| 759 |     sc_apdu_t    apdu; |
|---|
| 760 |     sc_context_t  *ctx = card->ctx; |
|---|
| 761 | |
|---|
| 762 |     if (ctx->debug >= 3) |
|---|
| 763 | Â Â Â Â Â Â Â Â sc_debug(ctx, "creating EF\n"); |
|---|
| 764 | |
|---|
| 765 | Â Â Â Â sc_format_apdu(card,&apdu,SC_APDU_CASE_3_SHORT,0xE0,0x03,0x00); |
|---|
| 766 | Â Â Â Â apdu.cla |= 0x80; |
|---|
| 767 |     apdu.lc  = 16; |
|---|
| 768 | Â Â Â Â apdu.datalen = 16; |
|---|
| 769 | Â Â Â Â apdu.data = (u8 *) data->data.ef.header; |
|---|
| 770 | |
|---|
| 771 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 772 | Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 773 |     return sc_check_sw(card, apdu.sw1, apdu.sw2); |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | |
|---|
| 777 | |
|---|
| 778 | |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | static int starcos_create_end(sc_card_t *card, sc_file_t *file) |
|---|
| 786 | { |
|---|
| 787 |     int r; |
|---|
| 788 | Â Â Â Â u8Â fid[2]; |
|---|
| 789 |     sc_apdu_t    apdu; |
|---|
| 790 | |
|---|
| 791 |     if (file->type != SC_FILE_TYPE_DF) |
|---|
| 792 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 793 | |
|---|
| 794 | Â Â Â Â fid[0] = (file->id >> 8) & 0xff; |
|---|
| 795 | Â Â Â Â fid[1] = file->id & 0xff; |
|---|
| 796 | Â Â Â Â sc_format_apdu(card,&apdu,SC_APDU_CASE_3_SHORT, 0xE0, 0x02, 0x00); |
|---|
| 797 | Â Â Â Â apdu.cla |= 0x80; |
|---|
| 798 |     apdu.lc  = 2; |
|---|
| 799 | Â Â Â Â apdu.datalen = 2; |
|---|
| 800 | Â Â Â Â apdu.data = fid; |
|---|
| 801 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 802 | Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 803 |     return sc_check_sw(card, apdu.sw1, apdu.sw2); |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | static int starcos_create_file(sc_card_t *card, sc_file_t *file) |
|---|
| 815 | {Â Â Â Â |
|---|
| 816 |     int  r; |
|---|
| 817 | Â Â Â Â sc_starcos_create_data data; |
|---|
| 818 | |
|---|
| 819 | Â Â Â Â SC_FUNC_CALLED(card->ctx, 1); |
|---|
| 820 | |
|---|
| 821 |     if (file->type == SC_FILE_TYPE_DF) { |
|---|
| 822 |         if (file->id == 0x3f00) { |
|---|
| 823 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 824 | Â Â Â Â Â Â Â Â Â Â Â Â r = starcos_process_acl(card, file, &data); |
|---|
| 825 |             if (r != SC_SUCCESS) |
|---|
| 826 |                 return r; |
|---|
| 827 |             return starcos_create_mf(card, &data); |
|---|
| 828 |         } else { |
|---|
| 829 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 830 | Â Â Â Â Â Â Â Â Â Â Â Â r = starcos_process_acl(card, file, &data); |
|---|
| 831 |             if (r != SC_SUCCESS) |
|---|
| 832 |                 return r; |
|---|
| 833 |             return starcos_create_df(card, &data); |
|---|
| 834 | Â Â Â Â Â Â Â Â } |
|---|
| 835 |     } else if (file->type == SC_FILE_TYPE_WORKING_EF) { |
|---|
| 836 | Â Â Â Â Â Â Â Â |
|---|
| 837 | Â Â Â Â Â Â Â Â r = starcos_process_acl(card, file, &data); |
|---|
| 838 |         if (r != SC_SUCCESS) |
|---|
| 839 |             return r; |
|---|
| 840 |         return starcos_create_ef(card, &data); |
|---|
| 841 | Â Â Â Â } else |
|---|
| 842 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 843 | } |
|---|
| 844 | |
|---|
| 845 | |
|---|
| 846 | |
|---|
| 847 | |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | static int starcos_erase_card(sc_card_t *card) |
|---|
| 853 | {Â Â Â Â |
|---|
| 854 |     int r; |
|---|
| 855 | Â Â Â Â u8Â sbuf[2]; |
|---|
| 856 | Â Â Â Â sc_apdu_t apdu; |
|---|
| 857 | |
|---|
| 858 | Â Â Â Â sbuf[0] = 0x3f; |
|---|
| 859 | Â Â Â Â sbuf[1] = 0x00; |
|---|
| 860 | Â Â Â Â sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, 0x00, 0x00); |
|---|
| 861 | Â Â Â Â apdu.cla |= 0x80; |
|---|
| 862 |     apdu.lc  = 2; |
|---|
| 863 | Â Â Â Â apdu.datalen = 2; |
|---|
| 864 | Â Â Â Â apdu.data = sbuf; |
|---|
| 865 | Â Â Â Â |
|---|
| 866 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 867 | Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 868 | Â Â Â Â |
|---|
| 869 | Â Â Â Â card->cache_valid = 0; |
|---|
| 870 |     if (apdu.sw1 == 0x69 && apdu.sw2 == 0x85) |
|---|
| 871 | Â Â Â Â Â Â Â Â |
|---|
| 872 |         return SC_SUCCESS; |
|---|
| 873 |     else return sc_check_sw(card, apdu.sw1, apdu.sw2); |
|---|
| 874 | } |
|---|
| 875 | |
|---|
| 876 | #define STARCOS_WKEY_CSIZE   124 |
|---|
| 877 | |
|---|
| 878 | |
|---|
| 879 | |
|---|
| 880 | |
|---|
| 881 | |
|---|
| 882 | |
|---|
| 883 | |
|---|
| 884 | |
|---|
| 885 | |
|---|
| 886 | |
|---|
| 887 | |
|---|
| 888 | static int starcos_write_key(sc_card_t *card, sc_starcos_wkey_data *data) |
|---|
| 889 | { |
|---|
| 890 |     int    r; |
|---|
| 891 | Â Â Â Â u8Â Â Â Â sbuf[SC_MAX_APDU_BUFFER_SIZE]; |
|---|
| 892 |     const u8 *p; |
|---|
| 893 |     size_t  len = sizeof(sbuf), tlen, offset = 0; |
|---|
| 894 |     sc_apdu_t    apdu; |
|---|
| 895 | |
|---|
| 896 |     if (data->mode == 0) { |
|---|
| 897 | Â Â Â Â Â Â Â Â |
|---|
| 898 | Â Â Â Â Â Â Â Â sbuf[0] = 0xc1; |
|---|
| 899 | Â Â Â Â Â Â Â Â sbuf[1] = 0x0c; |
|---|
| 900 | Â Â Â Â Â Â Â Â memcpy(sbuf + 2, data->key_header, 12); |
|---|
| 901 | Â Â Â Â Â Â Â Â sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xf4, |
|---|
| 902 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â data->mode, 0x00); |
|---|
| 903 | Â Â Â Â Â Â Â Â apdu.cla |= 0x80; |
|---|
| 904 |         apdu.lc  = 14; |
|---|
| 905 | Â Â Â Â Â Â Â Â apdu.datalen = 14; |
|---|
| 906 | Â Â Â Â Â Â Â Â apdu.data = sbuf; |
|---|
| 907 | |
|---|
| 908 | Â Â Â Â Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 909 | Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 910 |         if (apdu.sw1 != 0x90 || apdu.sw2 != 0x00) |
|---|
| 911 |             return sc_check_sw(card, apdu.sw1, apdu.sw2); |
|---|
| 912 |         if (data->key == NULL) |
|---|
| 913 |             return SC_SUCCESS; |
|---|
| 914 | Â Â Â Â } |
|---|
| 915 | |
|---|
| 916 |     if (data->key == NULL) |
|---|
| 917 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 918 | |
|---|
| 919 |     p  = data->key; |
|---|
| 920 | Â Â Â Â tlen = data->key_len; |
|---|
| 921 |     while (tlen != 0) { |
|---|
| 922 | Â Â Â Â Â Â Â Â |
|---|
| 923 | Â Â Â Â Â Â Â Â u8 clen = tlen < STARCOS_WKEY_CSIZE ? tlen : STARCOS_WKEY_CSIZE; |
|---|
| 924 | Â Â Â Â Â Â Â Â sbuf[0] = 0xc2; |
|---|
| 925 | Â Â Â Â Â Â Â Â sbuf[1] = 3 + clen; |
|---|
| 926 | Â Â Â Â Â Â Â Â sbuf[2] = data->kid; |
|---|
| 927 | Â Â Â Â Â Â Â Â sbuf[3] = (offset >> 8) & 0xff; |
|---|
| 928 | Â Â Â Â Â Â Â Â sbuf[4] = offset & 0xff; |
|---|
| 929 | Â Â Â Â Â Â Â Â memcpy(sbuf+5, p, clen); |
|---|
| 930 |         len   = 5 + clen; |
|---|
| 931 | Â Â Â Â Â Â Â Â sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xf4, |
|---|
| 932 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â data->mode, 0x00); |
|---|
| 933 |         apdu.cla  |= 0x80; |
|---|
| 934 |         apdu.lc   = len; |
|---|
| 935 | Â Â Â Â Â Â Â Â apdu.datalen = len; |
|---|
| 936 |         apdu.data  = sbuf; |
|---|
| 937 | |
|---|
| 938 | Â Â Â Â Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 939 | Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 940 |         if (apdu.sw1 != 0x90 || apdu.sw2 != 0x00) |
|---|
| 941 |             return sc_check_sw(card, apdu.sw1, apdu.sw2); |
|---|
| 942 | Â Â Â Â Â Â Â Â offset += clen; |
|---|
| 943 |         p   += clen; |
|---|
| 944 | Â Â Â Â Â Â Â |
|---|