Ejemplo n.º 1
0
        public void GenerateKeyPairUnsupported()
        {
            // Ensure we have the correct first value of the enum.
            Assert.AreEqual((double)KeyPair.KeyTypes.Ecc, 0);

            const KeyPair.KeyTypes invalidEnumType = KeyPair.KeyTypes.Ecc - 1;

            Assert.Throws <InvalidEnumArgumentException>(() => AsymmetricCryptoUtil.GenerateKeyPair(invalidEnumType));
        }
Ejemplo n.º 2
0
        public void GenerateKeyPairEcc()
        {
            // Generation of a keyPair is non-deterministic, so we'll perform some extra checks to be sure.
            var keyPair = AsymmetricCryptoUtil.GenerateKeyPair(KeyPair.KeyTypes.Ecc);

            Assert.IsNotEmpty(keyPair.PrivatePem);
            Assert.IsNotEmpty(keyPair.PublicPem);
            Assert.IsTrue(VerifyKeyPairSignatures(TestString, keyPair.PrivatePem, keyPair.PublicPem));
        }
Ejemplo n.º 3
0
        public bool RegisterInitialOtpDevice(string linkCodeString)
        {
            var linkCode   = new LinkCodeRegisterInitialOtpDevice(linkCodeString);
            var newKeyPair = AsymmetricCryptoUtil.GenerateKeyPair();

            var apiClient = new ApiClient(
                linkCode.ServerHttps,
                linkCode.ServerHost,
                linkCode.ServerPort,
                linkCode.ServerApiVersion);
            var request = new ServerAPIRequestsUnauthenticated.RegisterSecondDevice
            {
                LoginRequestIdentifier = linkCode.LoginIdentifier,
                PublicKeyPem           = newKeyPair.PublicPem,
                DeviceLabel            = "My Test Device",
                DeviceType             = "phone",
                DeviceSubtype          = "testdevice"
            };
            var response = request.GetResponse(apiClient);

            var apiClientAuthenticated = new ApiClient(
                linkCode.ServerHttps,
                linkCode.ServerHost,
                linkCode.ServerPort,
                linkCode.ServerApiVersion,
                response.ApiKey,
                newKeyPair.PrivatePem);

            var totpcode = new TimeBasedOtpGenerator(new Key(response.Secret), response.Digits, new SHA1HMACAlgorithm())
                           .GenerateOtp(DateTime.UtcNow);

            try
            {
                var confirmRequest = new ServerAPIRequests.ConfirmSecondDevice
                {
                    OtpCode = totpcode
                };
                confirmRequest.GetResponse(apiClientAuthenticated);
            }
            catch (RequestException)
            {
                MessageBox.Show(@"Unable to validate the device. Please try again.");
                return(false);
            }

            var serverInfo = apiClientAuthenticated.GetServerInfo(new GetServerInfoRequest());

            var newServerId = AppModel.ServerAccounts.Create(new AppModels.ServerAccount
            {
                ServerIdentifier = serverInfo.ServerIdentifier
            });

            var model = AccountModel.GetModel(newServerId);

            var newOtpAccountId = model.OtpAccounts.Create(new OtpAccount
            {
                Type      = response.Type,
                Label     = response.Label,
                Issuer    = response.Issuer,
                Secret    = response.Secret,
                Algorithm = response.Algorithm,
                Digits    = response.Digits,
                Counter   = response.Counter,
                Period    = response.Period
            });

            var cryptoKeyId = model.CryptoKeys.Create(new CryptoKey
            {
                OwnKey           = true,
                Trust            = true,
                PrivateKeyPem    = newKeyPair.PrivatePem,
                PublicKeyPem     = newKeyPair.PublicPem,
                PublicKeyPemHash = HashUtil.Sha256(newKeyPair.PublicPem)
            });

            var serverAccountSettingsId = model.ServerAccountSettings.Create(new ServerAccountSetting
            {
                Identifier       = serverInfo.ServerIdentifier,
                Label            = serverInfo.ServerLabel,
                HttpsEnabled     = linkCode.ServerHttps,
                Host             = linkCode.ServerHost,
                Port             = linkCode.ServerPort,
                ApiVersion       = linkCode.ServerApiVersion,
                UserIdentifier   = response.UserIdentifier,
                DeviceIdentifier = response.DeviceIdentifier,
                OtpAccountId     = newOtpAccountId,
                ApiKey           = response.ApiKey,
                EmailAddress     = linkCode.EmailAddress,
                ApiCryptoKeyId   = cryptoKeyId
            });

            var linkedDeviceRequest = new ServerAPIRequests.GetLinkedDevice();

            ServerAPIRequests.GetLinkedDevice.ResponseParams linkedDeviceResponse;
            linkedDeviceResponse = linkedDeviceRequest.GetResponse(apiClientAuthenticated);

            if (linkCode.InitiatingDevicePublicKeyPem != HashUtil.Sha256(linkedDeviceResponse.PublicKeyPem))
            {
                MessageBox.Show(@"Unable to verify device keys. You will need to do this manually from the desktop app.");
            }
            else
            {
                var linkedDeviceCryptoKeyId = model.CryptoKeys.Create(new CryptoKey
                {
                    Trust            = true,
                    PublicKeyPem     = linkedDeviceResponse.PublicKeyPem,
                    PublicKeyPemHash = HashUtil.Sha256(linkedDeviceResponse.PublicKeyPem)
                });
                model.ServerAccountSettings.Update(serverAccountSettingsId, new ServerAccountSetting
                {
                    LinkedDeviceCryptoKeyId = linkedDeviceCryptoKeyId
                });
            }

            _syncAccounts.Add(newServerId, new SyncAccounts(this, model));
            _syncAccounts[newServerId].Start();

            _mainForm.UpdateForm();

            return(true);
        }