summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/UserKeypairService.ts
blob: aa90f1e209e74fd77578ac3df44ac3d0292503b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import * as Redis from 'ioredis';
import { genEd25519KeyPair, importPrivateKey, PrivateKey, PrivateKeyWithPem } from '@misskey-dev/node-http-message-signatures';
import type { MiUser } from '@/models/User.js';
import type { UserKeypairsRepository } from '@/models/_.js';
import { RedisKVCache, MemoryKVCache } from '@/misc/cache.js';
import type { MiUserKeypair } from '@/models/UserKeypair.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import { GlobalEventService, GlobalEvents } from '@/core/GlobalEventService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import type { webcrypto } from 'node:crypto';

@Injectable()
export class UserKeypairService implements OnApplicationShutdown {
	private keypairEntityCache: RedisKVCache<MiUserKeypair>;
	private privateKeyObjectCache: MemoryKVCache<webcrypto.CryptoKey>;

	constructor(
		@Inject(DI.redis)
		private redisClient: Redis.Redis,
		@Inject(DI.redisForSub)
		private redisForSub: Redis.Redis,
		@Inject(DI.userKeypairsRepository)
		private userKeypairsRepository: UserKeypairsRepository,

		private globalEventService: GlobalEventService,
		private userEntityService: UserEntityService,
	) {
		this.keypairEntityCache = new RedisKVCache<MiUserKeypair>(this.redisClient, 'userKeypair', {
			lifetime: 1000 * 60 * 60 * 24, // 24h
			memoryCacheLifetime: Infinity,
			fetcher: (key) => this.userKeypairsRepository.findOneByOrFail({ userId: key }),
			toRedisConverter: (value) => JSON.stringify(value),
			fromRedisConverter: (value) => JSON.parse(value),
		});
		this.privateKeyObjectCache = new MemoryKVCache<webcrypto.CryptoKey>(1000 * 60 * 60 * 1);

		this.redisForSub.on('message', this.onMessage);
	}

	@bindThis
	public async getUserKeypair(userId: MiUser['id']): Promise<MiUserKeypair> {
		return await this.keypairEntityCache.fetch(userId);
	}

	/**
	 * Get private key [Only PrivateKeyWithPem for queue data etc.]
	 * @param userIdOrHint user id or MiUserKeypair
	 * @param preferType
	 *		If ed25519-like(`ed25519`, `01`, `11`) is specified, ed25519 keypair will be returned if exists.
	 *		Otherwise, main keypair will be returned.
	 * @returns
	 */
	@bindThis
	public async getLocalUserPrivateKeyPem(
		userIdOrHint: MiUser['id'] | MiUserKeypair,
		preferType?: string,
	): Promise<PrivateKeyWithPem> {
		const keypair = typeof userIdOrHint === 'string' ? await this.getUserKeypair(userIdOrHint) : userIdOrHint;
		if (
			preferType && ['01', '11', 'ed25519'].includes(preferType.toLowerCase()) &&
			keypair.ed25519PublicKey != null && keypair.ed25519PrivateKey != null
		) {
			return {
				keyId: `${this.userEntityService.genLocalUserUri(keypair.userId)}#ed25519-key`,
				privateKeyPem: keypair.ed25519PrivateKey,
			};
		}
		return {
			keyId: `${this.userEntityService.genLocalUserUri(keypair.userId)}#main-key`,
			privateKeyPem: keypair.privateKey,
		};
	}

	/**
	 * Get private key [Only PrivateKey for ap request]
	 * Using cache due to performance reasons of `crypto.subtle.importKey`
	 * @param userIdOrHint user id, MiUserKeypair, or PrivateKeyWithPem
	 * @param preferType
	 * 		If ed25519-like(`ed25519`, `01`, `11`) is specified, ed25519 keypair will be returned if exists.
	 *		Otherwise, main keypair will be returned. (ignored if userIdOrHint is PrivateKeyWithPem)
	 * @returns
	 */
	@bindThis
	public async getLocalUserPrivateKey(
		userIdOrHint: MiUser['id'] | MiUserKeypair | PrivateKeyWithPem,
		preferType?: string,
	): Promise<PrivateKey> {
		if (typeof userIdOrHint === 'object' && 'privateKeyPem' in userIdOrHint) {
			// userIdOrHint is PrivateKeyWithPem
			return {
				keyId: userIdOrHint.keyId,
				privateKey: await this.privateKeyObjectCache.fetch(userIdOrHint.keyId, async () => {
					return await importPrivateKey(userIdOrHint.privateKeyPem);
				}),
			};
		}

		const userId = typeof userIdOrHint === 'string' ? userIdOrHint : userIdOrHint.userId;
		const getKeypair = () => typeof userIdOrHint === 'string' ? this.getUserKeypair(userId) : userIdOrHint;

		if (preferType && ['01', '11', 'ed25519'].includes(preferType.toLowerCase())) {
			const keyId = `${this.userEntityService.genLocalUserUri(userId)}#ed25519-key`;
			const fetched = await this.privateKeyObjectCache.fetchMaybe(keyId, async () => {
				const keypair = await getKeypair();
				if (keypair.ed25519PublicKey != null && keypair.ed25519PrivateKey != null) {
					return await importPrivateKey(keypair.ed25519PrivateKey);
				}
				return;
			});
			if (fetched) {
				return {
					keyId,
					privateKey: fetched,
				};
			}
		}

		const keyId = `${this.userEntityService.genLocalUserUri(userId)}#main-key`;
		return {
			keyId,
			privateKey: await this.privateKeyObjectCache.fetch(keyId, async () => {
				const keypair = await getKeypair();
				return await importPrivateKey(keypair.privateKey);
			}),
		};
	}

	@bindThis
	public async refresh(userId: MiUser['id']): Promise<void> {
		return await this.keypairEntityCache.refresh(userId);
	}

	/**
	 * If DB has ed25519 keypair, refresh cache and return it.
	 * If not, create, save and return ed25519 keypair.
	 * @param userId user id
	 * @returns MiUserKeypair if keypair is created, void if keypair is already exists
	 */
	@bindThis
	public async refreshAndPrepareEd25519KeyPair(userId: MiUser['id']): Promise<MiUserKeypair | void> {
		await this.refresh(userId);
		const keypair = await this.keypairEntityCache.fetch(userId);
		if (keypair.ed25519PublicKey != null) {
			return;
		}

		const ed25519 = await genEd25519KeyPair();
		await this.userKeypairsRepository.update({ userId }, {
			ed25519PublicKey: ed25519.publicKey,
			ed25519PrivateKey: ed25519.privateKey,
		});
		this.globalEventService.publishInternalEvent('userKeypairUpdated', { userId });
		const result = {
			...keypair,
			ed25519PublicKey: ed25519.publicKey,
			ed25519PrivateKey: ed25519.privateKey,
		};
		this.keypairEntityCache.set(userId, result);
		return result;
	}

	@bindThis
	private async onMessage(_: string, data: string): Promise<void> {
		const obj = JSON.parse(data);

		if (obj.channel === 'internal') {
			const { type, body } = obj.message as GlobalEvents['internal']['payload'];
			switch (type) {
				case 'userKeypairUpdated': {
					this.refresh(body.userId);
					break;
				}
			}
		}
	}
	@bindThis
	public dispose(): void {
		this.keypairEntityCache.dispose();
	}

	@bindThis
	public onApplicationShutdown(signal?: string | undefined): void {
		this.dispose();
	}
}