]>
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 <security_utilities/utilities.h>
34 #include <security_utilities/errors.h>
35 #include <security_utilities/transactions.h>
36 #include <security_utilities/debugging.h>
37 #include <PCSC/winscard.h>
49 // PCSC-domain error exceptions
51 class Error
: public CommonError
{
53 Error(unsigned long err
);
55 const unsigned long error
;
56 OSStatus
osStatus() const;
57 int unixError() const;
58 const char *what () const throw ();
60 static void check(unsigned long err
) { if (err
!= SCARD_S_SUCCESS
) throwMe(err
); }
61 static void throwMe(unsigned long err
);
66 // A PODWrapper for the PCSC READERSTATE structure
68 class ReaderState
: public PodWrapper
<ReaderState
, SCARD_READERSTATE
> {
70 void set(const char *name
, unsigned long known
= SCARD_STATE_UNAWARE
);
72 const char *name() const { return szReader
; }
73 void name(const char *s
) { szReader
= s
; }
75 unsigned long lastKnown() const { return dwCurrentState
; }
76 void lastKnown(unsigned long s
);
78 unsigned long state() const { return dwEventState
; }
79 bool state(unsigned long it
) const { return state() & it
; }
80 bool changed() const { return state(SCARD_STATE_CHANGED
); }
83 T
* &userData() { return reinterpret_cast<T
* &>(pvUserData
); }
85 // DataOid access to the ATR data
86 const void *data() const { return rgbAtr
; }
87 size_t length() const { return cbAtr
; }
88 void setATR(const void *atr
, size_t size
);
95 // A Session represents the entire process state for the PCSC protocol
105 bool isOpen() const { return mIsOpen
; }
107 void listReaders(vector
<string
> &readers
, const char *groups
= NULL
);
109 void statusChange(ReaderState
*readers
, unsigned int nReaders
, long timeout
= 0);
110 void statusChange(ReaderState
&reader
, long timeout
= 0)
111 { return statusChange(&reader
, 1, timeout
); }
112 void statusChange(vector
<ReaderState
> &readers
, long timeout
= 0)
113 { return statusChange(&readers
[0], (unsigned int)readers
.size(), timeout
); }
121 SCARDCONTEXT mContext
;
122 std::vector
<char> mReaderBuffer
;
127 // A Card represents a PCSC-managed card slot
131 static const unsigned long defaultProtocols
= SCARD_PROTOCOL_T0
| SCARD_PROTOCOL_T1
;
136 void connect(Session
&session
, const char *reader
,
137 unsigned long share
= SCARD_SHARE_SHARED
,
138 unsigned long protocols
= defaultProtocols
);
139 void reconnect(unsigned long share
= SCARD_SHARE_SHARED
,
140 unsigned long protocols
= defaultProtocols
,
141 unsigned long initialization
= SCARD_LEAVE_CARD
);
142 void disconnect(unsigned long disposition
= SCARD_LEAVE_CARD
);
143 virtual void didDisconnect();
144 virtual void didEnd();
146 void checkReset(unsigned int rv
);
147 bool isConnected() const { return mConnectedState
== kConnected
; }
148 bool isInTransaction() const { return mTransactionNestLevel
> 0; }
150 void transmit(const unsigned char *pbSendBuffer
, size_t cbSendLength
,
151 unsigned char *pbRecvBuffer
, size_t &pcbRecvLength
);
153 // primitive transaction interface
155 void end(unsigned long disposition
= SCARD_LEAVE_CARD
);
159 void setIOType(unsigned long activeProtocol
);
161 IFDUMP(void dump(const char *direction
, const unsigned char *buffer
, size_t length
);)
172 int mTransactionNestLevel
;
173 SCARD_IO_REQUEST
*mIOType
;
178 // A PCSC-layer transaction (exclusive sequence of calls)
180 class Transaction
: public ManagedTransaction
<Card
> {
182 Transaction(Card
&card
, Outcome outcome
= conditional
)
183 : ManagedTransaction
<Card
>(card
, outcome
), mDisposition(SCARD_LEAVE_CARD
) { }
185 void disposition(unsigned long disp
); // change disposition on successful outcome
191 unsigned long mDisposition
; // disposition on success
196 } // namespace Security