00001
00002
00003
00004
00005
00006 #ifndef _EMV_INTERNAL_H
00007 #define _EMV_INTERNAL_H
00008
00009 #define EMV_PIN_BLOCK_LEN 8
00010
00011 #include <openssl/sha.h>
00012 #include <openssl/rsa.h>
00013 #include <openssl/engine.h>
00014
00015 #include <gang.h>
00016 #include <mpool.h>
00017
00018 #define EMV_ERR_TYPE_SHIFT 30
00019 #define EMV_ERR_CODE_MASK ((1 << EMV_ERR_TYPE_SHIFT) - 1)
00020
00021 typedef uint8_t emv_pb_t[EMV_PIN_BLOCK_LEN];
00022
00023 #define EMV_DATA_SDA (1<<0)
00024 #define EMV_DATA_DDA (1<<1)
00025
00026 #define EMV_DATA_ATOMIC (1<<15)
00027 #define EMV_DATA_DOL (1<<14)
00028 #define EMV_DATA_TYPE_MASK ((1<<14)-1)
00029 struct _emv_tag {
00030 uint16_t t_tag;
00031 uint16_t t_type;
00032 uint8_t t_min, t_max;
00033 const char *t_label;
00034 };
00035
00036 struct _emv_data {
00037 const struct _emv_tag *d_tag;
00038 uint16_t d_id;
00039 uint16_t d_flags;
00040 const uint8_t *d_data;
00041 size_t d_len;
00042 struct _emv_data **d_elem;
00043 unsigned int d_nmemb;
00044 };
00045
00046 static inline int emv_data_atomic(struct _emv_data *d)
00047 {
00048 return !!(d->d_tag->t_type & EMV_DATA_ATOMIC);
00049 }
00050 static inline int emv_data_composite(struct _emv_data *d)
00051 {
00052 return !(d->d_tag->t_type & EMV_DATA_ATOMIC);
00053 }
00054
00055 struct _emv_db {
00056 unsigned int db_nmemb;
00057 struct _emv_data **db_elem;
00058 unsigned int db_numrec;
00059 struct _emv_data **db_rec;
00060 unsigned int db_numsda;
00061 struct _emv_data **db_sda;
00062 };
00063
00064 struct _emv_app {
00065 uint8_t a_recno;
00066 uint8_t a_prio;
00067 uint8_t a_id_sz;
00068
00069 uint8_t a_id[16];
00070 char a_name[16];
00071 char a_pname[16];
00072 struct list_head a_list;
00073 };
00074
00075 struct _emv {
00076
00077 chipcard_t e_dev;
00078 xfr_t e_xfr;
00079
00080 mpool_t e_data;
00081 gang_t e_files;
00082 struct _emv_db e_db;
00083
00084
00085 unsigned int e_num_apps;
00086 struct list_head e_apps;
00087 struct _emv_app *e_app;
00088
00089 emv_aip_t e_aip;
00090 uint8_t *e_afl;
00091 size_t e_afl_len;
00092
00093
00094 uint8_t e_sda_ok;
00095 uint8_t e_dda_ok;
00096 uint8_t e_cda_ok;
00097 uint8_t _pad0;
00098
00099 RSA *e_ca_pk;
00100 RSA *e_iss_pk;
00101 RSA *e_icc_pk;
00102
00103 emv_err_t e_err;
00104 };
00105
00106 #define DOL_NUM_TAGS(x) (sizeof(x)/sizeof(struct dol_tag))
00107 struct dol_tag {
00108 const char *tag;
00109 size_t tag_len;
00110 int(*op)(uint8_t *ptr, size_t len, void *priv);
00111 };
00112
00113
00114 _private uint8_t _emv_sw1(emv_t e);
00115 _private uint8_t _emv_sw2(emv_t e);
00116 _private int _emv_pin2pb(const char *pin, uint8_t *pb);
00117
00118
00119 _private void _emv_free_applist(emv_t e);
00120 _private void _emv_init_applist(emv_t e);
00121
00122
00123 _private int _emv_app_init(emv_t e, const uint8_t *aid, size_t aid_len);
00124
00125
00126 _private int _emv_read_app_data(struct _emv *e);
00127 _private const struct _emv_data *_emv_retrieve_data(emv_t, uint16_t id);
00128
00129
00130 _private uint8_t *_emv_construct_dol(emv_dol_cb_t cbfn,
00131 const uint8_t *ptr, size_t len,
00132 size_t *ret_len, void *priv);
00133
00134
00135 _private int _emv_read_record(emv_t e, uint8_t sfi, uint8_t record);
00136 _private int _emv_select(emv_t e, const uint8_t *name, size_t nlen);
00137 _private int _emv_select_next(emv_t e, const uint8_t *name, size_t nlen);
00138 _private int _emv_verify(emv_t e, uint8_t fmt, const uint8_t *p, uint8_t plen);
00139 _private int _emv_get_data(emv_t e, uint8_t p1, uint8_t p2);
00140 _private int _emv_get_proc_opts(emv_t e, const uint8_t *pdol, uint8_t len);
00141
00142 _private int _emv_generate_ac(emv_t e, uint8_t ref,
00143 const uint8_t *data, uint8_t len);
00144 _private int _emv_int_authenticate(emv_t e, const uint8_t *data, uint8_t len);
00145
00146 _private void _emv_sys_error(struct _emv *e);
00147 _private void _emv_ccid_error(struct _emv *e);
00148 _private void _emv_icc_error(struct _emv *e);
00149 _private void _emv_error(struct _emv *e, unsigned int code);
00150 _private void _emv_success(struct _emv *e);
00151
00152
00153 _private int _emsa_pss_decode(const uint8_t *msg, size_t msg_len,
00154 const uint8_t *em, size_t em_len);
00155
00156 #endif