Exemple #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            var certs = CertificateFinder.GetCertificateLocations();

            certificate = CertificateSelecter.ShowSelectionDialog(certs, out string password);
            if (certificate == null)
            {
                MessageBox.Show("선택되지 않음", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                certificate.PrivateKeyInfo = CertificateLoader.DecryptPrivateKey(certificate, password);
            }
            catch (LibNPKI.Exceptions.IncorretPasswordException)
            {
                MessageBox.Show("잘못된 비밀번호입니다.", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
                certificate = null;
                return;
            }
            groupBox1.Visible = true;
            groupBox1.Enabled = true;
        }
        /// <summary>
        /// Enumerates all certificates associated with this service account from a specific location
        /// -- Either the service certificate container in Azure blob storage, or the Azure Certificate store
        /// </summary>
        public IEnumerable<X509Certificate2> EnumerateServiceCertificates(CertificateLocation location)
        {
            List<X509Certificate2> certs = new List<X509Certificate2>();

            if (location == CertificateLocation.AzureManagement)
            {
                // Submit the Azure request
                AzureManagementClient client = new AzureManagementClient(this.certificate, this.subscriptionId);
                AzureManagementResponse response = client.SubmitRequest(
                    RequestType.GET,
                    "2009-10-01",
                    "services/hostedservices/{0}/certificates",
                    this.serviceName
                    );

                XmlNode certXml = response.GetXmlNode("Certificates");
                if (certXml == null)
                {
                    return certs;
                }

                foreach (XmlNode certificate in certXml.ChildNodes)
                {
                    string certData = response.GetXmlValue(certificate, "Data");
                    byte[] certBytes = Convert.FromBase64String(certData);
                    certs.Add(new X509Certificate2(certBytes));
                }
            }
            else // Blob storage
            {
                CloudBlobClient blobClient;
                CloudTableClient tableClient;
                CloudQueueClient queueClient;
                this.GetStorageClients(out tableClient, out queueClient, out blobClient);

                string certContainer = AzureNaming.GenerateAzureEntityName("HpcAzureCertificates", this.clusterName, this.subscriptionId, this.serviceName);
                CloudBlobContainer certBlob = blobClient.GetContainerReference(certContainer);

                try
                {
                    foreach (IListBlobItem item in certBlob.ListBlobs())
                    {
                        try
                        {
                            CloudBlockBlob blob = certBlob.GetBlockBlobReference(item.Uri.ToString());
                            X509Certificate2 cert = new X509Certificate2(blob.DownloadByteArray());
                            certs.Add(cert);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }
                catch (Exception)
                {
                    return certs;
                }
            }

            return certs;
        }
        /// <summary>
        /// Uploads a certificate to Azure if it doesn't already exist there
        /// -- Certificate location specifies whether the cert will be stored in Azure's cert store, or blob storage
        /// </summary>
        public IAsyncAzureOperation AddCertificateToService(X509Certificate2 cert, string password, CertificateLocation location)
        {
            if (location == CertificateLocation.AzureManagement)
            {
                return this.AddCertificateToAzureStore(cert, password);
            }
            else // Blob storage
            {
                CloudBlobClient blobClient;
                CloudTableClient tableClient;
                CloudQueueClient queueClient;
                this.GetStorageClients(out tableClient, out queueClient, out blobClient);

                string certContainer = AzureNaming.GenerateAzureEntityName("HpcAzureCertificates", this.clusterName, this.subscriptionId, this.serviceName);
                CloudBlobContainer certBlob = blobClient.GetContainerReference(certContainer);
                certBlob.CreateIfNotExist();

                CloudBlockBlob block = certBlob.GetBlockBlobReference(cert.Thumbprint);
                byte[] bytes = cert.Export(X509ContentType.Cert);
                block.UploadByteArray(bytes);
                return new AsyncAzureOperation(this, string.Empty, true);
            }
        }
 /// <summary>
 /// Uploads a certificate to Azure if it doesn't already exist there
 /// -- Certificate location specifies whether the cert will be stored in Azure's cert store, or blob storage
 /// </summary>
 public IAsyncAzureOperation AddCertificateToService(string thumbprint, string password, CertificateLocation location)
 {
     // FindCertificate may raise an exception
     X509Certificate2 cert = CertHelper.FindCertificate(thumbprint, FindType.Thumbprint);
     return this.AddCertificateToService(cert, password, location);
 }