]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/pcsc++.h
2 * Copyright (c) 2004,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // pcsc++ - PCSC client interface layer in C++
28 // NOTE: TO BE MOVED TO security_utilities LAYER.
33 #include <TargetConditionals.h>
37 #include <security_utilities/utilities.h>
38 #include <security_utilities/errors.h>
39 #include <security_utilities/transactions.h>
40 #include <security_utilities/debugging.h>
41 #include <security_utilities/debugging_internal.h>
42 #include <PCSC/winscard.h>
54 // PCSC-domain error exceptions
56 class Error
: public CommonError
{
58 Error(unsigned long err
);
60 const unsigned long error
;
61 OSStatus
osStatus() const;
62 int unixError() const;
63 const char *what () const throw ();
65 static void check(unsigned long err
) { if (err
!= SCARD_S_SUCCESS
) throwMe(err
); }
66 static void throwMe(unsigned long err
);
71 // A PODWrapper for the PCSC READERSTATE structure
73 class ReaderState
: public PodWrapper
<ReaderState
, SCARD_READERSTATE
> {
75 void set(const char *name
, unsigned long known
= SCARD_STATE_UNAWARE
);
77 const char *name() const { return szReader
; }
78 void name(const char *s
) { szReader
= s
; }
80 unsigned long lastKnown() const { return dwCurrentState
; }
81 void lastKnown(unsigned long s
);
83 unsigned long state() const { return dwEventState
; }
84 bool state(unsigned long it
) const { return state() & it
; }
85 bool changed() const { return state(SCARD_STATE_CHANGED
); }
88 T
* &userData() { return reinterpret_cast<T
* &>(pvUserData
); }
90 // DataOid access to the ATR data
91 const void *data() const { return rgbAtr
; }
92 size_t length() const { return cbAtr
; }
93 void setATR(const void *atr
, size_t size
);
100 // A Session represents the entire process state for the PCSC protocol
110 bool isOpen() const { return mIsOpen
; }
112 void listReaders(vector
<string
> &readers
, const char *groups
= NULL
);
114 void statusChange(ReaderState
*readers
, unsigned int nReaders
, long timeout
= 0);
115 void statusChange(ReaderState
&reader
, long timeout
= 0)
116 { return statusChange(&reader
, 1, timeout
); }
117 void statusChange(vector
<ReaderState
> &readers
, long timeout
= 0)
118 { return statusChange(&readers
[0], (unsigned int)readers
.size(), timeout
); }
126 SCARDCONTEXT mContext
;
127 std::vector
<char> mReaderBuffer
;
132 // A Card represents a PCSC-managed card slot
136 static const unsigned long defaultProtocols
= SCARD_PROTOCOL_T0
| SCARD_PROTOCOL_T1
;
141 void connect(Session
&session
, const char *reader
,
142 unsigned long share
= SCARD_SHARE_SHARED
,
143 unsigned long protocols
= defaultProtocols
);
144 void reconnect(unsigned long share
= SCARD_SHARE_SHARED
,
145 unsigned long protocols
= defaultProtocols
,
146 unsigned long initialization
= SCARD_LEAVE_CARD
);
147 void disconnect(unsigned long disposition
= SCARD_LEAVE_CARD
);
148 virtual void didDisconnect();
149 virtual void didEnd();
151 void checkReset(unsigned int rv
);
152 bool isConnected() const { return mConnectedState
== kConnected
; }
153 bool isInTransaction() const { return mTransactionNestLevel
> 0; }
155 void transmit(const unsigned char *pbSendBuffer
, size_t cbSendLength
,
156 unsigned char *pbRecvBuffer
, size_t &pcbRecvLength
);
158 // primitive transaction interface
160 void end(unsigned long disposition
= SCARD_LEAVE_CARD
);
164 void setIOType(unsigned long activeProtocol
);
166 IFDUMP(void dump(const char *direction
, const unsigned char *buffer
, size_t length
);)
177 int mTransactionNestLevel
;
178 SCARD_IO_REQUEST
*mIOType
;
183 // A PCSC-layer transaction (exclusive sequence of calls)
185 class Transaction
: public ManagedTransaction
<Card
> {
187 Transaction(Card
&card
, Outcome outcome
= conditional
)
188 : ManagedTransaction
<Card
>(card
, outcome
), mDisposition(SCARD_LEAVE_CARD
) { }
190 void disposition(unsigned long disp
); // change disposition on successful outcome
196 unsigned long mDisposition
; // disposition on success
201 } // namespace Security
203 #endif //TARGET_OS_OSX