/
Types.hs
431 lines (344 loc) · 11.9 KB
/
Types.hs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
{-# LANGUAGE CPP #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports#-}
-- | The KeyStore and Associated Types
--
-- Note that most of these types and functions were generated by the
-- api-tools ("Data.Api.Tools") from the schema in "Data.KeyStore.Types.Schema",
-- marked down in <https://github.com/cdornan/keystore/blob/master/schema.md>.
module Data.KeyStore.Types
( module Data.KeyStore.Types
, module Data.KeyStore.Types.NameAndSafeguard
, module Data.KeyStore.Types.E
, module Data.KeyStore.Types.UTC
, PublicKey(..)
, PrivateKey(..)
) where
import qualified Control.Lens as L
import qualified Crypto.PBKDF.ByteString as P
import Crypto.PubKey.RSA (PublicKey(..), PrivateKey(..))
import Data.KeyStore.Types.Schema
import Data.KeyStore.Types.NameAndSafeguard
import Data.KeyStore.Types.E
import Data.KeyStore.Types.UTC
import Data.KeyStore.Types.AesonCompat
import Data.API.Tools
import Data.API.JSON
import Data.API.Types
import qualified Data.ByteString as B
import Data.Coerce
import qualified Data.HashMap.Strict as HM
import Data.List
import qualified Data.Map as Map
import Data.Ord
import qualified Data.Text as T
import Data.Time
import Data.String
import qualified Data.Vector as V
import Text.KSRegex
$(generate keystoreSchema)
deriving instance Num Iterations
deriving instance Num Octets
-- | Keystore session context, created at the start of a session and passed
-- to the keystore access functions.
data Pattern =
Pattern
{ _pat_string :: String
, _pat_regex :: Regex
}
instance Eq Pattern where
(==) pat pat' = _pat_string pat == _pat_string pat'
instance Show Pattern where
show pat = "Pattern " ++ show(_pat_string pat) ++ " <regex>"
instance IsString Pattern where
fromString s =
Pattern
{ _pat_string = s
, _pat_regex = mkRegex s
}
pattern :: String -> Pattern
pattern = fromString
inj_pattern :: REP__Pattern -> ParserWithErrs Pattern
inj_pattern (REP__Pattern t) =
return $
Pattern
{ _pat_string = s
, _pat_regex = mkRegex s
}
where
s = T.unpack t
prj_pattern :: Pattern -> REP__Pattern
prj_pattern = REP__Pattern . T.pack . _pat_string
newtype Settings = Settings { _Settings :: HM.HashMap T.Text Value }
deriving (Eq,Show)
inj_settings :: REP__Settings -> ParserWithErrs Settings
inj_settings REP__Settings { _stgs_json = Object o}
= return $ Settings $ fromKM o
inj_settings _ = fail "object expected for settings"
prj_settings :: Settings -> REP__Settings
prj_settings (Settings hm) = REP__Settings { _stgs_json = Object $ intoKM hm }
defaultSettings :: Settings
defaultSettings = mempty
#if __GLASGOW_HASKELL__ >= 804
instance Semigroup Settings where
(<>) = mappendSettings
#endif
instance Monoid Settings where
mempty = Settings mempty
-- mappend = mappendSettings
mappendSettings :: Settings -> Settings -> Settings
mappendSettings (Settings fm_0) (Settings fm_1) =
Settings $ HM.unionWith cmb fm_0 fm_1
where
cmb v0 v1 =
case (v0,v1) of
(Array v_0,Array v_1) -> Array $ v_0 V.++ v_1
_ -> marker
checkSettingsCollisions :: Settings -> [SettingID]
checkSettingsCollisions (Settings hm) =
[ SettingID k | (k,v)<-HM.toList hm, v==marker ]
marker :: Value
marker = String "*** Collision * in * Settings ***"
inj_safeguard :: REP__Safeguard -> ParserWithErrs Safeguard
inj_safeguard = return . safeguard . _sg_names
prj_safeguard :: Safeguard -> REP__Safeguard
prj_safeguard = REP__Safeguard . safeguardKeys
inj_name :: REP__Name -> ParserWithErrs Name
inj_name = e2p . name . T.unpack . _REP__Name
prj_name :: Name -> REP__Name
prj_name = REP__Name . T.pack . _name
inj_PublicKey :: REP__PublicKey -> ParserWithErrs PublicKey
inj_PublicKey REP__PublicKey{..} =
return
PublicKey
{ public_size = _puk_size
, public_n = _puk_n
, public_e = _puk_e
}
prj_PublicKey :: PublicKey -> REP__PublicKey
prj_PublicKey PublicKey{..} =
REP__PublicKey
{ _puk_size = public_size
, _puk_n = public_n
, _puk_e = public_e
}
inj_PrivateKey :: REP__PrivateKey -> ParserWithErrs PrivateKey
inj_PrivateKey REP__PrivateKey{..} =
return
PrivateKey
{ private_pub = _prk_pub
, private_d = _prk_d
, private_p = _prk_p
, private_q = _prk_q
, private_dP = _prk_dP
, private_dQ = _prk_dQ
, private_qinv = _prk_qinv
}
prj_PrivateKey :: PrivateKey -> REP__PrivateKey
prj_PrivateKey PrivateKey{..} =
REP__PrivateKey
{ _prk_pub = private_pub
, _prk_d = private_d
, _prk_p = private_p
, _prk_q = private_q
, _prk_dP = private_dP
, _prk_dQ = private_dQ
, _prk_qinv = private_qinv
}
e2p :: E a -> ParserWithErrs a
e2p = either (fail . showReason) return
data Dirctn
= Encrypting
| Decrypting
deriving (Show)
pbkdf :: HashPRF
-> ClearText
-> Salt
-> Iterations
-> Octets
-> (B.ByteString->a)
-> a
pbkdf hp (ClearText dat) (Salt st) (Iterations k) (Octets wd) c =
c $ fn (_Binary dat) (_Binary st) k wd
where
fn = case hp of
PRF_sha1 -> P.sha1PBKDF2
PRF_sha256 -> P.sha256PBKDF2
PRF_sha512 -> P.sha512PBKDF2
keyWidth :: Cipher -> Octets
keyWidth aes =
case aes of
CPH_aes128 -> Octets 16
CPH_aes192 -> Octets 24
CPH_aes256 -> Octets 32
void_ :: Void
void_ = Void 0
map_from_list :: Ord a
=> String
-> (c->[b])
-> (b->a)
-> (a->T.Text)
-> c
-> ParserWithErrs (Map.Map a b)
map_from_list ty xl xf xt c =
case [ xt $ xf b | b:_:_<-obss ] of
[] -> return $ Map.fromDistinctAscList ps
ds -> fail $ ty ++ ": " ++ show ds ++ "duplicated"
where
ps = [ (xf b,b) | [b]<-obss ]
obss = groupBy same $ sortBy (comparing xf) $ xl c
same b b' = comparing xf b b' == EQ
$(generateAPITools keystoreSchema
[ enumTool
, jsonTool'
, lensTool
])
instance ToJSON KeyStore where
toJSON = toJSON . toKeyStore_
instance FromJSON KeyStore where
parseJSON = fmap fromKeyStore_ . parseJSON
instance FromJSONWithErrs KeyStore where
parseJSONWithErrs = fmap fromKeyStore_ . parseJSONWithErrs
data KeyStore =
KeyStore
{ _ks_config :: Configuration
, _ks_keymap :: KeyMap
}
deriving (Show,Eq)
toKeyStore_ :: KeyStore -> KeyStore_
toKeyStore_ KeyStore{..} =
KeyStore_
{ _z_ks_config = toConfiguration_ _ks_config
, _z_ks_keymap = toKeyMap_ _ks_keymap
}
fromKeyStore_ :: KeyStore_ -> KeyStore
fromKeyStore_ KeyStore_{..} =
KeyStore
{ _ks_config = fromConfiguration_ _z_ks_config
, _ks_keymap = fromKeyMap_ _z_ks_keymap
}
emptyKeyStore :: Configuration -> KeyStore
emptyKeyStore cfg =
KeyStore
{ _ks_config = cfg
, _ks_keymap = emptyKeyMap
}
data Configuration =
Configuration
{ _cfg_settings :: Settings
, _cfg_triggers :: TriggerMap
}
deriving (Show,Eq)
toConfiguration_ :: Configuration -> Configuration_
toConfiguration_ Configuration{..} =
Configuration_
{ _z_cfg_settings = _cfg_settings
, _z_cfg_triggers = toTriggerMap_ _cfg_triggers
}
fromConfiguration_ :: Configuration_ -> Configuration
fromConfiguration_ Configuration_{..} =
Configuration
{ _cfg_settings = _z_cfg_settings
, _cfg_triggers = fromTriggerMap_ _z_cfg_triggers
}
defaultConfiguration :: Settings -> Configuration
defaultConfiguration stgs =
Configuration
{ _cfg_settings = stgs
, _cfg_triggers = Map.empty
}
type TriggerMap = Map.Map TriggerID Trigger
toTriggerMap_ :: TriggerMap -> TriggerMap_
toTriggerMap_ mp = TriggerMap_ $ Map.elems mp
fromTriggerMap_ :: TriggerMap_ -> TriggerMap
fromTriggerMap_ TriggerMap_{..} = Map.fromList
[ (,) _trg_id trg
| trg@Trigger{..} <- _z_tmp_map
]
type KeyMap = Map.Map Name Key
toKeyMap_ :: KeyMap -> KeyMap_
toKeyMap_ mp = KeyMap_ $
[ NameKeyAssoc_ nm $ toKey_ ky
| (nm,ky) <- Map.assocs mp
]
fromKeyMap_ :: KeyMap_ -> KeyMap
fromKeyMap_ mp_ = Map.fromList
[ (_z_nka_name,fromKey_ _z_nka_key)
| NameKeyAssoc_{..} <- _z_kmp_map mp_
]
emptyKeyMap :: KeyMap
emptyKeyMap = Map.empty
data Key =
Key
{ _key_name :: Name
, _key_comment :: Comment
, _key_identity :: Identity
, _key_is_binary :: Bool
, _key_env_var :: Maybe EnvVar
, _key_hash :: Maybe Hash
, _key_public :: Maybe PublicKey
, _key_secret_copies :: EncrypedCopyMap
, _key_clear_text :: Maybe ClearText
, _key_clear_private :: Maybe PrivateKey
, _key_created_at :: UTCTime
}
deriving (Show,Eq)
toKey_ :: Key -> Key_
toKey_ Key{..} =
Key_
{ _z_key_name = _key_name
, _z_key_comment = _key_comment
, _z_key_identity = _key_identity
, _z_key_is_binary = _key_is_binary
, _z_key_env_var = _key_env_var
, _z_key_hash = _key_hash
, _z_key_public = _key_public
, _z_key_secret_copies = toEncrypedCopyMap _key_secret_copies
, _z_key_clear_text = _key_clear_text
, _z_key_clear_private = _key_clear_private
, _z_key_created_at = UTC _key_created_at
}
fromKey_ :: Key_ -> Key
fromKey_ Key_{..} =
Key
{ _key_name = _z_key_name
, _key_comment = _z_key_comment
, _key_identity = _z_key_identity
, _key_is_binary = _z_key_is_binary
, _key_env_var = _z_key_env_var
, _key_hash = _z_key_hash
, _key_public = _z_key_public
, _key_secret_copies = fromEncrypedCopyMap _z_key_secret_copies
, _key_clear_text = _z_key_clear_text
, _key_clear_private = _z_key_clear_private
, _key_created_at = _UTC _z_key_created_at
}
type EncrypedCopyMap = Map.Map Safeguard EncrypedCopy
toEncrypedCopyMap :: EncrypedCopyMap -> EncrypedCopyMap_
toEncrypedCopyMap mp = EncrypedCopyMap_ $ Map.elems mp
fromEncrypedCopyMap :: EncrypedCopyMap_ -> EncrypedCopyMap
fromEncrypedCopyMap EncrypedCopyMap_{..} = Map.fromList
[ (,) _ec_safeguard ec
| ec@EncrypedCopy{..} <- _z_ecm_map
]
L.makeLenses ''KeyStore
L.makeLenses ''Configuration
L.makeLenses ''Key