|
| 1 | +// Copyright 2019 SoloKeys Developers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or |
| 4 | +// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 | +// http://opensource.org/licenses/MIT>, at your option. This file may not be |
| 6 | +// copied, modified, or distributed except according to those terms. |
| 7 | + |
| 8 | +// iso7816:2013. 5.3.2 Decoding conventions for command bodies |
| 9 | + |
| 10 | +#include "apdu.h" |
| 11 | + |
| 12 | +int apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu) |
| 13 | +{ |
| 14 | + EXT_APDU_HEADER *hapdu = (EXT_APDU_HEADER *)data; |
| 15 | + |
| 16 | + apdu->cla = hapdu->cla; |
| 17 | + apdu->ins = hapdu->ins; |
| 18 | + apdu->p1 = hapdu->p1; |
| 19 | + apdu->p2 = hapdu->p2; |
| 20 | + |
| 21 | + apdu->lc = 0; |
| 22 | + apdu->data = NULL; |
| 23 | + apdu->le = 0; |
| 24 | + apdu->extended_apdu = false; |
| 25 | + apdu->case_type = 0x00; |
| 26 | + |
| 27 | + uint8_t b0 = hapdu->lc[0]; |
| 28 | + |
| 29 | + // case 1 |
| 30 | + if (len == 4) |
| 31 | + { |
| 32 | + apdu->case_type = 0x01; |
| 33 | + } |
| 34 | + |
| 35 | + // case 2S (Le) |
| 36 | + if (len == 5) |
| 37 | + { |
| 38 | + apdu->case_type = 0x02; |
| 39 | + apdu->le = b0; |
| 40 | + if (!apdu->le) |
| 41 | + apdu->le = 0x100; |
| 42 | + } |
| 43 | + |
| 44 | + // case 3S (Lc + data) |
| 45 | + if (len == 5U + b0 && b0 != 0) |
| 46 | + { |
| 47 | + apdu->case_type = 0x03; |
| 48 | + apdu->lc = b0; |
| 49 | + } |
| 50 | + |
| 51 | + // case 4S (Lc + data + Le) |
| 52 | + if (len == 5U + b0 + 1U && b0 != 0) |
| 53 | + { |
| 54 | + apdu->case_type = 0x04; |
| 55 | + apdu->lc = b0; |
| 56 | + apdu->le = data[len - 1]; |
| 57 | + if (!apdu->le) |
| 58 | + apdu->le = 0x100; |
| 59 | + } |
| 60 | + |
| 61 | + // extended length apdu |
| 62 | + if (len >= 7 && b0 == 0) |
| 63 | + { |
| 64 | + uint16_t extlen = (hapdu->lc[1] << 8) + hapdu->lc[2]; |
| 65 | + |
| 66 | + // case 2E (Le) - extended |
| 67 | + if (len == 7) |
| 68 | + { |
| 69 | + apdu->case_type = 0x12; |
| 70 | + apdu->extended_apdu = true; |
| 71 | + apdu->le = extlen; |
| 72 | + if (!apdu->le) |
| 73 | + apdu->le = 0x10000; |
| 74 | + } |
| 75 | + |
| 76 | + // case 3E (Lc + data) - extended |
| 77 | + if (len == 7U + extlen) |
| 78 | + { |
| 79 | + apdu->case_type = 0x13; |
| 80 | + apdu->extended_apdu = true; |
| 81 | + apdu->lc = extlen; |
| 82 | + } |
| 83 | + |
| 84 | + // case 4E (Lc + data + Le) - extended 2-byte Le |
| 85 | + if (len == 7U + extlen + 2U) |
| 86 | + { |
| 87 | + apdu->case_type = 0x14; |
| 88 | + apdu->extended_apdu = true; |
| 89 | + apdu->lc = extlen; |
| 90 | + apdu->le = (data[len - 2] << 8) + data[len - 1]; |
| 91 | + if (!apdu->le) |
| 92 | + apdu->le = 0x10000; |
| 93 | + } |
| 94 | + |
| 95 | + // case 4E (Lc + data + Le) - extended 3-byte Le |
| 96 | + if (len == 7U + extlen + 3U && data[len - 3] == 0) |
| 97 | + { |
| 98 | + apdu->case_type = 0x24; |
| 99 | + apdu->extended_apdu = true; |
| 100 | + apdu->lc = extlen; |
| 101 | + apdu->le = (data[len - 2] << 8) + data[len - 1]; |
| 102 | + if (!apdu->le) |
| 103 | + apdu->le = 0x10000; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (!apdu->case_type) |
| 108 | + return 1; |
| 109 | + |
| 110 | + if (apdu->lc) |
| 111 | + { |
| 112 | + if (apdu->extended_apdu) |
| 113 | + { |
| 114 | + apdu->data = data + 7; |
| 115 | + } else { |
| 116 | + apdu->data = data + 5; |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + return 0; |
| 122 | +} |
0 commit comments