示例#1
0
        public override int Encode()
        {
            int len = 0;

            // first need to encode keys into bitstring
            keys.Encode();
            publicKey.value = keys.derValue;

            len += algorithm.Encode();
            len += publicKey.Encode();

            byte[] lengthBytes = EncodeLength(len);

            derValue    = new byte[1 + lengthBytes.Length + len];
            derValue[0] = 0x30; // sequence
            int d = 1;

            Array.Copy(lengthBytes, 0, derValue, d, lengthBytes.Length);
            d += lengthBytes.Length;
            Array.Copy(algorithm.derValue, 0, derValue, d, algorithm.derValue.Length);
            d += algorithm.derValue.Length;
            Array.Copy(publicKey.derValue, 0, derValue, d, publicKey.derValue.Length);

            PrependContextTag();

            return(derValue.Length);
        }
        public override int Encode()
        {
            int length           = 0;
            int extensionsLength = 0;

            byte[] extensionsLengthBytes = null;

            int versionLength = version.Encode();

            byte[] versionLengthBytes = EncodeLength(versionLength);
            length += 1 + versionLengthBytes.Length;

            length += versionLength;
            length += serialNumber.Encode();
            length += signature.Encode();
            length += issuer.Encode();
            length += validity.Encode();
            length += subject.Encode();
            length += subjectPKInfo.Encode();

            // optional
            if (issuerUniqueID != null)
            {
                length += 1 + issuerUniqueID.Encode();
            }
            if (subjectUniqueID != null)
            {
                length += 1 + subjectUniqueID.Encode();
            }
            if (extensions != null)
            {
                extensionsLength      = extensions.Encode();
                extensionsLengthBytes = EncodeLength(extensionsLength);
                length += 1 + extensionsLengthBytes.Length + extensionsLength;
            }

            byte[] lengthBytes = EncodeLength(length);
            derValue = new byte[1 + lengthBytes.Length + length];

            int d = 0;

            derValue[d] = 0x30; // sequence
            d++;

            Array.Copy(lengthBytes, 0, derValue, d, lengthBytes.Length);
            d += lengthBytes.Length;

            derValue[d] = 0xa0; // explicit tag
            d++;
            Array.Copy(versionLengthBytes, 0, derValue, d, versionLengthBytes.Length);
            d += versionLengthBytes.Length;

            Array.Copy(version.derValue, 0, derValue, d, version.derValue.Length);
            d += version.derValue.Length;
            Array.Copy(serialNumber.derValue, 0, derValue, d, serialNumber.derValue.Length);
            d += serialNumber.derValue.Length;
            Array.Copy(signature.derValue, 0, derValue, d, signature.derValue.Length);
            d += signature.derValue.Length;
            Array.Copy(issuer.derValue, 0, derValue, d, issuer.derValue.Length);
            d += issuer.derValue.Length;
            Array.Copy(validity.derValue, 0, derValue, d, validity.derValue.Length);
            d += validity.derValue.Length;
            Array.Copy(subject.derValue, 0, derValue, d, subject.derValue.Length);
            d += subject.derValue.Length;
            Array.Copy(subjectPKInfo.derValue, 0, derValue, d, subjectPKInfo.derValue.Length);
            d += subjectPKInfo.derValue.Length;

            if (issuerUniqueID != null)
            {
                derValue[d] = 0xa1;
                d++;

                Array.Copy(issuerUniqueID.derValue, 0, derValue, d, issuerUniqueID.derValue.Length);
                d += issuerUniqueID.derValue.Length;
            }
            if (subjectUniqueID != null)
            {
                derValue[d] = 0xa2;
                d++;

                Array.Copy(subjectUniqueID.derValue, 0, derValue, d, subjectUniqueID.derValue.Length);
                d += subjectUniqueID.derValue.Length;
            }
            if (extensions != null)
            {
                derValue[d] = 0xa3;
                d++;

                Array.Copy(extensionsLengthBytes, 0, derValue, d, extensionsLengthBytes.Length);
                d += extensionsLengthBytes.Length;

                Array.Copy(extensions.derValue, 0, derValue, d, extensions.derValue.Length);
                d += extensions.derValue.Length;
            }

            PrependContextTag();

            return(derValue.Length);
        }