< Summary

Class:IntegrationTestCommon.CertificateBuilder
Assembly:Microsoft.Azure.Batch.IntegrationTestCommon
File(s):C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\tests\IntegrationTestCommon\CertificateBuilder.cs
Covered lines:38
Uncovered lines:0
Coverable lines:38
Total lines:100
Line coverage:100% (38 of 38)
Covered branches:2
Total branches:2
Branch coverage:100% (2 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
CreateSelfSignedInFile(...)-100%100%
GenerateSerialNumber()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\tests\IntegrationTestCommon\CertificateBuilder.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for license information.
 3
 4namespace IntegrationTestCommon
 5{
 6    using System;
 7    using System.IO;
 8    using System.Security.Cryptography;
 9    using System.Linq;
 10    using Org.BouncyCastle.Crypto.Prng;
 11    using Org.BouncyCastle.Math;
 12    using Org.BouncyCastle.Security;
 13    using Org.BouncyCastle.X509;
 14    using Org.BouncyCastle.Asn1.X509;
 15    using Org.BouncyCastle.Crypto;
 16    using Org.BouncyCastle.Crypto.Generators;
 17    using System.Security.Cryptography.X509Certificates;
 18    using Org.BouncyCastle.Pkcs;
 19    using Org.BouncyCastle.Crypto.Operators;
 20
 21    /// <summary>
 22    /// Static class for generating pfx and cer files.
 23    /// </summary>
 24    public static class CertificateBuilder
 25    {
 226        public static string Sha1Algorithm = "sha1WithRSA";
 227        public static string Sha256Algorithm = "sha256WithRSA";
 28
 29        /// <summary>
 30        /// Create a self signed certificate in the specified file.
 31        /// </summary>
 32        /// <param name="subjectName">The subject of the certificate to create.</param>
 33        /// <param name="fileName">The file name to write the certificate to.</param>
 34        /// <param name="signatureAlgorithm">The signature algorithm to use</param>
 35        /// <param name="password">True if there is a password, false otherwise.  Note that if there is a password, PFX 
 36        public static void CreateSelfSignedInFile(string subjectName, string fileName, string signatureAlgorithm, string
 37        {
 838            byte[] serialNumber = GenerateSerialNumber();
 839            string subject = string.Format("CN={0}", subjectName);
 40
 841            var subjectDN = new X509Name(subject);
 842            var issuerDN = subjectDN;
 43
 44            const int keyStrength = 2048;
 845            var randomGenerator = new CryptoApiRandomGenerator();
 846            var random = new SecureRandom(randomGenerator);
 847            var keyGenerationParameters = new KeyGenerationParameters(random, keyStrength);
 848            var keyPairGenerator = new RsaKeyPairGenerator();
 849            keyPairGenerator.Init(keyGenerationParameters);
 850            var subjectKeyPair = keyPairGenerator.GenerateKeyPair();
 851            var issuerKeyPair = subjectKeyPair;
 52
 853            ISignatureFactory signatureFactory = new Asn1SignatureFactory(signatureAlgorithm, issuerKeyPair.Private, ran
 54
 55
 856            var certificateGenerator = new X509V3CertificateGenerator();
 857            certificateGenerator.AddExtension(X509Extensions.ExtendedKeyUsage.Id, true, new ExtendedKeyUsage(KeyPurposeI
 858            certificateGenerator.SetSerialNumber(new BigInteger(serialNumber.Concat(new Byte[] { 0 }).ToArray()));
 859            certificateGenerator.SetIssuerDN(issuerDN);
 860            certificateGenerator.SetSubjectDN(subjectDN);
 861            certificateGenerator.SetNotBefore(DateTime.Now);
 862            certificateGenerator.SetNotAfter(DateTime.Now);
 863            certificateGenerator.SetPublicKey(subjectKeyPair.Public);
 864            var certificate = certificateGenerator.Generate(signatureFactory);
 65
 66
 867            var store = new Pkcs12Store();
 868            string friendlyName = certificate.SubjectDN.ToString();
 869            var certificateEntry = new X509CertificateEntry(certificate);
 870            store.SetCertificateEntry(friendlyName, certificateEntry);
 871            store.SetKeyEntry(friendlyName, new AsymmetricKeyEntry(subjectKeyPair.Private), new[] { certificateEntry });
 872            var stream = new MemoryStream();
 873            store.Save(stream, password.ToCharArray(), random);
 74
 875            var convertedCertificate = new X509Certificate2(stream.ToArray(), password, X509KeyStorageFlags.PersistKeySe
 76
 77            //If password is not empty, generate a PKCS#12 formatted file
 878            if (!string.IsNullOrEmpty(password))
 79            {
 480                File.WriteAllBytes(fileName, stream.ToArray());
 81            }
 82            //If password is empty generate a DER formatted file
 83            else
 84            {
 485                File.WriteAllBytes(fileName, convertedCertificate.RawData);
 86            }
 87
 488        }
 89
 90        private static byte[] GenerateSerialNumber()
 91        {
 892            byte[] sn = Guid.NewGuid().ToByteArray();
 93
 94            //The high bit must be unset
 895            sn[0] &= 0x7F;
 96
 897            return sn;
 98        }
 99    }
 100}