Esempio n. 1
0
        public void Serializing_and_deserializing_a_private_key_should_result_in_equal_keys()
        {            
            // Arrange
            var rsa = new RSACryptoServiceProvider(2048);
            var rsaParameters = rsa.ExportParameters(true);
            var asn1Parser = new Asn1Parser();
            var rsaParser = new RSAPrivateKeyParser(asn1Parser);
            var asn1Serializer = new Asn1Serializer();
            var asn1Rsa = new RSAPrivateKey(rsaParameters);

            // Act
            var serializedPEM = asn1Serializer.Serialize(asn1Rsa).ToArray().EncodeAsPEM(PEMExtensions.RSAPrivateKey);
            var parsedRsaKey = rsaParser.ParsePem(new MemoryStream(Encoding.ASCII.GetBytes(serializedPEM)));

            //TODO this test sometimes has a missing leading '0' byte.


            // Assert
            parsedRsaKey.Key.Exponent.Should().Equal(rsaParameters.Exponent);
            parsedRsaKey.Key.Modulus.Should().Equal(rsaParameters.Modulus);
            parsedRsaKey.Key.P.Should().Equal(rsaParameters.P);
            parsedRsaKey.Key.D.Should().Equal(rsaParameters.D);
            parsedRsaKey.Key.DP.Should().Equal(rsaParameters.DP);
            parsedRsaKey.Key.Q.Should().Equal(rsaParameters.Q);
            parsedRsaKey.Key.DQ.Should().Equal(rsaParameters.DQ);
            parsedRsaKey.Key.InverseQ.Should().Equal(rsaParameters.InverseQ);            
        }
Esempio n. 2
0
        public static RSAPrivateKey ParsePem(string pem)
        {
            var asn1Parser = new Asn1Parser();

            var rsaParser = new RSAPrivateKeyParser(asn1Parser);

            return(rsaParser.ParsePem(pem));
        }
Esempio n. 3
0
        public RSAPrivateKey ParsePem(Stream input)
        {
            var der = DecodePem(input);
            using (var derStream = new MemoryStream(der))
            {
                //TODO add more validation, ensure that the algorithm used is RSA

                var asn1 = (Sequence)parser.Parse(derStream).First();
                var octet = (OctetString) asn1.Children.Last();
                using (var octetStream = new MemoryStream(octet.UnencodedValue))
                {
                    var rsaParser = new RSAPrivateKeyParser(parser);
                    return rsaParser.ParseDer(octetStream);
                }
            }
        }
Esempio n. 4
0
        // This function supports both PKCS#1 & PKCS#8 encodings

        public RSAPrivateKey ParsePem(Stream input)
        {
            var der = PEM.Decode(input, PEM.PrivateKey);

            using (var derStream = new MemoryStream(der))
            {
                // TODO add more validation, ensure that the algorithm used is RSA

                var asn1  = (Sequence)parser.Parse(derStream).First();
                var octet = (OctetString)asn1.Children.Last();
                using (var octetStream = new MemoryStream(octet.UnencodedValue))
                {
                    var rsaParser = new RSAPrivateKeyParser(parser);
                    return(rsaParser.ParseDer(octetStream));
                }
            }
        }
Esempio n. 5
0
        public void Can_read_a_private_key_from_a_PEM_file()
        {
            // Arrange
            var asn1Parser = new Asn1Parser();
            var sut = new RSAPrivateKeyParser(asn1Parser);

            // Act
            var rsa = sut.ParsePem(new MemoryStream(Encoding.ASCII.GetBytes(TestPrivateKey)));

            // Assert
            rsa.Key.Exponent.Should().Equal(1, 0, 1);

            rsa.Key.Modulus.Length.Should().Be(256);            
            rsa.Key.Modulus[0].Should().Be(0xb2);
            rsa.Key.Modulus[255].Should().Be(0xab);

            rsa.Key.P.Length.Should().Be(128);
        }