-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgokey_vpcd.go
192 lines (150 loc) · 3.38 KB
/
gokey_vpcd.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// https://github.com/usbarmory/GoKey
//
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
//go:build vpcd
package main
import (
"encoding/binary"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"path/filepath"
"time"
"github.com/usbarmory/GoKey/internal/icc"
)
var server string
// http://frankmorgner.github.io/vsmartcard/virtualsmartcard/api.html
const (
POWER_OFF = 0x00
POWER_ON = 0x01
RESET = 0x02
GET_ATR = 0x04
)
// dummyUID for virtual smart card operation
var dummyUID = [4]byte{0xaa, 0xbb, 0xcc, 0xdd}
func init() {
log.SetFlags(0)
log.SetOutput(os.Stdout)
flag.StringVar(&server, "c", "127.0.0.1:35963", "vpcd address:port pair")
}
func main() {
flag.Parse()
// Initialize an OpenPGP card with the bundled key information (defined
// in `keys.go` and generated at compilation time).
card := &icc.Interface{
Serial: dummyUID,
SNVS: SNVS,
ArmoredKey: pgpSecretKey,
Name: NAME,
Language: LANGUAGE,
Sex: SEX,
URL: URL,
Debug: true,
}
if err := card.Init(); err != nil {
log.Printf("initialization error: %v", err)
}
go serveRPC(card)
// never returns
dialVPCD(card)
}
func serveRPC(card *icc.Interface) {
tmp, err := os.MkdirTemp("", "")
if err != nil {
log.Fatalf("error creating temporary directory %v", err)
}
defer os.RemoveAll(tmp)
path := filepath.Join(tmp, "p11kit.sock")
l, err := net.Listen("unix", path)
if err != nil {
log.Fatalf("listening on %s: %v", path, err)
}
defer l.Close()
log.Printf("export P11_KIT_SERVER_ADDRESS=unix:path=%s", path)
for {
conn, err := l.Accept()
if err != nil {
log.Printf("cannot accept, %v", err)
continue
}
go func() {
if err := card.ServeRPC(conn); err != nil {
log.Printf("cannot handle request, %v", err)
}
conn.Close()
}()
}
}
func dialVPCD(card *icc.Interface) {
for {
conn, err := net.Dial("tcp", server)
if err != nil {
log.Printf("retrying (%v)", err)
conn.Close()
time.Sleep(1 * time.Second)
continue
}
handleVPCDConnection(conn, card)
}
}
func handleVPCDConnection(conn net.Conn, card *icc.Interface) {
defer conn.Close()
for {
length := make([]byte, 2)
if _, err := conn.Read(length); err != nil {
log.Printf("cannot read, %v", err)
return
}
res, err := handleVPCDRequest(conn, length, card)
if err != nil {
log.Fatalf("cannot handle request, %v", err)
}
if _, err = conn.Write(res); err != nil {
log.Printf("cannot send response, %v", err)
return
}
}
}
func handleVPCDRequest(conn net.Conn, length []byte, card *icc.Interface) (res []byte, err error) {
if len(length) < 2 {
err = fmt.Errorf("request too short (%d)", len(length))
return
}
n := binary.BigEndian.Uint16(length[0:2])
if n < 1 {
err = fmt.Errorf("length too short (%d)", n)
return
}
req := make([]byte, n)
io.ReadAtLeast(conn, req, int(n))
if card.Debug {
log.Printf("vpcd << %x", req)
}
if n == 1 {
switch req[0] {
case POWER_OFF, POWER_ON, RESET:
// no response
return
case GET_ATR:
res = card.ATR()
}
} else {
if res, err = card.RawCommand(req[0:]); err != nil {
return
}
}
length = make([]byte, 2)
binary.BigEndian.PutUint16(length, uint16(len(res)))
res = append(length, res...)
if card.Debug {
log.Printf("vpcd >> %x", res)
}
return
}