| 1 | /* |
|---|
| 2 | * Build Extended CTBCS APDUs for those readers that |
|---|
| 3 | * support them (such as Kobil Kaan). |
|---|
| 4 | * |
|---|
| 5 | * Copyright (C) 2003, Olaf Kirch <okir@suse.de> |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #include "internal.h" |
|---|
| 9 | #include <stdlib.h> |
|---|
| 10 | #include <string.h> |
|---|
| 11 | #include <time.h> |
|---|
| 12 | #include <unistd.h> |
|---|
| 13 | #include "ctbcs.h" |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * Start building CTBCS apdu |
|---|
| 17 | */ |
|---|
| 18 | void ctbcs_begin(ct_buf_t * bp, unsigned int ins, unsigned int p1, |
|---|
| 19 | unsigned int p2) |
|---|
| 20 | { |
|---|
| 21 | ct_buf_putc(bp, 0x20); |
|---|
| 22 | ct_buf_putc(bp, ins); |
|---|
| 23 | ct_buf_putc(bp, p1); |
|---|
| 24 | ct_buf_putc(bp, p2); |
|---|
| 25 | ct_buf_putc(bp, 0); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | /* |
|---|
| 29 | * Finish CTBCS apdu |
|---|
| 30 | */ |
|---|
| 31 | int ctbcs_finish(ct_buf_t * bp) |
|---|
| 32 | { |
|---|
| 33 | unsigned int len; |
|---|
| 34 | |
|---|
| 35 | if (ct_buf_overrun(bp)) |
|---|
| 36 | return IFD_ERROR_BUFFER_TOO_SMALL; |
|---|
| 37 | |
|---|
| 38 | len = ct_buf_avail(bp); |
|---|
| 39 | bp->base[4] = len - 5; /* lc */ |
|---|
| 40 | return len; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /* |
|---|
| 44 | * Output a string to the display |
|---|
| 45 | */ |
|---|
| 46 | int ctbcs_build_output(unsigned char *cmd, size_t size, const char *message) |
|---|
| 47 | { |
|---|
| 48 | ct_buf_t buf; |
|---|
| 49 | |
|---|
| 50 | if (message == NULL) |
|---|
| 51 | return IFD_ERROR_INVALID_ARG; |
|---|
| 52 | |
|---|
| 53 | ct_buf_init(&buf, cmd, size); |
|---|
| 54 | ctbcs_begin(&buf, 0x17, 0x40, 0x00); |
|---|
| 55 | ctbcs_add_message(&buf, message); |
|---|
| 56 | return ctbcs_finish(&buf); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /* |
|---|
| 60 | * Generic Verify APDU |
|---|
| 61 | */ |
|---|
| 62 | static int ctbcs_build_verify_apdu(unsigned char *cmd, size_t size, |
|---|
| 63 | unsigned char ins, unsigned char p1, |
|---|
| 64 | const char *prompt, unsigned int timeout, |
|---|
| 65 | const unsigned char *data, size_t data_len) |
|---|
| 66 | { |
|---|
| 67 | ct_buf_t buf; |
|---|
| 68 | |
|---|
| 69 | if (!data || !data_len) |
|---|
| 70 | return IFD_ERROR_INVALID_ARG; |
|---|
| 71 | |
|---|
| 72 | if (prompt == NULL) |
|---|
| 73 | return IFD_ERROR_INVALID_ARG; |
|---|
| 74 | ct_buf_init(&buf, cmd, size); |
|---|
| 75 | ctbcs_begin(&buf, ins, p1, 0x00); |
|---|
| 76 | |
|---|
| 77 | ctbcs_add_timeout(&buf, timeout); |
|---|
| 78 | ctbcs_add_message(&buf, prompt); |
|---|
| 79 | |
|---|
| 80 | ct_buf_putc(&buf, 0x52); |
|---|
| 81 | ct_buf_putc(&buf, data_len); |
|---|
| 82 | ct_buf_put(&buf, data, data_len); |
|---|
| 83 | if (ct_buf_overrun(&buf)) |
|---|
| 84 | return IFD_ERROR_BUFFER_TOO_SMALL; |
|---|
| 85 | |
|---|
| 86 | cmd[4] = ct_buf_avail(&buf) - 5; /* lc */ |
|---|
| 87 | return ct_buf_avail(&buf); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /* |
|---|
| 91 | * Build Perform Verify APDU |
|---|
| 92 | */ |
|---|
| 93 | int ctbcs_build_perform_verify_apdu(unsigned char *cmd, size_t size, |
|---|
| 94 | unsigned int p1, const char *prompt, |
|---|
| 95 | unsigned int timeout, |
|---|
| 96 | const unsigned char *data, size_t data_len) |
|---|
| 97 | { |
|---|
| 98 | return ctbcs_build_verify_apdu(cmd, size, 0x18, p1, |
|---|
| 99 | prompt, timeout, data, data_len); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /* |
|---|
| 103 | * Build Modify Verify APDU |
|---|
| 104 | */ |
|---|
| 105 | int ctbcs_build_modify_verify_apdu(unsigned char *cmd, size_t size, |
|---|
| 106 | unsigned int p1, const char *prompt, |
|---|
| 107 | unsigned int timeout, |
|---|
| 108 | const unsigned char *data, size_t data_len) |
|---|
| 109 | { |
|---|
| 110 | return ctbcs_build_verify_apdu(cmd, size, 0x19, p1, |
|---|
| 111 | prompt, timeout, data, data_len); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /* |
|---|
| 115 | * Helper function add message/timeout arguments to command |
|---|
| 116 | * buffer |
|---|
| 117 | */ |
|---|
| 118 | int ctbcs_add_timeout(ct_buf_t * bp, unsigned int timeout) |
|---|
| 119 | { |
|---|
| 120 | if (!timeout) |
|---|
| 121 | return 0; |
|---|
| 122 | ct_buf_putc(bp, 0x80); |
|---|
| 123 | ct_buf_putc(bp, 1); |
|---|
| 124 | ct_buf_putc(bp, timeout); |
|---|
| 125 | return ct_buf_avail(bp); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | int ctbcs_add_message(ct_buf_t * bp, const char *message) |
|---|
| 129 | { |
|---|
| 130 | int n; |
|---|
| 131 | |
|---|
| 132 | if (!message || !strcmp(message, "@")) |
|---|
| 133 | return 0; |
|---|
| 134 | |
|---|
| 135 | if ((n = strlen(message)) > 32) |
|---|
| 136 | n = 32; |
|---|
| 137 | |
|---|
| 138 | ct_buf_putc(bp, 0x50); |
|---|
| 139 | ct_buf_putc(bp, n); |
|---|
| 140 | ct_buf_put(bp, message, n); |
|---|
| 141 | |
|---|
| 142 | return ct_buf_avail(bp); |
|---|
| 143 | } |
|---|