-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpcsc.clj
70 lines (54 loc) · 2.4 KB
/
pcsc.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(ns clj-emv.pcsc
"PC/SC related smart card reader functionality"
(:require [clj-emv.bit-ops :as bit-ops])
(:require [clj-emv.tags :as tags])
(:import (javax.smartcardio TerminalFactory CommandAPDU)))
; remove
(defn hexify-old[value]
(format "%02X" value))
(defn get-terminals[]
(defn get-factory[] (TerminalFactory/getDefault))
(defn my-terminal[terminal]
{:is-card-present (.isCardPresent terminal)
:name (.getName terminal)
:instance terminal})
(let [terminals (seq (.list (.terminals (get-factory))))
my-terminals (map my-terminal terminals)]
my-terminals))
(defn get-card[terminal]
(let [instance (.connect (:instance terminal) "*")
atr (.getATR instance)]
{:instance instance
:protocol (.getProtocol instance)}))
(defn get-channel[card]
(let [instance (.getBasicChannel (:instance card))
number (.getChannelNumber instance)]
{:instance instance
:number number}))
(defn response-apdu[response]
{:number (.getNr response)
:bytes (vec (map bit-ops/unsigned (.getBytes response)))
:data (vec (map bit-ops/unsigned (.getData response)))
:sw1 (.getSW1 response)
:sw2 (.getSW2 response)
:status-description (:status (tags/status-bytes-to-description (.getSW1 response) (.getSW2 response)))
:success (and (= (.getSW1 response) 0x90) (= (.getSW2 response) 0x00))})
(defn transmit[channel apdu]
(response-apdu (.transmit (:instance channel) apdu)))
(defn select-command[channel aid]
(transmit channel (CommandAPDU. 0x00 0xA4 0x04 0x00 (byte-array aid))))
;; The last magic number 100 as the length is needed due to the strange behavior of javax.smartcardio
(defn read-record-command[channel sfi rec]
(transmit channel (CommandAPDU. 0x00 0xB2 (int rec) (bit-or (bit-shift-left sfi 3) 4) 100)))
(defn get-processing-options-command[channel pdol]
(transmit channel (CommandAPDU. 0x80 0xA8 0x00 0x00 (byte-array pdol))))
(defn get-data-command[channel p1 p2]
(transmit channel (CommandAPDU. 0x80 0xCA p1 p2)))
(defn generate-ac-command[channel p1 data]
(transmit channel (CommandAPDU. 0x80 0xAE p1 0x00 (byte-array data))))
(defn get-dynamic-number-command[channel]
(transmit channel (CommandAPDU. 0x00 0x84 0x00 0x00 100)))
(defn verify-command[channel p2 data]
(transmit channel (CommandAPDU. 0x00 0x20 0x00 p2 data)))
(defn internal-authenticate-command[channel data]
(transmit channel (CommandAPDU. 0x00 0x88 0x00 0x00 data)))