示例#1
0
        private static AsnType CreateOctetString(AsnType value)
        {
            if (IsEmpty(value))
            {
                return(new AsnType(0x04, 0x00));
            }

            return(new AsnType(0x04, value.GetBytes()));
        }
示例#2
0
        private static AsnType CreateBitString(AsnType value)
        {
            if (IsEmpty(value))
            {
                return(new AsnType(0x03, EMPTY));
            }

            return(CreateBitString(value.GetBytes(), 0x00));
        }
    private byte[] PublicKeyToX509(RSAParameters publicKey)
    {
        AsnType oid         = AsnType.CreateOid("1.2.840.113549.1.1.1");
        AsnType algorithmID = AsnType.CreateSequence(new AsnType[] { oid, AsnType.CreateNull() });

        AsnType n             = AsnType.CreateIntegerPos(publicKey.Modulus);
        AsnType e             = AsnType.CreateIntegerPos(publicKey.Exponent);
        AsnType key           = AsnType.CreateBitString(AsnType.CreateSequence(new AsnType[] { n, e }));
        AsnType publicKeyInfo = AsnType.CreateSequence(new AsnType[] { algorithmID, key });

        return(new AsnMessage(publicKeyInfo.GetBytes(), "X.509").GetBytes());
    }
示例#4
0
        internal static byte[] PrivateKeyToPKCS8(RSAParameters privateKey)
        {
            AsnType n       = CreateIntegerPos(privateKey.Modulus);
            AsnType e       = CreateIntegerPos(privateKey.Exponent);
            AsnType d       = CreateIntegerPos(privateKey.D);
            AsnType p       = CreateIntegerPos(privateKey.P);
            AsnType q       = CreateIntegerPos(privateKey.Q);
            AsnType dp      = CreateIntegerPos(privateKey.DP);
            AsnType dq      = CreateIntegerPos(privateKey.DQ);
            AsnType iq      = CreateIntegerPos(privateKey.InverseQ);
            AsnType version = CreateInteger(new byte[] { 0 });
            AsnType key     = CreateOctetString(
                CreateSequence(new AsnType[] { version, n, e, d, p, q, dp, dq, iq })
                );

            AsnType algorithmID = CreateSequence(new AsnType[] { CreateOid("1.2.840.113549.1.1.1"), CreateNull() });

            AsnType privateKeyInfo = CreateSequence(new AsnType[] { version, algorithmID, key });

            return(new AsnMessage(privateKeyInfo.GetBytes(), "PKCS#8").GetBytes());
        }
        /// <summary>
        /// <para>An ordered collection zero, one or more types.
        /// Returns the AsnType representing an ASN.1 encoded sequence.</para>
        /// <para>If the AsnType value is null,an
        /// empty sequence (length 0) is returned.</para>
        /// </summary>
        /// <param name="value">An AsnType consisting of
        /// a single value to be encoded.</param>
        /// <returns>Returns the AsnType representing an ASN.1
        /// encoded sequence.</returns>
        /// <seealso cref="CreateSet(AsnType)"/>
        /// <seealso cref="CreateSet(AsnType[])"/> 
        /// <seealso cref="CreateSetOf(AsnType)"/>
        /// <seealso cref="CreateSetOf(AsnType[])"/>
        /// <seealso cref="CreateSequence(AsnType)"/>
        /// <seealso cref="CreateSequence(AsnType[])"/>
        /// <seealso cref="CreateSequenceOf(AsnType)"/>
        /// <seealso cref="CreateSequenceOf(AsnType[])"/>
        internal static AsnType CreateSequenceOf(AsnType value)
        {
            // From the ASN.1 Mailing List
            if (IsEmpty(value))
            { return new AsnType(0x30, EMPTY); }

            // Sequence: Tag 0x30 (16, Universal, Constructed)
            return new AsnType(0x30, value.GetBytes());
        }
        /// <summary>
        /// <para>An ordered collection of one or more types.
        /// Returns the AsnType representing an ASN.1 encoded sequence.</para>
        /// <para>If the AsnType is null, an empty sequence (length 0)
        /// is returned.</para>
        /// </summary>
        /// <param name="value">An AsnType consisting of
        /// a single value to be encoded.</param>
        /// <returns>Returns the AsnType representing an ASN.1
        /// encoded sequence.</returns>
        /// <seealso cref="CreateSet(AsnType)"/>
        /// <seealso cref="CreateSet(AsnType[])"/> 
        /// <seealso cref="CreateSetOf(AsnType)"/>
        /// <seealso cref="CreateSetOf(AsnType[])"/>
        /// <seealso cref="CreateSequence(AsnType)"/>
        /// <seealso cref="CreateSequence(AsnType[])"/>
        /// <seealso cref="CreateSequenceOf(AsnType)"/>
        /// <seealso cref="CreateSequenceOf(AsnType[])"/>
        internal static AsnType CreateSequence(AsnType value)
        {
            // Should be at least 1...
            Debug.Assert(!IsEmpty(value));

            // One or more required
            if (IsEmpty(value))
            { throw new ArgumentException("A sequence requires at least one value."); }

            // Sequence: Tag 0x30 (16, Universal, Constructed)
            return new AsnType(0x30, value.GetBytes());
        }
        /// <summary>
        /// An ordered sequence of zero, one or more octets. Returns
        /// the byte[] representing an ASN.1 encoded octet string.
        /// If octets is null or length is 0, an empty (0 length)
        /// o ctet string is returned.
        /// </summary>
        /// <param name="value">An AsnType object to be encoded.</param>
        /// <returns>Returns the AsnType representing an ASN.1
        /// encoded octet string.</returns>
        /// <seealso cref="CreateBitString(byte[])"/>
        /// <seealso cref="CreateBitString(byte[], uint)"/>
        /// <seealso cref="CreateBitString(AsnType)"/>
        /// <seealso cref="CreateBitString(String)"/>
        /// <seealso cref="CreateOctetString(byte[])"/>
        /// <seealso cref="CreateOctetString(String)"/>
        internal static AsnType CreateOctetString(AsnType value)
        {
            if (IsEmpty(value))
            {
                // Empty octet string
                return new AsnType(0x04, 0x00);
            }

            // OctetString: Tag 0x04 (4, Universal, Primitive)
            return new AsnType(0x04, value.GetBytes());
        }
        /// <summary>
        /// An ordered sequence of zero, one or more bits. Returns
        /// the AsnType representing an ASN.1 encoded bit string.
        /// If value is null, an empty (0 length) bit string is
        /// returned.
        /// </summary>
        /// <param name="value">An AsnType object to be encoded.</param>
        /// <returns>Returns the AsnType representing an ASN.1
        /// encoded bit string.</returns>
        /// <seealso cref="CreateBitString(byte[])"/>
        /// <seealso cref="CreateBitString(byte[], uint)"/>
        /// <seealso cref="CreateBitString(AsnType[])"/>
        /// <seealso cref="CreateBitString(String)"/>
        /// <seealso cref="CreateOctetString(byte[])"/>
        /// <seealso cref="CreateOctetString(AsnType)"/>
        /// <seealso cref="CreateOctetString(AsnType[])"/>
        /// <seealso cref="CreateOctetString(String)"/>
        internal static AsnType CreateBitString(AsnType value)
        {
            if (IsEmpty(value))
            { return new AsnType(0x03, EMPTY); }

            // BitString: Tag 0x03 (3, Universal, Primitive)
            return CreateBitString(value.GetBytes(), 0x00);
        }
示例#9
0
 private static AsnType CreateBitString(AsnType value)
 {
     return(IsEmpty(value) ? new AsnType(0x03, empty) : CreateBitString(value.GetBytes()));
 }