Skip to content

Commit 1a5e131

Browse files
authored
Merge pull request #3384 from vinatara/dev
Reading and passing the entire pfx file so that we also get the certi…
2 parents 3af2cf4 + d28891a commit 1a5e131

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

src/ResourceManager/Network/Commands.Network/ApplicationGateway/SslCertificate/AzureApplicationGatewaySslCertificateBase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Azure.Commands.Network.Models;
1616
using System;
17+
using System.IO;
1718
using System.Management.Automation;
1819
using System.Security.Cryptography.X509Certificates;
1920

@@ -41,12 +42,10 @@ public class AzureApplicationGatewaySslCertificateBase : NetworkBaseCmdlet
4142

4243
public PSApplicationGatewaySslCertificate NewObject()
4344
{
44-
X509Certificate2 cert = new X509Certificate2(CertificateFile, Password, X509KeyStorageFlags.Exportable);
45-
4645
var sslCertificate = new PSApplicationGatewaySslCertificate();
4746

4847
sslCertificate.Name = this.Name;
49-
sslCertificate.Data = Convert.ToBase64String(cert.Export(X509ContentType.Pfx, Password));
48+
sslCertificate.Data = Convert.ToBase64String(File.ReadAllBytes(CertificateFile));
5049
sslCertificate.Password = this.Password;
5150
sslCertificate.Id =
5251
ApplicationGatewayChildResourceHelper.GetResourceNotSetId(

src/ServiceManagement/Network/Commands.Network/NetworkClient.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network
2626
using Routes.Model;
2727
using System;
2828
using System.Collections.Generic;
29+
using System.IO;
2930
using System.Linq;
3031
using System.Management.Automation;
3132
using System.Security.Cryptography.X509Certificates;
@@ -188,12 +189,9 @@ public ApplicationGatewayOperationResponse ExecuteApplicationGatewayOperation(st
188189

189190
public ApplicationGatewayOperationResponse AddApplicationGatewayCertificate(string gatewayName, string certificateName, string password, string certificateFile)
190191
{
191-
X509Certificate2 cert = new X509Certificate2(certificateFile, password, X509KeyStorageFlags.Exportable);
192-
193192
ApplicationGatewayCertificate appGwCert = new ApplicationGatewayCertificate()
194193
{
195-
Data = Convert.ToBase64String(cert.Export(X509ContentType.Pfx, password)),
196-
//CertificateFormat = "pfx",
194+
Data = Convert.ToBase64String(File.ReadAllBytes(certificateFile)),
197195
Password = password
198196
};
199197

@@ -203,17 +201,52 @@ public ApplicationGatewayOperationResponse AddApplicationGatewayCertificate(stri
203201
public PowerShellAppGwModel.ApplicationGatewayCertificate GetApplicationGatewayCertificate(string gatewayName, string certificateName)
204202
{
205203
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+
207225
return (new PowerShellAppGwModel.ApplicationGatewayCertificate
208226
{
209227
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,
213231
State = certificate.State
214232
});
215233
}
216234

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+
217250
public List<PowerShellAppGwModel.ApplicationGatewayCertificate> ListApplicationGatewayCertificate(string gatewayName)
218251
{
219252
ApplicationGatewayListCertificate hydraCertList = client.ApplicationGateways.ListCertificate(gatewayName);

0 commit comments

Comments
 (0)