public async Task TestAddingSingleV2AsymmetricKeyWrap() { EncryptionParameters encryptionParameters = new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("allan")); IAsymmetricPublicKey publicKey = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); await encryptionParameters.AddAsync(new UserPublicKey[] { new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey), }); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(encryptionParameters, 1000); IEnumerable <V2AsymmetricKeyWrapHeaderBlock> wraps = documentHeaders.Headers.HeaderBlocks.OfType <V2AsymmetricKeyWrapHeaderBlock>(); Assert.That(wraps.Count(), Is.EqualTo(1), "There should be one V2AsymmetricKeyWrapHeaderBlock found."); V2AsymmetricKeyWrapHeaderBlock block1 = wraps.First(); ICryptoFactory cryptoFactory = Resolve.CryptoFactory.Create(encryptionParameters.CryptoId); IAsymmetricPrivateKey privateKey1 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); block1.SetPrivateKey(cryptoFactory, privateKey1); ICrypto cryptoFromAsymmetricKey = block1.Crypto(0); V2KeyWrapHeaderBlock symmetricKeyWrap = documentHeaders.Headers.HeaderBlocks.OfType <V2KeyWrapHeaderBlock>().First(); ICrypto cryptoFromSymmetricKey = cryptoFactory.CreateCrypto(symmetricKeyWrap.MasterKey, symmetricKeyWrap.MasterIV, 0); Assert.That(cryptoFromAsymmetricKey.Key, Is.EqualTo(cryptoFromSymmetricKey.Key), "The keys from Asymmetric and Symmetric should be equal."); IAsymmetricPrivateKey privateKey2 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey2); block1.SetPrivateKey(cryptoFactory, privateKey2); ICrypto cryptoFromAsymmetricKey1WithKey2 = block1.Crypto(0); Assert.That(cryptoFromAsymmetricKey1WithKey2, Is.Null, "There should be no valid key set and thus no ICrypto instance returned."); }
public async Task <SubscriptionLevel> ValidateLevelAsync(UserAccount userAccount) { if (userAccount == null) { throw new ArgumentNullException(nameof(userAccount)); } if (userAccount.SubscriptionLevel <= SubscriptionLevel.Free) { return(userAccount.SubscriptionLevel); } if (userAccount.Signature == string.Empty) { return(SubscriptionLevel.Unknown); } IAsymmetricPublicKey publicKey = await New <ILicenseAuthority>().PublicKeyAsync(); if (new Verifier(publicKey).Verify(Convert.FromBase64String(userAccount.Signature), SignedFields(userAccount))) { return(userAccount.SubscriptionLevel); } using (KnownPublicKeys knownKeys = New <KnownPublicKeys>()) { publicKey = (await knownKeys.GetAsync(EmailAddress.Parse(New <UserSettings>().LicenseAuthorityEmail), New <KnownIdentities>().DefaultEncryptionIdentity))?.PublicKey; if (publicKey != null && new Verifier(publicKey).Verify(Convert.FromBase64String(userAccount.Signature), SignedFields(userAccount))) { return(userAccount.SubscriptionLevel); } } return(SubscriptionLevel.Unknown); }
public void TestAddingIdentiticalPublicKey() { Mock <FakeInMemoryDataStoreItem> storeMock = new Mock <FakeInMemoryDataStoreItem>("KnownPublicKeys.txt") { CallBase = true, }; IAsymmetricPublicKey key1 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); UserPublicKey userPublicKey1 = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key1); IAsymmetricPublicKey key2 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey2); UserPublicKey userPublicKey2 = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key2); using (KnownPublicKeys knownPublicKeys = KnownPublicKeys.Load(storeMock.Object, Resolve.Serializer)) { Assert.That(knownPublicKeys.PublicKeys.Count(), Is.EqualTo(0), "There should be no entries now."); knownPublicKeys.AddOrReplace(userPublicKey1); knownPublicKeys.AddOrReplace(userPublicKey2); Assert.That(knownPublicKeys.PublicKeys.Count(), Is.EqualTo(2), "There should be two entries now."); } storeMock.Verify(m => m.OpenWrite(), Times.Once); Assert.That(userPublicKey1, Is.Not.EqualTo(userPublicKey2), "Checking that the two public keys really are different."); storeMock.ResetCalls(); using (KnownPublicKeys knownPublicKeys = KnownPublicKeys.Load(storeMock.Object, Resolve.Serializer)) { Assert.That(knownPublicKeys.PublicKeys.Count(), Is.EqualTo(2), "There should be two entries now."); knownPublicKeys.AddOrReplace(userPublicKey2); knownPublicKeys.AddOrReplace(userPublicKey1); Assert.That(knownPublicKeys.PublicKeys.Count(), Is.EqualTo(2), "There should be two entries now."); } storeMock.Verify(m => m.OpenWrite(), Times.Never); }
public async Task TestRemoveNonexistingFromShared() { IAsymmetricPublicKey key1 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); UserPublicKey userPublicKey1 = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key1); IAsymmetricPublicKey key2 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey2); UserPublicKey userPublicKey2 = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key2); using (KnownPublicKeys knownPublicKeys = New <KnownPublicKeys>()) { knownPublicKeys.AddOrReplace(userPublicKey1); knownPublicKeys.AddOrReplace(userPublicKey2); } SharingListViewModel model = await SharingListViewModel.CreateForFilesAsync(new string[0], LogOnIdentity.Empty); Assert.That(model.SharedWith.Any(), Is.False, "There are no known public keys, and none are set as shared."); Assert.That(model.NotSharedWith.Count(), Is.EqualTo(2), "There are two known public keys, so they should be available as unshared."); await model.AddKeyShares.ExecuteAsync(new[] { userPublicKey1.Email, }); Assert.That(model.SharedWith.Count(), Is.EqualTo(1), "One was set as shared, so there should be one here now."); Assert.That(model.NotSharedWith.Count(), Is.EqualTo(1), "One unshared was set as shared, so there should be one here now."); await model.RemoveKeyShares.ExecuteAsync(new[] { userPublicKey2, }); Assert.That(model.SharedWith.Count(), Is.EqualTo(1), "A key that was not set as shared was attempted to remove, nothing should happen."); Assert.That(model.NotSharedWith.Count(), Is.EqualTo(1), "A key that was not set as shared was attempted to remove, nothing should happen."); }
public bool Equals(IAsymmetricPublicKey other) { if (Object.ReferenceEquals(other, null)) { return(false); } return(ToString() == other.ToString()); }
public static void TestAsymmetricEncryption() { IAsymmetricPublicKey key = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); string text = "AxCrypt is Great!"; byte[] encryptedBytes = key.Transform(Encoding.UTF8.GetBytes(text)); Assert.That(encryptedBytes.Length, Is.EqualTo(512)); }
public byte[] Calculate(IAsymmetricPublicKey publicKey) { General.Utils.ApprovedModeCheck("service", algDetails.Algorithm); byte[] receivedKey = ((AsymmetricNHPublicKey)publicKey).GetKeyData(); byte[] sharedKey = new byte[NewHopeImpl.AgreementSize]; NewHopeImpl.SharedA(sharedKey, privateKey.privateKeyData, receivedKey); return(sharedKey); }
/// <summary> /// Convert an external representation of a public key to an internal representation suitable for actual use. /// </summary> /// <param name="accountKey">The account key.</param> /// <returns>A UserPublicKey</returns> /// <exception cref="System.ArgumentNullException"></exception> public static UserPublicKey ToUserPublicKey(this Api.Model.AccountKey accountKey) { if (accountKey == null) { throw new ArgumentNullException(nameof(accountKey)); } IAsymmetricPublicKey publicKey = Resolve.AsymmetricFactory.CreatePublicKey(accountKey.KeyPair.PublicPem); return(new UserPublicKey(EmailAddress.Parse(accountKey.User), publicKey)); }
/// <summary> /// Set the public key that this certificate identifies. /// </summary> /// <param name="publicKey">The public key to be carried by the generated certificate.</param> public void SetPublicKey( IAsymmetricPublicKey publicKey) { try { tbsGen.SetSubjectPublicKeyInfo(SubjectPublicKeyInfo.GetInstance(publicKey.GetEncoded())); } catch (Exception e) { throw new ArgumentException("unable to process key - " + e.ToString()); } }
public static async Task TestGetTwoAsymmetricCryptosFromHeaders(CryptoImplementation cryptoImplementation) { SetupAssembly.AssemblySetupCrypto(cryptoImplementation); Headers headers = new Headers(); EncryptionParameters parameters = new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("secrets")); IAsymmetricPublicKey publicKey1 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); IAsymmetricPublicKey publicKey2 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey2); await parameters.AddAsync(new UserPublicKey[] { new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey1), new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey2), }); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(parameters, 10); using (V2HmacStream <MemoryStream> stream = V2HmacStream.Create <MemoryStream>(new V2HmacCalculator(new SymmetricKey(new byte[0])), new MemoryStream())) { documentHeaders.WriteStartWithHmac(stream); stream.Flush(); stream.Chained.Position = 0; using (V2AxCryptReader reader = new V2AxCryptReader(new LookAheadStream(stream.Chained))) { while (reader.Read()) { if (reader.CurrentItemType == AxCryptItemType.HeaderBlock) { headers.HeaderBlocks.Add(reader.CurrentHeaderBlock); } } SymmetricKey dataEncryptingKey = documentHeaders.Headers.FindHeaderBlock <V2KeyWrapHeaderBlock>().MasterKey; IEnumerable <V2AsymmetricKeyWrapHeaderBlock> readerAsymmetricKeys = headers.HeaderBlocks.OfType <V2AsymmetricKeyWrapHeaderBlock>(); Assert.That(readerAsymmetricKeys.Count(), Is.EqualTo(2), "There should be two asymmetric keys in the headers."); V2AsymmetricKeyWrapHeaderBlock asymmetricKey1 = readerAsymmetricKeys.First(); IAsymmetricPrivateKey privateKey1 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); asymmetricKey1.SetPrivateKey(new V2Aes256CryptoFactory(), privateKey1); Assert.That(dataEncryptingKey, Is.EqualTo(asymmetricKey1.Crypto(0).Key), "The asymmetric wrapped key should be the one in the ICrypto instance."); IAsymmetricPrivateKey privateKey2 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey2); asymmetricKey1.SetPrivateKey(new V2Aes256CryptoFactory(), privateKey2); Assert.That(asymmetricKey1.Crypto(0), Is.Null, "The ICrypto instance should be null, since the private key was wrong."); V2AsymmetricKeyWrapHeaderBlock asymmetricKey2 = readerAsymmetricKeys.Last(); asymmetricKey2.SetPrivateKey(new V2Aes256CryptoFactory(), privateKey2); Assert.That(dataEncryptingKey, Is.EqualTo(asymmetricKey2.Crypto(0).Key), "The asymmetric wrapped key should be the one in the ICrypto instance."); asymmetricKey2.SetPrivateKey(new V2Aes256CryptoFactory(), privateKey1); Assert.That(asymmetricKey2.Crypto(0), Is.Null, "The ICrypto instance should be null, since the private key was wrong."); } } }
public ExchangePair GenerateExchange(IAsymmetricPublicKey otherKey) { General.Utils.ApprovedModeCheck("generator", Alg); byte[] ourSharedKey = new byte[NewHopeImpl.AgreementSize]; byte[] ourSend = new byte[NewHopeImpl.SendBBytes]; AsymmetricNHPublicKey publicKey = (AsymmetricNHPublicKey)otherKey; NewHopeImpl.SharedB(random, ourSharedKey, ourSend, publicKey.GetKeyData()); return(new ExchangePair(new AsymmetricNHPublicKey(ourSend), ourSharedKey)); }
public void TestOnePublicKey() { IAsymmetricPublicKey key = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); UserPublicKey userPublicKey = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key); UserPublicKeyUpdateStatus updateStatus = new UserPublicKeyUpdateStatus(); Assert.That(updateStatus.Status(userPublicKey), Is.EqualTo(PublicKeyUpdateStatus.NotRecentlyUpdated)); updateStatus.SetStatus(userPublicKey, PublicKeyUpdateStatus.RecentlyUpdated); Assert.That(updateStatus.Status(userPublicKey), Is.EqualTo(PublicKeyUpdateStatus.RecentlyUpdated)); updateStatus.Clear(); Assert.That(updateStatus.Status(userPublicKey), Is.EqualTo(PublicKeyUpdateStatus.NotRecentlyUpdated)); }
public static void TestAsymmetricEncryptionFailedDecryptionWrongKey2() { IAsymmetricPublicKey publicKey = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey2); string text = "AxCrypt is really very great!"; byte[] encryptedBytes = publicKey.Transform(Encoding.UTF8.GetBytes(text)); Assert.That(encryptedBytes.Length, Is.EqualTo(512)); IAsymmetricPrivateKey privateKey = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); byte[] decryptedBytes = privateKey.Transform(encryptedBytes); Assert.That(decryptedBytes, Is.Null); }
public byte[] Calculate(IAsymmetricPublicKey publicKey) { ECDomainParameters domainParams = ((AsymmetricECPublicKey)publicKey).DomainParameters; byte[] zBytes = BigIntegers.AsUnsignedByteArray((domainParams.Curve.FieldSize + 7) / 8, agreement.CalculateAgreement(GetPublicKeyParameters((AsymmetricECPublicKey)publicKey))); byte[] keyMaterial = parameters.KeyMaterialGenerator.Generate(zBytes); // ZEROIZE Arrays.Fill(zBytes, (byte)0); return(keyMaterial); }
public async Task TestInitialOneKeyState() { IAsymmetricPublicKey key = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); UserPublicKey userPublicKey = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key); using (KnownPublicKeys knownPublicKeys = New <KnownPublicKeys>()) { knownPublicKeys.AddOrReplace(userPublicKey); } SharingListViewModel model = await SharingListViewModel.CreateForFilesAsync(new string[0], LogOnIdentity.Empty); Assert.That(model.SharedWith.Any(), Is.False, "There are no known public keys, and none are set as shared."); Assert.That(model.NotSharedWith.Count(), Is.EqualTo(1), "There is one known public key, so this should be available as unshared."); }
public static async Task TestEncryptWithOneAsymmetricKey() { EncryptionParameters encryptionParameters = new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("allan")); IAsymmetricPublicKey publicKey = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); await encryptionParameters.AddAsync(new UserPublicKey[] { new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey), }); byte[] plainText = Resolve.RandomGenerator.Generate(25000); byte[] output = EncrytionHelper(encryptionParameters, "TestEncryptWithOneAsymmetricKey.txt", AxCryptOptions.EncryptWithCompression, plainText); IAsymmetricPrivateKey privateKey1 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); DecryptionParameter decryptionParameter = new DecryptionParameter(privateKey1, new V2Aes256CryptoFactory().CryptoId); byte[] decryptedText = DecryptionHelper(new DecryptionParameter[] { decryptionParameter }, output); Assert.That(decryptedText, Is.Not.Null, "The deryption failed because no valid decryption parameter was found."); Assert.That(decryptedText, Is.EquivalentTo(plainText), "The decrypted text should be the same as was originally encrypted."); }
public void TestCreateSingleKey() { FakeInMemoryDataStoreItem store = new FakeInMemoryDataStoreItem("KnownPublicKeys.txt"); IAsymmetricPublicKey key = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); UserPublicKey userPublicKey = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key); using (KnownPublicKeys knownPublicKeys = KnownPublicKeys.Load(store, Resolve.Serializer)) { Assert.That(knownPublicKeys.PublicKeys.Count(), Is.EqualTo(0), "There should be no entries now."); knownPublicKeys.AddOrReplace(userPublicKey); Assert.That(knownPublicKeys.PublicKeys.Count(), Is.EqualTo(1), "There should be one entry now."); } using (KnownPublicKeys knownPublicKeys = KnownPublicKeys.Load(store, Resolve.Serializer)) { Assert.That(knownPublicKeys.PublicKeys.Count(), Is.EqualTo(1), "There should be one entry now."); Assert.That(knownPublicKeys.PublicKeys.First(), Is.EqualTo(userPublicKey), "The instances should compare equal"); } }
public async Task TestMoveTwoFromUnsharedToShared() { IAsymmetricPublicKey key1 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); UserPublicKey userPublicKey1 = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key1); IAsymmetricPublicKey key2 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey2); UserPublicKey userPublicKey2 = new UserPublicKey(EmailAddress.Parse("*****@*****.**"), key2); using (KnownPublicKeys knownPublicKeys = New <KnownPublicKeys>()) { knownPublicKeys.AddOrReplace(userPublicKey1); knownPublicKeys.AddOrReplace(userPublicKey2); } SharingListViewModel model = await SharingListViewModel.CreateForFilesAsync(new string[0], LogOnIdentity.Empty); Assert.That(model.SharedWith.Any(), Is.False, "There are no known public keys, and none are set as shared."); Assert.That(model.NotSharedWith.Count(), Is.EqualTo(2), "There are two known public keys, so they should be available as unshared."); await model.AddKeyShares.ExecuteAsync(new[] { userPublicKey2.Email, userPublicKey1.Email, }); Assert.That(model.SharedWith.Count(), Is.EqualTo(2), "Two were set as shared, so there should be two here now."); Assert.That(model.NotSharedWith.Count(), Is.EqualTo(0), "Both unshared were set as shared, so there should be none here now."); }
public UserPublicKey(EmailAddress email, IAsymmetricPublicKey publicKey) { Email = email; PublicKey = publicKey; }
public PkixSignerInformationVerifierProvider(X509Certificate cert) { this.publicKey = cert.GetPublicKey(); this.certificate = cert; }
public PkixSignerInformationVerifierProvider(IAsymmetricPublicKey publicKey) { this.publicKey = publicKey; this.certificate = null; }
/// <summary> /// Return a RFC 5280 type 1 key identifier. As in: "(1) The keyIdentifier is composed of the 160-bit SHA-1 hash of the /// value of the BIT STRING subjectPublicKey(excluding the tag, length, and number of unused bits)." /// </summary> /// <param name="publicKey">the public key to base the identifier on.</param> /// <returns>the key identifier.</returns> public SubjectKeyIdentifier CreateSubjectKeyIdentifier(IAsymmetricPublicKey publicKey) { return(CreateSubjectKeyIdentifier(SubjectPublicKeyInfo.GetInstance(publicKey.GetEncoded()))); }
/// <summary> /// Create an AuthorityKeyIdentifier from the passed in public key. /// </summary> /// <param name="publicKey">the public key to base the key identifier on.</param> /// <returns>an AuthorityKeyIdentifier.</returns> public AuthorityKeyIdentifier CreateAuthorityKeyIdentifier(IAsymmetricPublicKey publicKey) { return(new AuthorityKeyIdentifier(calculateIdentifier(SubjectPublicKeyInfo.GetInstance(publicKey.GetEncoded())))); }
public CmsKeyTransRecipientInfoGenerator(X509Certificate recipCert, IParameters <Algorithm> wrapParameters) : base(new Asn1.Cms.IssuerAndSerialNumber(recipCert.IssuerDN, new DerInteger(recipCert.SerialNumber))) { this.wrapAlgID = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdRsaesOaep, new RsaesOaepParameters()); this.wrapParams = FipsRsa.WrapOaep; this.asymmetricPublicKey = recipCert.GetPublicKey(); }
/// <summary> /// Base constructor. /// </summary> /// <param name="key">The responding party's public key.</param> /// <param name="shared">The calculated shared value.</param> public ExchangePair(IAsymmetricPublicKey key, byte[] shared) { this.mKey = key; this.mShared = Arrays.Clone(shared); }
public CmsKeyTransRecipientInfoGenerator(byte[] subjectKeyID, AlgorithmIdentifier wrappingAlgID, IAsymmetricPublicKey asymmetricPublicKey) : base(subjectKeyID) { this.wrapAlgID = wrappingAlgID; this.wrapParams = FipsRsa.WrapOaep; this.asymmetricPublicKey = asymmetricPublicKey; }
public CmsKeyTransRecipientInfoGenerator(byte[] subjectKeyID, IParameters <Algorithm> wrapParameters, IAsymmetricPublicKey asymmetricPublicKey) : base(subjectKeyID) { this.wrapAlgID = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdRsaesOaep, new RsaesOaepParameters()); this.wrapParams = FipsRsa.WrapOaep; this.asymmetricPublicKey = asymmetricPublicKey; }
/// <summary> /// Set the public key that this certificate identifies. /// </summary> /// <param name="publicKey">The public key to be carried by the generated certificate.</param> public void SetPublicKey( IAsymmetricPublicKey publicKey) { tbsGen.SetSubjectPublicKeyInfo(SubjectPublicKeyInfo.GetInstance(publicKey.GetEncoded())); }
public Verifier(IAsymmetricPublicKey publicKey) { _publicKey = publicKey; }
public CmsKeyTransRecipientInfoGenerator(X509Certificate recipCert, AlgorithmIdentifier wrappingAlgID) : base(new Asn1.Cms.IssuerAndSerialNumber(recipCert.IssuerDN, new DerInteger(recipCert.SerialNumber))) { this.wrapAlgID = wrappingAlgID; this.wrapParams = FipsRsa.WrapOaep; this.asymmetricPublicKey = recipCert.GetPublicKey(); }