From a7a6966184bf5eda9e71e29fe9c4ac01102dfd12 Mon Sep 17 00:00:00 2001 From: Sudha Bheemanna Date: Tue, 20 Dec 2016 15:43:00 +0530 Subject: [PATCH] [C# Smartcard] Adding code for Smartcard Adding initial code for Smartcard. This code to be used as base for further development Change-Id: I06fdbd390cf94b5753bbe2fa1b5a41b75c5c8905 Signed-off-by: Sudha Bheemanna --- LICENSE | 202 +++++++++++++++++ Tizen.Smartcard/Interop/Interop.Glib.cs | 30 +++ Tizen.Smartcard/Interop/Interop.Libc.cs | 27 +++ Tizen.Smartcard/Interop/Interop.Libraries.cs | 25 +++ Tizen.Smartcard/Interop/Interop.Smartcard.cs | 83 +++++++ Tizen.Smartcard/Properties/AssemblyInfo.cs | 36 +++ Tizen.Smartcard/Tizen.Smartcard.Net45.csproj | 75 +++++++ Tizen.Smartcard/Tizen.Smartcard.Net45.project.json | 11 + Tizen.Smartcard/Tizen.Smartcard.csproj | 85 ++++++++ Tizen.Smartcard/Tizen.Smartcard.nuspec | 21 ++ Tizen.Smartcard/Tizen.Smartcard.project.json | 9 + Tizen.Smartcard/Tizen.Smartcard.snk | Bin 0 -> 596 bytes .../Tizen.Smartcard/SmartcardChannel.cs | 241 +++++++++++++++++++++ .../Tizen.Smartcard/SmartcardErrorFactory.cs | 70 ++++++ .../Tizen.Smartcard/SmartcardManager.cs | 38 ++++ .../Tizen.Smartcard/SmartcardManagerImpl.cs | 122 +++++++++++ Tizen.Smartcard/Tizen.Smartcard/SmartcardReader.cs | 150 +++++++++++++ .../Tizen.Smartcard/SmartcardSession.cs | 229 ++++++++++++++++++++ packaging/csapi-smartcard.manifest | 5 + packaging/csapi-smartcard.spec | 76 +++++++ 20 files changed, 1535 insertions(+) create mode 100644 LICENSE create mode 100644 Tizen.Smartcard/Interop/Interop.Glib.cs create mode 100644 Tizen.Smartcard/Interop/Interop.Libc.cs create mode 100644 Tizen.Smartcard/Interop/Interop.Libraries.cs create mode 100644 Tizen.Smartcard/Interop/Interop.Smartcard.cs create mode 100644 Tizen.Smartcard/Properties/AssemblyInfo.cs create mode 100644 Tizen.Smartcard/Tizen.Smartcard.Net45.csproj create mode 100644 Tizen.Smartcard/Tizen.Smartcard.Net45.project.json create mode 100644 Tizen.Smartcard/Tizen.Smartcard.csproj create mode 100644 Tizen.Smartcard/Tizen.Smartcard.nuspec create mode 100644 Tizen.Smartcard/Tizen.Smartcard.project.json create mode 100644 Tizen.Smartcard/Tizen.Smartcard.snk create mode 100644 Tizen.Smartcard/Tizen.Smartcard/SmartcardChannel.cs create mode 100644 Tizen.Smartcard/Tizen.Smartcard/SmartcardErrorFactory.cs create mode 100644 Tizen.Smartcard/Tizen.Smartcard/SmartcardManager.cs create mode 100644 Tizen.Smartcard/Tizen.Smartcard/SmartcardManagerImpl.cs create mode 100644 Tizen.Smartcard/Tizen.Smartcard/SmartcardReader.cs create mode 100644 Tizen.Smartcard/Tizen.Smartcard/SmartcardSession.cs create mode 100644 packaging/csapi-smartcard.manifest create mode 100644 packaging/csapi-smartcard.spec diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Tizen.Smartcard/Interop/Interop.Glib.cs b/Tizen.Smartcard/Interop/Interop.Glib.cs new file mode 100644 index 0000000..576a6e2 --- /dev/null +++ b/Tizen.Smartcard/Interop/Interop.Glib.cs @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Glib + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool GSourceFunc(IntPtr userData); + + [DllImport(Libraries.Glib, EntryPoint = "g_idle_add", CallingConvention = CallingConvention.Cdecl)] + internal static extern uint IdleAdd(GSourceFunc d, IntPtr data); + } +} diff --git a/Tizen.Smartcard/Interop/Interop.Libc.cs b/Tizen.Smartcard/Interop/Interop.Libc.cs new file mode 100644 index 0000000..825599e --- /dev/null +++ b/Tizen.Smartcard/Interop/Interop.Libc.cs @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Libc + { + [DllImport(Libraries.Libc, EntryPoint = "free", CallingConvention = CallingConvention.Cdecl)] + internal static extern int Free(IntPtr ptr); + } +} diff --git a/Tizen.Smartcard/Interop/Interop.Libraries.cs b/Tizen.Smartcard/Interop/Interop.Libraries.cs new file mode 100644 index 0000000..f231ed1 --- /dev/null +++ b/Tizen.Smartcard/Interop/Interop.Libraries.cs @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +internal static partial class Interop +{ + internal static partial class Libraries + { + public const string Smartcard = "libcapi-network-smartcard.so.0"; + public const string Glib = "libglib-2.0.so.0"; + public const string Libc = "libc.so.6"; + } +} diff --git a/Tizen.Smartcard/Interop/Interop.Smartcard.cs b/Tizen.Smartcard/Interop/Interop.Smartcard.cs new file mode 100644 index 0000000..234de80 --- /dev/null +++ b/Tizen.Smartcard/Interop/Interop.Smartcard.cs @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Smartcard + { + //capi-network-smartcard-0.0.6-2.1.armv7l.rpm + //Smartcard Manager + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_initialize")] + internal static extern int Initialize(); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_deinitialize")] + internal static extern int Deinitialize(); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_get_readers")] + internal static extern int GetReaders(out IntPtr readers, out int len); + + internal static class Reader + { + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_reader_get_name")] + internal static extern int ReaderGetName(int reader, out IntPtr readerName); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_reader_is_secure_element_present")] + internal static extern int ReaderIsSecureElementPresent(int reader, out bool present); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_reader_open_session")] + internal static extern int ReaderOpenSession(int reader, out int session); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_reader_close_sessions")] + internal static extern int ReaderCloseSessions(int reader); + } + + internal static class Session + { + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_session_get_reader")] + internal static extern int SessionGetReader(int session, out int reader); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_session_get_atr")] + internal static extern int SessionGetAtr(int session, out IntPtr atr, out int len); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_session_close")] + internal static extern int SessionClose(int session); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_session_is_closed")] + internal static extern int SessionIsClosed(int session, out bool closed); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_session_close_channels")] + internal static extern int SessionCloseChannels(int session); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_session_open_basic_channel")] + internal static extern int SessionOpenBasicChannel(int session, byte[] aid, int aidLen, byte p2, out int channel); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_session_open_logical_channel")] + internal static extern int SessionOpenLogicalChannel(int session, byte[] aid, int aidLen, byte p2, out int channel); + } + + internal static class Channel + { + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_channel_close")] + internal static extern int ChannelClose(int channel); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_channel_is_basic_channel")] + internal static extern int ChannelIsBasicChannel(int channel, out bool basicChannel); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_channel_is_closed")] + internal static extern int ChannelIsClosed(int channel, out bool closed); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_channel_get_select_response")] + internal static extern int ChannelGetSelectResponse(int channel, out IntPtr selectResp, out int len); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_channel_get_session")] + internal static extern int ChannelGetSession(int channel, out int session); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_channel_transmit")] + internal static extern int ChannelTransmit(int channel, byte[] cmd, int cmdLen, out IntPtr resp, out int len); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_channel_transmit_retrieve_response")] + internal static extern int ChannelTransmitRetrieveResponse(int channel, out IntPtr name, out int len); + [DllImport(Libraries.Smartcard, EntryPoint = "smartcard_channel_select_next")] + internal static extern int ChannelSelectNext(int channel, out bool success); + } + } +} diff --git a/Tizen.Smartcard/Properties/AssemblyInfo.cs b/Tizen.Smartcard/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d19c003 --- /dev/null +++ b/Tizen.Smartcard/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Tizen.Smartcard")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Tizen.Smartcard")] +[assembly: AssemblyCopyright("Copyright \u00A9 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("99a90243-7db1-4704-a078-3529c2da4e81")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Tizen.Smartcard/Tizen.Smartcard.Net45.csproj b/Tizen.Smartcard/Tizen.Smartcard.Net45.csproj new file mode 100644 index 0000000..bd6b371 --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard.Net45.csproj @@ -0,0 +1,75 @@ + + + + Debug + AnyCPU + {BF719F11-CDEA-4D04-A85B-4521A69E3614} + Library + Properties + Tizen.Smartcard + Tizen.Smartcard + 512 + v4.5 + + + true + full + false + bin\Debug\Net45\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\Net45\ + TRACE + prompt + 4 + false + + + true + + + Tizen.Smartcard.snk + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tizen.Smartcard/Tizen.Smartcard.Net45.project.json b/Tizen.Smartcard/Tizen.Smartcard.Net45.project.json new file mode 100644 index 0000000..1ec1af2 --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard.Net45.project.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "Tizen": "1.0.2" + }, + "frameworks": { + "net45": {} + }, + "runtimes": { + "win": {} + } +} diff --git a/Tizen.Smartcard/Tizen.Smartcard.csproj b/Tizen.Smartcard/Tizen.Smartcard.csproj new file mode 100644 index 0000000..b3127c7 --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard.csproj @@ -0,0 +1,85 @@ + + + + Debug + AnyCPU + {3EE5842E-7004-43BC-825B-29FFDEC18D4E} + Library + Properties + Tizen.Smartcard + Tizen.Smartcard + 512 + + + .NETStandard + v1.3 + .NETStandard,Version=v1.3 + false + true + $(NoWarn);1701;1702 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + true + + + Tizen.Smartcard.snk + + + + + + + + + + + + + + + + + + + + + + + + + + <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory) + <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory) + true + + \ No newline at end of file diff --git a/Tizen.Smartcard/Tizen.Smartcard.nuspec b/Tizen.Smartcard/Tizen.Smartcard.nuspec new file mode 100644 index 0000000..b37f61d --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard.nuspec @@ -0,0 +1,21 @@ + + + + Tizen.Smartcard + $version$ + Samsung Electronics + false + https://www.apache.org/licenses/LICENSE-2.0 + https://www.tizen.org/ + https://developer.tizen.org/sites/default/files/images/tizen-pinwheel-on-light-rgb_64_64.png + © Samsung Electronics Co., Ltd All Rights Reserved + Provides the Smartcard API for Tizen.Net + + + + + + + + + diff --git a/Tizen.Smartcard/Tizen.Smartcard.project.json b/Tizen.Smartcard/Tizen.Smartcard.project.json new file mode 100644 index 0000000..a4c6be0 --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard.project.json @@ -0,0 +1,9 @@ +{ + "dependencies": { + "NETStandard.Library": "1.6.0", + "Tizen": "1.0.2" + }, + "frameworks": { + "netstandard1.3": {} + } +} \ No newline at end of file diff --git a/Tizen.Smartcard/Tizen.Smartcard.snk b/Tizen.Smartcard/Tizen.Smartcard.snk new file mode 100644 index 0000000000000000000000000000000000000000..f3d151da2fc456d8bbcb2f021a4de6d87f6c7848 GIT binary patch literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50097P4sii;46ZVFQ{JF}nc-4ZfpLv(NR1pi z?m$k2D#fK`3|TgR1@#@=AEl-^3ju@BIbzbciYg;D(_?Vp-0xvWCh8ONf2^}B93LP$6N(k&I+#~54Hk#Gu`a(!Hh^M8Tu z5w|is)Jt0%W%z}AEZn~*{D zF;Vt|3@FdHH#@e0UW|b_jysDHV<_l6e#2lcDSYEgRhS!FAEFx*dmGD{Zje<;lsWJ0 zKI)`#KWmVV&vFBU-6*Wnl{W&eAMCYhkWMNZJK`OoGeameAXFtN_(2Rug`SryZdCq> zFokQ?-9B8vV{S2y)h@RIG!Ma@xD>gt;{OdQu@vLeWB{Ro-9@4GVn?Dj2V6WP{1$1Q zW}5tl{Xo(zq1%Zg_>m0;U)FdBo}{mY!p6v)j`;eK6_gdpj i)UCgJe^=Yg(SD_v#S+|umS!3|x#f>w*1sCHQI2mN>lz6F literal 0 HcmV?d00001 diff --git a/Tizen.Smartcard/Tizen.Smartcard/SmartcardChannel.cs b/Tizen.Smartcard/Tizen.Smartcard/SmartcardChannel.cs new file mode 100644 index 0000000..05d6455 --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard/SmartcardChannel.cs @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; +using System.Collections.Generic; +using Tizen.Network; + +namespace Tizen.Network.Smartcard +{ + /// + /// A class for Smartcard channel informations. It allows applications to handle channel informations. + /// + /// http://tizen.org/privilege/secureelement + public class SmartcardChannel : IDisposable + { + private int _channelHandle = -1; + private bool disposed = false; + private SmartcardSession _sessionObject; + + /// + /// Whether the kind of channel is basic. + /// + public bool IsBasicChannel + { + get + { + bool isBasicChannel; + int ret = Interop.Smartcard.Channel.ChannelIsBasicChannel(_channelHandle, out isBasicChannel); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get basic channel, Error - " + (SmartcardError)ret); + } + return isBasicChannel; + } + } + + /// + /// Whether the kind of channel is logical. + /// + public bool IsLogicalChannel + { + get + { + bool isBasicChannel; + int ret = Interop.Smartcard.Channel.ChannelIsBasicChannel(_channelHandle, out isBasicChannel); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get logical channel, Error - " + (SmartcardError)ret); + } + return !isBasicChannel; + } + } + + /// + /// Whether the channel is closed. + /// + public bool IsClosed + { + get + { + bool isClosed; + int ret = Interop.Smartcard.Channel.ChannelIsClosed(_channelHandle, out isClosed); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get closed, Error - " + (SmartcardError)ret); + } + return isClosed; + } + } + + /// + /// The session that has opened the given channel. + /// + public SmartcardSession Session + { + get + { + int session; + int ret = Interop.Smartcard.Channel.ChannelGetSession(_channelHandle, out session); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get session, Error - " + (SmartcardError)ret); + } + + if (_sessionObject.GetHandle() != session) + { + Log.Error(Globals.LogTag, "Does not correspond with session, Error - " + _sessionObject.GetHandle() + " " + session); + } + + return _sessionObject; + } + } + + internal SmartcardChannel(SmartcardSession sessionHandle, int channelHandle) + { + _sessionObject = sessionHandle; + _channelHandle = channelHandle; + } + + ~SmartcardChannel() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + } + //Free unmanaged objects + disposed = true; + } + + /// + /// Closes the given channel to the Secure Element. + /// + public void Close() + { + int ret = Interop.Smartcard.Channel.ChannelClose(_channelHandle); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to channel close, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + Dispose(true); + } + + /// + /// Gets the response to the select command. + /// + /// Byte array to retrieve the SELECT response. + public byte[] GetSelectedResponse() + { + byte[] respList; + IntPtr strAtr; + int len; + int ret = Interop.Smartcard.Channel.ChannelGetSelectResponse(_channelHandle, out strAtr, out len); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get select response, Error - " + (SmartcardError)ret); + } + + respList = new byte[len]; + for (int i = 0; i < len; i++) + { + respList[i] = Marshal.ReadByte(strAtr); + strAtr += sizeof(byte); + } + return respList; + } + + /// + /// Transmits an APDU command (as per ISO/IEC 7816-4) to the Secure Element. + /// + /// Byte array for the response APDU plus status words. + /// Command APDU to be send to the secure element. + public byte[] Transmit(byte[] cmd) + { + byte[] atrList; + IntPtr strAtr; + int len; + int ret = Interop.Smartcard.Channel.ChannelTransmit(_channelHandle, cmd, cmd.Length, out strAtr, out len); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to transmit, Error - " + (SmartcardError)ret); + } + + atrList = new byte[len]; + for (int i = 0; i < len; i++) + { + atrList[i] = Marshal.ReadByte(strAtr); + strAtr += sizeof(byte); + } + + return atrList; + } + + /// + /// Helper function to retrieves the response APDU of the previous transmit() call. + /// + /// Byte array for the response APDU plus status words. + public byte[] GetTransmittedResponse() + { + byte[] respList; + IntPtr strAtr; + int len; + int ret = Interop.Smartcard.Channel.ChannelTransmitRetrieveResponse(_channelHandle, out strAtr, out len); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get trasmit retrieve response, Error - " + (SmartcardError)ret); + } + + respList = new byte[len]; + for (int i = 0; i < len; i++) + { + respList[i] = Marshal.ReadByte(strAtr); + strAtr += sizeof(byte); + } + return respList; + } + + /// + /// Performs a selection of the next Applet on the given channel that matches to the partial Application ID(AID). + /// + /// True or false depending whether another applet with the partial Application ID(AID). + public bool SelectNext() + { + bool selectNext; + int ret = Interop.Smartcard.Channel.ChannelSelectNext(_channelHandle, out selectNext); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to select next, Error - " + (SmartcardError)ret); + } + return selectNext; + } + } +} diff --git a/Tizen.Smartcard/Tizen.Smartcard/SmartcardErrorFactory.cs b/Tizen.Smartcard/Tizen.Smartcard/SmartcardErrorFactory.cs new file mode 100644 index 0000000..70fafb9 --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard/SmartcardErrorFactory.cs @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using Tizen.Internals.Errors; + +namespace Tizen.Network.Smartcard +{ + internal enum SmartcardError + { + None = ErrorCode.None, + IoError = ErrorCode.IoError, + InvalidParameterError = ErrorCode.InvalidParameter, + PermissionDeniedError = ErrorCode.PermissionDenied, + NotSupportedError = ErrorCode.NotSupported, + GeneralError = -0x01C70000 | 0x01, + NoSuchElementError = -0x01C70000 | 0x02, + IllegalStateError = -0x01C70000 | 0x03, + IllegalReferenceError = -0x01C70000 | 0x04, + OperationNotSupportError = -0x01C70000 | 0x05, + ChannelNotAvailableError = -0x01C70000 | 0x06, + NotInitializedError = -0x01C70000 | 0x07 + } + + internal static class SmartcardErrorFactory + { + static internal void ThrowSmartcardException(int e) + { + ThrowException(e, false); + } + + static internal void ThrowSmartcardException(int e, int handle) + { + ThrowException(e, (handle < 0)); + } + + static private void ThrowException(int e, bool isHandleNull) + { + SmartcardError err = (SmartcardError)e; + + if (isHandleNull) + { + throw new InvalidOperationException("Invalid instance (object may have been disposed or released)"); + } + + if (err == SmartcardError.InvalidParameterError) + { + throw new ArgumentException(err.ToString()); + } + else + { + throw new InvalidOperationException(err.ToString()); + } + } + + } +} diff --git a/Tizen.Smartcard/Tizen.Smartcard/SmartcardManager.cs b/Tizen.Smartcard/Tizen.Smartcard/SmartcardManager.cs new file mode 100644 index 0000000..b5d1903 --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard/SmartcardManager.cs @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Tizen.Network.Smartcard +{ + /// + /// A class for Smartcard management. It allows applications to use Smartcard service. + /// + /// http://tizen.org/privilege/secureelement + static public class SmartcardManager + { + /// + /// Gets the list of available Secure Element readers. + /// + /// List of SmartcardReader objects. + static public IEnumerable GetReaders() + { + return SmartcardManagerImpl.Instance.GetReaders(); + } + } +} diff --git a/Tizen.Smartcard/Tizen.Smartcard/SmartcardManagerImpl.cs b/Tizen.Smartcard/Tizen.Smartcard/SmartcardManagerImpl.cs new file mode 100644 index 0000000..e7a0f9c --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard/SmartcardManagerImpl.cs @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Threading.Tasks; + +namespace Tizen.Network.Smartcard +{ + static internal class Globals + { + internal const string LogTag = "Tizen.Network.Smartcard"; + } + + internal class SmartcardManagerImpl : IDisposable + { + private static readonly SmartcardManagerImpl _instance = new SmartcardManagerImpl(); + private List _readerList = new List(); + private bool disposed = false; + + internal static SmartcardManagerImpl Instance + { + get + { + return _instance; + } + } + + private SmartcardManagerImpl() + { + initialize(); + } + + ~SmartcardManagerImpl() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + foreach (SmartcardReader reader in _readerList) + { + reader.Dispose(); + _readerList.Remove(reader); + } + } + //Free unmanaged objects + deinitialize(); + disposed = true; + } + + private void initialize() + { + int ret = Interop.Smartcard.Initialize(); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to initialize smartcard, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + } + + private void deinitialize() + { + int ret = Interop.Smartcard.Deinitialize(); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to deinitialize smartcard, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + } + + internal IEnumerable GetReaders() + { + IntPtr readerPtr; + int len = 0; + + int ret = Interop.Smartcard.GetReaders(out readerPtr, out len); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to remove with AP, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + + for (int i = 0; i < len; i++) + { + int readerID = Marshal.ReadInt32(readerPtr); + + SmartcardReader readerItem = new SmartcardReader(readerID); + _readerList.Add(readerItem); + readerPtr += sizeof(int); + } + + return _readerList; + } + } +} diff --git a/Tizen.Smartcard/Tizen.Smartcard/SmartcardReader.cs b/Tizen.Smartcard/Tizen.Smartcard/SmartcardReader.cs new file mode 100644 index 0000000..5c0f542 --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard/SmartcardReader.cs @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; +using System.Collections.Generic; +using Tizen.Network; + +namespace Tizen.Network.Smartcard +{ + /// + /// A class for Smartcard reader informations. It allows applications to handle reader informations. + /// + /// http://tizen.org/privilege/secureelement + public class SmartcardReader : IDisposable + { + private int _readerHandle = -1; + private int _session; + private bool disposed = false; + private List _sessionList = new List(); + + /// + /// The name of reader. + /// + public string Name + { + get + { + IntPtr strPtr; + int ret = Interop.Smartcard.Reader.ReaderGetName(_readerHandle, out strPtr); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get reader name, Error - " + (SmartcardError)ret); + return ""; + } + return Marshal.PtrToStringAnsi(strPtr); + } + } + + /// + /// The existence of secure element. + /// + public bool IsSecureElementPresent + { + get + { + bool isPresent; + int ret = Interop.Smartcard.Reader.ReaderIsSecureElementPresent(_readerHandle, out isPresent); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get present, Error - " + (SmartcardError)ret); + } + return isPresent; + } + } + + internal SmartcardReader(int handle) + { + _readerHandle = handle; + } + + ~SmartcardReader() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + foreach (SmartcardSession session in _sessionList) + { + session.Dispose(); + _sessionList.Remove(session); + } + } + //Free unmanaged objects + disposed = true; + } + + internal int GetHandle() + { + return _readerHandle; + } + + internal int GetSession() + { + return _session; + } + + /// + /// Connects to a Secure Element in the given reader. + /// + /// The SmartcardSession object. + public SmartcardSession OpenSession() + { + int ret = Interop.Smartcard.Reader.ReaderOpenSession(_readerHandle, out _session); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get session handle, Error - " + (SmartcardError)ret); + } + + SmartcardSession session = new SmartcardSession(this, _session); + _sessionList.Add(session); + return session; + } + + /// + /// Closes all the sessions opened on the given reader. + /// + public void CloseSessions() + { + int ret = Interop.Smartcard.Reader.ReaderCloseSessions(_readerHandle); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to close sessions, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + + foreach (SmartcardSession session in _sessionList) + { + session.Dispose(); + _sessionList.Remove(session); + } + } + } +} diff --git a/Tizen.Smartcard/Tizen.Smartcard/SmartcardSession.cs b/Tizen.Smartcard/Tizen.Smartcard/SmartcardSession.cs new file mode 100644 index 0000000..a6a4f3a --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard/SmartcardSession.cs @@ -0,0 +1,229 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; +using System.Collections.Generic; +using Tizen.Network; + +namespace Tizen.Network.Smartcard +{ + /// + /// A class for Smartcard session informations. It allows applications to handle session informations. + /// + /// http://tizen.org/privilege/secureelement + public class SmartcardSession : IDisposable + { + private int _sessionHandle = -1; + private bool disposed = false; + private List _basicChannelList = new List(); + private List _logicalChannelList = new List(); + private SmartcardReader _readerObject; + private int _basicChannel = 0; + private int _logicalChannel = 0; + + /// + /// The reader object that provides the given session. + /// + public SmartcardReader Reader + { + get + { + int reader; + int ret = Interop.Smartcard.Session.SessionGetReader(_sessionHandle, out reader); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get reader, Error - " + (SmartcardError)ret); + } + + if (_readerObject.GetHandle() != reader) + { + Log.Error(Globals.LogTag, "Does not correspond with reader, Error - " + _readerObject.GetHandle() + " " + reader); + } + + return _readerObject; + } + } + + /// + /// The Answer to Reset(ATR) of this Secure Element. + /// + public byte[] Atr + { + get + { + byte[] atrList; + IntPtr strAtr; + int len; + int ret = Interop.Smartcard.Session.SessionGetAtr(_sessionHandle, out strAtr, out len); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get atr, Error - " + (SmartcardError)ret); + } + + atrList = new byte[len]; + for (int i = 0; i < len; i++) + { + atrList[i] = Marshal.ReadByte(strAtr); + strAtr += sizeof(byte); + } + return atrList; + } + } + + /// + /// Whether the session is closed. + /// + public bool IsClosed + { + get + { + bool isClosed; + int ret = Interop.Smartcard.Session.SessionIsClosed(_sessionHandle, out isClosed); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to get present, Error - " + (SmartcardError)ret); + } + return isClosed; + } + } + + internal SmartcardSession(SmartcardReader readerHandle, int sessionHandle) + { + _readerObject = readerHandle; + _sessionHandle = sessionHandle; + } + + ~SmartcardSession() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + foreach (SmartcardChannel channel in _basicChannelList) + { + channel.Dispose(); + _basicChannelList.Remove(channel); + } + + foreach (SmartcardChannel channel in _logicalChannelList) + { + channel.Dispose(); + _logicalChannelList.Remove(channel); + } + } + //Free unmanaged objects + disposed = true; + } + + internal int GetHandle() + { + return _sessionHandle; + } + + /// + /// Closes the connection with the Secure Element. + /// + public void Close() + { + int ret = Interop.Smartcard.Session.SessionClose(_sessionHandle); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to close, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + Dispose(true); + } + + /// + /// Closes any channel opened on the given session. + /// + public void CloseChannels() + { + int ret = Interop.Smartcard.Session.SessionCloseChannels(_sessionHandle); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to close, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + + foreach (SmartcardChannel channel in _basicChannelList) + { + channel.Dispose(); + _basicChannelList.Remove(channel); + } + + foreach (SmartcardChannel channel in _logicalChannelList) + { + channel.Dispose(); + _logicalChannelList.Remove(channel); + } + } + + /// + /// Gets an access to the basic channel, as defined in the ISO/IEC 7816-4 specification (the one that has number 0). + /// + /// The SmartcardChannel object for basic channel. + /// Byte array containing the Application ID(AID) to be selected on the given channel. + /// P2 byte of the SELECT command if executed. + public SmartcardChannel OpenBasicChannel(byte[] aid, byte p2) + { + int ret = Interop.Smartcard.Session.SessionOpenBasicChannel(_sessionHandle, aid, aid.Length, p2, out _basicChannel); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to open basic channel, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + SmartcardChannel basicChannel = new SmartcardChannel(this, _basicChannel); + _basicChannelList.Add(basicChannel); + + return basicChannel; + } + + /// + /// Open a logical channel with the Secure Element, selecting the Applet represented by the given Application ID(AID). + /// + /// The SmartcardChannel object for logical channel. + /// Byte array containing the Application ID(AID) to be selected on the given channel. + /// P2 byte of the SELECT command if executed. + public SmartcardChannel OpenLogicalChannel(byte[] aid, byte p2) + { + int ret = Interop.Smartcard.Session.SessionOpenLogicalChannel(_sessionHandle, aid, aid.Length, p2, out _logicalChannel); + if (ret != (int)SmartcardError.None) + { + Log.Error(Globals.LogTag, "Failed to open logical channel, Error - " + (SmartcardError)ret); + SmartcardErrorFactory.ThrowSmartcardException(ret); + } + SmartcardChannel logicalChannel = new SmartcardChannel(this, _logicalChannel); + _logicalChannelList.Add(logicalChannel); + + return logicalChannel; + } + } +} diff --git a/packaging/csapi-smartcard.manifest b/packaging/csapi-smartcard.manifest new file mode 100644 index 0000000..75b0fa5 --- /dev/null +++ b/packaging/csapi-smartcard.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/csapi-smartcard.spec b/packaging/csapi-smartcard.spec new file mode 100644 index 0000000..b1c2e0b --- /dev/null +++ b/packaging/csapi-smartcard.spec @@ -0,0 +1,76 @@ +%{!?dotnet_assembly_path: %define dotnet_assembly_path /opt/usr/share/dotnet.tizen/framework} + +%if 0%{?tizen_build_devel_mode} +%define BUILDCONF Debug +%else +%define BUILDCONF Release +%endif + +Name: csapi-smartcard +Summary: Tizen Smartcard API for C# +Version: 1.0.2 +Release: 1 +Group: Development/Libraries +License: Apache-2.0 +URL: https://www.tizen.org +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest + +AutoReqProv: no +ExcludeArch: %{ix86} aarch64 + +BuildRequires: mono-compiler +BuildRequires: mono-devel + +BuildRequires: dotnet-build-tools + +# C# API Requries +BuildRequires: csapi-tizen-nuget + +%description +Tizen Smartcard API for C# + +%prep +%setup -q +cp %{SOURCE1} . + +%define Assemblies Tizen.Smartcard + +%build +for ASM in %{Assemblies}; do +# NuGet Restore +find $ASM/*.project.json -exec nuget restore {} \; +# Build +find $ASM/*.csproj -exec xbuild {} /p:Configuration=%{BUILDCONF} \; +# NuGet Pack +nuget pack $ASM/$ASM.nuspec -Version %{version} -Properties Configuration=%{BUILDCONF} +done + +%install +# Runtime Binary +mkdir -p %{buildroot}%{dotnet_assembly_path} +for ASM in %{Assemblies}; do +%if 0%{?_with_corefx} + install -p -m 644 $ASM/bin/%{BUILDCONF}/$ASM.dll %{buildroot}%{dotnet_assembly_path} +%else + install -p -m 644 $ASM/bin/%{BUILDCONF}/Net45/$ASM.dll %{buildroot}%{dotnet_assembly_path} +%endif +done +# NuGet +mkdir -p %{buildroot}/nuget +install -p -m 644 *.nupkg %{buildroot}/nuget + +%files +%manifest %{name}.manifest +%license LICENSE +%attr(644,root,root) %{dotnet_assembly_path}/*.dll + +%package nuget +Summary: NuGet package for %{name} +Group: Development/Libraries + +%description nuget +NuGet package for %{name} + +%files nuget +/nuget/*.nupkg -- 2.7.4