]> git.saurik.com Git - apple/securityd.git/blob - src/pcsc++.h
6c839aa532c1ff2df9e757bbb5d2cb96e8d09e58
[apple/securityd.git] / src / pcsc++.h
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26
27 //
28 // pcsc++ - PCSC client interface layer in C++
29 //
30 // NOTE: TO BE MOVED TO security_utilities LAYER.
31 //
32 #ifndef _H_PCSC_PP
33 #define _H_PCSC_PP
34
35 #include <security_utilities/utilities.h>
36 #include <security_utilities/errors.h>
37 #include <security_utilities/debugging.h>
38 #include <PCSC/winscard.h>
39 #include <vector>
40 #include <string>
41
42 #include <cstdio>
43
44
45 namespace Security {
46 namespace PCSC {
47
48
49 //
50 // PCSC-domain error exceptions
51 //
52 class Error : public CommonError {
53 public:
54 Error(long err);
55
56 const long error;
57 OSStatus osStatus() const;
58 int unixError() const;
59 const char *what () const throw ();
60
61 static void check(long err) { if (err != SCARD_S_SUCCESS) throwMe(err); }
62 static void throwMe(long err);
63
64 private:
65 IFDEBUG(void debugDiagnose(const void *id) const);
66 };
67
68
69 //
70 // A PODWrapper for the PCSC READERSTATE structure
71 //
72 class ReaderState : public PodWrapper<ReaderState, SCARD_READERSTATE> {
73 public:
74 void set(const char *name, unsigned long known = SCARD_STATE_UNAWARE);
75
76 const char *name() const { return szReader; }
77 void name(const char *s) { szReader = s; }
78
79 unsigned long lastKnown() const { return dwCurrentState; }
80 void lastKnown(unsigned long s);
81
82 unsigned long state() const { return dwEventState; }
83 bool state(unsigned long it) const { return state() & it; }
84 bool changed() const { return state(SCARD_STATE_CHANGED); }
85
86 template <class T>
87 T * &userData() { return reinterpret_cast<T * &>(pvUserData); }
88
89 // DataOid access to the ATR data
90 const void *data() const { return rgbAtr; }
91 size_t length() const { return cbAtr; }
92
93 IFDUMP(void dump());
94 };
95
96
97 //
98 // A Session represents the entire process state for the PCSC protocol
99 //
100 class Session {
101 friend class Card;
102 public:
103 Session();
104 virtual ~Session();
105
106 void open();
107 bool isOpen() const { return mIsOpen; }
108
109 void listReaders(vector<string> &readers, const char *groups = NULL);
110
111 void statusChange(ReaderState *readers, unsigned int nReaders,
112 long timeout = 0);
113 void statusChange(vector<ReaderState> &readers, long timeout = 0)
114 { statusChange(&readers[0], readers.size(), timeout); }
115
116 private:
117 bool check(long rc);
118
119 private:
120 bool mIsOpen;
121 SCARDCONTEXT mContext;
122 std::vector<char> mReaderBuffer;
123 };
124
125
126 //
127 // A Card represents a PCSC-managed card slot
128 //
129 class Card {
130 public:
131 static const unsigned long defaultProtocols = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
132
133 Card();
134 ~Card();
135
136 void open(Session &session, const char *reader,
137 unsigned long share = SCARD_SHARE_SHARED,
138 unsigned long protocols = defaultProtocols);
139 void close(unsigned long disposition = SCARD_LEAVE_CARD);
140
141 private:
142 bool mIsOpen;
143 long mHandle;
144 unsigned long mProtocol;
145 };
146
147
148 } // namespce PCSC
149 } // namespace Security
150
151
152 #endif //_H_PCSC_PP