Пример #1
0
        private object[] GetData()
        {
            GXAsn1ObjectIdentifier alg;

            if (PublicKey.Scheme == Ecdsa.Enums.Ecc.P256)
            {
                alg = new GXAsn1ObjectIdentifier("1.2.840.10045.3.1.7");
            }
            else
            {
                alg = new GXAsn1ObjectIdentifier("1.3.132.0.34");
            }
            object subjectPKInfo = new GXAsn1BitString(PublicKey.RawValue, 0);

            object[] tmp = new object[] { new GXAsn1ObjectIdentifier("1.2.840.10045.2.1"),
                                          alg };
            GXAsn1Context attributes = new GXAsn1Context();

            foreach (KeyValuePair <PkcsObjectIdentifier, object[]> it in Attributes)
            {
                GXAsn1Sequence s = new GXAsn1Sequence();
                s.Add(new GXAsn1ObjectIdentifier(PkcsObjectIdentifierConverter.GetString(it.Key)));
                //Convert object array to list.
                List <object> values = new List <object>();
                foreach (object v in it.Value)
                {
                    values.Add(v);
                }
                s.Add(new KeyValuePair <object, object>(values, null));
                attributes.Add(s);
            }
            return(new object[] { (sbyte)Version, GXAsn1Converter.EncodeSubject(Subject), new object[] { tmp, subjectPKInfo }, attributes });
        }
Пример #2
0
        private object[] Getdata()
        {
            object subjectPKInfo = GXAsn1Converter.FromByteArray(PublicKey.RawValue);

            object[] list;
            if (Attributes != null)
            {
                list = new object[] { (byte)Version, GXAsn1Converter.EncodeSubject(Subject), subjectPKInfo, Attributes };
            }
            else
            {
                list = new object[] { (byte)Version, GXAsn1Converter.EncodeSubject(Subject), subjectPKInfo, new GXAsn1Context() };
            }
            return(list);
        }
