mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathProperties.cs
More file actions
217 lines (167 loc) · 9.47 KB
/
Copy pathProperties.cs
File metadata and controls
217 lines (167 loc) · 9.47 KB
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
using System;
using System.Collections.Generic;
using System.Threading;
namespace Org.BouncyCastle.Utilities
{
/// <summary>Utility methods for managing properties.</summary>
/// <remarks>Properties may be thread properties, managed by this class, or environment variables
/// visible only when the corresponding thread property is not set. This API has no facilities for modifying
/// environment variables, though it may read them.
/// </remarks>
public static class Properties
{
public static readonly string Asn1AllowUnsafeInteger = "Org.BouncyCastle.Asn1.AllowUnsafeInteger";
public static readonly string Asn1MaxDepth = "Org.BouncyCastle.Asn1.MaxDepth";
public static readonly string Asn1MaxLimit = "Org.BouncyCastle.Asn1.MaxLimit";
public static readonly string DHMaxSize = "Org.BouncyCastle.DH.MaxSize";
public static readonly string DsaMaxSize = "Org.BouncyCastle.Dsa.MaxSize";
public static readonly string ECF2mMaxSize = "Org.BouncyCastle.EC.F2m_MaxSize";
public static readonly string ECFpMaxSize = "Org.BouncyCastle.EC.Fp_MaxSize";
public static readonly string ECFpCertainty = "Org.BouncyCastle.EC.Fp_Certainty";
public static readonly string FpeDisable = "Org.BouncyCastle.Fpe.Disable";
public static readonly string FpeDisableFf1 = "Org.BouncyCastle.Fpe.Disable_Ff1";
/// <summary>
/// Upper bound on the PBKDF2 iteration count honoured when decrypting a PBES2-protected PKCS#8/PEM private key.
/// </summary>
/// <remarks>
/// The key-derivation parameters travel inside the (unauthenticated) encrypted-key container, so an unbounded
/// count makes decrypting attacker-supplied key material a CPU-exhaustion vector. Default 10,000,000.
/// </remarks>
public static readonly string PbeMaxIterationCount = "Org.BouncyCastle.Pbe.MaxIterationCount";
/// <summary>
/// When set to <c>true</c>, suppresses the error raised when loading a PKCS12 store with a password, for data
/// that does not require a password.
/// </summary>
public static readonly string Pkcs12IgnoreUselessPassword = "Org.BouncyCastle.Pkcs12.IgnoreUselessPassword";
/// <summary>If set, a PKCS12 file with a larger iteration count on PBE processing will be rejected.</summary>
public static readonly string Pkcs12MaxIterationCount = "Org.BouncyCastle.Pkcs12.MaxIterationCount";
public static readonly string Pkcs1NotStrict = "Org.BouncyCastle.Pkcs1.NotStrict";
/// <summary>
/// Upper bound on the RFC 4211 PKMAC / CMP password-based-MAC iteration count honoured when no explicit ceiling
/// is supplied.
/// </summary>
/// <remarks>
/// The count travels in the (unauthenticated) PBMParameter of an incoming CMP message and drives an iterated
/// hash, so an unbounded count makes verifying an attacker-supplied message a CPU-exhaustion vector. Default
/// 10,000,000.
/// </remarks>
public static readonly string PKMacMaxIterationCount = "Org.BouncyCastle.PKMac.MaxIterationCount";
public static readonly string RsaAllowUnsafeModulus = "Org.BouncyCastle.Rsa.AllowUnsafeModulus";
public static readonly string RsaMaxMRTests = "Org.BouncyCastle.Rsa.MaxMRTests";
public static readonly string RsaMaxSize = "Org.BouncyCastle.Rsa.MaxSize";
public static readonly string X509AllowNonDerTbsCertificate = "Org.BouncyCastle.X509.Allow_Non-DER_TBSCert";
public static readonly string X509MaxPolicyNodes = "Org.BouncyCastle.X509.MaxPolicyNodes";
private static readonly ThreadLocal<Dictionary<string, string>> ThreadProperties =
new ThreadLocal<Dictionary<string, string>>();
public static void Clear() => ThreadProperties.Value = null;
public static void ClearThreadProperties() => ThreadProperties.Value?.Clear();
public static bool GetBoolean(string propertyName, bool defaultValue) =>
TryGetBoolean(propertyName, out bool propertyValue) ? propertyValue : defaultValue;
public static int GetInt32(string propertyName, int defaultValue) =>
TryGetInt32(propertyName, out int propertyValue) ? propertyValue : defaultValue;
public static long GetInt64(string propertyName, long defaultValue) =>
TryGetInt64(propertyName, out long propertyValue) ? propertyValue : defaultValue;
/// <summary>
/// Return the <c>string</c> value of the property <paramref name="name"/>.
/// </summary>
/// <remarks>
/// Property evaluation order is thread properties first, then environment variables.
/// </remarks>
/// <param name="name">The name of the property.</param>
/// <returns>
/// The <c>string</c> value of the <paramref name="name"/> property, or <c>null</c> if not defined.
/// </returns>
public static string GetProperty(string name) =>
GetThreadProperty(name) ?? Platform.GetEnvironmentVariable(name);
/// <summary>
/// Return the <c>string</c> value of the property <paramref name="name"/>, or
/// <paramref name="defaultValue"/> if that property is not defined.
/// </summary>
/// <remarks>
/// Property evaluation order is thread properties first, then environment variables.
/// </remarks>
/// <param name="name">The name of the property.</param>
/// <param name="defaultValue">The default value to return in case the property is not defined.</param>
/// <returns>
/// The <c>string</c> value of the <paramref name="name"/> property, or <paramref name="defaultValue"/>
/// if not defined.
/// </returns>
public static string GetProperty(string name, string defaultValue) => GetProperty(name) ?? defaultValue;
public static string GetThreadProperty(string name)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
var threadProperties = ThreadProperties.Value;
if (threadProperties != null && threadProperties.TryGetValue(name, out var value))
return value;
return null;
}
public static bool RemoveThreadProperty(string name)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
var threadProperties = ThreadProperties.Value;
if (threadProperties != null)
return threadProperties.Remove(name);
return false;
}
public static void SetThreadBoolean(string propertyName, bool propertyValue) =>
SetThreadProperty(propertyName, propertyValue.ToString());
public static void SetThreadInt32(string propertyName, int propertyValue) =>
SetThreadProperty(propertyName, propertyValue.ToString());
public static void SetThreadInt64(string propertyName, long propertyValue) =>
SetThreadProperty(propertyName, propertyValue.ToString());
public static void SetThreadProperty(string name, string value)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
if (value == null)
throw new ArgumentNullException(nameof(value));
var threadProperties = ThreadProperties.Value ?? InitThreadProperties();
threadProperties[name] = value;
}
public static bool TryGetBoolean(string propertyName, out bool propertyValue) =>
bool.TryParse(GetProperty(propertyName), out propertyValue);
public static bool TryGetInt32(string propertyName, out int propertyValue) =>
int.TryParse(GetProperty(propertyName), out propertyValue);
public static bool TryGetInt64(string propertyName, out long propertyValue) =>
long.TryParse(GetProperty(propertyName), out propertyValue);
public static void WithThreadProperty(string name, string value, Action action) =>
WithThreadProperty<object, object>(name, value, arg: null, (object ignore) => { action(); return null; });
public static TResult WithThreadProperty<TResult>(string name, string value, Func<TResult> func) =>
WithThreadProperty<object, TResult>(name, value, arg: null, (object ignore) => func());
public static TResult WithThreadProperty<TArg, TResult>(string name, string value, TArg arg,
Func<TArg, TResult> func)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
if (value == null)
throw new ArgumentNullException(nameof(value));
if (func == null)
throw new ArgumentNullException(nameof(func));
string previousValue = GetThreadProperty(name);
SetThreadProperty(name, value);
try
{
return func.Invoke(arg);
}
finally
{
if (previousValue == null)
{
RemoveThreadProperty(name);
}
else
{
SetThreadProperty(name, previousValue);
}
}
}
private static Dictionary<string, string> InitThreadProperties()
{
var threadProperties = new Dictionary<string, string>();
ThreadProperties.Value = threadProperties;
return threadProperties;
}
}
}