-
Notifications
You must be signed in to change notification settings - Fork 1
/
scard_nix.go
2213 lines (1980 loc) · 65.1 KB
/
scard_nix.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build linux || darwin
// +build linux darwin
package goscard
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"runtime"
"syscall"
"unsafe"
"github.com/ebitengine/purego"
)
//////////////////////////////////////////////////////////////////////////////////////
// Misc.
//////////////////////////////////////////////////////////////////////////////////////
func hexStringToByteArray(s string) ([]byte, error) {
return hex.DecodeString(s)
}
func byteArrayToHexString(data []byte) string {
return hex.EncodeToString(data)
}
// multiByteStringToStrings splits a []byte, which contains one or
// more UTF-8 char strings separated with \0 (multi-string),
// into separate UTF-8 strings and returns them in as a string
// array.
func multiByteStringToStrings(multiByteString []byte) []string {
var strings []string
for len(multiByteString) > 0 && multiByteString[0] != 0 {
i := 0
for i = range multiByteString {
if multiByteString[i] == 0 {
break
}
}
str := string(multiByteString[:i])
strings = append(strings, str)
multiByteString = multiByteString[i+1:]
}
return strings
}
// stringsToMultiByteString creates a UTF-8 char multi-string
// from the passed string array. The char strings are
// separated with \0, and the whole multi-string is terminated
// with a double \0.
func stringsToMultiByteString(strings []string) []byte {
var multiByteString []byte
for _, str := range strings {
byteString := []byte(str)
byteString = append(byteString, 0x00)
multiByteString = append(multiByteString, byteString...)
}
multiByteString = append(multiByteString, 0x00) // Add terminating \0 to get a double trailing zero.
return multiByteString
}
//////////////////////////////////////////////////////////////////////////////////////
// PCSC headers content.
//
// Linux:
// From https://github.com/LudovicRousseau/PCSC/blob/master/src/PCSC/pcsclite.h.in
// https://github.com/LudovicRousseau/PCSC/blob/master/src/PCSC/winscard.h,
// https://github.com/LudovicRousseau/PCSC/blob/master/src/PCSC/reader.h and
// https://salsa.debian.org/rousseau/CCID/-/blob/master/src/ccid_ifdhandler.h.
//
// MaxOSX:
// From /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/
// SDKs/MacOSX.sdk/System/Library/Frameworks/PCSC.framework/Headers/
// {pcsclite.h, winscard.h, wintypes.h}
// From https://github.com/apple-oss-distributions/SmartCardServices/blob/main/src/PCSC/reader.h
// From https://github.com/apple-oss-distributions/SmartcardCCID/blob/main/ccid/ccid/src/ccid_ifdhandler.h
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
// pcsclite.h
//////////////////////////////////////////////////////////////////////////////////////
type SCardContext hnd
type SCardHandle hnd
const invalidHandleValue = ^hnd(0)
const scardAutoAllocate = ^dword(0)
const maxAtrSize = 33
// This is the actual golang equivalent of pcsc's SCardReaderState.
//
// Note that Apple's SCardReaderState is packed, unlike Linux's.
// In Go, there is no direct equivalent of a packed C struct, as Go does
// not provide explicit control over padding or memory alignment for struct
// fields.
// This means that if we try to use scardReaderState struct
// directly, the same way we did on Linux, we would end up with crashes
// as the pcsc C code expects a packed struct and we're feeding it an
// unpacked one
// (it expects a 61 byte long struct and we're feeding it a 64 byte long one).
// That is why we need Encode / Decode functions to ensure we get a byte
// array that actually corresponds to the memory layout and alignment
// that the pcsc C code expects. This is the only way I know of that can
// mimic a packed struct on Go.
type scardReaderState struct {
Reader *byte // reader name
UserData unsafe.Pointer // user defined data
CurrentState SCardState // current state of reader at time of call
EventState SCardState // state of reader after state change
AtrLen dword // Number of bytes in the returned ATR
Atr [maxAtrSize]byte // Atr of inserted card
}
func (rs *scardReaderState) encode() ([]byte, error) {
var err error
buf := new(bytes.Buffer)
if unsafe.Sizeof(uintptr(0)) == 8 {
err = binary.Write(buf, binary.LittleEndian, uint64(uintptr(unsafe.Pointer(rs.Reader))))
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, uint64(uintptr(rs.UserData)))
if err != nil {
return nil, err
}
} else {
err = binary.Write(buf, binary.LittleEndian, uint32(uintptr(unsafe.Pointer(rs.Reader))))
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, uint32(uintptr(rs.UserData)))
if err != nil {
return nil, err
}
}
err = binary.Write(buf, binary.LittleEndian, rs.CurrentState)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, rs.EventState)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, rs.AtrLen)
if err != nil {
return nil, err
}
_, err = buf.Write(rs.Atr[:])
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (rs *scardReaderState) decode(data []byte) error {
var err error
buf := bytes.NewReader(data)
if unsafe.Sizeof(uintptr(0)) == 8 {
var Reader, UserData uint64
err = binary.Read(buf, binary.LittleEndian, &Reader)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &UserData)
if err != nil {
return err
}
rs.Reader = (*byte)(unsafe.Pointer(uintptr(Reader)))
rs.UserData = unsafe.Pointer(uintptr(UserData))
} else {
var Reader, UserData uint32
err = binary.Read(buf, binary.LittleEndian, &Reader)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &UserData)
if err != nil {
return err
}
rs.Reader = (*byte)(unsafe.Pointer(uintptr(Reader)))
rs.UserData = unsafe.Pointer(uintptr(UserData))
}
err = binary.Read(buf, binary.LittleEndian, &rs.CurrentState)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &rs.EventState)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &rs.AtrLen)
if err != nil {
return err
}
_, err = buf.Read(rs.Atr[:])
if err != nil {
return err
}
return nil
}
func encodeReaderStateArray(rsArray []scardReaderState) ([]byte, error) {
buf := new(bytes.Buffer)
for _, rs := range rsArray {
encoded, err := rs.encode()
if err != nil {
return nil, err
}
_, err = buf.Write(encoded)
if err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
func decodeReaderStateArray(data []byte, itemCount int) ([]scardReaderState, error) {
// Calculate the item size from an encoded instance
itemSize := len(data) / itemCount
if len(data)%itemCount != 0 {
return nil, errors.New("input data length does not match expected array size")
}
rsArray := make([]scardReaderState, itemCount)
for i := 0; i < itemCount; i++ {
err := rsArray[i].decode(data[i*itemSize : (i+1)*itemSize])
if err != nil {
return nil, err
}
}
return rsArray, nil
}
type SCardReaderState struct {
Reader string // reader name
UserData unsafe.Pointer // user defined data
CurrentState SCardState // current state of reader at time of call
EventState SCardState // state of reader after state change
Atr string // Atr of inserted card
}
func (s *SCardReaderState) fromInternal(internalReaderState scardReaderState, readerNameLen int) {
readerNameChars := (*[1 << 30]byte)(unsafe.Pointer(internalReaderState.Reader))[:readerNameLen:readerNameLen]
s.Reader = string(readerNameChars)
s.UserData = internalReaderState.UserData
s.CurrentState = internalReaderState.CurrentState
s.EventState = internalReaderState.EventState
if internalReaderState.AtrLen > 0 {
s.Atr = byteArrayToHexString(internalReaderState.Atr[:internalReaderState.AtrLen])
}
}
func (s *SCardReaderState) toInternal() (scardReaderState, int, error) {
var atr [maxAtrSize]byte
var atrLen dword
readerNameChars := []byte(s.Reader)
readerNameLen := len(readerNameChars)
if len(s.Atr) > 0 {
atrBytes, err := hexStringToByteArray(s.Atr)
if err != nil {
return scardReaderState{}, 0, fmt.Errorf("failed to parse atr \"%s\" (%v)", s.Atr, err)
}
copy(atr[:], atrBytes)
atrLen = dword(len(atrBytes))
if len(atrBytes) > maxAtrSize {
atrLen = maxAtrSize
}
}
return scardReaderState{
Reader: &readerNameChars[0],
UserData: s.UserData,
CurrentState: s.CurrentState,
EventState: s.EventState,
AtrLen: atrLen,
Atr: atr,
}, readerNameLen, nil
}
// Protocol Control Information (PCI)
type SCardIORequest struct {
Protocol dword // Protocol identifier
PciLength dword // Protocol Control Information Length
}
var (
// Smart Card Error Codes.
scardErrNums = map[uint64]string{
0x80100001: "SCARD_F_INTERNAL_ERROR",
0x80100002: "SCARD_E_CANCELLED",
0x80100003: "SCARD_E_INVALID_HANDLE",
0x80100004: "SCARD_E_INVALID_PARAMETER",
0x80100005: "SCARD_E_INVALID_TARGET",
0x80100006: "SCARD_E_NO_MEMORY",
0x80100007: "SCARD_F_WAITED_TOO_LONG",
0x80100008: "SCARD_E_INSUFFICIENT_BUFFER",
0x80100009: "SCARD_E_UNKNOWN_READER",
0x8010000A: "SCARD_E_TIMEOUT",
0x8010000B: "SCARD_E_SHARING_VIOLATION",
0x8010000C: "SCARD_E_NO_SMARTCARD",
0x8010000D: "SCARD_E_UNKNOWN_CARD",
0x8010000E: "SCARD_E_CANT_DISPOSE",
0x8010000F: "SCARD_E_PROTO_MISMATCH",
0x80100010: "SCARD_E_NOT_READY",
0x80100011: "SCARD_E_INVALID_VALUE",
0x80100012: "SCARD_E_SYSTEM_CANCELLED",
0x80100013: "SCARD_F_COMM_ERROR",
0x80100014: "SCARD_F_UNKNOWN_ERROR",
0x80100015: "SCARD_E_INVALID_ATR",
0x80100016: "SCARD_E_NOT_TRANSACTED",
0x80100017: "SCARD_E_READER_UNAVAILABLE",
0x80100018: "SCARD_P_SHUTDOWN",
0x80100019: "SCARD_E_PCI_TOO_SMALL",
0x8010001A: "SCARD_E_READER_UNSUPPORTED",
0x8010001B: "SCARD_E_DUPLICATE_READER",
0x8010001C: "SCARD_E_CARD_UNSUPPORTED",
0x8010001D: "SCARD_E_NO_SERVICE",
0x8010001E: "SCARD_E_SERVICE_STOPPED",
0x8010001F: "SCARD_E_UNEXPECTED",
0x80100020: "SCARD_E_ICC_INSTALLATION",
0x80100021: "SCARD_E_ICC_CREATEORDER",
0x80100023: "SCARD_E_DIR_NOT_FOUND",
0x80100024: "SCARD_E_FILE_NOT_FOUND",
0x80100025: "SCARD_E_NO_DIR",
0x80100026: "SCARD_E_NO_FILE",
0x80100027: "SCARD_E_NO_ACCESS",
0x80100028: "SCARD_E_WRITE_TOO_MANY",
0x80100029: "SCARD_E_BAD_SEEK",
0x8010002A: "SCARD_E_INVALID_CHV",
0x8010002B: "SCARD_E_UNKNOWN_RES_MNG",
0x8010002C: "SCARD_E_NO_SUCH_CERTIFICATE",
0x8010002D: "SCARD_E_CERTIFICATE_UNAVAILABLE",
0x8010002E: "SCARD_E_NO_READERS_AVAILABLE",
0x8010002F: "SCARD_E_COMM_DATA_LOST",
0x80100030: "SCARD_E_NO_KEY_CONTAINER",
0x80100031: "SCARD_E_SERVER_TOO_BUSY",
0x80100065: "SCARD_W_UNSUPPORTED_CARD",
0x80100066: "SCARD_W_UNRESPONSIVE_CARD",
0x80100067: "SCARD_W_UNPOWERED_CARD",
0x80100068: "SCARD_W_RESET_CARD",
0x80100069: "SCARD_W_REMOVED_CARD",
0x8010006A: "SCARD_W_SECURITY_VIOLATION",
0x8010006B: "SCARD_W_WRONG_CHV",
0x8010006C: "SCARD_W_CHV_BLOCKED",
0x8010006D: "SCARD_W_EOF",
0x8010006E: "SCARD_W_CANCELLED_BY_USER",
0x8010006F: "SCARD_W_CARD_NOT_AUTHENTICATED",
}
)
func maybePcscErr(errNo dword) error {
if code, known := scardErrNums[uint64(errNo)]; known {
return fmt.Errorf("scard failure: 0x%X (%s) (%s)", errNo, code, PcscStringifyError(uint64(errNo)))
} else {
return fmt.Errorf("errno code: 0x%X (%s)", errNo, syscall.Errno(errNo).Error())
}
}
type SCardScope dword
const (
// Scope in user space
SCardScopeUser SCardScope = 0x0000
// Scope in terminal
SCardScopeTerminal SCardScope = 0x0001
// Scope in system
SCardScopeSystem SCardScope = 0x0002
// Scope is global
SCardScopeGlobal SCardScope = 0x0003
)
func (s *SCardScope) String() string {
switch *s {
case SCardScopeUser:
return "User"
case SCardScopeTerminal:
return "Terminal"
case SCardScopeSystem:
return "System"
case SCardScopeGlobal:
return "Global"
default:
return "N/A"
}
}
type SCardProtocol dword
const (
SCardProtocolUndefined SCardProtocol = 0x0000 // protocol not set
SCardProtocolUnset SCardProtocol = SCardProtocolUndefined // backward compat
SCardProtocolT0 SCardProtocol = 0x0001 // T=0 active protocol.
SCardProtocolT1 SCardProtocol = 0x0002 // T=1 active protocol.
SCardProtocolRaw SCardProtocol = 0x0004 // Raw active protocol.
SCardProtocolT15 SCardProtocol = 0x0008 // T=15 protocol.
SCardProtocolAny SCardProtocol = SCardProtocolT0 | SCardProtocolT1 // IFD determines prot.
)
func (s SCardProtocol) String() string {
output := ""
if s == SCardProtocolUndefined {
output += "Undefined"
} else {
if s&SCardProtocolT0 == SCardProtocolT0 {
output += "T0;"
}
if s&SCardProtocolT1 == SCardProtocolT1 {
output += "T1;"
}
if s&SCardProtocolRaw == SCardProtocolRaw {
output += "Raw;"
}
if s&SCardProtocolT15 == SCardProtocolT15 {
output += "T15;"
}
}
return output
}
type SCardShareMode dword
const (
// Exclusive mode only
SCardShareExclusive SCardShareMode = 0x0001
// Shared mode only
SCardShareShared SCardShareMode = 0x0002
// Raw mode only
SCardShareDirect SCardShareMode = 0x0003
)
func (m *SCardShareMode) String() string {
switch *m {
case SCardShareExclusive:
return "Exclusive"
case SCardShareShared:
return "Shared"
case SCardShareDirect:
return "Direct"
default:
return "N/A"
}
}
type SCardDisposition dword
const (
// Do nothing on close
SCardLeaveCard SCardDisposition = 0x0000
// Reset on close
SCardResetCard SCardDisposition = 0x0001
// Power down on close
SCardUnpowerCard SCardDisposition = 0x0002
// Eject on close
SCardEjectCard SCardDisposition = 0x0003
)
func (d *SCardDisposition) String() string {
switch *d {
case SCardLeaveCard:
return "LeaveCard"
case SCardResetCard:
return "ResetCard"
case SCardUnpowerCard:
return "UnpowerCard"
case SCardEjectCard:
return "EjectCard"
default:
return "N/A"
}
}
type ReaderState dword
const (
SCardUnknown ReaderState = 0x0001 // Unknown state
SCardAbsent ReaderState = 0x0002 // Card is absent
SCardPresent ReaderState = 0x0004 // Card is present
SCardSwallowed ReaderState = 0x0008 // Card not powered
SCardPowered ReaderState = 0x0010 // Card is powered
SCardNegotiable ReaderState = 0x0020 // Ready for PTS
SCardSpecific ReaderState = 0x0040 // PTS has been set
)
func (s *ReaderState) String() string {
output := ""
if *s&SCardUnknown == SCardUnknown {
output += "Unknown;"
}
if *s&SCardAbsent == SCardAbsent {
output += "Absent;"
}
if *s&SCardPresent == SCardPresent {
output += "Present;"
}
if *s&SCardSwallowed == SCardSwallowed {
output += "Swallowed;"
}
if *s&SCardPowered == SCardPowered {
output += "Powered;"
}
if *s&SCardNegotiable == SCardNegotiable {
output += "Negotiable;"
}
if *s&SCardSpecific == SCardSpecific {
output += "Specific;"
}
return output
}
type SCardState dword
const (
// App wants status
SCardStateUnaware SCardState = 0x0000
// Ignore this reader
SCardStateIgnore SCardState = 0x0001
// State has changed
SCardStateChanged SCardState = 0x0002
// Reader unknown
SCardStateUnknown SCardState = 0x0004
// Status unavailable
SCardStateUnavailable SCardState = 0x0008
// Card removed
SCardStateEmpty SCardState = 0x0010
// Card inserted
SCardStatePresent SCardState = 0x0020
// ATR matches card
SCardStateAtrmatch SCardState = 0x0040
// Exclusive Mode
SCardStateExclusive SCardState = 0x0080
// Shared Mode
SCardStateInuse SCardState = 0x0100
// Unresponsive card
SCardStateMute SCardState = 0x0200
// Unpowered card
SCardStateUnpowered SCardState = 0x0400
)
func (s *SCardState) String() string {
output := ""
if *s == SCardStateUnaware {
output += "Unaware;"
} else {
if *s&SCardStateIgnore == SCardStateIgnore {
output += "Ignore;"
}
if *s&SCardStateChanged == SCardStateChanged {
output += "Changed;"
}
if *s&SCardStateUnknown == SCardStateUnknown {
output += "Unknown;"
}
if *s&SCardStateUnavailable == SCardStateUnavailable {
output += "Unavailable;"
}
if *s&SCardStateEmpty == SCardStateEmpty {
output += "Empty;"
}
if *s&SCardStatePresent == SCardStatePresent {
output += "Present;"
}
if *s&SCardStateAtrmatch == SCardStateAtrmatch {
output += "Atrmatch;"
}
if *s&SCardStateExclusive == SCardStateExclusive {
output += "Exclusive;"
}
if *s&SCardStateInuse == SCardStateInuse {
output += "Inuse;"
}
if *s&SCardStateMute == SCardStateMute {
output += "Mute;"
}
if *s&SCardStateUnpowered == SCardStateUnpowered {
output += "Unpowered;"
}
}
return output
}
const (
infiniteTimeout dword = 0xFFFFFFFF
pcscLiteMaxReadersContexts dword = 16 // Maximum readers context (a slot is count as a reader)
maxReaderName dword = 128
scardAtrLength dword = maxAtrSize // Maximum ATR size
maxBufferSize dword = 264 // Maximum Tx/Rx Buffer for short APDU
)
//////////////////////////////////////////////////////////////////////////////////////
// reader.h
//////////////////////////////////////////////////////////////////////////////////////
type SCardAttr dword
type SCardClass dword
func scardAttrValue(class SCardClass, tag dword) SCardAttr {
return SCardAttr((dword(class) << 16) | tag)
}
const (
SCardClassVendorInfo SCardClass = 1 // Vendor information definitions
SCardClassCommunications SCardClass = 2 // Communication definitions
SCardClassProtocol SCardClass = 3 // Protocol definitions
SCardClassPowerMgmt SCardClass = 4 // Power Management definitions
SCardClassSecurity SCardClass = 5 // Security Assurance definitions
SCardClassMechanical SCardClass = 6 // Mechanical characteristic definitions
SCardClassVendorDefined SCardClass = 7 // Vendor specific definitions
SCardClassIFDProtocol SCardClass = 8 // Interface Device Protocol options
SCardClassICCState SCardClass = 9 // ICC State specific definitions
SCardClassSystem SCardClass = 0x7fff // System-specific definitions
)
func (c *SCardClass) String() string {
switch *c {
case SCardClassVendorInfo:
return "VendorInfo"
case SCardClassCommunications:
return "Communications"
case SCardClassProtocol:
return "Protocol"
case SCardClassPowerMgmt:
return "PowerMgmt"
case SCardClassSecurity:
return "Security"
case SCardClassMechanical:
return "Mechanical"
case SCardClassVendorDefined:
return "VendorDefined"
case SCardClassIFDProtocol:
return "IFDProtocol"
case SCardClassICCState:
return "ICCState"
case SCardClassSystem:
return "System"
default:
return "N/A"
}
}
var (
SCardAttrVendorName SCardAttr = scardAttrValue(SCardClassVendorInfo, 0x0100)
SCardAttrVendorIFDType SCardAttr = scardAttrValue(SCardClassVendorInfo, 0x0101)
SCardAttrVendorIFDVersion SCardAttr = scardAttrValue(SCardClassVendorInfo, 0x0102)
SCardAttrVendorIFDSerialNo SCardAttr = scardAttrValue(SCardClassVendorInfo, 0x0103)
SCardAttrChannelID SCardAttr = scardAttrValue(SCardClassCommunications, 0x0110)
SCardAttrAsyncProtocolTypes SCardAttr = scardAttrValue(SCardClassProtocol, 0x0120)
SCardAttrDefaultClk SCardAttr = scardAttrValue(SCardClassProtocol, 0x0121)
SCardAttrMaxClk SCardAttr = scardAttrValue(SCardClassProtocol, 0x0122)
SCardAttrDefaultDataRate SCardAttr = scardAttrValue(SCardClassProtocol, 0x0123)
SCardAttrMaxDataRate SCardAttr = scardAttrValue(SCardClassProtocol, 0x0124)
SCardAttrMaxIFSD SCardAttr = scardAttrValue(SCardClassProtocol, 0x0125)
SCardAttrSyncProtocolTypes SCardAttr = scardAttrValue(SCardClassProtocol, 0x0126)
SCardAttrPowerMgmtSupport SCardAttr = scardAttrValue(SCardClassPowerMgmt, 0x0131)
SCardAttrUserToCardAuthDevice SCardAttr = scardAttrValue(SCardClassSecurity, 0x0140)
SCardAttrUserAuthInputDevice SCardAttr = scardAttrValue(SCardClassSecurity, 0x0142)
SCardAttrCharacteristics SCardAttr = scardAttrValue(SCardClassMechanical, 0x0150)
SCardAttrCurrentProtocolType SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0201)
SCardAttrCurrentClk SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0202)
SCardAttrCurrentF SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0203)
SCardAttrCurrentD SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0204)
SCardAttrCurrentN SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0205)
SCardAttrCurrentW SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0206)
SCardAttrCurrentIFSC SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0207)
SCardAttrCurrentIFSD SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0208)
SCardAttrCurrentBWT SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x0209)
SCardAttrCurrentCWT SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x020a)
SCardAttrCurrentEBCEncoding SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x020b)
SCardAttrExtendedBWT SCardAttr = scardAttrValue(SCardClassIFDProtocol, 0x020c)
SCardAttrICCPresence SCardAttr = scardAttrValue(SCardClassICCState, 0x0300)
SCardAttrICCInterfaceStatus SCardAttr = scardAttrValue(SCardClassICCState, 0x0301)
SCardAttrCurrentIOState SCardAttr = scardAttrValue(SCardClassICCState, 0x0302)
SCardAttrATRString SCardAttr = scardAttrValue(SCardClassICCState, 0x0303)
SCardAttrICCTYPEPerATR SCardAttr = scardAttrValue(SCardClassICCState, 0x0304)
SCardAttrESCReset SCardAttr = scardAttrValue(SCardClassVendorDefined, 0xA000)
SCardAttrESCCancel SCardAttr = scardAttrValue(SCardClassVendorDefined, 0xA003)
SCardAttrESCAuthRequest SCardAttr = scardAttrValue(SCardClassVendorDefined, 0xA005)
SCardAttrMaxInput SCardAttr = scardAttrValue(SCardClassVendorDefined, 0xA007)
SCardAttrDeviceUnit SCardAttr = scardAttrValue(SCardClassSystem, 0x0001)
SCardAttrDeviceInUse SCardAttr = scardAttrValue(SCardClassSystem, 0x0002)
SCardAttrDeviceFriendlyNameA SCardAttr = scardAttrValue(SCardClassSystem, 0x0003)
SCardAttrDeviceSystemNameA SCardAttr = scardAttrValue(SCardClassSystem, 0x0004)
SCardAttrDeviceFriendlyNameW SCardAttr = scardAttrValue(SCardClassSystem, 0x0005)
SCardAttrDeviceSystemNameW SCardAttr = scardAttrValue(SCardClassSystem, 0x0006)
SCardAttrSuppressT1IFSRequest SCardAttr = scardAttrValue(SCardClassSystem, 0x0007)
SCardAttrDeviceFriendlyName SCardAttr = SCardAttrDeviceFriendlyNameA
SCardAttrDeviceSystemName SCardAttr = SCardAttrDeviceSystemNameA
)
func (a *SCardAttr) String() string {
switch *a {
case SCardAttrVendorName:
return "VendorName"
case SCardAttrVendorIFDType:
return "VendorIFDType"
case SCardAttrVendorIFDVersion:
return "VendorIFDVersion"
case SCardAttrVendorIFDSerialNo:
return "VendorIFDSerialNo"
case SCardAttrChannelID:
return "ChannelID"
case SCardAttrDefaultClk:
return "DefaultClk"
case SCardAttrMaxClk:
return "MaxClk"
case SCardAttrDefaultDataRate:
return "DefaultDataRate"
case SCardAttrMaxDataRate:
return "MaxDataRate"
case SCardAttrMaxIFSD:
return "MaxIFSD"
case SCardAttrPowerMgmtSupport:
return "PowerMgmtSupport"
case SCardAttrUserToCardAuthDevice:
return "UserToCardAuthDevice"
case SCardAttrUserAuthInputDevice:
return "UserAuthInputDevice"
case SCardAttrCharacteristics:
return "Characteristics"
case SCardAttrCurrentProtocolType:
return "CurrentProtocolType"
case SCardAttrCurrentClk:
return "CurrentClk"
case SCardAttrCurrentF:
return "CurrentF"
case SCardAttrCurrentD:
return "CurrentD"
case SCardAttrCurrentN:
return "CurrentN"
case SCardAttrCurrentW:
return "CurrentW"
case SCardAttrCurrentIFSC:
return "CurrentIFSC"
case SCardAttrCurrentIFSD:
return "CurrentIFSD"
case SCardAttrCurrentBWT:
return "CurrentBWT"
case SCardAttrCurrentCWT:
return "CurrentCWT"
case SCardAttrCurrentEBCEncoding:
return "CurrentEBCEncoding"
case SCardAttrExtendedBWT:
return "ExtendedBWT"
case SCardAttrICCPresence:
return "ICCPresence"
case SCardAttrICCInterfaceStatus:
return "ICCInterfaceStatus"
case SCardAttrCurrentIOState:
return "CurrentIOState"
case SCardAttrATRString:
return "ATRString"
case SCardAttrICCTYPEPerATR:
return "ICCTYPEPerATR"
case SCardAttrESCReset:
return "ESCReset"
case SCardAttrESCCancel:
return "ESCCancel"
case SCardAttrESCAuthRequest:
return "ESCAuthRequest"
case SCardAttrMaxInput:
return "MaxInput"
case SCardAttrDeviceUnit:
return "DeviceUnit"
case SCardAttrDeviceInUse:
return "DeviceInUse"
case SCardAttrDeviceFriendlyNameA:
return "DeviceFriendlyNameA"
case SCardAttrDeviceSystemNameA:
return "DeviceSystemNameA"
case SCardAttrDeviceFriendlyNameW:
case SCardAttrDeviceFriendlyName:
return "DeviceFriendlyNameW"
case SCardAttrDeviceSystemNameW:
case SCardAttrDeviceSystemName:
return "DeviceSystemNameW"
case SCardAttrSuppressT1IFSRequest:
return "SuppressT1IFSRequest"
}
return "N/A"
}
type SCardCtlCode dword
func scardCtlCodeFunc(code dword) SCardCtlCode {
return SCardCtlCode(0x42000000 + code)
}
type Feature dword
const (
FeatureVerifyPinStart Feature = 0x01
FeatureVerifyPinFinish Feature = 0x02
FeatureModifyPinStart Feature = 0x03
FeatureModifyPinFinish Feature = 0x04
FeatureGetKeyPressed Feature = 0x05
FeatureVerifyPinDirect Feature = 0x06
FeatureModifyPinDirect Feature = 0x07
FeatureMctReaderDirect Feature = 0x08
FeatureMctUniversal Feature = 0x09
FeatureIfdPinProperties Feature = 0x0A
FeatureAbort Feature = 0x0B
FeatureSetSPEMessage Feature = 0x0C
FeatureVerifyPinDirectAppID Feature = 0x0D
FeatureModifyPinDirectAppID Feature = 0x0E
FeatureWriteDisplay Feature = 0x0F
FeatureGetKey Feature = 0x10
FeatureIfdDisplayProperties Feature = 0x11
FeatureGetTlvProperties Feature = 0x12
FeatureCcidEscCommand Feature = 0x13
)
type PcscTlvStructure struct {
Tag uint8
Length uint8
Value uint32 // This value is always in BIG ENDIAN format as documented in PCSC v2 part 10 ch 2.2 page 2. You can use ntohl() for example
}
// Structure used with FEATURE_VERIFY_PIN_DIRECT
type PinVerifyStructure struct {
TimerOut uint8 // timeout is seconds (00 means use default timeout)
TimerOut2 uint8 // timeout in seconds after first key stroke
FormatString uint8 // formatting options
PINBlockString uint8 // bits 7-4 bit size of PIN length in APDU, bits 3-0 PIN block size in bytes after justification and formatting
PINLengthFormat uint8 // bits 7-5 RFU, bit 4 set if system units are bytes, clear if system units are bits, bits 3-0 PIN length position in system units
PINMaxExtraDigit uint16 // 0xXXYY where XX is minimum PIN size in digits, and YY is maximum PIN size in digits
EntryValidationCondition uint8 // Conditions under which PIN entry should be considered complete
NumberMessage uint8 // Number of messages to display for PIN verification
LangId uint16 // Language for messages. https://docs.microsoft.com/en-us/windows/win32/intl/language-identifier-constants-and-strings
MsgIndex uint8 // Message index (should be 00)
TeoPrologue [3]uint8 // T=1 block prologue field to use (fill with 00)
DataLength uint32 // length of Data to be sent to the ICC
Data []uint8 // Data to send to the ICC
}
// Structure used with FEATURE_MODIFY_PIN_DIRECT
type PinModifyStructure struct {
TimerOut uint8 // timeout is seconds (00 means use default timeout)
TimerOut2 uint8 // timeout in seconds after first key stroke
FormatString uint8 // formatting options
PINBlockString uint8 // bits 7-4 bit size of PIN length in APDU, bits 3-0 PIN block size in bytes after justification and formatting
PINLengthFormat uint8 // bits 7-5 RFU, bit 4 set if system units are bytes, clear if system units are bits, bits 3-0 PIN length position in system units
InsertionOffsetOld uint8 // Insertion position offset in bytes for the current PIN
InsertionOffsetNew uint8 // Insertion position offset in bytes for the new PIN
PINMaxExtraDigit uint16 // 0xXXYY where XX is minimum PIN size in digits, and YY is maximum PIN size in digits
ConfirmPIN uint8 // Flags governing need for confirmation of new PIN
EntryValidationCondition uint8 // Conditions under which PIN entry should be considered complete
NumberMessage uint8 // Number of messages to display for PIN verification*/
LangId uint16 // Language for messages. https://docs.microsoft.com/en-us/windows/win32/intl/language-identifier-constants-and-strings
MsgIndex1 uint8 // index of 1st prompting message
MsgIndex2 uint8 // index of 2d prompting message
MsgIndex3 uint8 // index of 3d prompting message
TeoPrologue [3]uint8 // T=1 block prologue field to use (fill with 00)
DataLength uint32 // length of Data to be sent to the ICC
Data []uint8 // Data to send to the ICC
}
// Structure used with FEATURE_IFD_PIN_PROPERTIES
type PinPropertiesStructure struct {
LcdLayout uint16 // display characteristics
EntryValidationCondition uint8
TimeOut2 uint8
}
//////////////////////////////////////////////////////////////////////////////////////
// ccid_ifdhandler.h
//////////////////////////////////////////////////////////////////////////////////////
var (
class2IoctlMagic dword = 0x330000
IoctlSmartCardVendorIfdExchange SCardCtlCode = scardCtlCodeFunc(1)
IoctlFeatureVerifyPinDirect SCardCtlCode = scardCtlCodeFunc(dword(FeatureVerifyPinDirect) + class2IoctlMagic)
IoctlFeatureModifyPinDirect SCardCtlCode = scardCtlCodeFunc(dword(FeatureModifyPinDirect) + class2IoctlMagic)
IoctlFeatureMctReaderDirect SCardCtlCode = scardCtlCodeFunc(dword(FeatureMctReaderDirect) + class2IoctlMagic)
IoctlFeatureIfdPinProperties SCardCtlCode = scardCtlCodeFunc(dword(FeatureIfdPinProperties) + class2IoctlMagic)
IoctlFeatureGetTlvProperties SCardCtlCode = scardCtlCodeFunc(dword(FeatureGetTlvProperties) + class2IoctlMagic)
)
const ccidDriverMaxReaders dword = 16
//////////////////////////////////////////////////////////////////////////////////////
// winscard.h
//////////////////////////////////////////////////////////////////////////////////////
const (
SCardAllReaders = "SCard$AllReaders"
SCardDefaultReaders = "SCard$DefaultReaders"
SCardLocalReaders = "SCard$LocalReaders"
SCardSystemReaders = "SCard$SystemReaders"
)
////////////////////////////////////////////////////////////////////
// The following functions are common to both Linux and MacOSX.
////////////////////////////////////////////////////////////////////
type pcscStringifyError func(pcscError scardRet) string
type scardEstablishContext func(
dwScope SCardScope, // in
pvReserved1 uintptr, // in
pvReserved2 uintptr, // in
phContext *SCardContext, // out
) dword
type scardReleaseContext func(
hContext SCardContext, // in
) dword
type scardIsValidContext func(
hContext SCardContext, // in
) dword
type scardConnect func(
hContext SCardContext, // in
szReader string, // in
dwShareMode SCardShareMode, // in
dwPreferredProtocols SCardProtocol, // in
phCard *SCardHandle, // out
pdwActiveProtocol *SCardProtocol, // out
) dword
type scardReconnect func(
hCard SCardHandle, // in
dwShareMode SCardShareMode, // in
dwPreferredProtocols SCardProtocol, // in
dwInitialization SCardDisposition, // in
pdwActiveProtocol *SCardProtocol, // out
) dword
type scardDisconnect func(
hCard SCardHandle, // in
dwDisposition SCardDisposition, // in
) dword
type scardBeginTransaction func(
hCard SCardHandle, // in
) dword
type scardEndTransaction func(
hCard SCardHandle, // in
dwDisposition SCardDisposition, // in
) dword
type scardStatus func(
hCard SCardHandle, // in
szReaderName str, // in, out
pcchReaderLen *dword, // in, out
pdwState *ReaderState, // out
pdwProtocol *SCardProtocol, // out
pbAtr *byte, // out
pcbAtrLen *dword, // out
) dword
type scardTransmit func(
hCard SCardHandle, // in
pioSendPci *SCardIORequest, // in
pbSendBuffer *byte, // in
cbSendLength dword, // in
pioRecvPci *SCardIORequest, // in, out
pbRecvBuffer *byte, // out
pcbRecvLength *dword, // in, out
) dword
type scardListReaderGroups func(
hContext SCardContext, // in
mszGroups str, // out
pcchGroups *dword, // in, out
) dword
type scardListReaders func(
hContext SCardContext, // in
mszGroups str, // in
mszReaders str, // out
pcchReaders *dword, // in, out
) dword
type scardFreeMemory func(
hContext SCardContext, // in
pvMem unsafe.Pointer, // in
) dword