public void TestSerializeCertificate() { var testCertificate = LightCertificatesHelper.GenerateSelfSignedCertificate( ELightCertificateSignType.Rsa, 512, ELightCertificateEncryptType.Rsa, 512, EPrivateKeysKeyDerivationFunction.ScryptDefault, Guid.NewGuid(), "TestCertificate", Encoding.UTF8.GetBytes("TestPassword") ); var serializedTestCert = testCertificate.WriteObjectToJson(); _output.WriteLine(serializedTestCert); /**/ var publicTestCert = testCertificate.GetOnlyPublic(); var serializedPublicTestCert = publicTestCert.WriteObjectToJson(); _output.WriteLine(serializedPublicTestCert); /**/ Assert.True(testCertificate.Equals(publicTestCert)); /**/ var testCertCopy = serializedTestCert.ParseJsonToType <LightCertificate>(); var publicTestCertCopy = serializedPublicTestCert.ParseJsonToType <LightCertificate>(); Assert.True(testCertCopy.Equals(publicTestCertCopy)); Assert.True(testCertificate.Equals(testCertCopy)); Assert.True(publicTestCert.Equals(publicTestCertCopy)); }
public void TestCovariantSignedData() { var passBytes = Encoding.UTF8.GetBytes("TestPassword"); var testCertificate = LightCertificatesHelper.GenerateSelfSignedCertificate( ELightCertificateSignType.Rsa, 2048, ELightCertificateEncryptType.Rsa, 2048, EPrivateKeysKeyDerivationFunction.ScryptDefault, Guid.NewGuid(), "TestCertificate", passBytes ); var testB = new B() { Field1 = 1, Field2 = "sdasda", Field3 = Guid.NewGuid() }; _output.WriteLine(testB.WriteObjectToJson()); var testSignedData = new SignedData <B>( testB, testCertificate, passBytes ); var signedData2 = testSignedData.To <A>(); var testA = signedData2.GetValue(); _output.WriteLine(testA.WriteObjectToJson()); }
public void TestCreateSimpleSignedCACertificate() { var testCertificate1 = LightCertificatesHelper.GenerateSelfSignedCertificate( ELightCertificateSignType.Rsa, 2048, ELightCertificateEncryptType.Rsa, 2048, EPrivateKeysKeyDerivationFunction.ScryptDefault, Guid.NewGuid(), "TestCertificate1", Encoding.UTF8.GetBytes("TestPassword1") ); var testCertificate2 = LightCertificatesHelper.GenerateSelfSignedCertificate( ELightCertificateSignType.Rsa, 2048, ELightCertificateEncryptType.Rsa, 2048, EPrivateKeysKeyDerivationFunction.ScryptDefault, Guid.NewGuid(), "TestCertificate2", Encoding.UTF8.GetBytes("TestPassword2") ); LightCertificatesHelper.SignCertificate( testCertificate2, testCertificate1, Encoding.UTF8.GetBytes("TestPassword1") ); _output.WriteLine("{0}", testCertificate1.WriteObjectToJson()); _output.WriteLine("{0}", testCertificate2.WriteObjectToJson()); }
public void TestPerformance() { var allCertPass = Encoding.UTF8.GetBytes("TestPassword"); var testCertificate = LightCertificatesHelper.GenerateSelfSignedCertificate( ELightCertificateSignType.Rsa, 2048, ELightCertificateEncryptType.Rsa, 2048, EPrivateKeysKeyDerivationFunction.ScryptDefault, Guid.NewGuid(), "TestCertificate", allCertPass ); byte[] testData = Enumerable .Range(0, 10000) .Select(x => (byte)(x % 256)) .ToArray(); var sw = new Stopwatch(); sw.Start(); const int calc1It = 10; int a = 0; for (int i = 0; i < calc1It; i++) { var sd = testCertificate.SignData(testData, allCertPass); a += sd.Signature.SignatureBytes[0]; } sw.Stop(); _output.WriteLine( "{0} ms for calculating {1} signatures {2}", sw.ElapsedMilliseconds, calc1It, a ); var testSignedData = testCertificate.SignData(testData, allCertPass); const int calc2It = 5000; int b = 0; var sw2 = new Stopwatch(); sw2.Start(); for (int i = 0; i < calc2It; i++) { bool verifiedData = testCertificate.VerifyData(testSignedData); b += verifiedData ? 1 : 0; } sw2.Stop(); _output.WriteLine( "{0} ms for verifying {1} signatures {2}", sw2.ElapsedMilliseconds, calc2It, b ); }
public void TestCreateSimpleSelfSignedCertificate() { var testCertificate = LightCertificatesHelper.GenerateSelfSignedCertificate( ELightCertificateSignType.Rsa, 2048, ELightCertificateEncryptType.Rsa, 2048, EPrivateKeysKeyDerivationFunction.ScryptDefault, Guid.NewGuid(), "TestCertificate", Encoding.UTF8.GetBytes("TestPassword") ); _output.WriteLine("{0}", testCertificate); }
public void TestEncryptDecrypt() { var passBytes = Encoding.UTF8.GetBytes("TestPassword"); var testCertificate = LightCertificatesHelper.GenerateSelfSignedCertificate( ELightCertificateSignType.Rsa, 2048, ELightCertificateEncryptType.Rsa, 2048, EPrivateKeysKeyDerivationFunction.ScryptDefault, Guid.NewGuid(), "TestCertificate", passBytes ); foreach (var n in new[] { 1, 2, 3, 5, 10, 16, 32, 48, 64, 63, 65, 191 }) { var originData = new byte[n]; MiscFuncs.GetRandomBytes(originData); var encryptedData = testCertificate.EncryptData(originData); var originDataCopy = testCertificate.DecryptData(encryptedData, passBytes); Assert.Equal(originData.Length, originDataCopy.Length); Assert.Equal(originData, originDataCopy); } for (int i = 0; i < 10; i++) { var aesPair = AesKeyIvPair.GenAesKeyIvPair(); var encryptedPair = LightCertificatesHelper.EncryptAesKeyIvPair( aesPair, testCertificate ); var decryptedPair = LightCertificatesHelper.DecryptAesKeyIvPair( encryptedPair, testCertificate, passBytes ); Assert.Equal( aesPair.WriteObjectToJson(), decryptedPair.WriteObjectToJson() ); } }
/**/ if (readPrivate) { result.P = reader.ReadBytesOrThrow(reader.ReadInt32()); result.Q = reader.ReadBytesOrThrow(reader.ReadInt32()); result.DP = reader.ReadBytesOrThrow(reader.ReadInt32()); result.DQ = reader.ReadBytesOrThrow(reader.ReadInt32()); result.InverseQ = reader.ReadBytesOrThrow(reader.ReadInt32()); result.D = reader.ReadBytesOrThrow(reader.ReadInt32()); } } //_rng.GetBytes(ms.GetBuffer()); } return(result); } private static readonly LittleEndianBitConverter _littleConverter = new LittleEndianBitConverter(); /**/ public void CheckMe( bool checkPrivateKeys = false, byte[] privateKeysPass = null ) { if (OnlyPublic && checkPrivateKeys) { throw new ArgumentException( MyNameof.GetLocalVarName(() => checkPrivateKeys) ); } if (Id == Guid.Empty) { throw new ArgumentException( this.MyNameOfProperty(e => e.Id) ); } if (AdditionalData == null) { throw new ArgumentNullException( this.MyNameOfProperty(e => e.AdditionalData) ); } if (Signatures == null) { throw new ArgumentNullException( this.MyNameOfProperty(e => e.Signatures) ); } if ( checkPrivateKeys && privateKeysPass == null ) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => privateKeysPass) ); } if (SignType != (int)ELightCertificateSignType.None) { if (SignType == (int)ELightCertificateSignType.Rsa) { if (SignKeySize <= 0) { throw new ArgumentOutOfRangeException( this.MyNameOfProperty(e => e.SignKeySize) ); } if ( PublicSignParameters == null ) { throw new ArgumentNullException( this.MyNameOfProperty(e => e.PublicSignParameters) ); } var publicRsaParameters = ReadRsaParameters( PublicSignParameters, false ); using (var csp = GetRsaCsp()) { csp.ImportParameters( publicRsaParameters ); if (csp.KeySize != SignKeySize) { throw new ArgumentException( this.MyNameOfProperty(e => e.PublicSignParameters) ); } } if (checkPrivateKeys) { if (PrivateSignParameters == null) { throw new ArgumentNullException( this.MyNameOfProperty(e => e.PrivateSignParameters) ); } PrivateSignParameters.CheckMe(); var privateRsaParameters = ReadRsaParameters( PrivateSignParameters.GetOriginData( privateKeysPass ), true ); if ( !privateRsaParameters.Exponent.SequenceEqual( publicRsaParameters.Exponent ) || !privateRsaParameters.Modulus.SequenceEqual( publicRsaParameters.Modulus ) ) { throw new ArgumentException( this.MyNameOfProperty(e => e.PrivateSignParameters) ); } using (var csp = GetRsaCsp()) { csp.ImportParameters( privateRsaParameters ); if (csp.KeySize != SignKeySize) { throw new ArgumentException( this.MyNameOfProperty(e => e.PrivateSignParameters) ); } } } } else { throw new ArgumentOutOfRangeException( this.MyNameOfProperty(e => e.SignType) ); } } if (EncryptType != (int)ELightCertificateEncryptType.None) { if (EncryptType == (int)ELightCertificateEncryptType.Rsa) { if (EncryptKeySize <= 0) { throw new ArgumentOutOfRangeException( this.MyNameOfProperty(e => e.EncryptKeySize) ); } if ( PublicEncryptParameters == null ) { throw new ArgumentNullException( this.MyNameOfProperty(e => e.PublicEncryptParameters) ); } var publicRsaParameters = ReadRsaParameters( PublicEncryptParameters, false ); using (var csp = GetRsaCsp()) { csp.ImportParameters( publicRsaParameters ); if (csp.KeySize != SignKeySize) { throw new ArgumentException( this.MyNameOfProperty(e => e.PublicEncryptParameters) ); } } if (checkPrivateKeys) { if (PrivateEncryptParameters == null) { throw new ArgumentNullException( this.MyNameOfProperty(e => e.PrivateEncryptParameters) ); } PrivateEncryptParameters.CheckMe(); var privateRsaParameters = ReadRsaParameters( PrivateEncryptParameters.GetOriginData( privateKeysPass ), true ); if ( !privateRsaParameters.Exponent.SequenceEqual( publicRsaParameters.Exponent ) || !privateRsaParameters.Modulus.SequenceEqual( publicRsaParameters.Modulus ) ) { throw new ArgumentException( this.MyNameOfProperty(e => e.PrivateEncryptParameters) ); } using (var csp = GetRsaCsp()) { csp.ImportParameters( privateRsaParameters ); if (csp.KeySize != SignKeySize) { throw new ArgumentException( this.MyNameOfProperty(e => e.PrivateEncryptParameters) ); } } } } else { throw new ArgumentOutOfRangeException( this.MyNameOfProperty(e => e.EncryptType) ); } } if (Signatures.Any(_ => _.SignerCertificateId == this.Id)) { Assert.True( LightCertificatesHelper.CheckCertificateSignature( this, this ) ); } }