-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
default-user-asymmetric-key-regeneration.service.ts
158 lines (143 loc) · 5.79 KB
/
default-user-asymmetric-key-regeneration.service.ts
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
import { combineLatest, firstValueFrom, map } from "rxjs";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
import { EncString } from "@bitwarden/common/platform/models/domain/enc-string";
import { UserId } from "@bitwarden/common/types/guid";
import { UserKey } from "@bitwarden/common/types/key";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { KeyService } from "../../abstractions/key.service";
import { UserAsymmetricKeysRegenerationApiService } from "../abstractions/user-asymmetric-key-regeneration-api.service";
import { UserAsymmetricKeysRegenerationService } from "../abstractions/user-asymmetric-key-regeneration.service";
export class DefaultUserAsymmetricKeysRegenerationService
implements UserAsymmetricKeysRegenerationService
{
constructor(
private keyService: KeyService,
private cipherService: CipherService,
private userAsymmetricKeysRegenerationApiService: UserAsymmetricKeysRegenerationApiService,
private logService: LogService,
private sdkService: SdkService,
private apiService: ApiService,
private configService: ConfigService,
) {}
async regenerateIfNeeded(userId: UserId): Promise<void> {
try {
const privateKeyRegenerationFlag = await this.configService.getFeatureFlag(
FeatureFlag.PrivateKeyRegeneration,
);
if (privateKeyRegenerationFlag) {
const shouldRegenerate = await this.shouldRegenerate(userId);
if (shouldRegenerate) {
await this.regenerateUserAsymmetricKeys(userId);
}
}
} catch (error) {
this.logService.error(
"[UserAsymmetricKeyRegeneration] An error occurred: " +
error +
" Skipping regeneration for the user.",
);
}
}
private async shouldRegenerate(userId: UserId): Promise<boolean> {
const [userKey, userKeyEncryptedPrivateKey, publicKeyResponse] = await firstValueFrom(
combineLatest([
this.keyService.userKey$(userId),
this.keyService.userEncryptedPrivateKey$(userId),
this.apiService.getUserPublicKey(userId),
]),
);
const verificationResponse = await firstValueFrom(
this.sdkService.client$.pipe(
map((sdk) => {
if (sdk === undefined) {
throw new Error("SDK is undefined");
}
return sdk.crypto().verify_asymmetric_keys({
userKey: userKey.keyB64,
userPublicKey: publicKeyResponse.publicKey,
userKeyEncryptedPrivateKey: userKeyEncryptedPrivateKey,
});
}),
),
);
if (verificationResponse.privateKeyDecryptable) {
if (verificationResponse.validPrivateKey) {
// The private key is decryptable and valid. Should not regenerate.
return false;
} else {
// The private key is decryptable but not valid so we should regenerate it.
this.logService.info(
"[UserAsymmetricKeyRegeneration] User's private key is decryptable but not a valid key, attempting regeneration.",
);
return true;
}
}
// The private isn't decryptable, check to see if we can decrypt something with the userKey.
const userKeyCanDecrypt = await this.userKeyCanDecrypt(userKey);
if (userKeyCanDecrypt) {
this.logService.info(
"[UserAsymmetricKeyRegeneration] User Asymmetric Key decryption failure detected, attempting regeneration.",
);
return true;
}
this.logService.warning(
"[UserAsymmetricKeyRegeneration] User Asymmetric Key decryption failure detected, but unable to determine User Symmetric Key validity, skipping regeneration.",
);
return false;
}
private async regenerateUserAsymmetricKeys(userId: UserId): Promise<void> {
const userKey = await firstValueFrom(this.keyService.userKey$(userId));
const makeKeyPairResponse = await firstValueFrom(
this.sdkService.client$.pipe(
map((sdk) => {
if (sdk === undefined) {
throw new Error("SDK is undefined");
}
return sdk.crypto().make_key_pair(userKey.keyB64);
}),
),
);
try {
await this.userAsymmetricKeysRegenerationApiService.regenerateUserAsymmetricKeys(
makeKeyPairResponse.userPublicKey,
new EncString(makeKeyPairResponse.userKeyEncryptedPrivateKey),
);
} catch (error: any) {
if (error?.message === "Key regeneration not supported for this user.") {
this.logService.info(
"[UserAsymmetricKeyRegeneration] Regeneration not supported for this user at this time.",
);
} else {
this.logService.error(
"[UserAsymmetricKeyRegeneration] Regeneration error when submitting the request to the server: " +
error,
);
}
return;
}
await this.keyService.setPrivateKey(makeKeyPairResponse.userKeyEncryptedPrivateKey, userId);
this.logService.info(
"[UserAsymmetricKeyRegeneration] User's asymmetric keys successfully regenerated.",
);
}
private async userKeyCanDecrypt(userKey: UserKey): Promise<boolean> {
const ciphers = await this.cipherService.getAll();
const cipher = ciphers.find((cipher) => cipher.organizationId == null);
if (cipher != null) {
try {
await cipher.decrypt(userKey);
return true;
} catch (error) {
this.logService.error(
"[UserAsymmetricKeyRegeneration] User Symmetric Key validation error: " + error,
);
return false;
}
}
return false;
}
}