private static byte[] GetKeyAsDER(RSAParameters key) { var asn1Key = new RSAPrivateKey(key); var serializer = new Asn1Serializer(); var keyBytes = serializer.Serialize(asn1Key).ToArray(); return keyBytes; }
public void Should_serialize_a_certificate_signing_request() { // Arrange var keyManager = new FileKeyStore(Environment.CurrentDirectory); var rsa = keyManager.GetOrCreateKey("test.startliste.info"); var key = rsa.ExportParameters(true); var data = new CertificateRequestData("test.startliste.info", key) { C = "DE", S = "NRW", L = "Werther", O = "Aero Club Bünde", OU = "" }; var serializer = new Asn1Serializer(); var sut = new CertificateRequestAsn1DEREncoder(serializer); // Act var csr = sut.Encode(data); var der = sut.EncodeAsDER(data); var base64 = sut.EncodeAsBase64(data); var bytes = serializer.Serialize(csr).ToArray(); // Assert File.WriteAllBytes(@"request.der", der); File.WriteAllText(@"request.txt", base64); bytes.Should().NotBeNull(); // openssl req -in r:\request.txt -noout -text }
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_serialize_a_sequence() { // Arrange var sequence = new Sequence(new ObjectIdentifier("2.5.4.8"), new UTF8String("NRW")); var sut = new Asn1Serializer(); // Act var bytes = sut.Serialize(sequence); // Assert bytes.Should().Equal(0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x03, 0x4E, 0x52, 0x57); }
public void Should_serialize_integer_from_int() { // Arrange var ints = new int[] {0, 127, 128, 256*256}; var asn1ints = ints.Select(i => new Integer(i)); var sut = new Asn1Serializer(); // Act var bytes = asn1ints.Select(i => sut.Serialize(i)).ToArray(); // Assert bytes[0].Should().Equal(0x02, 1, 0); bytes[1].Should().Equal(0x02, 1, 127); bytes[2].Should().Equal(0x02, 2, 0, 128); // da das 1. Bit zur Vorzeichenerkennung genutzt wird, wird bei >= 128 ein 0-Byte voran gestellt bytes[3].Should().Equal(0x02, 3, 1, 0, 0); }
public void Should_serialize_integer_from_bytes() { // Arrange var byteArrays = new byte[][] { new byte[] {0}, new byte[] {127}, new byte[] {0, 127}, new byte[] {128}, new byte[] {1, 0, 0}, new byte[0], new byte[] {0, 165, 163, 214, 2, 169, 62} }.Select(data => new Integer(data)); var expectedSerializedValues = new[] { new byte[] {2, 1, 0}, new byte[] {2, 1, 127}, new byte[] {2, 2, 0, 127}, new byte[] {2, 2, 0, 128}, // da das 1. Bit zur Vorzeichenerkennung genutzt wird, wird bei >= 128 ein 0-Byte voran gestellt new byte[] {2, 3, 1, 0, 0}, new byte[] {2, 0}, new byte[] {2, 7, 0, 165, 163, 214, 2, 169, 62} }; var sut = new Asn1Serializer(); // Act var result = byteArrays.Select(i => sut.Serialize(i)).ToArray(); // Assert result.Length.Should().Be(expectedSerializedValues.Length); for (int i = 0; i < expectedSerializedValues.Length; i++) { result[i].Should().Equal(expectedSerializedValues[i]); } }