13
13
// ----------------------------------------------------------------------------------
14
14
15
15
using System ;
16
+ using System . IO ;
16
17
using System . Security . Cryptography ;
17
18
using System . Security . Cryptography . X509Certificates ;
18
19
using Security . Cryptography ;
19
20
using Security . Cryptography . X509Certificates ;
20
- using System . IO ;
21
- using System . Runtime . InteropServices ;
22
- using System . ComponentModel ;
23
21
24
- namespace Microsoft . Azure . Commands . RecoveryServices . lib
22
+ namespace Microsoft . Azure . Commands . RecoveryServices
25
23
{
24
+ /// <summary>
25
+ /// Class to provide methods to manage the certificates.
26
+ /// </summary>
26
27
public static class CertUtils
27
28
{
28
- public const string MsEnhancedProv = "Microsoft Enhanced Cryptographic Provider v1.0" ; // MS_ENHANCED_PROV;
29
- private const string DefaultIssuer = "CN=Windows Azure Tools" ;
30
- private const string DefaultPassword = "" ;
29
+ /// <summary>
30
+ /// Enhancement provider
31
+ /// </summary>
32
+ private const string MsEnhancedProv = "Microsoft Enhanced Cryptographic Provider v1.0" ;
31
33
32
- public const string OIDClientAuthValue = "1.3.6.1.5.5.7.3.2" ;
33
- public const string OIDClientAuthFriendlyName = "Client Authentication" ;
34
- public const int KeySize2048 = 2048 ;
34
+ /// <summary>
35
+ /// Client Authentication Value
36
+ /// </summary>
37
+ private const string OIDClientAuthValue = "1.3.6.1.5.5.7.3.2" ;
35
38
36
39
/// <summary>
37
- /// Windows Azure Service Management API requires 2048bit RSA keys.
38
- /// The private key needs to be exportable so we can save it to .pfx for sharing with team members.
40
+ /// Client Authentication Friendly name
39
41
/// </summary>
40
- /// <returns>A 2048 bit RSA key</returns>
41
- private static CngKey Create2048RsaKey ( )
42
- {
43
- var keyCreationParameters = new CngKeyCreationParameters
44
- {
45
- ExportPolicy = CngExportPolicies . AllowExport ,
46
- KeyCreationOptions = CngKeyCreationOptions . None ,
47
- KeyUsage = CngKeyUsages . AllUsages ,
48
- Provider = new CngProvider ( MsEnhancedProv )
49
- } ;
42
+ private const string OIDClientAuthFriendlyName = "Client Authentication" ;
50
43
51
- keyCreationParameters . Parameters . Add ( new CngProperty ( "Length" , BitConverter . GetBytes ( KeySize2048 ) , CngPropertyOptions . None ) ) ;
44
+ /// <summary>
45
+ /// Key size
46
+ /// </summary>
47
+ private const int KeySize2048 = 2048 ;
52
48
53
- return CngKey . Create ( CngAlgorithm2 . Rsa , null , keyCreationParameters ) ;
54
- }
49
+ /// <summary>
50
+ /// default issuer name
51
+ /// </summary>
52
+ private const string DefaultIssuer = "CN=Windows Azure Tools" ;
55
53
56
54
/// <summary>
57
- /// Method to generate a self signed certifficate
55
+ /// default password.
56
+ /// </summary>
57
+ private const string DefaultPassword = "" ;
58
+
59
+ /// <summary>
60
+ /// Method to generate a self signed certificate
58
61
/// </summary>
59
62
/// <param name="validForHours">number of hours for which the certificate is valid.</param>
60
63
/// <param name="subscriptionId">subscriptionId in question</param>
61
64
/// <param name="certificateNamePrefix">prefix for the certificate name</param>
62
65
/// <param name="issuer">issuer for the certificate</param>
63
- /// <param name="password">certificate passwor </param>
66
+ /// <param name="password">certificate password </param>
64
67
/// <returns>certificate as an object</returns>
65
68
public static X509Certificate2 CreateSelfSignedCertificate (
66
69
int validForHours ,
@@ -103,18 +106,7 @@ public static X509Certificate2 CreateSelfSignedCertificate(
103
106
var bytes = cert . Export ( X509ContentType . Pfx , password ) ;
104
107
105
108
// PfxValidation is not done here because these are newly created certs and assumed valid.
106
- return NewX509Certificate2 ( bytes , password , X509KeyStorageFlags . PersistKeySet | X509KeyStorageFlags . Exportable , doPfxValidation : false ) ;
107
- }
108
-
109
- /// <summary>
110
- /// Generates friendly name
111
- /// </summary>
112
- /// <param name="subscriptionId">subscription id</param>
113
- /// <param name="prefix">prefix, likely resource name</param>
114
- /// <returns>friendly name</returns>
115
- private static string GenerateCertFriendlyName ( string subscriptionId , string prefix = "" )
116
- {
117
- return string . Format ( "{0}{1}-{2}-vaultcredentials" , prefix , subscriptionId , DateTime . Now . ToString ( "M-d-yyyy" ) ) ;
109
+ return NewX509Certificate2 ( bytes , password , X509KeyStorageFlags . PersistKeySet | X509KeyStorageFlags . Exportable , shouldValidatePfx : false ) ;
118
110
}
119
111
120
112
/// <summary>
@@ -123,9 +115,9 @@ private static string GenerateCertFriendlyName(string subscriptionId, string pre
123
115
/// <param name="rawData">The bytes that represent the certificate</param>
124
116
/// <param name="password">The certificate private password</param>
125
117
/// <param name="keyStorageFlags">The certificate loading options</param>
126
- /// <param name="doPfxValidation ">Flag to indicate if pfx file should validated. Set to true if the rawData is retrieved from an untrusted source.</param>
118
+ /// <param name="shouldValidatePfx ">Flag to indicate if file should validated. Set to true if the rawData is retrieved from an untrusted source.</param>
127
119
/// <returns>An instance of the X509Certificate</returns>
128
- public static X509Certificate2 NewX509Certificate2 ( byte [ ] rawData , string password , X509KeyStorageFlags keyStorageFlags , bool doPfxValidation )
120
+ public static X509Certificate2 NewX509Certificate2 ( byte [ ] rawData , string password , X509KeyStorageFlags keyStorageFlags , bool shouldValidatePfx )
129
121
{
130
122
string temporaryFileName = Path . GetTempFileName ( ) ;
131
123
@@ -147,5 +139,36 @@ public static X509Certificate2 NewX509Certificate2(byte[] rawData, string passwo
147
139
}
148
140
}
149
141
}
142
+
143
+ /// <summary>
144
+ /// Generates friendly name
145
+ /// </summary>
146
+ /// <param name="subscriptionId">subscription id</param>
147
+ /// <param name="prefix">prefix, likely resource name</param>
148
+ /// <returns>friendly name</returns>
149
+ private static string GenerateCertFriendlyName ( string subscriptionId , string prefix = "" )
150
+ {
151
+ return string . Format ( "{0}{1}-{2}-vaultcredentials" , prefix , subscriptionId , DateTime . Now . ToString ( "M-d-yyyy" ) ) ;
152
+ }
153
+
154
+ /// <summary>
155
+ /// Windows Azure Service Management API requires 2048bit RSA keys.
156
+ /// The private key needs to be exportable so we can save it for sharing with team members.
157
+ /// </summary>
158
+ /// <returns>A 2048 bit RSA key</returns>
159
+ private static CngKey Create2048RsaKey ( )
160
+ {
161
+ var keyCreationParameters = new CngKeyCreationParameters
162
+ {
163
+ ExportPolicy = CngExportPolicies . AllowExport ,
164
+ KeyCreationOptions = CngKeyCreationOptions . None ,
165
+ KeyUsage = CngKeyUsages . AllUsages ,
166
+ Provider = new CngProvider ( MsEnhancedProv )
167
+ } ;
168
+
169
+ keyCreationParameters . Parameters . Add ( new CngProperty ( "Length" , BitConverter . GetBytes ( KeySize2048 ) , CngPropertyOptions . None ) ) ;
170
+
171
+ return CngKey . Create ( CngAlgorithm2 . Rsa , null , keyCreationParameters ) ;
172
+ }
150
173
}
151
174
}
0 commit comments