private UInt32 ToNumeric()
        {
            UInt32 ret = 0;

            if (Value != null)
            {
                UInt16 bytePos = 0;
                foreach (byte it in Value)
                {
                    ret     |= (UInt32)(GXCommon.SwapBits(it) << bytePos);
                    bytePos += 8;
                }
            }
            return(ret);
        }
Example #2
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);
        }