Пример #3
0
        private object[] GetDataList()
        {
            if (string.IsNullOrEmpty(Issuer))
            {
                throw new ArgumentNullException("Issuer is empty.");
            }
            if (string.IsNullOrEmpty(Subject))
            {
                throw new ArgumentNullException("Subject is empty.");
            }
            GXAsn1ObjectIdentifier a = new GXAsn1ObjectIdentifier(HashAlgorithmConverter.GetString(SignatureAlgorithm));
            GXAsn1Sequence         seq;
            GXAsn1Context          p = new GXAsn1Context();

            p.Add((sbyte)Version);
            GXAsn1Sequence s = new GXAsn1Sequence();
            GXAsn1Sequence s1;

            if (SubjectKeyIdentifier != null)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.SubjectKeyIdentifier)));
                GXByteBuffer bb = new GXByteBuffer();
                bb.SetUInt8(BerType.OctetString);
                GXCommon.SetObjectCount(SubjectKeyIdentifier.Length, bb);
                bb.Set(SubjectKeyIdentifier);
                s1.Add(bb.Array());
                s.Add(s1);
            }
            if (AuthorityKeyIdentifier != null || AuthorityCertIssuer != null || AuthorityCertificationSerialNumber != null)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.AuthorityKeyIdentifier)));
                s.Add(s1);
                GXAsn1Context s2 = new GXAsn1Context()
                {
                    Index = 3
                };
                GXAsn1Sequence c1 = new GXAsn1Sequence();
                if (AuthorityKeyIdentifier != null)
                {
                    GXAsn1Context c4 = new GXAsn1Context()
                    {
                        Constructed = false, Index = 0
                    };
                    c4.Add(AuthorityKeyIdentifier);
                    c1.Add(c4);
                    s1.Add(GXAsn1Converter.ToByteArray(c1));
                }
                if (AuthorityCertIssuer != null)
                {
                    GXAsn1Context c2 = new GXAsn1Context();
                    c2.Index = 1;
                    c1.Add(c2);
                    GXAsn1Context c3 = new GXAsn1Context()
                    {
                        Index = 4
                    };
                    c2.Add(c3);
                    c3.Add(GXAsn1Converter.EncodeSubject(AuthorityCertIssuer));
                    s2.Add(c1);
                }
                if (AuthorityCertificationSerialNumber != null)
                {
                    GXAsn1Context c4 = new GXAsn1Context()
                    {
                        Constructed = false, Index = 2
                    };
                    c4.Add(AuthorityCertificationSerialNumber);
                    c1.Add(c4);
                    s1.Add(GXAsn1Converter.ToByteArray(c1));
                }
            }
            // BasicConstraints
            s1 = new GXAsn1Sequence();
            s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.BasicConstraints)));
            seq = new GXAsn1Sequence();
            if (BasicConstraints)
            {
                //BasicConstraints is critical if it exists.
                s1.Add(BasicConstraints);
            }
            else if (KeyUsage == KeyUsage.None)
            {
                throw new Exception("Key usage not present.");
            }
            s1.Add(GXAsn1Converter.ToByteArray(seq));
            s.Add(s1);
            s1 = new GXAsn1Sequence();
            s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.KeyUsage)));
            byte value    = 0;
            int  min      = 255;
            byte keyUsage = GXCommon.SwapBits((byte)KeyUsage);

            foreach (KeyUsage it in Enum.GetValues(typeof(KeyUsage)))
            {
                if ((((byte)it) & keyUsage) != 0)
                {
                    byte val = (byte)it;
                    value |= val;
                    if (val < min)
                    {
                        min = val;
                    }
                }
            }
            int ignore = 0;

            while ((min >>= 1) != 0)
            {
                ++ignore;
            }
            byte[] tmp = GXAsn1Converter.ToByteArray(new GXAsn1BitString(new byte[] { (byte)(ignore % 8), value }));
            s1.Add(tmp);
            s.Add(s1);
            GXAsn1Sequence valid = new GXAsn1Sequence();

            valid.Add(ValidFrom);
            valid.Add(ValidTo);
            GXAsn1ObjectIdentifier alg;

            if (PublicKey.Scheme == Ecdsa.Enums.Ecc.P256)
            {
                alg = new GXAsn1ObjectIdentifier("1.2.840.10045.3.1.7");
            }
            else
            {
                alg = new GXAsn1ObjectIdentifier("1.3.132.0.34");
            }
            object[] list;
            object[] tmp3 = new object[] { new GXAsn1ObjectIdentifier("1.2.840.10045.2.1"),
                                           alg };
            GXAsn1Context tmp4 = new GXAsn1Context();

            tmp4.Index = 3;
            tmp4.Add(s);
            object[] tmp2 = new object[] { tmp3, new GXAsn1BitString(PublicKey.RawValue, 0) };
            object[] p2;
            if (SignatureParameters == null)
            {
                p2 = new object[] { a };
            }
            else
            {
                p2 = new object[] { a, SignatureParameters };
            }
            list = new object[] { p, new GXAsn1Integer(SerialNumber.ToByteArray()), p2, GXAsn1Converter.EncodeSubject(Issuer), valid, GXAsn1Converter.EncodeSubject(Subject), tmp2, tmp4 };
            return(list);
        }
        private object[] GetData()
        {
            GXAsn1ObjectIdentifier a = new GXAsn1ObjectIdentifier(HashAlgorithmConverter.GetString(SignatureAlgorithm));
            GXAsn1Context          p = new GXAsn1Context();

            p.Add((sbyte)Version);
            object         subjectPKInfo = GXAsn1Converter.FromByteArray(PublicKey.RawValue);
            GXAsn1Sequence s             = new GXAsn1Sequence();
            GXAsn1Sequence s1;

            if (SubjectKeyIdentifier != null)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.SubjectKeyIdentifier)));
                GXByteBuffer bb = new GXByteBuffer();
                bb.SetUInt8(BerType.OctetString);
                GXCommon.SetObjectCount(SubjectKeyIdentifier.Length, bb);
                bb.Set(SubjectKeyIdentifier);
                s1.Add(bb.Array());
                s.Add(s1);
            }
            if (AuthorityKeyIdentifier != null)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.AuthorityKeyIdentifier)));
                GXAsn1Sequence seq = new GXAsn1Sequence();
                seq.Add(AuthorityKeyIdentifier);
                s1.Add(GXAsn1Converter.ToByteArray(seq));
                s.Add(s1);
            }
            if (BasicConstraints)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.BasicConstraints)));
                GXAsn1Sequence seq = new GXAsn1Sequence();
                seq.Add(BasicConstraints);
                s1.Add(GXAsn1Converter.ToByteArray(seq));
                s.Add(s1);
            }
            if (KeyUsage == KeyUsage.None)
            {
                throw new Exception("Key usage not present.");
            }
            s1 = new GXAsn1Sequence();
            s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.KeyUsage)));
            byte value = 0;
            int  min   = 255;

            foreach (KeyUsage it in Enum.GetValues(typeof(KeyUsage)))
            {
                if ((it & KeyUsage) != 0)
                {
                    byte val = (byte)it;
                    value |= val;
                    if (val < min)
                    {
                        min = val;
                    }
                }
            }
            int offset = 7;

            while ((min >>= 2) != 0)
            {
                ++offset;
            }
            byte[] tmp = GXAsn1Converter.ToByteArray(new GXAsn1BitString(new byte[] { 0, value }));
            s1.Add(tmp);
            s.Add(s1);
            GXAsn1Sequence valid = new GXAsn1Sequence();

            valid.Add(ValidFrom);
            valid.Add(ValidTo);
            object[] list;
            if (s.Count == 0)
            {
                list = new object[] { p, SerialNumber, new object[] { a, SignatureParameters }, GXAsn1Converter.EncodeSubject(Issuer), valid, GXAsn1Converter.EncodeSubject(Subject), subjectPKInfo };
            }
            else
            {
                GXAsn1Context tmp2 = new GXAsn1Context();
                tmp2.Index = 3;
                tmp2.Add(s);
                list = new object[] { p, SerialNumber, new object[] { a, SignatureParameters }, GXAsn1Converter.EncodeSubject(Issuer), valid, GXAsn1Converter.EncodeSubject(Subject), subjectPKInfo, tmp2 };
            }
            return(list);
        }