Commit 80764d1d authored by David Kline's avatar David Kline Committed by GitHub
Browse files

Merge pull request #46 from davidkline-ms/master

Fix UWP Get() call and update Certificate handling
parents dcca7cc1 12317015
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
        /// <summary>
        /// Issuer for the device certificate.
        /// </summary>
        public static readonly string DevicePortalCertificateIssuer = "CN=Microsoft Windows Web Management";
        public static readonly string DevicePortalCertificateIssuer = "Microsoft Windows Web Management";

        /// <summary>
        /// Endpoint used to access the certificate.
@@ -149,11 +149,9 @@ namespace Microsoft.Tools.WindowsDevicePortal
                        DeviceConnectionStatus.Connecting,
                        DeviceConnectionPhase.AcquiringCertificate,
                        connectionPhaseDescription);                  
#if WINDOWS_UWP
                    this.SetDeviceCertificate(await this.GetDeviceCertificate());
#else

                    this.deviceConnection.SetDeviceCertificate(await this.GetDeviceCertificate());
#endif

                    certificateAcquired = true;
                }
                catch
+7 −24
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ using Windows.Foundation;
using Windows.Security.Cryptography.Certificates;
using Windows.Storage.Streams;
using Windows.Web.Http;
using Windows.Web.Http.Filters;

namespace Microsoft.Tools.WindowsDevicePortal
{
@@ -46,7 +47,11 @@ namespace Microsoft.Tools.WindowsDevicePortal

                try
                {
                    using (HttpClient client = new HttpClient())
                    HttpBaseProtocolFilter requestSettings = new HttpBaseProtocolFilter();
                    requestSettings.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
                    requestSettings.AllowUI = false;

                    using (HttpClient client = new HttpClient(requestSettings))
                    {
                        IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> responseOperation = client.GetAsync(uri);
                        TaskAwaiter<HttpResponseMessage> responseAwaiter = responseOperation.GetAwaiter();
@@ -65,7 +70,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                                }

                                certificate = new Certificate(bufferOperation.GetResults());
                                if (!certificate.Issuer.StartsWith(DevicePortalCertificateIssuer))
                                if (!certificate.Issuer.Contains(DevicePortalCertificateIssuer))
                                {
                                    certificate = null;
                                    throw new DevicePortalException(
@@ -94,27 +99,5 @@ namespace Microsoft.Tools.WindowsDevicePortal
            }
        }
#pragma warning restore 1998

        /// <summary>
        /// Sets the device's root certificate in the certificate store. 
        /// </summary>
        /// <param name="certificate">The device's root certificate.</param>
        private void SetDeviceCertificate(Certificate certificate)
        {
            // Verify that the certificate is one we recognize.
            if (!certificate.Issuer.StartsWith(DevicePortalCertificateIssuer))
            {
                certificate = null;
                throw new DevicePortalException(
                    (HttpStatusCode)0,
                    "Invalid certificate issuer",
                    null,
                    "Failed to set the device certificate");
            }

            // Install the certificate.
            CertificateStore trustedStore = CertificateStores.TrustedRootCertificationAuthorities;
            trustedStore.Add(certificate);
        }
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Security.Credentials;
@@ -68,7 +69,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                }
            }

            return null;
            return (dataBuffer != null) ? dataBuffer.AsStream() : null;
        }
#pragma warning restore 1998
    }
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ namespace Microsoft.Tools.WindowsDevicePortal

                            // Validate the issuer.
                            certificate = new X509Certificate2(certData);
                            if (!certificate.IssuerName.Name.StartsWith(DevicePortalCertificateIssuer))
                            if (!certificate.IssuerName.Name.Contains(DevicePortalCertificateIssuer))
                            {
                                certificate = null;
                                throw new DevicePortalException(