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 /// The class for the Smartcard session information. It allows applications to handle the session information.
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>
157 /// <exception cref="NotSupportedException">Thrown when the Smartcard is not supported.</exception>
158 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
161 int ret = Interop.Smartcard.Session.SessionClose(_sessionHandle);
162 if (ret != (int)SmartcardError.None)
164 Log.Error(Globals.LogTag, "Failed to close, Error - " + (SmartcardError)ret);
165 SmartcardErrorFactory.ThrowSmartcardException(ret);
171 /// Closes any channel opened on the given session.
173 /// <since_tizen> 3 </since_tizen>
174 /// <exception cref="NotSupportedException">Thrown when the Smartcard is not supported.</exception>
175 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
176 public void CloseChannels()
178 int ret = Interop.Smartcard.Session.SessionCloseChannels(_sessionHandle);
179 if (ret != (int)SmartcardError.None)
181 Log.Error(Globals.LogTag, "Failed to close, Error - " + (SmartcardError)ret);
182 SmartcardErrorFactory.ThrowSmartcardException(ret);
185 foreach (SmartcardChannel channel in _basicChannelList)
190 foreach (SmartcardChannel channel in _logicalChannelList)
197 /// Gets an access to the basic channel, as defined in the ISO/IEC 7816-4 specification (the one that has number 0).
199 /// <since_tizen> 3 </since_tizen>
200 /// <returns>The SmartcardChannel object for the basic channel.</returns>
201 /// <param name="aid">The byte array containing the Application ID(AID) to be selected on the given channel.</param>
202 /// <param name="p2">P2 byte of the SELECT command if executed.</param>
203 /// <exception cref="NotSupportedException">Thrown when the Smartcard is not supported.</exception>
204 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
205 public SmartcardChannel OpenBasicChannel(byte[] aid, byte p2)
207 int ret = Interop.Smartcard.Session.SessionOpenBasicChannel(_sessionHandle, aid, aid.Length, p2, out _basicChannel);
208 if (ret != (int)SmartcardError.None)
210 Log.Error(Globals.LogTag, "Failed to open basic channel, Error - " + (SmartcardError)ret);
211 SmartcardErrorFactory.ThrowSmartcardException(ret);
213 SmartcardChannel basicChannel = new SmartcardChannel(this, _basicChannel);
214 _basicChannelList.Add(basicChannel);
220 /// Open a logical channel with the secure element, selecting the Applet represented by the given application ID (AID).
222 /// <since_tizen> 3 </since_tizen>
223 /// <returns>The SmartcardChannel object for the logical channel.</returns>
224 /// <param name="aid">The byte array containing the Application ID(AID) to be selected on the given channel.</param>
225 /// <param name="p2">P2 byte of the SELECT command if executed.</param>
226 /// <exception cref="NotSupportedException">Thrown when the Smartcard is not supported.</exception>
227 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
228 public SmartcardChannel OpenLogicalChannel(byte[] aid, byte p2)
230 int ret = Interop.Smartcard.Session.SessionOpenLogicalChannel(_sessionHandle, aid, aid.Length, p2, out _logicalChannel);
231 if (ret != (int)SmartcardError.None)
233 Log.Error(Globals.LogTag, "Failed to open logical channel, Error - " + (SmartcardError)ret);
234 SmartcardErrorFactory.ThrowSmartcardException(ret);
236 SmartcardChannel logicalChannel = new SmartcardChannel(this, _logicalChannel);
237 _logicalChannelList.Add(logicalChannel);
239 return logicalChannel;