forked from Pkcs11Interop/Pkcs11Interop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cs
219 lines (183 loc) · 8.35 KB
/
Settings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2012-2020 The Pkcs11Interop Project
*
* 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.
*/
/*
* Written for the Pkcs11Interop project by:
* Jaroslav IMRICH <[email protected]>
*/
using System;
using System.IO;
using System.Reflection;
using Net.Pkcs11Interop.Common;
using Net.Pkcs11Interop.HighLevelAPI;
using Net.Pkcs11Interop.Logging;
using LLA40 = Net.Pkcs11Interop.LowLevelAPI40;
using LLA41 = Net.Pkcs11Interop.LowLevelAPI41;
using LLA80 = Net.Pkcs11Interop.LowLevelAPI80;
using LLA81 = Net.Pkcs11Interop.LowLevelAPI81;
// Note: Code in this file is maintained manually.
namespace Net.Pkcs11Interop.Tests
{
/// <summary>
/// Test settings.
/// </summary>
public static class Settings
{
#region Properties that almost always need to be configured before the tests are executed
/// <summary>
/// Factories to be used by Developer and Pkcs11Interop library
/// </summary>
public static Pkcs11InteropFactories Factories = new Pkcs11InteropFactories();
/// <summary>
/// Relative name or absolute path of unmanaged PKCS#11 library provided by smartcard or HSM vendor.
/// </summary>
public static string Pkcs11LibraryPath = GetPkcs11MockLibraryPath();
/// <summary>
/// Type of application that will be using PKCS#11 library.
/// When set to AppType.MultiThreaded unmanaged PKCS#11 library performs locking to ensure thread safety.
/// </summary>
public static AppType AppType = AppType.MultiThreaded;
/// <summary>
/// Serial number of token (smartcard) that should be used by these tests.
/// First slot with token present is used when both TokenSerial and TokenLabel properties are null.
/// </summary>
public static string TokenSerial = null;
/// <summary>
/// Label of the token (smartcard) that should be used by these tests.
/// First slot with token present is used when both TokenSerial and TokenLabel properties are null.
/// </summary>
public static string TokenLabel = null;
/// <summary>
/// PIN of the SO user a.k.a. PUK.
/// </summary>
public static string SecurityOfficerPin = @"11111111";
/// <summary>
/// PIN of the normal user.
/// </summary>
public static string NormalUserPin = @"11111111";
/// <summary>
/// Application name that is used as a label for all objects created by these tests.
/// </summary>
public static string ApplicationName = @"Pkcs11Interop";
#endregion
#region Properties that are set automatically in class constructor
/// <summary>
/// Arguments passed to the C_Initialize function in LowLevelAPI40 tests.
/// </summary>
public static LLA40.CK_C_INITIALIZE_ARGS InitArgs40 = null;
/// <summary>
/// Arguments passed to the C_Initialize function in LowLevelAPI41 tests.
/// </summary>
public static LLA41.CK_C_INITIALIZE_ARGS InitArgs41 = null;
/// <summary>
/// Arguments passed to the C_Initialize function in LowLevelAPI80 tests.
/// </summary>
public static LLA80.CK_C_INITIALIZE_ARGS InitArgs80 = null;
/// <summary>
/// Arguments passed to the C_Initialize function in LowLevelAPI81 tests.
/// </summary>
public static LLA81.CK_C_INITIALIZE_ARGS InitArgs81 = null;
/// <summary>
/// PIN of the SO user a.k.a. PUK.
/// </summary>
public static byte[] SecurityOfficerPinArray = null;
/// <summary>
/// PIN of the normal user.
/// </summary>
public static byte[] NormalUserPinArray = null;
/// <summary>
/// Application name that is used as a label for all objects created by these tests.
/// </summary>
public static byte[] ApplicationNameArray = null;
/// <summary>
/// PKCS#11 URI that identifies private key usable in signature creation tests.
/// </summary>
public static string PrivateKeyUri = null;
#endregion
/// <summary>
/// Static class constructor
/// </summary>
static Settings()
{
// Uncomment following three lines to enable managed logging via System.Diagnostics.Trace class
// SimplePkcs11InteropLoggerFactory simpleLoggerFactory = new SimplePkcs11InteropLoggerFactory();
// simpleLoggerFactory.EnableDiagnosticsTraceOutput();
// Pkcs11InteropLoggerFactory.SetLoggerFactory(simpleLoggerFactory);
// Uncomment following three lines to enable unmanaged logging via PKCS11-LOGGER library
// System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LIBRARY_PATH", Pkcs11LibraryPath);
// System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LOG_FILE_PATH", @"c:\pkcs11-logger.txt");
// Pkcs11LibraryPath = @"c:\pkcs11-logger-x86.dll";
// Setup arguments passed to the C_Initialize function
if (AppType == AppType.MultiThreaded)
{
InitArgs40 = new LLA40.CK_C_INITIALIZE_ARGS();
InitArgs40.Flags = CKF.CKF_OS_LOCKING_OK;
InitArgs41 = new LLA41.CK_C_INITIALIZE_ARGS();
InitArgs41.Flags = CKF.CKF_OS_LOCKING_OK;
InitArgs80 = new LLA80.CK_C_INITIALIZE_ARGS();
InitArgs80.Flags = CKF.CKF_OS_LOCKING_OK;
InitArgs81 = new LLA81.CK_C_INITIALIZE_ARGS();
InitArgs81.Flags = CKF.CKF_OS_LOCKING_OK;
}
// Convert strings to byte arrays
SecurityOfficerPinArray = ConvertUtils.Utf8StringToBytes(SecurityOfficerPin);
NormalUserPinArray = ConvertUtils.Utf8StringToBytes(NormalUserPin);
ApplicationNameArray = ConvertUtils.Utf8StringToBytes(ApplicationName);
// Build PKCS#11 URI that identifies private key usable in signature creation tests
Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder();
pkcs11UriBuilder.ModulePath = Pkcs11LibraryPath;
pkcs11UriBuilder.Serial = TokenSerial;
pkcs11UriBuilder.Token = TokenLabel;
pkcs11UriBuilder.PinValue = NormalUserPin;
pkcs11UriBuilder.Type = CKO.CKO_PRIVATE_KEY;
pkcs11UriBuilder.Object = ApplicationName;
PrivateKeyUri = pkcs11UriBuilder.ToString();
}
/// <summary>
/// Returns path to PKCS11-MOCK library.
/// WARNING: It is not a real cryptographic module but just a dummy unmanaged PKCS#11 library designed specifically for unit testing of Pkcs11Interop library.
/// </summary>
/// <returns>Path to PKCS11-MOCK library</returns>
private static string GetPkcs11MockLibraryPath()
{
#if __ANDROID__
return @"libpkcs11-mock.so";
#elif __IOS__
return string.Empty;
#else
string path = typeof(Settings).Assembly.CodeBase;
path = new Uri(path).LocalPath;
path = Path.GetDirectoryName(path);
path = Path.Combine(path, "pkcs11-mock");
if (Platform.IsWindows)
{
path = Path.Combine(path, "windows");
path = Path.Combine(path, "pkcs11-mock-" + (Platform.Uses32BitRuntime ? "x86" : "x64") + ".dll");
}
else if (Platform.IsLinux)
{
path = Path.Combine(path, "linux");
path = Path.Combine(path, "pkcs11-mock-" + (Platform.Uses32BitRuntime ? "x86" : "x64") + ".so");
}
else if (Platform.IsMacOsX)
{
path = Path.Combine(path, "osx");
path = Path.Combine(path, "pkcs11-mock-" + (Platform.Uses32BitRuntime ? "x86" : "x64") + ".dylib");
}
return path;
#endif
}
}
}