public static void ReadIntegers()
        {
            byte[] derEncoded =
            {
                /* SEQUENCE */     0x30, 23,
                /* INTEGER(0) */   0x02, 0x01, 0x00,
                /* INTEGER(256) */ 0x02, 0x02, 0x01, 0x00,
                /* INTEGER(-1) */  0x02, 0x01, 0xFF,
                /* Big integer */  0x02, 0x0B, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
            };

            DerSequenceReader reader = new DerSequenceReader(derEncoded);
            Assert.True(reader.HasData);
            Assert.Equal(23, reader.ContentLength);

            int first = reader.ReadInteger();
            Assert.Equal(0, first);

            int second = reader.ReadInteger();
            Assert.Equal(256, second);

            int third = reader.ReadInteger();
            Assert.Equal(-1, third);

            // Reader reads Big-Endian, BigInteger reads Little-Endian
            byte[] fourthBytes = reader.ReadIntegerBytes();
            Array.Reverse(fourthBytes);
            BigInteger fourth = new BigInteger(fourthBytes);
            Assert.Equal(BigInteger.Parse("3645759592820458633497613"), fourth);

            // And... done.
            Assert.False(reader.HasData);
        }
Example #2
0
        /// <summary>
        /// Convert Der format of (r, s) to Ieee1363 format
        /// </summary>
        public static byte[] ConvertDerToIeee1363(byte[] input, int inputOffset, int inputCount, int fieldSizeBits)
        {
            int size = BitsToBytes(fieldSizeBits);

            try
            {
                DerSequenceReader reader   = new DerSequenceReader(input, inputOffset, inputCount);
                byte[]            rDer     = reader.ReadIntegerBytes();
                byte[]            sDer     = reader.ReadIntegerBytes();
                byte[]            response = new byte[2 * size];

                CopySignatureField(rDer, response, 0, size);
                CopySignatureField(sDer, response, size, size);

                return(response);
            }
            catch (InvalidOperationException e)
            {
                throw new CryptographicException(SR.Arg_CryptographyException, e);
            }
        }
        public static byte[] ConvertDerToIeee1363(byte[] input, int inputOffset, int inputCount, int fieldSizeBits)
        {
            int bytes = BitsToBytes(fieldSizeBits);

            try
            {
                DerSequenceReader derSequenceReader =
                    new DerSequenceReader(input, inputOffset, inputCount);

                byte[] signatureField1 = derSequenceReader.ReadIntegerBytes();
                byte[] signatureField2 = derSequenceReader.ReadIntegerBytes();
                byte[] response        = new byte[2 * bytes];

                CopySignatureField(signatureField1, response, 0, bytes);
                CopySignatureField(signatureField2, response, bytes, bytes);

                return(response);
            }
            catch (System.InvalidOperationException ex)
            {
                throw new System.Security.Cryptography.CryptographicException($"Cryptography Exception", ex);
            }
        }
        internal static byte[] GetAsn1IntegerBytes(SafeSharedAsn1IntegerHandle asn1Integer)
        {
            CheckValidOpenSslHandle(asn1Integer);

            // OpenSSL stores negative numbers in their two's complement (positive) form, but
            // sets an internal negative bit.
            //
            // If the number was positive, but could sign-test as negative, DER puts in a leading
            // 0x00 byte, which reading OpenSSL's data directly won't have.
            //
            // So to ensure we're getting a set of bytes compatible with BigInteger (though with the
            // wrong endianness here), DER encode it, then use the DER reader to skip past the tag
            // and length.
            byte[] derEncoded = OpenSslEncode(
                handle => GetAsn1IntegerDerSize(handle),
                (handle, buf) => EncodeAsn1Integer(handle, buf),
                asn1Integer);

            DerSequenceReader reader = DerSequenceReader.CreateForPayload(derEncoded);

            return(reader.ReadIntegerBytes());
        }
        public static void ReadIntegers()
        {
            byte[] derEncoded =
            {
                /* SEQUENCE */ 0x30,       23,
                /* INTEGER(0) */ 0x02,   0x01, 0x00,
                /* INTEGER(256) */ 0x02, 0x02, 0x01, 0x00,
                /* INTEGER(-1) */ 0x02,  0x01, 0xFF,
                /* Big integer */ 0x02,  0x0B, 0x03, 0x04,0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
            };

            DerSequenceReader reader = new DerSequenceReader(derEncoded);

            Assert.True(reader.HasData);
            Assert.Equal(23, reader.ContentLength);

            int first = reader.ReadInteger();

            Assert.Equal(0, first);

            int second = reader.ReadInteger();

            Assert.Equal(256, second);

            int third = reader.ReadInteger();

            Assert.Equal(-1, third);

            // Reader reads Big-Endian, BigInteger reads Little-Endian
            byte[] fourthBytes = reader.ReadIntegerBytes();
            Array.Reverse(fourthBytes);
            BigInteger fourth = new BigInteger(fourthBytes);

            Assert.Equal(BigInteger.Parse("3645759592820458633497613"), fourth);

            // And... done.
            Assert.False(reader.HasData);
        }