macieg_b
macieg_b

Reputation: 175

How to generate certificate - UWP, BouncyCastle

I've got problem with generating certificate. I use Portable-BouncyCastle on Universal Windows Platform. It's 1.7 version. I can't create SecureRandom and ISignatureFactory because in this verion of library there is problem with: CryptoApiRandomGenerator, ISignatureFactory and Asn1SignatureFactory. Do anyone knows how to generate certificate without them? Here is my function:

                DateTime startDate = DateTime.Now;
                DateTime expiryDate = startDate.AddDays(365);
                SecureRandom random = new SecureRandom(new CryptoApiRandomGenerator());
                ISignatureFactory signatureAlgorithm = new Asn1SignatureFactory("SHA256WITHRSA", _caPrivateKey.GetKey, random);
                X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
                certGen.SetSerialNumber(BigInteger.ValueOf(DateTime.Now.Millisecond));
                certGen.SetIssuerDN(_caCertificate.CertificateProp.SubjectDN);
                certGen.SetNotBefore(startDate);
                certGen.SetNotAfter(expiryDate);
                certGen.SetSubjectDN(_subjectInfo);
                certGen.SetPublicKey(_subjectPublicKey.GetKey);
                X509Certificate newCertificate = certGen.Generate(signatureAlgorithm);

Upvotes: 1

Views: 304

Answers (1)

macieg_b
macieg_b

Reputation: 175

I found answer to my question. The solution is:

                DateTime startDate = DateTime.Now;
                DateTime expiryDate = startDate.AddDays(365);
                X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
                certGen.SetSerialNumber(BigInteger.ValueOf(DateTime.Now.Millisecond));
                certGen.SetIssuerDN(_caCertificate.CertificateProp.SubjectDN);
                certGen.SetNotBefore(startDate);
                certGen.SetNotAfter(expiryDate);
                certGen.SetSubjectDN(_subjectInfo);
                certGen.SetPublicKey(_subjectPublicKey.GetKey);
                certGen.SetSignatureAlgorithm("SHA1WithRSAEncryption");
                X509Certificate newCertificate = certGen.Generate(_caPrivateKey.GetKey);

Upvotes: 1

Related Questions