Add basically support for encrypted app list syncing

This commit is contained in:
Jonas Lochmann
2022-07-25 02:00:00 +02:00
parent acdec990ea
commit 8a5e46811e
104 changed files with 5287 additions and 55 deletions
@@ -0,0 +1,34 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { FinishKeyRequestAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchFinishKeyRequestAction ({ action, cache, deviceId }: {
deviceId: string
action: FinishKeyRequestAction
cache: Cache
}) {
await cache.database.keyRequest.destroy({
where: {
familyId: cache.familyId,
senderDeviceId: deviceId,
senderSequenceNumber: action.deviceSequenceNumber.toString(10)
},
transaction: cache.transaction
})
}
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -20,13 +20,18 @@ import {
AddUsedTimeAction,
AddUsedTimeActionVersion2,
AppLogicAction,
FinishKeyRequestAction,
ForceSyncAction,
MarkTaskPendingAction,
ReplyToKeyRequestAction,
RemoveInstalledAppsAction,
SendKeyRequestAction,
SignOutAtDeviceAction,
TriedDisablingDeviceAdminAction,
UpdateAppActivitiesAction,
UpdateDeviceStatusAction
UpdateDeviceStatusAction,
UpdateInstalledAppsAction,
UploadDevicePublicKeyAction
} from '../../../../action'
import { EventHandler } from '../../../../monitoring/eventhandler'
import { Cache } from '../cache'
@@ -34,13 +39,18 @@ import { ActionObjectTypeNotHandledException } from '../exception/illegal-state'
import { dispatchAddInstalledApps } from './addinstalledapps'
import { dispatchAddUsedTime } from './addusedtime'
import { dispatchAddUsedTimeVersion2 } from './addusedtime2'
import { dispatchFinishKeyRequestAction } from './finishkeyrequest'
import { dispatchForceSyncAction } from './forcesync'
import { dispatchMarkTaskPendingAction } from './marktaskpendingaction'
import { dispatchReplyToKeyRequestAction } from './replytokeyrequest'
import { dispatchRemoveInstalledApps } from './removeinstalledapps'
import { dispatchSendKeyRequestAction } from './sendkeyrequest'
import { dispatchSignOutAtDevice } from './signoutatdevice'
import { dispatchTriedDisablingDeviceAdmin } from './trieddisablingdeviceadmin'
import { dispatchUpdateAppActivities } from './updateappactivities'
import { dispatchUpdateDeviceStatus } from './updatedevicestatus'
import { dispatchUpdateInstalledApps } from './updateinstalledapps'
import { dispatchUploadDevicePublicKeyAction } from './uploaddevicepublickey'
export const dispatchAppLogicAction = async ({ action, deviceId, cache, eventHandler }: {
action: AppLogicAction
@@ -54,12 +64,18 @@ export const dispatchAppLogicAction = async ({ action, deviceId, cache, eventHan
await dispatchAddUsedTime({ deviceId, action, cache })
} else if (action instanceof AddUsedTimeActionVersion2) {
await dispatchAddUsedTimeVersion2({ deviceId, action, cache, eventHandler })
} else if (action instanceof FinishKeyRequestAction) {
await dispatchFinishKeyRequestAction({ deviceId, action, cache })
} else if (action instanceof ForceSyncAction) {
await dispatchForceSyncAction({ deviceId, action, cache })
} else if (action instanceof MarkTaskPendingAction) {
await dispatchMarkTaskPendingAction({ deviceId, action, cache })
} else if (action instanceof ReplyToKeyRequestAction) {
await dispatchReplyToKeyRequestAction({ deviceId, action, cache, eventHandler })
} else if (action instanceof RemoveInstalledAppsAction) {
await dispatchRemoveInstalledApps({ deviceId, action, cache })
} else if (action instanceof SendKeyRequestAction) {
await dispatchSendKeyRequestAction({ deviceId, action, cache })
} else if (action instanceof SignOutAtDeviceAction) {
await dispatchSignOutAtDevice({ deviceId, action, cache })
} else if (action instanceof UpdateDeviceStatusAction) {
@@ -68,6 +84,10 @@ export const dispatchAppLogicAction = async ({ action, deviceId, cache, eventHan
await dispatchUpdateAppActivities({ deviceId, action, cache })
} else if (action instanceof TriedDisablingDeviceAdminAction) {
await dispatchTriedDisablingDeviceAdmin({ deviceId, action, cache })
} else if (action instanceof UpdateInstalledAppsAction) {
await dispatchUpdateInstalledApps({ deviceId, action, cache })
} else if (action instanceof UploadDevicePublicKeyAction) {
await dispatchUploadDevicePublicKeyAction({ deviceId, action, cache, eventHandler })
} else {
throw new ActionObjectTypeNotHandledException()
}
@@ -0,0 +1,108 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { ReplyToKeyRequestAction } from '../../../../action'
import { Cache } from '../cache'
import { EventHandler } from '../../../../monitoring/eventhandler'
import { SourceDeviceNotFoundException } from '../exception/illegal-state'
export async function dispatchReplyToKeyRequestAction ({ deviceId, action, cache, eventHandler }: {
deviceId: string
action: ReplyToKeyRequestAction
cache: Cache
eventHandler: EventHandler
}) {
const requestUnsafe = await cache.database.keyRequest.findOne({
where: {
familyId: cache.familyId,
serverSequenceNumber: action.requestServerSequenceNumber.toString(10)
},
attributes: ['senderDeviceId', 'senderSequenceNumber'],
transaction: cache.transaction
})
if (!requestUnsafe) {
eventHandler.countEvent('dispatchReplyToKeyRequestAction:request does not exists (anymore)')
return
}
const request = {
senderDeviceId: requestUnsafe.senderDeviceId,
senderSequenceNumber: requestUnsafe.senderSequenceNumber
}
const oldReplyCounter = await cache.database.keyResponse.count({
where: {
familyId: cache.familyId,
receiverDeviceId: request.senderDeviceId,
requestServerSequenceNumber: action.requestServerSequenceNumber.toString(10),
senderDeviceId: deviceId
},
transaction: cache.transaction
})
if (oldReplyCounter !== 0) {
eventHandler.countEvent('dispatchReplyToKeyRequestAction:got duplicate reply which was ignored')
return
}
const deviceEntryUnsafe = await cache.database.device.findOne({
where: {
familyId: cache.familyId,
deviceId
},
transaction: cache.transaction,
attributes: ['nextKeyReplySequenceNumber']
})
if (!deviceEntryUnsafe) {
throw new SourceDeviceNotFoundException()
}
const deviceEntry = {
nextKeyReplySequenceNumber: deviceEntryUnsafe.nextKeyReplySequenceNumber
}
await cache.database.device.update({
nextKeyReplySequenceNumber: (parseInt(deviceEntry.nextKeyReplySequenceNumber) + 1).toString(10)
}, {
where: {
familyId: cache.familyId,
deviceId
},
transaction: cache.transaction
})
await cache.database.keyResponse.create({
familyId: cache.familyId,
receiverDeviceId: request.senderDeviceId,
requestServerSequenceNumber: action.requestServerSequenceNumber.toString(10),
senderDeviceId: deviceId,
replyServerSequenceNumber: deviceEntry.nextKeyReplySequenceNumber,
requestClientSequenceNumber: requestUnsafe.senderSequenceNumber,
tempKey: action.tempKey,
encryptedKey: action.encryptedKey,
signature: action.signature
}, {
transaction: cache.transaction
})
// there is no way (yet) to inform the specific device only
cache.areChangesImportant = true
}
@@ -0,0 +1,76 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SendKeyRequestAction } from '../../../../action'
import { Cache } from '../cache'
import { SourceFamilyNotFoundException } from '../exception/illegal-state'
export async function dispatchSendKeyRequestAction ({ action, cache, deviceId }: {
deviceId: string
action: SendKeyRequestAction
cache: Cache
}) {
const familyEntryUnsafe = await cache.database.family.findOne({
where: {
familyId: cache.familyId
},
transaction: cache.transaction,
attributes: ['nextServerKeyRequestSeq']
})
if (!familyEntryUnsafe) {
throw new SourceFamilyNotFoundException()
}
const serverSequenceNumber = familyEntryUnsafe.nextServerKeyRequestSeq
await cache.database.family.update({
nextServerKeyRequestSeq: (parseInt(serverSequenceNumber, 10) + 1).toString(10)
}, {
where: {
familyId: cache.familyId
},
transaction: cache.transaction
})
await cache.database.keyRequest.destroy({
where: {
familyId: cache.familyId,
senderDeviceId: deviceId,
type: action.type,
deviceId: action.deviceId || null,
categoryId: action.categoryId || null
},
transaction: cache.transaction
})
await cache.database.keyRequest.create({
familyId: cache.familyId,
serverSequenceNumber: serverSequenceNumber,
senderDeviceId: deviceId,
senderSequenceNumber: action.deviceSequenceNumber.toString(10),
deviceId: action.deviceId || null,
categoryId: action.categoryId || null,
type: action.type,
tempKey: action.tempKey,
signature: action.signature
}, {
transaction: cache.transaction
})
cache.areChangesImportant = true
}
@@ -0,0 +1,59 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateInstalledAppsAction } from '../../../../action'
import { types } from '../../../../database/encryptedapplist'
import { generateVersionId } from '../../../../util/token'
import { Cache } from '../cache'
export async function dispatchUpdateInstalledApps ({ deviceId, action, cache }: {
deviceId: string
action: UpdateInstalledAppsAction
cache: Cache
}) {
if (action.base) {
await cache.database.encryptedAppList.upsert({
familyId: cache.familyId,
deviceId,
type: types.base,
version: generateVersionId(),
data: action.base
}, { transaction: cache.transaction })
}
if (action.diff) {
await cache.database.encryptedAppList.upsert({
familyId: cache.familyId,
deviceId,
type: types.diff,
version: generateVersionId(),
data: action.diff
}, { transaction: cache.transaction })
}
if (action.wipe) {
await cache.database.app.destroy({
where: {
familyId: cache.familyId,
deviceId
},
transaction: cache.transaction
})
cache.devicesWithModifiedInstalledApps.add(deviceId)
}
}
@@ -0,0 +1,50 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UploadDevicePublicKeyAction } from '../../../../action'
import { Cache } from '../cache'
import { EventHandler } from '../../../../monitoring/eventhandler'
import { SourceDeviceNotFoundException } from '../exception/illegal-state'
export async function dispatchUploadDevicePublicKeyAction ({ deviceId, action, cache, eventHandler }: {
deviceId: string
action: UploadDevicePublicKeyAction
cache: Cache
eventHandler: EventHandler
}) {
const deviceEntry = await cache.database.device.findOne({
where: {
familyId: cache.familyId,
deviceId
},
transaction: cache.transaction
})
if (deviceEntry === null) {
throw new SourceDeviceNotFoundException()
} else if (deviceEntry.publicKey === null) {
deviceEntry.publicKey = action.key
await deviceEntry.save({ transaction: cache.transaction })
cache.invalidiateDeviceList = true
} else if (deviceEntry.publicKey.equals(action.key)) {
eventHandler.countEvent('dispatchUploadDevicePublicKeyAction:duplicate action')
} else {
eventHandler.countEvent('dispatchUploadDevicePublicKeyAction:got new public key for existing device')
}
}