1
+ use error_stack:: ensure;
1
2
use masking:: PeekInterface ;
2
3
use rayon:: prelude:: * ;
3
4
@@ -15,6 +16,8 @@ use crate::{
15
16
} ,
16
17
} ;
17
18
19
+ use super :: custodian:: Custodian ;
20
+
18
21
#[ async_trait:: async_trait]
19
22
pub trait KeyEncrypter < ToType > {
20
23
async fn encrypt ( self , state : & AppState ) -> errors:: CustomResult < ToType , errors:: CryptoError > ;
@@ -47,6 +50,7 @@ impl KeyEncrypter<DataKeyNew> for Key {
47
50
time:: OffsetDateTime :: now_utc ( ) . date ( ) ,
48
51
time:: OffsetDateTime :: now_utc ( ) . time ( ) ,
49
52
) ,
53
+ token : self . token ,
50
54
} )
51
55
}
52
56
}
@@ -71,6 +75,7 @@ impl KeyDecrypter<Key> for DataKey {
71
75
version : self . version ,
72
76
key : decrypted_key. into ( ) ,
73
77
source,
78
+ token : self . token ,
74
79
} )
75
80
}
76
81
}
@@ -81,6 +86,7 @@ pub trait DataEncrypter<ToType> {
81
86
self ,
82
87
state : & AppState ,
83
88
identifier : & Identifier ,
89
+ custodian : Custodian ,
84
90
) -> errors:: CustomResult < ToType , errors:: CryptoError > ;
85
91
}
86
92
@@ -90,6 +96,7 @@ pub trait DataDecrypter<ToType> {
90
96
self ,
91
97
state : & AppState ,
92
98
identifier : & Identifier ,
99
+ custodian : Custodian ,
93
100
) -> errors:: CustomResult < ToType , errors:: CryptoError > ;
94
101
}
95
102
@@ -99,11 +106,20 @@ impl DataEncrypter<EncryptedDataGroup> for DecryptedDataGroup {
99
106
self ,
100
107
state : & AppState ,
101
108
identifier : & Identifier ,
109
+ custodian : Custodian ,
102
110
) -> errors:: CustomResult < EncryptedDataGroup , errors:: CryptoError > {
103
111
let version = Version :: get_latest ( identifier, state) . await ;
104
112
let decrypted_key = Key :: get_key ( state, identifier, version) . await . switch ( ) ?;
105
113
let key = GcmAes256 :: new ( decrypted_key. key ) ?;
106
114
115
+ let stored_token = decrypted_key. token ;
116
+ let provided_token = custodian. into_access_token ( state) ;
117
+
118
+ ensure ! (
119
+ !identifier. is_entity( ) || ( stored_token. eq( & provided_token) ) ,
120
+ errors:: CryptoError :: AuthenticationFailed
121
+ ) ;
122
+
107
123
state. thread_pool . install ( || {
108
124
self . 0
109
125
. into_par_iter ( )
@@ -125,12 +141,21 @@ impl DataDecrypter<DecryptedDataGroup> for EncryptedDataGroup {
125
141
self ,
126
142
state : & AppState ,
127
143
identifier : & Identifier ,
144
+ custodian : Custodian ,
128
145
) -> errors:: CustomResult < DecryptedDataGroup , errors:: CryptoError > {
129
146
let version = FxHashSet :: from_iter ( self . 0 . values ( ) . map ( |d| d. version ) ) ;
130
147
let decrypted_keys = Key :: get_multiple_keys ( state, identifier, version)
131
148
. await
132
149
. switch ( ) ?;
133
150
151
+ let mut stored_tokens = decrypted_keys. values ( ) . map ( |k| & k. token ) ;
152
+ let provided_token = custodian. into_access_token ( state) ;
153
+
154
+ ensure ! (
155
+ !identifier. is_entity( ) || stored_tokens. all( |t| t. eq( & provided_token) ) ,
156
+ errors:: CryptoError :: AuthenticationFailed
157
+ ) ;
158
+
134
159
state. thread_pool . install ( || {
135
160
self
136
161
. 0
@@ -160,9 +185,19 @@ impl DataEncrypter<EncryptedData> for DecryptedData {
160
185
self ,
161
186
state : & AppState ,
162
187
identifier : & Identifier ,
188
+ custodian : Custodian ,
163
189
) -> errors:: CustomResult < EncryptedData , errors:: CryptoError > {
164
190
let version = Version :: get_latest ( identifier, state) . await ;
165
191
let decrypted_key = Key :: get_key ( state, identifier, version) . await . switch ( ) ?;
192
+
193
+ let stored_token = decrypted_key. token ;
194
+ let provided_token = custodian. into_access_token ( state) ;
195
+
196
+ ensure ! (
197
+ !identifier. is_entity( ) || ( stored_token. eq( & provided_token) ) ,
198
+ errors:: CryptoError :: AuthenticationFailed
199
+ ) ;
200
+
166
201
let key = GcmAes256 :: new ( decrypted_key. key ) ?;
167
202
168
203
let encrypted_data = key. encrypt ( self . inner ( ) ) ?;
@@ -180,9 +215,19 @@ impl DataDecrypter<DecryptedData> for EncryptedData {
180
215
self ,
181
216
state : & AppState ,
182
217
identifier : & Identifier ,
218
+ custodian : Custodian ,
183
219
) -> errors:: CustomResult < DecryptedData , errors:: CryptoError > {
184
220
let version = self . version ;
185
221
let decrypted_key = Key :: get_key ( state, identifier, version) . await . switch ( ) ?;
222
+
223
+ let stored_token = decrypted_key. token ;
224
+ let provided_token = custodian. into_access_token ( state) ;
225
+
226
+ ensure ! (
227
+ !identifier. is_entity( ) || ( stored_token. eq( & provided_token) ) ,
228
+ errors:: CryptoError :: AuthenticationFailed
229
+ ) ;
230
+
186
231
let key = GcmAes256 :: new ( decrypted_key. key ) ?;
187
232
188
233
let decrypted_data = key. decrypt ( self . inner ( ) ) ?;
0 commit comments