2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
21 namespace Tizen.Network.Smartcard
24 /// A class for Smartcard session informations. It allows applications to handle session informations.
26 /// <since_tizen> 3 </since_tizen>
27 /// <privilege>http://tizen.org/privilege/secureelement</privilege>
28 public class SmartcardSession : IDisposable
30 private int _sessionHandle = -1;
31 private bool disposed = false;
32 private List<SmartcardChannel> _basicChannelList = new List<SmartcardChannel>();
33 private List<SmartcardChannel> _logicalChannelList = new List<SmartcardChannel>();
34 private SmartcardReader _readerObject;
35 private int _basicChannel = 0;
36 private int _logicalChannel = 0;
39 /// The reader object that provides the given session.
41 /// <since_tizen> 3 </since_tizen>
42 public SmartcardReader Reader
47 int ret = Interop.Smartcard.Session.SessionGetReader(_sessionHandle, out reader);
48 if (ret != (int)SmartcardError.None)
50 Log.Error(Globals.LogTag, "Failed to get reader, Error - " + (SmartcardError)ret);
53 if (_readerObject.GetHandle() != reader)
55 Log.Error(Globals.LogTag, "Does not correspond with reader, Error - " + _readerObject.GetHandle() + " " + reader);
63 /// The Answer to Reset(ATR) of this Secure Element.
65 /// <since_tizen> 3 </since_tizen>
73 int ret = Interop.Smartcard.Session.SessionGetAtr(_sessionHandle, out strAtr, out len);
74 if (ret != (int)SmartcardError.None)
76 Log.Error(Globals.LogTag, "Failed to get atr, Error - " + (SmartcardError)ret);
79 atrList = new byte[len];
80 for (int i = 0; i < len; i++)
82 atrList[i] = Marshal.ReadByte(strAtr);
83 strAtr += sizeof(byte);
90 /// Whether the session is closed.
92 /// <since_tizen> 3 </since_tizen>
98 int ret = Interop.Smartcard.Session.SessionIsClosed(_sessionHandle, out isClosed);
99 if (ret != (int)SmartcardError.None)
101 Log.Error(Globals.LogTag, "Failed to get present, Error - " + (SmartcardError)ret);
107 internal SmartcardSession(SmartcardReader readerHandle, int sessionHandle)
109 _readerObject = readerHandle;
110 _sessionHandle = sessionHandle;
118 public void Dispose()
121 GC.SuppressFinalize(this);
124 private void Dispose(bool disposing)
131 // Free managed objects.
132 foreach (SmartcardChannel channel in _basicChannelList)
135 _basicChannelList.Remove(channel);
138 foreach (SmartcardChannel channel in _logicalChannelList)
141 _logicalChannelList.Remove(channel);
144 //Free unmanaged objects
148 internal int GetHandle()
150 return _sessionHandle;
154 /// Closes the connection with the Secure Element.
156 /// <since_tizen> 3 </since_tizen>
159 int ret = Interop.Smartcard.Session.SessionClose(_sessionHandle);
160 if (ret != (int)SmartcardError.None)
162 Log.Error(Globals.LogTag, "Failed to close, Error - " + (SmartcardError)ret);
163 SmartcardErrorFactory.ThrowSmartcardException(ret);
169 /// Closes any channel opened on the given session.
171 /// <since_tizen> 3 </since_tizen>
172 public void CloseChannels()
174 int ret = Interop.Smartcard.Session.SessionCloseChannels(_sessionHandle);
175 if (ret != (int)SmartcardError.None)
177 Log.Error(Globals.LogTag, "Failed to close, Error - " + (SmartcardError)ret);
178 SmartcardErrorFactory.ThrowSmartcardException(ret);
181 foreach (SmartcardChannel channel in _basicChannelList)
186 foreach (SmartcardChannel channel in _logicalChannelList)
193 /// Gets an access to the basic channel, as defined in the ISO/IEC 7816-4 specification (the one that has number 0).
195 /// <since_tizen> 3 </since_tizen>
196 /// <returns>The SmartcardChannel object for basic channel.</returns>
197 /// <param name="aid">Byte array containing the Application ID(AID) to be selected on the given channel.</param>
198 /// <param name="p2">P2 byte of the SELECT command if executed.</param>
199 public SmartcardChannel OpenBasicChannel(byte[] aid, byte p2)
201 int ret = Interop.Smartcard.Session.SessionOpenBasicChannel(_sessionHandle, aid, aid.Length, p2, out _basicChannel);
202 if (ret != (int)SmartcardError.None)
204 Log.Error(Globals.LogTag, "Failed to open basic channel, Error - " + (SmartcardError)ret);
205 SmartcardErrorFactory.ThrowSmartcardException(ret);
207 SmartcardChannel basicChannel = new SmartcardChannel(this, _basicChannel);
208 _basicChannelList.Add(basicChannel);
214 /// Open a logical channel with the Secure Element, selecting the Applet represented by the given Application ID(AID).
216 /// <since_tizen> 3 </since_tizen>
217 /// <returns>The SmartcardChannel object for logical channel.</returns>
218 /// <param name="aid">Byte array containing the Application ID(AID) to be selected on the given channel.</param>
219 /// <param name="p2">P2 byte of the SELECT command if executed.</param>
220 public SmartcardChannel OpenLogicalChannel(byte[] aid, byte p2)
222 int ret = Interop.Smartcard.Session.SessionOpenLogicalChannel(_sessionHandle, aid, aid.Length, p2, out _logicalChannel);
223 if (ret != (int)SmartcardError.None)
225 Log.Error(Globals.LogTag, "Failed to open logical channel, Error - " + (SmartcardError)ret);
226 SmartcardErrorFactory.ThrowSmartcardException(ret);
228 SmartcardChannel logicalChannel = new SmartcardChannel(this, _logicalChannel);
229 _logicalChannelList.Add(logicalChannel);
231 return logicalChannel;