예제 #1
0
        private static BigInteger ImplRandomFieldElementMult(SecureRandom r, BigInteger p)
        {
            BigInteger x;

            do
            {
                x = BigIntegers.CreateRandomBigInteger(p.BitLength, r);
            }while (x.SignValue <= 0 || x.CompareTo(p) >= 0);
            return(x);
        }
예제 #2
0
        private static BigInteger ImplRandomFieldElementMult(SecureRandom r, int m)
        {
            BigInteger x;

            do
            {
                x = BigIntegers.CreateRandomBigInteger(m, r);
            }while (x.SignValue <= 0);
            return(x);
        }
예제 #3
0
        public BigInteger[] GenerateSignature(byte[] message)
        {
            if (!forSigning)
            {
                throw new InvalidOperationException("not initialized for signing");
            }

            byte[] mRev = new byte[message.Length]; // conversion is little-endian
            for (int i = 0; i != mRev.Length; i++)
            {
                mRev[i] = message[mRev.Length - 1 - i];
            }
            BigInteger e = new BigInteger(1, mRev);

            ECDomainParameters ec = key.Parameters;
            BigInteger         n  = ec.N;
            BigInteger         d  = ((ECPrivateKeyParameters)key).D;

            BigInteger r, s;

            ECMultiplier basePointMultiplier = CreateBasePointMultiplier();

            do // generate s
            {
                BigInteger k;
                do // generate r
                {
                    do
                    {
                        k = BigIntegers.CreateRandomBigInteger(n.BitLength, secureRandom);
                    }while (k.Equals(BigInteger.Zero)); //  ECConstants.ZERO));

                    ECPoint p = basePointMultiplier.Multiply(ec.G, k).Normalize();

                    r = p.AffineXCoord.ToBigInteger().Mod(n);
                }while (r.Equals(BigInteger.Zero)); //  ECConstants.ZERO));

                s = (k.Multiply(e)).Add(d.Multiply(r)).Mod(n);
            }while (s.Equals(BigInteger.Zero)); //   ECConstants.ZERO));

            return(new BigInteger[] { r, s });
        }
        public static byte[] GetSignedCertificate(AlgParSet parameters, Subj subj)
        {
            X509Certificate root = new X509CertificateParser().ReadCertificate(GetRootCertificate());
            Cert            data = CreateDatabaseInfoFromCertificate(root);

            var    keys   = GenerateKeyPair(parameters);
            string serial = string.Empty;

            do
            {
                serial = BigIntegers.CreateRandomBigInteger(512, new SecureRandom()).ToString();
            }while (DatabaseInstance.GetInstance().Certs.FirstOrDefault(elm => elm.SerialNumber_Cert == serial) != null);

            Cert certificate = new Cert
            {
                Ver_Cert     = DatabaseInstance.GetInstance().Vers.FirstOrDefault(),
                SignAlg_Cert = new SignAlg
                {
                    AlgParSet_SignAlg  = parameters,
                    PrivateKey_SignAlg = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keys.Private).ToAsn1Object().GetEncoded(),
                    PublicKey_SignAlg  = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keys.Public).ToAsn1Object().GetEncoded()
                },
                Issuer_Cert           = data.Issuer_Cert,
                Subj_Cert             = subj,
                SerialNumber_Cert     = serial,
                ValidFrom_Cert        = DateTime.Now,
                ValidBy_Cert          = DateTimeOffset.Now.AddYears(5).UtcDateTime,
                SignSerialNumber_Cert = data.SerialNumber_Cert
            };

            var cert = CreateCertificateFromDatabaseInfo(certificate);

            certificate.SignValue_Cert = string.Join("", BitConverter.ToString(cert.GetSignature()).Split('-'));

            DatabaseInstance.GetInstance().Certs.Add(certificate);
            DatabaseInstance.GetInstance().SaveChanges();

            return(cert.GetEncoded());
        }
        public static byte[] GetRootCertificate()
        {
            Issuer certificationAuthority = GetCurrentCertificationAuthority();

            if (certificationAuthority == null)
            {
                throw new Exception("Cannot find certification authority!");
            }

            var certificate = DatabaseInstance.GetInstance().Certs
                              .Include(elm => elm.Ver_Cert)
                              .Include(elm => elm.SignAlg_Cert).ThenInclude(elm => elm.AlgParSet_SignAlg).ThenInclude(elm => elm.AlgName_AlgParSet)
                              .Include(elm => elm.SignAlg_Cert).ThenInclude(elm => elm.AlgParSet_SignAlg).ThenInclude(elm => elm.HashType_AlgParSet)
                              .Include(elm => elm.SignAlg_Cert).ThenInclude(elm => elm.AlgParSet_SignAlg).ThenInclude(elm => elm.Len_AlgParSet)
                              .Include(elm => elm.Subj_Cert).ThenInclude(elm => elm.Gender_Subj)
                              .Include(elm => elm.Subj_Cert).ThenInclude(elm => elm.Citizen_Subj)
                              .Include(elm => elm.Subj_Cert).ThenInclude(elm => elm.City_Subj).ThenInclude(elm => elm.Region_City).ThenInclude(elm => elm.Country_Region)
                              .Include(elm => elm.Issuer_Cert).ThenInclude(elm => elm.City_Issuer).ThenInclude(elm => elm.Region_City).ThenInclude(elm => elm.Country_Region)
                              .FirstOrDefault(elm => elm.SerialNumber_Cert == elm.SignSerialNumber_Cert);

            if (certificate == null || !IsValid(CreateCertificateFromDatabaseInfo(certificate).GetEncoded()))
            {
                var par = DatabaseInstance.GetInstance().AlgParSets
                          .Include(elm => elm.Len_AlgParSet)
                          .Include(elm => elm.AlgName_AlgParSet)
                          .Include(elm => elm.HashType_AlgParSet)
                          .FirstOrDefault(elm => elm.Len_AlgParSet.Value_Len == 256);
                var    keys   = GenerateKeyPair(par);
                string serial = string.Empty;
                do
                {
                    serial = BigIntegers.CreateRandomBigInteger(512, new SecureRandom()).ToString();
                }while (DatabaseInstance.GetInstance().Certs.FirstOrDefault(elm => elm.SerialNumber_Cert == serial) != null);

                certificate = new Cert
                {
                    Ver_Cert     = DatabaseInstance.GetInstance().Vers.FirstOrDefault(),
                    SignAlg_Cert = new SignAlg
                    {
                        AlgParSet_SignAlg  = par,
                        PrivateKey_SignAlg = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keys.Private).ToAsn1Object().GetEncoded(),
                        PublicKey_SignAlg  = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keys.Public).ToAsn1Object().GetEncoded()
                    },
                    Issuer_Cert = certificationAuthority,
                    Subj_Cert   = new Subj
                    {
                        Surname_Subj        = "",
                        Name_Subj           = certificationAuthority.Name_Issuer,
                        BirthDate_Subj      = DateTime.Now,
                        Gender_Subj         = DatabaseInstance.GetInstance().Genders.FirstOrDefault(),
                        Citizen_Subj        = DatabaseInstance.GetInstance().Citizens.FirstOrDefault(),
                        City_Subj           = certificationAuthority.City_Issuer,
                        Phone_Subj          = certificationAuthority.Phone_Issuer,
                        EMail_Subj          = certificationAuthority.EMail_Issuer,
                        PassportSerias_Subj = "",
                        PassportNumber_Subj = "",
                        INN_Subj            = certificationAuthority.INN_Issuer,
                        SNILS_Subj          = ""
                    },
                    SerialNumber_Cert     = serial,
                    ValidFrom_Cert        = DateTime.Now,
                    ValidBy_Cert          = DateTimeOffset.Now.AddYears(5).UtcDateTime,
                    SignSerialNumber_Cert = serial
                };

                var cert = CreateCertificateFromDatabaseInfo(certificate);
                certificate.SignValue_Cert = string.Join("", BitConverter.ToString(cert.GetSignature()).Split('-'));

                DatabaseInstance.GetInstance().Certs.Add(certificate);
                DatabaseInstance.GetInstance().SaveChanges();

                return(cert.GetEncoded());
            }

            return(CreateCertificateFromDatabaseInfo(certificate).GetEncoded());
        }
예제 #6
0
        public override ECFieldElement RandomFieldElement(SecureRandom r)
        {
            int m = FieldSize;

            return(FromBigInteger(BigIntegers.CreateRandomBigInteger(m, r)));
        }