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 Smartcard channel information. It allows applications to handle the channel information.
26 /// <since_tizen> 3 </since_tizen>
27 /// <privilege>http://tizen.org/privilege/secureelement</privilege>
28 public class SmartcardChannel : IDisposable
30 private int _channelHandle = -1;
31 private bool disposed = false;
32 private SmartcardSession _sessionObject;
35 /// Whether the kind of channel is basic.
37 /// <since_tizen> 3 </since_tizen>
38 public bool IsBasicChannel
43 int ret = Interop.Smartcard.Channel.ChannelIsBasicChannel(_channelHandle, out isBasicChannel);
44 if (ret != (int)SmartcardError.None)
46 Log.Error(Globals.LogTag, "Failed to get basic channel, Error - " + (SmartcardError)ret);
48 return isBasicChannel;
53 /// Whether the kind of channel is logical.
55 /// <since_tizen> 3 </since_tizen>
56 public bool IsLogicalChannel
61 int ret = Interop.Smartcard.Channel.ChannelIsBasicChannel(_channelHandle, out isBasicChannel);
62 if (ret != (int)SmartcardError.None)
64 Log.Error(Globals.LogTag, "Failed to get logical channel, Error - " + (SmartcardError)ret);
66 return !isBasicChannel;
71 /// Whether the channel is closed.
73 /// <since_tizen> 3 </since_tizen>
79 int ret = Interop.Smartcard.Channel.ChannelIsClosed(_channelHandle, out isClosed);
80 if (ret != (int)SmartcardError.None)
82 Log.Error(Globals.LogTag, "Failed to get closed, Error - " + (SmartcardError)ret);
89 /// The session that has opened the given channel.
91 /// <since_tizen> 3 </since_tizen>
92 public SmartcardSession Session
97 int ret = Interop.Smartcard.Channel.ChannelGetSession(_channelHandle, out session);
98 if (ret != (int)SmartcardError.None)
100 Log.Error(Globals.LogTag, "Failed to get session, Error - " + (SmartcardError)ret);
103 if (_sessionObject.GetHandle() != session)
105 Log.Error(Globals.LogTag, "Does not correspond with session, Error - " + _sessionObject.GetHandle() + " " + session);
108 return _sessionObject;
112 internal SmartcardChannel(SmartcardSession sessionHandle, int channelHandle)
114 _sessionObject = sessionHandle;
115 _channelHandle = channelHandle;
119 /// SmartcardChannel destructor.
129 public void Dispose()
132 GC.SuppressFinalize(this);
135 private void Dispose(bool disposing)
142 // Free managed objects.
144 //Free unmanaged objects
149 /// Closes the given channel to the Secure Element.
151 /// <since_tizen> 3 </since_tizen>
152 /// <exception cref="NotSupportedException">Thrown when the Smartcard is not supported.</exception>
153 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
156 int ret = Interop.Smartcard.Channel.ChannelClose(_channelHandle);
157 if (ret != (int)SmartcardError.None)
159 Log.Error(Globals.LogTag, "Failed to channel close, Error - " + (SmartcardError)ret);
160 SmartcardErrorFactory.ThrowSmartcardException(ret);
166 /// Gets the response to the select command.
168 /// <since_tizen> 3 </since_tizen>
169 /// <returns>Byte array to retrieve the select response.</returns>
170 public byte[] GetSelectedResponse()
175 int ret = Interop.Smartcard.Channel.ChannelGetSelectResponse(_channelHandle, out strAtr, out len);
176 if (ret != (int)SmartcardError.None)
178 Log.Error(Globals.LogTag, "Failed to get select response, Error - " + (SmartcardError)ret);
181 respList = new byte[len];
182 for (int i = 0; i < len; i++)
184 respList[i] = Marshal.ReadByte(strAtr);
185 strAtr += sizeof(byte);
191 /// Transmits the APDU command (as per ISO/IEC 7816-4) to the secure element.
193 /// <since_tizen> 3 </since_tizen>
194 /// <returns>Byte array for the response APDU plus status words.</returns>
195 /// <param name="cmd">Command APDU to be sent to the secure element.</param>
196 public byte[] Transmit(byte[] cmd)
201 int ret = Interop.Smartcard.Channel.ChannelTransmit(_channelHandle, cmd, cmd.Length, out strAtr, out len);
202 if (ret != (int)SmartcardError.None)
204 Log.Error(Globals.LogTag, "Failed to transmit, Error - " + (SmartcardError)ret);
207 atrList = new byte[len];
208 for (int i = 0; i < len; i++)
210 atrList[i] = Marshal.ReadByte(strAtr);
211 strAtr += sizeof(byte);
218 /// Helper function to retrieve the response APDU of the previous transmit() call.
220 /// <since_tizen> 3 </since_tizen>
221 /// <returns>Byte array for the response APDU plus status words.</returns>
222 public byte[] GetTransmittedResponse()
227 int ret = Interop.Smartcard.Channel.ChannelTransmitRetrieveResponse(_channelHandle, out strAtr, out len);
228 if (ret != (int)SmartcardError.None)
230 Log.Error(Globals.LogTag, "Failed to get trasmit retrieve response, Error - " + (SmartcardError)ret);
233 respList = new byte[len];
234 for (int i = 0; i < len; i++)
236 respList[i] = Marshal.ReadByte(strAtr);
237 strAtr += sizeof(byte);
243 /// Performs a selection of the next applet on the given channel that matches to the partial application ID (AID).
245 /// <since_tizen> 3 </since_tizen>
246 /// <returns>True or false depending whether another applet with the partial application ID (AID).</returns>
247 public bool SelectNext()
250 int ret = Interop.Smartcard.Channel.ChannelSelectNext(_channelHandle, out selectNext);
251 if (ret != (int)SmartcardError.None)
253 Log.Error(Globals.LogTag, "Failed to select next, Error - " + (SmartcardError)ret);