diff options
Diffstat (limited to 'Tizen.Smartcard')
17 files changed, 1252 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{BF719F11-CDEA-4D04-A85B-4521A69E3614}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Tizen.Smartcard</RootNamespace>
+ <AssemblyName>Tizen.Smartcard</AssemblyName>
+ <FileAlignment>512</FileAlignment>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\Net45\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <Prefer32Bit>false</Prefer32Bit>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\Net45\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <Prefer32Bit>false</Prefer32Bit>
+ </PropertyGroup>
+ <PropertyGroup>
+ <SignAssembly>true</SignAssembly>
+ </PropertyGroup>
+ <PropertyGroup>
+ <AssemblyOriginatorKeyFile>Tizen.Smartcard.snk</AssemblyOriginatorKeyFile>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Interop\Interop.Glib.cs" />
+ <Compile Include="Interop\Interop.Libc.cs" />
+ <Compile Include="Interop\Interop.Libraries.cs" />
+ <Compile Include="Interop\Interop.Smartcard.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardChannel.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardErrorFactory.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardManager.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardManagerImpl.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardReader.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardSession.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="Tizen.Smartcard.nuspec" />
+ <None Include="Tizen.Smartcard.Net45.project.json" />
+ <None Include="Tizen.Smartcard.snk" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
\ 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 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{3EE5842E-7004-43BC-825B-29FFDEC18D4E}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Tizen.Smartcard</RootNamespace>
+ <AssemblyName>Tizen.Smartcard</AssemblyName>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup>
+ <TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
+ <TargetFrameworkVersion>v1.3</TargetFrameworkVersion>
+ <NuGetTargetMoniker>.NETStandard,Version=v1.3</NuGetTargetMoniker>
+ <AddAdditionalExplicitAssemblyReferences>false</AddAdditionalExplicitAssemblyReferences>
+ <NoStdLib>true</NoStdLib>
+ <NoWarn>$(NoWarn);1701;1702</NoWarn>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <Prefer32Bit>false</Prefer32Bit>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <Prefer32Bit>false</Prefer32Bit>
+ </PropertyGroup>
+ <PropertyGroup>
+ <SignAssembly>true</SignAssembly>
+ </PropertyGroup>
+ <PropertyGroup>
+ <AssemblyOriginatorKeyFile>Tizen.Smartcard.snk</AssemblyOriginatorKeyFile>
+ </PropertyGroup>
+ <ItemGroup>
+ <Compile Include="Interop\Interop.Glib.cs" />
+ <Compile Include="Interop\Interop.Libc.cs" />
+ <Compile Include="Interop\Interop.Libraries.cs" />
+ <Compile Include="Interop\Interop.Smartcard.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardChannel.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardErrorFactory.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardManager.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardManagerImpl.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardReader.cs" />
+ <Compile Include="Tizen.Smartcard\SmartcardSession.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="Tizen.Smartcard.nuspec" />
+ <None Include="Tizen.Smartcard.project.json" />
+ <None Include="Tizen.Smartcard.snk" />
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath)\Tizen\Tizen.CSharp.GBS.targets" Condition="Exists('$(MSBuildExtensionsPath)\Tizen\Tizen.CSharp.GBS.targets')" />
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+ <PropertyGroup>
+ <!-- https://github.com/dotnet/corefxlab/tree/master/samples/NetCoreSample and
+ https://docs.microsoft.com/en-us/dotnet/articles/core/tutorials/target-dotnetcore-with-msbuild
+ -->
+ <!-- We don't use any of MSBuild's resolution logic for resolving the framework, so just set these two
+ properties to any folder that exists to skip the GetReferenceAssemblyPaths task (not target) and
+ to prevent it from outputting a warning (MSB3644).
+ -->
+ <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory)</_TargetFrameworkDirectories>
+ <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory)</_FullFrameworkReferenceAssemblyPaths>
+ <AutoUnifyAssemblyReferences>true</AutoUnifyAssemblyReferences>
+ </PropertyGroup>
+</Project>
\ 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 @@ +<?xml version="1.0"?> +<package> + <metadata> + <id>Tizen.Smartcard</id> + <version>$version$</version> + <authors>Samsung Electronics</authors> + <requireLicenseAcceptance>false</requireLicenseAcceptance> + <licenseUrl>https://www.apache.org/licenses/LICENSE-2.0</licenseUrl> + <projectUrl>https://www.tizen.org/</projectUrl> + <iconUrl>https://developer.tizen.org/sites/default/files/images/tizen-pinwheel-on-light-rgb_64_64.png</iconUrl> + <copyright>© Samsung Electronics Co., Ltd All Rights Reserved</copyright> + <description>Provides the Smartcard API for Tizen.Net</description> + <dependencies> + <dependency id="Tizen" version="1.0.2" /> + </dependencies> + </metadata> + <files> + <file src="bin/$Configuration$/Tizen.Smartcard.dll" target="lib/netstandard1.3" /> + <file src="bin/$Configuration$/Net45/Tizen.Smartcard.dll" target="lib/net45" /> + </files> +</package> 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 Binary files differnew file mode 100644 index 0000000..f3d151d --- /dev/null +++ b/Tizen.Smartcard/Tizen.Smartcard.snk 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 +{ + /// <summary> + /// A class for Smartcard channel informations. It allows applications to handle channel informations. + /// </summary> + /// <privilege>http://tizen.org/privilege/secureelement</privilege> + public class SmartcardChannel : IDisposable + { + private int _channelHandle = -1; + private bool disposed = false; + private SmartcardSession _sessionObject; + + /// <summary> + /// Whether the kind of channel is basic. + /// </summary> + 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; + } + } + + /// <summary> + /// Whether the kind of channel is logical. + /// </summary> + 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; + } + } + + /// <summary> + /// Whether the channel is closed. + /// </summary> + 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; + } + } + + /// <summary> + /// The session that has opened the given channel. + /// </summary> + 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; + } + + /// <summary> + /// Closes the given channel to the Secure Element. + /// </summary> + 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); + } + + /// <summary> + /// Gets the response to the select command. + /// </summary> + /// <returns>Byte array to retrieve the SELECT response.</returns> + 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; + } + + /// <summary> + /// Transmits an APDU command (as per ISO/IEC 7816-4) to the Secure Element. + /// </summary> + /// <returns>Byte array for the response APDU plus status words.</returns> + /// <param name="cmd">Command APDU to be send to the secure element.</param> + 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; + } + + /// <summary> + /// Helper function to retrieves the response APDU of the previous transmit() call. + /// </summary> + /// <returns>Byte array for the response APDU plus status words.</returns> + 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; + } + + /// <summary> + /// Performs a selection of the next Applet on the given channel that matches to the partial Application ID(AID). + /// </summary> + /// <returns>True or false depending whether another applet with the partial Application ID(AID).</returns> + 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 +{ + /// <summary> + /// A class for Smartcard management. It allows applications to use Smartcard service. + /// </summary> + /// <privilege>http://tizen.org/privilege/secureelement</privilege> + static public class SmartcardManager + { + /// <summary> + /// Gets the list of available Secure Element readers. + /// </summary> + /// <returns>List of SmartcardReader objects.</returns> + static public IEnumerable<SmartcardReader> 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<SmartcardReader> _readerList = new List<SmartcardReader>(); + 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<SmartcardReader> 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 +{ + /// <summary> + /// A class for Smartcard reader informations. It allows applications to handle reader informations. + /// </summary> + /// <privilege>http://tizen.org/privilege/secureelement</privilege> + public class SmartcardReader : IDisposable + { + private int _readerHandle = -1; + private int _session; + private bool disposed = false; + private List<SmartcardSession> _sessionList = new List<SmartcardSession>(); + + /// <summary> + /// The name of reader. + /// </summary> + 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); + } + } + + /// <summary> + /// The existence of secure element. + /// </summary> + 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; + } + + /// <summary> + /// Connects to a Secure Element in the given reader. + /// </summary> + /// <returns>The SmartcardSession object.</returns> + 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; + } + + /// <summary> + /// Closes all the sessions opened on the given reader. + /// </summary> + 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 +{ + /// <summary> + /// A class for Smartcard session informations. It allows applications to handle session informations. + /// </summary> + /// <privilege>http://tizen.org/privilege/secureelement</privilege> + public class SmartcardSession : IDisposable + { + private int _sessionHandle = -1; + private bool disposed = false; + private List<SmartcardChannel> _basicChannelList = new List<SmartcardChannel>(); + private List<SmartcardChannel> _logicalChannelList = new List<SmartcardChannel>(); + private SmartcardReader _readerObject; + private int _basicChannel = 0; + private int _logicalChannel = 0; + + /// <summary> + /// The reader object that provides the given session. + /// </summary> + 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; + } + } + + /// <summary> + /// The Answer to Reset(ATR) of this Secure Element. + /// </summary> + 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; + } + } + + /// <summary> + /// Whether the session is closed. + /// </summary> + 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; + } + + /// <summary> + /// Closes the connection with the Secure Element. + /// </summary> + 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); + } + + /// <summary> + /// Closes any channel opened on the given session. + /// </summary> + 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); + } + } + + /// <summary> + /// Gets an access to the basic channel, as defined in the ISO/IEC 7816-4 specification (the one that has number 0). + /// </summary> + /// <returns>The SmartcardChannel object for basic channel.</returns> + /// <param name="aid">Byte array containing the Application ID(AID) to be selected on the given channel.</param> + /// <param name="p2">P2 byte of the SELECT command if executed.</param> + 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; + } + + /// <summary> + /// Open a logical channel with the Secure Element, selecting the Applet represented by the given Application ID(AID). + /// </summary> + /// <returns>The SmartcardChannel object for logical channel.</returns> + /// <param name="aid">Byte array containing the Application ID(AID) to be selected on the given channel.</param> + /// <param name="p2">P2 byte of the SELECT command if executed.</param> + 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; + } + } +} |