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 channel informations. It allows applications to handle channel informations.
26 /// <privilege>http://tizen.org/privilege/secureelement</privilege>
27 public class SmartcardChannel : IDisposable
29 private int _channelHandle = -1;
30 private bool disposed = false;
31 private SmartcardSession _sessionObject;
34 /// Whether the kind of channel is basic.
36 public bool IsBasicChannel
41 int ret = Interop.Smartcard.Channel.ChannelIsBasicChannel(_channelHandle, out isBasicChannel);
42 if (ret != (int)SmartcardError.None)
44 Log.Error(Globals.LogTag, "Failed to get basic channel, Error - " + (SmartcardError)ret);
46 return isBasicChannel;
51 /// Whether the kind of channel is logical.
53 public bool IsLogicalChannel
58 int ret = Interop.Smartcard.Channel.ChannelIsBasicChannel(_channelHandle, out isBasicChannel);
59 if (ret != (int)SmartcardError.None)
61 Log.Error(Globals.LogTag, "Failed to get logical channel, Error - " + (SmartcardError)ret);
63 return !isBasicChannel;
68 /// Whether the channel is closed.
75 int ret = Interop.Smartcard.Channel.ChannelIsClosed(_channelHandle, out isClosed);
76 if (ret != (int)SmartcardError.None)
78 Log.Error(Globals.LogTag, "Failed to get closed, Error - " + (SmartcardError)ret);
85 /// The session that has opened the given channel.
87 public SmartcardSession Session
92 int ret = Interop.Smartcard.Channel.ChannelGetSession(_channelHandle, out session);
93 if (ret != (int)SmartcardError.None)
95 Log.Error(Globals.LogTag, "Failed to get session, Error - " + (SmartcardError)ret);
98 if (_sessionObject.GetHandle() != session)
100 Log.Error(Globals.LogTag, "Does not correspond with session, Error - " + _sessionObject.GetHandle() + " " + session);
103 return _sessionObject;
107 internal SmartcardChannel(SmartcardSession sessionHandle, int channelHandle)
109 _sessionObject = sessionHandle;
110 _channelHandle = channelHandle;
118 public void Dispose()
121 GC.SuppressFinalize(this);
124 private void Dispose(bool disposing)
131 // Free managed objects.
133 //Free unmanaged objects
138 /// Closes the given channel to the Secure Element.
142 int ret = Interop.Smartcard.Channel.ChannelClose(_channelHandle);
143 if (ret != (int)SmartcardError.None)
145 Log.Error(Globals.LogTag, "Failed to channel close, Error - " + (SmartcardError)ret);
146 SmartcardErrorFactory.ThrowSmartcardException(ret);
152 /// Gets the response to the select command.
154 /// <returns>Byte array to retrieve the SELECT response.</returns>
155 public byte[] GetSelectedResponse()
160 int ret = Interop.Smartcard.Channel.ChannelGetSelectResponse(_channelHandle, out strAtr, out len);
161 if (ret != (int)SmartcardError.None)
163 Log.Error(Globals.LogTag, "Failed to get select response, Error - " + (SmartcardError)ret);
166 respList = new byte[len];
167 for (int i = 0; i < len; i++)
169 respList[i] = Marshal.ReadByte(strAtr);
170 strAtr += sizeof(byte);
176 /// Transmits an APDU command (as per ISO/IEC 7816-4) to the Secure Element.
178 /// <returns>Byte array for the response APDU plus status words.</returns>
179 /// <param name="cmd">Command APDU to be send to the secure element.</param>
180 public byte[] Transmit(byte[] cmd)
185 int ret = Interop.Smartcard.Channel.ChannelTransmit(_channelHandle, cmd, cmd.Length, out strAtr, out len);
186 if (ret != (int)SmartcardError.None)
188 Log.Error(Globals.LogTag, "Failed to transmit, Error - " + (SmartcardError)ret);
191 atrList = new byte[len];
192 for (int i = 0; i < len; i++)
194 atrList[i] = Marshal.ReadByte(strAtr);
195 strAtr += sizeof(byte);
202 /// Helper function to retrieves the response APDU of the previous transmit() call.
204 /// <returns>Byte array for the response APDU plus status words.</returns>
205 public byte[] GetTransmittedResponse()
210 int ret = Interop.Smartcard.Channel.ChannelTransmitRetrieveResponse(_channelHandle, out strAtr, out len);
211 if (ret != (int)SmartcardError.None)
213 Log.Error(Globals.LogTag, "Failed to get trasmit retrieve response, Error - " + (SmartcardError)ret);
216 respList = new byte[len];
217 for (int i = 0; i < len; i++)
219 respList[i] = Marshal.ReadByte(strAtr);
220 strAtr += sizeof(byte);
226 /// Performs a selection of the next Applet on the given channel that matches to the partial Application ID(AID).
228 /// <returns>True or false depending whether another applet with the partial Application ID(AID).</returns>
229 public bool SelectNext()
232 int ret = Interop.Smartcard.Channel.ChannelSelectNext(_channelHandle, out selectNext);
233 if (ret != (int)SmartcardError.None)
235 Log.Error(Globals.LogTag, "Failed to select next, Error - " + (SmartcardError)ret);