Esempio n. 1
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = (UserPublicKey != null ? UserPublicKey.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (KeyHandle != null ? KeyHandle.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (AttestationCertificate != null ? AttestationCertificate.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Signature != null ? Signature.GetHashCode() : 0);
         return(hashCode);
     }
 }
Esempio n. 2
0
        public V2AsymmetricKeyWrapHeaderBlock(UserPublicKey publicKey, SymmetricKey masterKey, SymmetricIV masterIV)
            : this(Resolve.RandomGenerator.Generate(DATABLOCK_LENGTH))
        {
            if (publicKey == null)
            {
                throw new ArgumentNullException("publicKey");
            }

            byte[] encrypted = publicKey.PublicKey.Transform(masterKey + masterIV);
            GetDataBlockBytesReference().SetFrom(encrypted);
        }
        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 Task AddAsync(IEnumerable <UserPublicKey> publicKeys)
        {
            foreach (UserPublicKey userPublicKey in publicKeys)
            {
                UserPublicKey existingKey = _publicKeys.FirstOrDefault(pk => pk.Email == userPublicKey.Email);
                if (existingKey != null)
                {
                    _publicKeys.Remove(existingKey);
                }
                _publicKeys.Add(userPublicKey);
            }

            return(Constant.CompletedTask);
        }
        public PublicKeyUpdateStatus Status(UserPublicKey userPublicKey)
        {
            if (userPublicKey == null)
            {
                throw new ArgumentNullException(nameof(userPublicKey));
            }

            PublicKeyUpdateStatus value;

            if (_publicKeyUpdateStatus.TryGetValue(userPublicKey.PublicKey.Thumbprint, out value))
            {
                return(value);
            }
            return(PublicKeyUpdateStatus.NotRecentlyUpdated);
        }
 /// <summary>
 /// Checks the public key of a user.
 /// </summary>
 /// <param name="publicKey">The public key to check.</param>
 /// <exception cref="Dracoon.Crypto.Sdk.InvalidKeyPairException"/>
 private static void ValidateUserPublicKey(UserPublicKey publicKey)
 {
     if (publicKey == null)
     {
         throw new InvalidKeyPairException("Public key container cannot be null.");
     }
     if (publicKey.Version == null || !publicKey.Version.Equals(CryptoConstants.defaultVersion))
     {
         throw new InvalidKeyPairException("Unknown public key version.");
     }
     if (publicKey.PublicKey == null | publicKey.PublicKey.Length == 0)
     {
         throw new InvalidKeyPairException("Public key cannot be null or empty.");
     }
 }
        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 async Task <UserPublicKey> OtherPublicKeyAsync(EmailAddress email)
        {
            if (Identity.UserEmail == EmailAddress.Empty)
            {
                throw new InvalidOperationException("The account service requires a user.");
            }

            return(await Task.Run(() =>
            {
                using (KnownPublicKeys knowPublicKeys = New <KnownPublicKeys>())
                {
                    UserPublicKey publicKey = knowPublicKeys.PublicKeys.Where(pk => pk.Email == email).FirstOrDefault();
                    return publicKey;
                }
            }).Free());
        }
        public static async Task <IEnumerable <UserPublicKey> > ToAvailableKnownPublicKeysAsync(this IEnumerable <EmailAddress> emails, LogOnIdentity identity)
        {
            List <UserPublicKey> availablePublicKeys = new List <UserPublicKey>();

            using (KnownPublicKeys knownPublicKeys = New <KnownPublicKeys>())
            {
                foreach (EmailAddress email in emails)
                {
                    UserPublicKey key = await knownPublicKeys.GetAsync(email, identity);

                    if (key != null)
                    {
                        availablePublicKeys.Add(key);
                    }
                }
            }
            return(availablePublicKeys);
        }
        public static async Task <UserPublicKey> GetAsync(this KnownPublicKeys knownPublicKeys, EmailAddress email, LogOnIdentity identity)
        {
            UserPublicKey key = knownPublicKeys.PublicKeys.FirstOrDefault(upk => upk.Email == email);

            if (key != null && New <UserPublicKeyUpdateStatus>().Status(key) == PublicKeyUpdateStatus.RecentlyUpdated)
            {
                return(key);
            }

            if (New <AxCryptOnlineState>().IsOffline)
            {
                return(key);
            }

            if (identity == LogOnIdentity.Empty || identity.UserEmail == EmailAddress.Empty)
            {
                return(key);
            }

            IAccountService accountService = New <LogOnIdentity, IAccountService>(identity);

            if (await accountService.IsAccountSourceLocalAsync())
            {
                return(key);
            }

            if (!New <LicensePolicy>().Capabilities.Has(LicenseCapability.KeySharing) && email != _licenseAuthorityEmail)
            {
                return(key);
            }

            AccountStorage          accountStorage = new AccountStorage(New <LogOnIdentity, IAccountService>(identity));
            CustomMessageParameters invitationMessageParameters = new CustomMessageParameters(new CultureInfo(New <UserSettings>().MessageCulture), New <UserSettings>().CustomInvitationMessage);
            UserPublicKey           userPublicKey = await accountStorage.GetOtherUserInvitePublicKeyAsync(email, invitationMessageParameters).Free();

            if (userPublicKey != null)
            {
                knownPublicKeys.AddOrReplace(userPublicKey);
                New <UserPublicKeyUpdateStatus>().SetStatus(userPublicKey, PublicKeyUpdateStatus.RecentlyUpdated);
            }
            return(userPublicKey);
        }
        public static async Task <IEnumerable <UserPublicKey> > GetKnownPublicKeysAsync(this IEnumerable <UserPublicKey> publicKeys, LogOnIdentity identity)
        {
            List <UserPublicKey> knownKeys = new List <UserPublicKey>();

            using (KnownPublicKeys knownPublicKeys = New <KnownPublicKeys>())
            {
                foreach (UserPublicKey publicKey in publicKeys)
                {
                    UserPublicKey key = await knownPublicKeys.GetAsync(publicKey.Email, identity);

                    if (key == null)
                    {
                        knownKeys.Add(publicKey);
                        continue;
                    }

                    knownKeys.Add(key);
                }
            }
            return(knownKeys);
        }
        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");
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Import a public key manually by the user. The key is also marked as imported by the user.
        /// </summary>
        /// <param name="publicKeyStore"></param>
        /// <returns>true if the import was successful</returns>
        public bool UserImport(IDataStore publicKeyStore)
        {
            UserPublicKey publicKey = null;

            try
            {
                publicKey = _serializer.Deserialize <UserPublicKey>(publicKeyStore);
            }
            catch (JsonException jex)
            {
                New <IReport>().Exception(jex);
            }
            if (publicKey == null)
            {
                return(false);
            }

            publicKey.IsUserImported = true;
            AddOrReplace(publicKey);

            return(true);
        }
