Exemple #1
0
        /// <summary>
        /// Generate a new X509Certificate specifying a SecureRandom instance that you would like to use.
        /// </summary>
        /// <param name="privateKey">The private key of the issuer used to sign this certificate.</param>
        /// <param name="random">The Secure Random you want to use.</param>
        /// <returns></returns>
        public X509CertificateStructure generateX509Certificate(
            AsymmetricKeyParameter privateKey,
            SecureRandom random)

        {
            Signer sig = null;

            try {
                sig = SignerUtil.getSigner(sigOID);
            }
            catch (Exception ex)
            {
                throw new Exception("exception creating signature: " + ex.Message);
            }

            if (random != null)
            {
                sig.init(true, privateKey);
            }
            else
            {
                sig.init(true, new ParametersWithRandom(privateKey, random));
            }

            TBSCertificateStructure tbsCert = tbsGen.generateTBSCertificate();

            try
            {
                MemoryStream    mStr = new MemoryStream();
                DEROutputStream dOut = new DEROutputStream(mStr);
                dOut.writeObject(tbsCert);
                mStr.Flush();
                byte[] b = mStr.ToArray();
                sig.update(b, 0, b.Length);
            }
            catch (Exception e)
            {
                throw new Exception("exception encoding TBS cert - " + e);
            }

            ASN1EncodableVector v = new ASN1EncodableVector();

            v.add(tbsCert);
            v.add(sigAlgId);
            v.add(new DERBitString(sig.generateSignature()));

            return(new X509CertificateStructure(new DERSequence(v)));
        }
Exemple #2
0
        /// <summary>
        /// Generate an X509Certificate using your own SecureRandom.
        /// </summary>
        /// <param name="key">The private key of the issuer that is signing this certificate.</param>
        /// <param name="random">You Secure Random instance.</param>
        /// <returns>An X509Certificate.</returns>
        public X509Certificate generateX509Certificate(AsymmetricKeyParameter key, SecureRandom random)
        {
            Signer sig = null;

            if (sigOID == null)
            {
                throw new Exception("no signature algorithm specified");
            }

            try
            {
                sig = SignerUtil.getSigner(sigOID);
            }
            catch
            {
                try
                {
                    sig = SignerUtil.getSigner(signatureAlgorithm);
                }
                catch (Exception e)
                {
                    throw new Exception("exception creating signature: " + e.Message);
                }
            }

            if (random != null)
            {
                sig.init(true, new ParametersWithRandom(key, random));
            }
            else
            {
                // Console.WriteLine("**" + sig.GetType());
                sig.init(true, key);
            }

            if (extensions != null)
            {
                tbsGen.setExtensions(new X509Extensions(extOrdering, extensions));
            }
            TBSCertificateStructure tbsCert = tbsGen.generateTBSCertificate();

            try
            {
                MemoryStream    mStr = new MemoryStream();
                DEROutputStream dOut = new DEROutputStream(mStr);
                dOut.writeObject(tbsCert);
                byte[] b = mStr.ToArray();
                sig.update(b, 0, b.Length);
            }
            catch (Exception e)
            {
                throw new Exception("exception encoding TBS cert - " + e);
            }
            ASN1EncodableVector v = new ASN1EncodableVector();

            v.add(tbsCert);
            v.add(sigAlgId);
            v.add(new DERBitString(sig.generateSignature()));

            return(new X509Certificate(new DERSequence(v)));
        }