]>
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 <PCSC/winscard.h>
53 // PCSC-domain error exceptions
55 class Error
: public CommonError
{
57 Error(unsigned long err
);
59 const unsigned long error
;
60 OSStatus
osStatus() const;
61 int unixError() const;
62 const char *what () const throw ();
64 static void check(unsigned long err
) { if (err
!= SCARD_S_SUCCESS
) throwMe(err
); }
65 static void throwMe(unsigned long err
);
70 // A PODWrapper for the PCSC READERSTATE structure
72 class ReaderState
: public PodWrapper
<ReaderState
, SCARD_READERSTATE
> {
74 void set(const char *name
, unsigned long known
= SCARD_STATE_UNAWARE
);
76 const char *name() const { return szReader
; }
77 void name(const char *s
) { szReader
= s
; }
79 unsigned long lastKnown() const { return dwCurrentState
; }
80 void lastKnown(unsigned long s
);
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
); }
87 T
* &userData() { return reinterpret_cast<T
* &>(pvUserData
); }
89 // DataOid access to the ATR data
90 const void *data() const { return rgbAtr
; }
91 size_t length() const { return cbAtr
; }
92 void setATR(const void *atr
, size_t size
);
99 // A Session represents the entire process state for the PCSC protocol
109 bool isOpen() const { return mIsOpen
; }
111 void listReaders(vector
<string
> &readers
, const char *groups
= NULL
);
113 void statusChange(ReaderState
*readers
, unsigned int nReaders
, long timeout
= 0);
114 void statusChange(ReaderState
&reader
, long timeout
= 0)
115 { return statusChange(&reader
, 1, timeout
); }
116 void statusChange(vector
<ReaderState
> &readers
, long timeout
= 0)
117 { return statusChange(&readers
[0], (unsigned int)readers
.size(), timeout
); }
125 SCARDCONTEXT mContext
;
126 std::vector
<char> mReaderBuffer
;
131 // A Card represents a PCSC-managed card slot
135 static const unsigned long defaultProtocols
= SCARD_PROTOCOL_T0
| SCARD_PROTOCOL_T1
;
140 void connect(Session
&session
, const char *reader
,
141 unsigned long share
= SCARD_SHARE_SHARED
,
142 unsigned long protocols
= defaultProtocols
);
143 void reconnect(unsigned long share
= SCARD_SHARE_SHARED
,
144 unsigned long protocols
= defaultProtocols
,
145 unsigned long initialization
= SCARD_LEAVE_CARD
);
146 void disconnect(unsigned long disposition
= SCARD_LEAVE_CARD
);
147 virtual void didDisconnect();
148 virtual void didEnd();
150 void checkReset(unsigned int rv
);
151 bool isConnected() const { return mConnectedState
== kConnected
; }
152 bool isInTransaction() const { return mTransactionNestLevel
> 0; }
154 void transmit(const unsigned char *pbSendBuffer
, size_t cbSendLength
,
155 unsigned char *pbRecvBuffer
, size_t &pcbRecvLength
);
157 // primitive transaction interface
159 void end(unsigned long disposition
= SCARD_LEAVE_CARD
);
163 void setIOType(unsigned long activeProtocol
);
165 IFDUMP(void dump(const char *direction
, const unsigned char *buffer
, size_t length
);)
176 int mTransactionNestLevel
;
177 SCARD_IO_REQUEST
*mIOType
;
182 // A PCSC-layer transaction (exclusive sequence of calls)
184 class Transaction
: public ManagedTransaction
<Card
> {
186 Transaction(Card
&card
, Outcome outcome
= conditional
)
187 : ManagedTransaction
<Card
>(card
, outcome
), mDisposition(SCARD_LEAVE_CARD
) { }
189 void disposition(unsigned long disp
); // change disposition on successful outcome
195 unsigned long mDisposition
; // disposition on success
200 } // namespace Security
202 #endif //TARGET_OS_OSX