-
Notifications
You must be signed in to change notification settings - Fork 55
/
AsymmetricAlgorithmCmnFunc.cs
299 lines (278 loc) · 11.7 KB
/
AsymmetricAlgorithmCmnFunc.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//**********************************************************************************
//* Copyright (C) 2007,2016 Hitachi Solutions,Ltd.
//**********************************************************************************
#region Apache License
//
// 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.
//
#endregion
//**********************************************************************************
//* クラス名 :RsaAndDsaCmnFunc
//* クラス日本語名 :RsaAndDsaCmnFuncクラス
//*
//* 作成者 :生技 西野
//* 更新履歴 :
//*
//* 日時 更新者 内容
//* ---------- ---------------- -------------------------------------------------
//* 2017/12/25 西野 大介 新規作成
//* 2018/11/09 西野 大介 RSAOpenSsl、DSAOpenSsl、HashAlgorithmName対応
//* 2019/01/29 西野 大介 X.509対応(JWE(RSAES-OAEP and AES GCM)対応
//* 2019/06/12 西野 大介 constructorのX509KeyStorageFlagsの既定値を変更
//**********************************************************************************
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Touryo.Infrastructure.Public.Util;
namespace Touryo.Infrastructure.Public.Security
{
/// <summary>
/// AsymmetricAlgorithmCmnFuncクラス
/// - RSACryptoServiceProvider:
/// MD5, SHA1, SHA256, SHA384, SHA512
/// - DSACryptoServiceProvider:SHA1
/// だけ、サポート。
/// </summary>
public class AsymmetricAlgorithmCmnFunc
{
#region Simple factories
/// <summary>RsaFactory</summary>
/// <returns>RSA</returns>
public static RSA RsaFactory()
{
// ハンドリングをRSA(ベースの型に)に変更したので行けるハズ
return RSA.Create();
}
/// <summary>DsaFactory</summary>
/// <returns>DSA</returns>
public static DSA DsaFactory()
{
// ハンドリングをDSA(ベースの型に)に変更したので行けるハズ
return DSA.Create();
}
/// <summary>CreateSameKeySizeSP</summary>
/// <param name="aa1">AsymmetricAlgorithm</param>
/// <returns>AsymmetricAlgorithm(RSA or DSA)</returns>
public static AsymmetricAlgorithm CreateSameKeySizeSP(AsymmetricAlgorithm aa1)
{
AsymmetricAlgorithm aa2 = (AsymmetricAlgorithm)Activator.CreateInstance(aa1.GetType());
aa2.KeySize = aa1.KeySize;
return aa2;
}
#endregion
#region CreateCryptographySP
/// <summary>対称アルゴリズム暗号化サービスプロバイダ生成</summary>
/// <param name="easa">EnumASymmetricAlgorithm</param>
/// <param name="certificateFilePath">X.509証明書(*.pfx, *.cer)へのパス</param>
/// <param name="password">パスワード</param>
/// <param name="flag">X509KeyStorageFlags</param>
/// <returns>AsymmetricAlgorithm</returns>
public static AsymmetricAlgorithm CreateCryptographySP(
EnumASymmetricAlgorithm easa, string certificateFilePath = "", string password = "",
X509KeyStorageFlags flag = X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet)
{
AsymmetricAlgorithm asa = null;
if (easa == EnumASymmetricAlgorithm.X509)
{
// X.509対応
X509Certificate2 x509Key = new X509Certificate2(certificateFilePath, password, flag);
if (string.IsNullOrEmpty(password))
{
asa = x509Key.PublicKey.Key;
}
else
{
asa = x509Key.PrivateKey;
}
}
else
{
if (easa == EnumASymmetricAlgorithm.RsaCsp)
{
// RSACryptoServiceProviderサービスプロバイダ
asa = RSACryptoServiceProvider.Create(); // devps(1703)
}
#if !NET45
else if (easa == EnumASymmetricAlgorithm.RsaCng)
{
// RSACngサービスプロバイダ
asa = RSACng.Create(); // devps(1703)
}
#endif
#if NETSTD
else if (easa == EnumASymmetricAlgorithm.RsaOpenSsl)
{
// RSAOpenSslサービスプロバイダ
asa = RSAOpenSsl.Create(); // devps(1703)
}
#endif
}
return asa;
}
#endregion
#region CreateDigitalSignSP
/// <summary>署名・検証サービスプロバイダの生成</summary>
/// <param name="eaa">EnumDigitalSignAlgorithm</param>
/// <param name="aa">
/// AsymmetricAlgorithm
/// - RSACryptoServiceProvider
/// - DSACryptoServiceProvider
/// </param>
/// <param name="ha">
/// HashAlgorithm
/// </param>
public static void CreateDigitalSignSP(
EnumDigitalSignAlgorithm eaa, out AsymmetricAlgorithm aa, out HashAlgorithm ha)
{
aa = null;
ha = null;
// 公開鍵・暗号化サービスプロバイダ
if (eaa == EnumDigitalSignAlgorithm.RsaCSP_MD5
|| eaa == EnumDigitalSignAlgorithm.RsaCSP_SHA1
|| eaa == EnumDigitalSignAlgorithm.RsaCSP_SHA256
|| eaa == EnumDigitalSignAlgorithm.RsaCSP_SHA384
|| eaa == EnumDigitalSignAlgorithm.RsaCSP_SHA512)
{
// RSACryptoServiceProviderサービスプロバイダ
aa = new RSACryptoServiceProvider();
switch (eaa)
{
case EnumDigitalSignAlgorithm.RsaCSP_MD5:
ha = MD5.Create();
break;
case EnumDigitalSignAlgorithm.RsaCSP_SHA1:
ha = SHA1.Create();
break;
case EnumDigitalSignAlgorithm.RsaCSP_SHA256:
ha = SHA256.Create();
break;
case EnumDigitalSignAlgorithm.RsaCSP_SHA384:
ha = SHA384.Create();
break;
case EnumDigitalSignAlgorithm.RsaCSP_SHA512:
ha = SHA512.Create();
break;
}
}
#if NETSTD
else if (eaa == EnumDigitalSignAlgorithm.RsaOpenSsl_MD5
|| eaa == EnumDigitalSignAlgorithm.RsaOpenSsl_SHA1
|| eaa == EnumDigitalSignAlgorithm.RsaOpenSsl_SHA256
|| eaa == EnumDigitalSignAlgorithm.RsaOpenSsl_SHA384
|| eaa == EnumDigitalSignAlgorithm.RsaOpenSsl_SHA512)
{
// RSAOpenSslサービスプロバイダ
aa = new RSAOpenSsl();
switch (eaa)
{
case EnumDigitalSignAlgorithm.RsaOpenSsl_MD5:
ha = MD5.Create();
break;
case EnumDigitalSignAlgorithm.RsaOpenSsl_SHA1:
ha = SHA1.Create();
break;
case EnumDigitalSignAlgorithm.RsaOpenSsl_SHA256:
ha = SHA256.Create();
break;
case EnumDigitalSignAlgorithm.RsaOpenSsl_SHA384:
ha = SHA384.Create();
break;
case EnumDigitalSignAlgorithm.RsaOpenSsl_SHA512:
ha = SHA512.Create();
break;
}
}
#endif
else if (eaa == EnumDigitalSignAlgorithm.DsaCSP_SHA1)
{
// DSACryptoServiceProvider
aa = new DSACryptoServiceProvider();
ha = SHA1.Create();
}
#if NETSTD
else if (eaa == EnumDigitalSignAlgorithm.DsaOpenSsl_SHA1)
{
// DSAOpenSslサービスプロバイダ
aa = new DSAOpenSsl();
ha = SHA1.Create();
}
#endif
else if (
eaa == EnumDigitalSignAlgorithm.ECDsaCng_P256
|| eaa == EnumDigitalSignAlgorithm.ECDsaCng_P384
|| eaa == EnumDigitalSignAlgorithm.ECDsaCng_P521)
{
// ECDsaCngはCngKeyが土台で、
// ECDsaCng生成後にオプションとして設定するのではなく
// CngKeyの生成時にCngAlgorithmの指定が必要であるもよう。
CngAlgorithm cngAlgorithm = null;
switch (eaa)
{
case EnumDigitalSignAlgorithm.ECDsaCng_P256:
cngAlgorithm = CngAlgorithm.ECDsaP256;
break;
case EnumDigitalSignAlgorithm.ECDsaCng_P384:
cngAlgorithm = CngAlgorithm.ECDsaP384;
break;
case EnumDigitalSignAlgorithm.ECDsaCng_P521:
cngAlgorithm = CngAlgorithm.ECDsaP521;
break;
}
aa = new ECDsaCng(CngKey.Create(cngAlgorithm));
ha = null; // ハッシュ無し
}
#if NETSTD
else if (
eaa == EnumDigitalSignAlgorithm.ECDsaOpenSsl_P256
|| eaa == EnumDigitalSignAlgorithm.ECDsaOpenSsl_P384
|| eaa == EnumDigitalSignAlgorithm.ECDsaOpenSsl_P521)
{
ECCurve eCCurve = ECCurve.NamedCurves.nistP256;
ECParameters eCParameters;
ECDsa eCDsa = null;
ECDsaOpenSsl eCDsaOpenSsl = null;
switch (eaa)
{
case EnumDigitalSignAlgorithm.ECDsaOpenSsl_P256:
eCCurve = ECCurve.NamedCurves.nistP256;
break;
case EnumDigitalSignAlgorithm.ECDsaOpenSsl_P384:
eCCurve = ECCurve.NamedCurves.nistP384;
break;
case EnumDigitalSignAlgorithm.ECDsaOpenSsl_P521:
eCCurve = ECCurve.NamedCurves.nistP521;
break;
}
// https://qiita.com/yoship1639/items/6dd0cc8623d7f3969d78
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
eCDsa = ECDsa.Create(); // ECDsaOpenSslと思われる。
eCDsa.GenerateKey(eCCurve);
eCParameters = eCDsa.ExportParameters(true);
eCDsaOpenSsl = new ECDsaOpenSsl(eCParameters.Curve);
eCDsaOpenSsl.ImportParameters(eCParameters);
}
aa = eCDsaOpenSsl;
ha = null; // ハッシュ無し
}
#endif
else
{
throw new ArgumentException(
PublicExceptionMessage.ARGUMENT_INCORRECT,
"EnumDigitalSignAlgorithm parameter is incorrect.");
}
}
#endregion
}
}