@@ -26,6 +26,7 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network
26
26
using Routes . Model ;
27
27
using System ;
28
28
using System . Collections . Generic ;
29
+ using System . IO ;
29
30
using System . Linq ;
30
31
using System . Management . Automation ;
31
32
using System . Security . Cryptography . X509Certificates ;
@@ -188,12 +189,9 @@ public ApplicationGatewayOperationResponse ExecuteApplicationGatewayOperation(st
188
189
189
190
public ApplicationGatewayOperationResponse AddApplicationGatewayCertificate ( string gatewayName , string certificateName , string password , string certificateFile )
190
191
{
191
- X509Certificate2 cert = new X509Certificate2 ( certificateFile , password , X509KeyStorageFlags . Exportable ) ;
192
-
193
192
ApplicationGatewayCertificate appGwCert = new ApplicationGatewayCertificate ( )
194
193
{
195
- Data = Convert . ToBase64String ( cert . Export ( X509ContentType . Pfx , password ) ) ,
196
- //CertificateFormat = "pfx",
194
+ Data = Convert . ToBase64String ( File . ReadAllBytes ( certificateFile ) ) ,
197
195
Password = password
198
196
} ;
199
197
@@ -203,17 +201,52 @@ public ApplicationGatewayOperationResponse AddApplicationGatewayCertificate(stri
203
201
public PowerShellAppGwModel . ApplicationGatewayCertificate GetApplicationGatewayCertificate ( string gatewayName , string certificateName )
204
202
{
205
203
ApplicationGatewayGetCertificate certificate = client . ApplicationGateways . GetCertificate ( gatewayName , certificateName ) ;
206
- X509Certificate2 certObject = new X509Certificate2 ( Convert . FromBase64String ( certificate . Data ) ) ;
204
+ X509Certificate2Collection certCollection = new X509Certificate2Collection ( ) ;
205
+ certCollection . Import ( Convert . FromBase64String ( certificate . Data ) ) ;
206
+
207
+ X509Certificate2 certToReturn = null ;
208
+ // We need to return the first non-CA cert.
209
+ // If there is no non-CA cert, return the first cert in the collection.
210
+ foreach ( var certObject in certCollection )
211
+ {
212
+ // Remember first cert in collection
213
+ if ( certToReturn == null )
214
+ {
215
+ certToReturn = certObject ;
216
+ }
217
+ // Non-CA cert, so this is the one we want
218
+ if ( ! IsCACert ( certObject ) )
219
+ {
220
+ certToReturn = certObject ;
221
+ break ;
222
+ }
223
+ }
224
+
207
225
return ( new PowerShellAppGwModel . ApplicationGatewayCertificate
208
226
{
209
227
Name = certificate . Name ,
210
- SubjectName = certObject . SubjectName . Name ,
211
- Thumbprint = certObject . Thumbprint ,
212
- ThumbprintAlgo = certObject . SignatureAlgorithm . FriendlyName ,
228
+ SubjectName = certToReturn . SubjectName . Name ,
229
+ Thumbprint = certToReturn . Thumbprint ,
230
+ ThumbprintAlgo = certToReturn . SignatureAlgorithm . FriendlyName ,
213
231
State = certificate . State
214
232
} ) ;
215
233
}
216
234
235
+ private static bool IsCACert ( X509Certificate2 cert )
236
+ {
237
+ const string BasicConstraintsOid = "2.5.29.19" ;
238
+ foreach ( var extension in cert . Extensions )
239
+ {
240
+ if ( extension . Oid . Value == BasicConstraintsOid )
241
+ {
242
+ X509BasicConstraintsExtension ext = ( X509BasicConstraintsExtension ) extension ;
243
+ return ext . CertificateAuthority ;
244
+ }
245
+ }
246
+
247
+ return false ;
248
+ }
249
+
217
250
public List < PowerShellAppGwModel . ApplicationGatewayCertificate > ListApplicationGatewayCertificate ( string gatewayName )
218
251
{
219
252
ApplicationGatewayListCertificate hydraCertList = client . ApplicationGateways . ListCertificate ( gatewayName ) ;
0 commit comments