]> git.saurik.com Git - apple/security.git/blob - securityd/src/reader.cpp
Security-58286.20.16.tar.gz
[apple/security.git] / securityd / src / reader.cpp
1 /*
2 * Copyright (c) 2004,2007-2008 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // reader - token reader objects
27 //
28 #include "reader.h"
29
30
31 //
32 // Construct a Reader
33 // This does not commence state tracking; call update to start up the reader.
34 //
35 Reader::Reader(TokenCache &tc, const PCSC::ReaderState &state)
36 : cache(tc), mType(pcsc), mToken(NULL)
37 {
38 mName = state.name(); // remember separate copy of name
39 mPrintName = mName; //@@@ how to make this readable? Use IOKit information?
40 secinfo("reader", "%p (%s) new PCSC reader", this, name().c_str());
41 }
42
43 Reader::Reader(TokenCache &tc, const string &identifier)
44 : cache(tc), mType(software), mToken(NULL)
45 {
46 mName = identifier;
47 mPrintName = mName;
48 secinfo("reader", "%p (%s) new software reader", this, name().c_str());
49 }
50
51 Reader::~Reader()
52 {
53 secinfo("reader", "%p (%s) destroyed", this, name().c_str());
54 }
55
56
57 //
58 // Type qualification. None matches anything.
59 //
60 bool Reader::isType(Type reqType) const
61 {
62 return reqType == this->type();
63 }
64
65
66 //
67 // Killing a reader forcibly removes its Token, if any
68 //
69 void Reader::kill()
70 {
71 if (mToken)
72 removeToken();
73 NodeCore::kill();
74 }
75
76
77 //
78 // State transition matrix for a reader, based on PCSC state changes
79 //
80 void Reader::update(const PCSC::ReaderState &state)
81 {
82 // set new state
83 unsigned long oldState = mState.state();
84 (void) oldState; // Be okay with not using this.
85
86 mState = state;
87 mState.name(mName.c_str()); // (fix name pointer, unchanged)
88
89 try {
90 if (state.state(SCARD_STATE_UNAVAILABLE)) {
91 // reader is unusable (probably being removed)
92 secinfo("reader", "%p (%s) unavailable (0x%lx)",
93 this, name().c_str(), state.state());
94 if (mToken)
95 removeToken();
96 } else if (state.state(SCARD_STATE_EMPTY)) {
97 // reader is empty (no token present)
98 secinfo("reader", "%p (%s) empty (0x%lx)",
99 this, name().c_str(), state.state());
100 if (mToken)
101 removeToken();
102 } else if (state.state(SCARD_STATE_PRESENT)) {
103 // reader has a token inserted
104 secinfo("reader", "%p (%s) card present (0x%lx)",
105 this, name().c_str(), state.state());
106 //@@@ is this hack worth it (with notifications in)??
107 if (mToken && CssmData(state) != CssmData(pcscState()))
108 removeToken(); // incomplete but better than nothing
109 //@@@ or should we call some verify-still-the-same function of tokend?
110 //@@@ (I think pcsc will return an error if the card changed?)
111 if (!mToken)
112 insertToken(NULL);
113 } else {
114 secinfo("reader", "%p (%s) unexpected state change (0x%lx to 0x%lx)",
115 this, name().c_str(), oldState, state.state());
116 }
117 } catch (...) {
118 secinfo("reader", "state update exception (ignored)");
119 }
120 }
121
122
123 void Reader::insertToken(TokenDaemon *tokend)
124 {
125 RefPointer<Token> token = new Token();
126 token->insert(*this, tokend);
127 mToken = token;
128 addReference(*token);
129 secinfo("reader", "%p (%s) inserted token %p",
130 this, name().c_str(), mToken);
131 }
132
133
134 void Reader::removeToken()
135 {
136 secinfo("reader", "%p (%s) removing token %p",
137 this, name().c_str(), mToken);
138 assert(mToken);
139 mToken->remove();
140 removeReference(*mToken);
141 mToken = NULL;
142 }
143
144
145 //
146 // Debug dump support
147 //
148 #if defined(DEBUGDUMP)
149
150 void Reader::dumpNode()
151 {
152 PerGlobal::dumpNode();
153 Debug::dump(" [%s] state=0x%lx", name().c_str(), mState.state());
154 }
155
156 #endif //DEBUGDUMP