예제 #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);            
        }
        public void Should_install_a_x509_certificate_and_update_bindings()
        {
            // Arrange
            var x509 = new X509Certificate2(Encoding.ASCII.GetBytes(TestCertificate), (string)null, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);            
            var sut = new IISServerConfigurationProvider();
            var asn1Parser = new Asn1Parser();
            var rsaParser = new PrivateKeyParser(asn1Parser);
            var privateKey = rsaParser.ParsePem(TestPrivateKey).Key;            
            var csp = new CspParameters { KeyContainerName = x509.GetCertHashString(), Flags = CspProviderFlags.UseMachineKeyStore };
            var rsa2 = new RSACryptoServiceProvider(csp);
            rsa2.ImportParameters(privateKey);
            x509.PrivateKey = rsa2;

            // Act            
            sut.ConfigureServer("test.startliste.info", x509.GetCertHash(), "my", null, null);
        }
예제 #3
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);
        }
예제 #4
0
        public void Should_parse_integer_to_bytes()
        {
            // Arrange
            var asn1Input = new[]
            {
                new byte[] {2, 1, 0},
                new byte[] {2, 1, 127},
                new byte[] {2, 2, 0, 127},
                new byte[] {2, 2, 0, 128},
                new byte[] {2, 3, 1, 0, 0},
                new byte[] {2, 0},
                new byte[] {2, 7, 0, 165, 163, 214, 2, 169, 62}
            }.Select(b => new MemoryStream(b));

            var expectedParsedValues = new[]
            {
                new byte[] {0},
                new byte[] {127},
                new byte[] {0, 127},
                new byte[] {128},
                new byte[] {1, 0, 0},
                new byte[0],
                new byte[] {165, 163, 214, 2, 169, 62}   //TODO as far as I undertand, it is correct that the parser removes the leading zero here. However, when parsing a PEM this must be taken into account and the parser must pad the parsed value with a leading zero
            };

            var sut = new Asn1Parser();

            // Act
            var result = asn1Input.SelectMany(sut.Parse).Cast<Integer>().Select(i => i.UnencodedValue).ToArray();

            // Assert
            result.Length.Should().Be(expectedParsedValues.Length);
            for (int i = 0; i < expectedParsedValues.Length; i++)
            {
                result[i].Should().Equal(expectedParsedValues[i]);
            }
        }
예제 #5
0
 public RSAPrivateKeyParser(Asn1Parser parser)
 {
     this.parser = parser;
 }