Exemplo n.º 1
0
        /// <summary>
        /// Create self signed cert
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="subject"></param>
        /// <returns></returns>
        internal static AsymmetricAlgorithm CreateCsr(this SignatureType signature, string subject,
                                                      out CertificateRequest csr)
        {
            AsymmetricAlgorithm alg = null;

            try {
                if (signature.IsRSA())
                {
                    var rsa = RSA.Create();
                    alg = rsa;
                    csr = new CertificateRequest(X500DistinguishedNameEx.Create(subject),
                                                 rsa, signature.ToHashAlgorithmName(), signature.ToRSASignaturePadding());
                    return(alg);
                }
                if (signature.IsECC())
                {
                    var ecdsa = ECDsa.Create();
                    alg = ecdsa;
                    csr = new CertificateRequest(X500DistinguishedNameEx.Create(subject),
                                                 ecdsa, signature.ToHashAlgorithmName());
                    return(alg);
                }
                throw new ArgumentException("Bad signature");
            }
            catch {
                alg?.Dispose();
                throw;
            }
        }
        /// <summary>
        /// Convert buffer to certificate request
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static CertificationRequest ToCertificationRequest(this byte[] buffer)
        {
            var csr        = buffer.ToCertificationRequestInfo();
            var key        = csr.GetPublicKey();
            var extensions = new List <X509Extension>();

            foreach (var extension in csr.GetX509Extensions().ToX509Extensions())
            {
                extensions.Add(extension);
            }
            return(new CertificationRequest {
                RawData = buffer,
                PublicKey = key,
                Extensions = extensions,
                Subject = X500DistinguishedNameEx.Create(csr.Subject.ToString())
            });
        }