@@ -17,15 +17,35 @@ public abstract class TestCrypto
17
17
protected readonly byte [ ] _bytes ;
18
18
protected readonly string _pemStringFull ;
19
19
20
+ /// <summary>
21
+ /// Initializes a new instance of TestCrypto with PEM-encoded data from a file.
22
+ /// </summary>
23
+ /// <param name="filePath">Path to the PEM file containing cryptographic data.</param>
20
24
protected TestCrypto ( string filePath )
21
25
{
22
- var pemString = File . ReadAllText ( filePath ) ;
23
- _pemStringFull = pemString . Replace ( "\n " , "" ) . Trim ( ) ;
26
+ _pemStringFull = File
27
+ . ReadAllText ( filePath )
28
+ . Replace ( "\n " , "" )
29
+ . Trim ( ) ;
24
30
_bytes = GetBytesFromPem ( _pemStringFull ) ;
25
31
}
26
32
33
+ /// <summary>
34
+ /// Returns the raw byte representation of the cryptographic data.
35
+ /// </summary>
36
+ /// <returns>Byte array containing the decoded cryptographic data.</returns>
27
37
public byte [ ] AsRawBytes ( ) => _bytes ;
28
- public string AsPem ( ) => _pemStringFull ;
38
+
39
+ /// <summary>
40
+ /// Returns the complete PEM-encoded string representation.
41
+ /// </summary>
42
+ /// <returns>String containing the full PEM data including headers and footers.</returns>
43
+ public string AsPemString ( ) => _pemStringFull ;
44
+
45
+ /// <summary>
46
+ /// Returns the Base64-encoded data without PEM headers and footers.
47
+ /// </summary>
48
+ /// <returns>Base64 string of the cryptographic data.</returns>
29
49
public string AsBase64 ( ) => StripPemHeaderFooter ( _pemStringFull ) ;
30
50
31
51
private static byte [ ] GetBytesFromPem ( string pemData )
@@ -53,17 +73,32 @@ private static string StripPemHeaderFooter(string pemData)
53
73
}
54
74
}
55
75
76
+ /// <summary>
77
+ /// Represents a cryptographic key for testing purposes, supporting both RSA and EC keys.
78
+ /// Provides conversion methods to standard .NET cryptographic types.
79
+ /// </summary>
56
80
public class TestKey : TestCrypto
57
81
{
58
82
private readonly string _curve ;
59
83
private readonly bool _isPrivate ;
60
84
85
+ /// <summary>
86
+ /// Loads a test key from the TestData directory.
87
+ /// </summary>
88
+ /// <param name="curve">The curve or key type (e.g., "rsa2048", "secp256r1")</param>
89
+ /// <param name="isPrivate">True for private key, false for public key</param>
90
+ /// <returns>A TestKey instance representing the loaded key</returns>
61
91
private TestKey ( string filePath , string curve , bool isPrivate ) : base ( filePath )
62
92
{
63
93
_curve = curve ;
64
94
_isPrivate = isPrivate ;
65
95
}
66
96
97
+ /// <summary>
98
+ /// Converts the key to an RSA instance if it represents an RSA key.
99
+ /// </summary>
100
+ /// <returns>RSA instance initialized with the key data</returns>
101
+ /// <exception cref="InvalidOperationException">Thrown if the key is not an RSA key</exception>
67
102
public RSA AsRSA ( )
68
103
{
69
104
if ( ! _curve . StartsWith ( "rsa" , StringComparison . OrdinalIgnoreCase ) )
@@ -77,6 +112,11 @@ public RSA AsRSA()
77
112
return rsa ;
78
113
}
79
114
115
+ /// <summary>
116
+ /// Converts the key to an ECDsa instance if it represents an EC key.
117
+ /// </summary>
118
+ /// <returns>ECDsa instance initialized with the key data</returns>
119
+ /// <exception cref="InvalidOperationException">Thrown if the key is not an EC key</exception>
80
120
public ECDsa AsECDsa ( )
81
121
{
82
122
if ( _curve . StartsWith ( "rsa" , StringComparison . OrdinalIgnoreCase ) )
@@ -90,49 +130,83 @@ public ECDsa AsECDsa()
90
130
return ecdsa ;
91
131
}
92
132
133
+ /// <summary>
134
+ /// Converts the key to a PIV private key format.
135
+ /// </summary>
136
+ /// <returns>PivPrivateKey instance</returns>
93
137
public static TestKey Load ( string curve , bool isPrivate )
94
138
{
95
139
var fileName = $ "{ curve } _{ ( isPrivate ? "private" : "public" ) } .pem";
96
140
var filePath = Path . Combine ( "TestData" , fileName ) ;
97
141
return new TestKey ( filePath , curve , isPrivate ) ;
98
142
}
99
-
100
- internal PivPrivateKey AsPrivateKey ( )
101
- {
102
- return new KeyConverter ( _pemStringFull ) . GetPivPrivateKey ( ) ;
103
- }
104
-
105
- internal PivPublicKey AsPublicKey ( )
106
- {
107
- return new KeyConverter ( _pemStringFull ) . GetPivPublicKey ( ) ;
108
- }
109
143
}
110
144
145
+ /// <summary>
146
+ /// Represents an X.509 certificate for testing purposes.
147
+ /// Supports both regular and attestation certificates.
148
+ /// </summary>
111
149
public class TestCertificate : TestCrypto
112
150
{
151
+ /// <summary>
152
+ /// Indicates whether this certificate is an attestation certificate.
153
+ /// </summary>
113
154
public readonly bool IsAttestation ;
114
155
115
156
private TestCertificate ( string filePath , bool isAttestation ) : base ( filePath )
116
157
{
117
158
IsAttestation = isAttestation ;
118
159
}
119
160
161
+ /// <summary>
162
+ /// Converts the certificate to an X509Certificate2 instance.
163
+ /// </summary>
164
+ /// <returns>X509Certificate2 instance initialized with the certificate data</returns>
120
165
public X509Certificate2 AsX509Certificate2 ( )
121
166
{
122
167
return new X509Certificate2 ( _bytes ) ;
123
168
}
124
169
170
+ /// <summary>
171
+ /// Loads a certificate from the TestData directory.
172
+ /// </summary>
173
+ /// <param name="curve">The curve or key type associated with the certificate</param>
174
+ /// <param name="isAttestation">True if loading an attestation certificate</param>
175
+ /// <returns>A TestCertificate instance</returns>
125
176
public static TestCertificate Load ( string curve , bool isAttestation = false )
126
177
{
127
- string fileName = $ "{ curve } _cert{ ( isAttestation ? "_attest" : "" ) } .pem";
128
- string filePath = Path . Combine ( "TestData" , fileName ) ;
178
+ var fileName = $ "{ curve } _cert{ ( isAttestation ? "_attest" : "" ) } .pem";
179
+ var filePath = Path . Combine ( "TestData" , fileName ) ;
129
180
return new TestCertificate ( filePath , isAttestation ) ;
130
181
}
131
182
}
132
183
184
+ /// <summary>
185
+ /// Provides convenient static methods to access test keys and certificates.
186
+ /// </summary>
133
187
public static class TestKeys
134
188
{
135
- public static TestKey GetKey ( string curve , bool isPrivate ) => TestKey . Load ( curve , isPrivate ) ;
189
+
190
+ /// <summary>
191
+ /// Gets a private key for the specified curve.
192
+ /// </summary>
193
+ /// <param name="curve">The curve or key type</param>
194
+ /// <returns>TestKey instance representing the private key</returns>
195
+ public static TestKey GetPrivateKey ( string curve ) => TestKey . Load ( curve , true ) ;
196
+
197
+ /// <summary>
198
+ /// Gets a public key for the specified curve.
199
+ /// </summary>
200
+ /// <param name="curve">The curve or key type</param>
201
+ /// <returns>TestKey instance representing the public key</returns>
202
+ public static TestKey GetPublicKey ( string curve ) => TestKey . Load ( curve , false ) ;
203
+
204
+ /// <summary>
205
+ /// Gets a certificate for the specified curve.
206
+ /// </summary>
207
+ /// <param name="curve">The curve or key type</param>
208
+ /// <param name="isAttestation">True to get an attestation certificate</param>
209
+ /// <returns>TestCertificate instance</returns>s
136
210
public static TestCertificate GetCertificate ( string curve , bool isAttestation = false ) =>
137
211
TestCertificate . Load ( curve , isAttestation ) ;
138
212
}
0 commit comments