Esempio n. 14
0
        public void AddOrReplace(UserPublicKey publicKey)
        {
            if (publicKey == null)
            {
                throw new ArgumentNullException("publicKey");
            }

            for (int i = 0; i < _publicKeys.Count; ++i)
            {
                if (_publicKeys[i] == publicKey)
                {
                    return;
                }
                if (_publicKeys[i].Email == publicKey.Email)
                {
                    _dirty         = true;
                    _publicKeys[i] = publicKey;
                    return;
                }
            }
            _dirty = true;
            _publicKeys.Add(publicKey);
        }
        public void ToStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (var binaryWriter = new BinaryWriter(stream))
            {
                binaryWriter.Write(RegistrationReservedByte);

                var publicKey = UserPublicKey.ToByteArray();
                binaryWriter.Write(publicKey);
                var keyHandle = KeyHandle.ToByteArray();
                binaryWriter.Write((byte)keyHandle.Length);
                binaryWriter.Write(keyHandle);

                var certBytes = AttestationCertificate.Certificate.GetEncoded();
                binaryWriter.Write(certBytes);

                binaryWriter.Write(Signature.ToByteArray());
            }
        }
        public void TestGenerateUserKeyPair_Success()
        {
            UserKeyPair testUkp = Crypto.GenerateUserKeyPair("A", "Qwer1234");

            Assert.IsNotNull(testUkp, "Key pair is null!");

            UserPrivateKey testPrivateKey = testUkp.UserPrivateKey;

            Assert.IsNotNull(testPrivateKey, "Private key container is null!");
            Assert.IsNotNull(testPrivateKey.Version, "Private key version is null");
            Assert.IsTrue(testPrivateKey.Version.Length > 0, "Private key version is empty!");
            Assert.IsTrue(testPrivateKey.Version.Equals("A"), "Private key version is invalid!");
            Assert.IsNotNull(testPrivateKey.PrivateKey, "Private key is null!");
            Assert.IsTrue(testPrivateKey.PrivateKey.StartsWith("-----BEGIN ENCRYPTED PRIVATE KEY-----"), "Privat ekey is invalid!");

            UserPublicKey testPublicKey = testUkp.UserPublicKey;

            Assert.IsNotNull(testPublicKey, "Public key container is null!");
            Assert.IsNotNull(testPublicKey.Version, "Public key version is null");
            Assert.IsTrue(testPublicKey.Version.Length > 0, "Public key version is empty!");
            Assert.IsTrue(testPublicKey.Version.Equals("A"), "Public key version is invalid!");
            Assert.IsNotNull(testPublicKey.PublicKey, "Public key is null!");
            Assert.IsTrue(testPublicKey.PublicKey.StartsWith("-----BEGIN PUBLIC KEY-----"), "Public ekey is invalid!");
        }
        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.");
        }
Esempio n. 18
0
        public EncFileUpload(IInternalDracoonClient client, string actionId, FileUploadRequest request, Stream input, UserPublicKey publicKey,
                             long fileSize) : base(client, actionId, request, input, fileSize)
        {
            _userPublicKey = publicKey;

            LogTag = nameof(EncFileUpload);
        }
 public V1AxCryptDocument()
 {
     CryptoFactory        = new V1Aes128CryptoFactory();
     AsymmetricRecipients = new UserPublicKey[0];
 }
Esempio n. 20
0
 protected bool Equals(KeyRegisterResponse other)
 {
     return(UserPublicKey.SequenceEqual(other.UserPublicKey) && KeyHandle.SequenceEqual(other.KeyHandle) &&
            Equals(AttestationCertificate, other.AttestationCertificate) &&
            Signature.SequenceEqual(other.Signature));
 }