-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.hs
414 lines (323 loc) · 16.9 KB
/
IO.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
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.X509.IO
( readASN1ObjectBytes, readASN1ObjectBytesLazy, readASN1ObjectFile
, writeASN1ObjectBytes, writeASN1ObjectBytesLazy, writeASN1ObjectFile
, readSignedObjectBytes, readSignedObjectBytesLazy, readSignedObjectFile
, writeSignedObjectBytes, writeSignedObjectBytesLazy
, writeSignedObjectFile
, readKeyBytes, readKeyBytesLazy, readKeyFile
, writeKeyBytes, writeKeyBytesLazy, writeKeyFile
, readCertificateBytes, readCertificateBytesLazy, readCertificateFile
, writeCertificateBytes, writeCertificateBytesLazy, writeCertificateFile
)
where
-- asn1-types ----------------------------------------------------------------
import Data.ASN1.Types
( ASN1Object, fromASN1, toASN1
, ASN1
( Start, BitString, IntVal, OctetString, OID, Null
, End
)
, ASN1Class (Context)
, ASN1ConstructionType (Container, Sequence)
)
import Data.ASN1.BitArray (BitArray (BitArray), bitArrayGetData)
-- asn1-encoding -------------------------------------------------------------
import Data.ASN1.BinaryEncoding (BER (BER), DER (DER))
import Data.ASN1.Encoding (encodeASN1', decodeASN1')
-- base ----------------------------------------------------------------------
import Data.Bifunctor (first)
import Data.Either (rights)
-- bytestring ----------------------------------------------------------------
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
-- cryptonite ----------------------------------------------------------------
import Crypto.Number.Serialize (i2osp, os2ip)
import qualified Crypto.PubKey.DSA as DSA
import qualified Crypto.PubKey.RSA as RSA
import Crypto.PubKey.ECC.Types (CurveName (..))
-- pem -----------------------------------------------------------------------
import Data.PEM
( PEM (PEM), pemWriteBS, pemParseBS
, pemWriteLBS, pemParseLBS
)
-- x509 ----------------------------------------------------------------------
import Data.X509
( PrivKey (PrivKeyRSA, PrivKeyDSA, PrivKeyEC)
, PrivKeyEC (PrivKeyEC_Named, PrivKeyEC_Prime)
, SerializedPoint (SerializedPoint)
, SignedCertificate, SignedExact
, decodeSignedObject, encodeSignedObject
)
import Data.X509.EC (lookupCurveNameByOID)
------------------------------------------------------------------------------
instance ASN1Object RSA.PrivateKey where
fromASN1 (Start Sequence : IntVal 0 : IntVal n : IntVal e : IntVal d
: IntVal p : IntVal q : IntVal dP : IntVal dQ : IntVal qinv
: End Sequence : as) = pure (key, as)
where
key = RSA.PrivateKey (RSA.PublicKey (go n 1) n e) d p q dP dQ qinv
go m i
| 2 ^ (i * 8) > m = i
| otherwise = go m (i + 1)
fromASN1 (Start Sequence : IntVal 0 : Start Sequence
: OID [1, 2, 840, 113549, 1, 1, 1] : Null : End Sequence
: OctetString bytes : End Sequence : as) = do
asn1 <- first failure $ decodeASN1' BER bytes
fmap (const as) <$> fromASN1 asn1
where
failure = ("RSA.PrivateKey.fromASN1: " ++) . show
fromASN1 _ = Left "RSA.PrivateKey.fromASN1: unexpected format"
toASN1 key = (++)
[ Start Sequence, IntVal 0, IntVal n, IntVal e, IntVal d, IntVal p
, IntVal q, IntVal dP, IntVal dQ, IntVal qinv, End Sequence
]
where
RSA.PrivateKey (RSA.PublicKey _ n e) d p q dP dQ qinv = key
------------------------------------------------------------------------------
instance ASN1Object DSA.PrivateKey where
fromASN1 (Start Sequence : IntVal 0 : IntVal p : IntVal q : IntVal g
: IntVal _ : IntVal x : End Sequence : as) =
pure (DSA.PrivateKey (DSA.Params p g q) x, as)
fromASN1 (Start Sequence : IntVal 0 : Start Sequence
: OID [1, 2, 840, 10040, 4, 1] : Start Sequence : IntVal p : IntVal q
: IntVal g : End Sequence : End Sequence : OctetString bytes
: End Sequence : as) = case decodeASN1' BER bytes of
Right [IntVal x] -> pure (DSA.PrivateKey (DSA.Params p g q) x, as)
Right _ -> Left "DSA.PrivateKey.fromASN1: unexpected format"
Left e -> Left $ "DSA.PrivateKey.fromASN1: " ++ show e
fromASN1 _ = Left "DSA.PrivateKey.fromASN1: unexpected format"
toASN1 (DSA.PrivateKey params@(DSA.Params p g q) y) = (++)
[ Start Sequence, IntVal 0, IntVal p, IntVal q, IntVal g, IntVal x
, IntVal y, End Sequence
]
where
x = DSA.calculatePublic params y
------------------------------------------------------------------------------
instance ASN1Object PrivKeyEC where
fromASN1 = go []
where
failing = ("ECDSA.PrivateKey.fromASN1: " ++)
go acc (Start Sequence : IntVal 1 : OctetString bytes : rest) = do
key <- subgo (oid ++ acc)
case rest'' of
End Sequence : rest''' -> pure (key, rest''')
_ -> Left $ failing "unexpected EC format"
where
d = os2ip bytes
(oid, rest') = spanTag 0 rest
(_, rest'') = spanTag 1 rest'
subgo (OID oid_ : _) = maybe failure success mcurve
where
failure = Left $ failing $ "unknown curve " ++ show oid_
success = Right . flip PrivKeyEC_Named d
mcurve = lookupCurveNameByOID oid_
subgo (Start Sequence : IntVal 1 : Start Sequence
: OID [1, 2, 840, 10045, 1, 1] : IntVal p : End Sequence
: Start Sequence : OctetString a : OctetString b : BitString s
: End Sequence : OctetString g : IntVal o : IntVal c
: End Sequence : _) =
pure $ PrivKeyEC_Prime d a' b' p g' o c s'
where
a' = os2ip a
b' = os2ip b
g' = SerializedPoint g
s' = os2ip $ bitArrayGetData s
subgo (Null : rest_) = subgo rest_
subgo [] = Left $ failing "curve is missing"
subgo _ = Left $ failing "unexpected curve format"
go acc (Start Sequence : IntVal 0 : Start Sequence
: OID [1, 2, 840, 10045, 2, 1] : rest) = case rest' of
(OctetString bytes : rest'') -> do
asn1 <- first (failing . show) (decodeASN1' BER bytes)
fmap (const rest'') <$> go (oid ++ acc) asn1
_ -> Left $ failing "unexpected EC format"
where
(oid, rest') = spanEnd 0 rest
go _ _ = Left $ failing "unexpected EC format"
toASN1 (PrivKeyEC_Named curveName d) = (++)
[ Start Sequence, IntVal 1, OctetString (i2osp d)
, Start (Container Context 0), OID oid, End (Container Context 0)
, End Sequence
]
where
oid = case curveName of
SEC_p112r1 -> [1, 3, 132, 0, 6]
SEC_p112r2 -> [1, 3, 132, 0, 7]
SEC_p128r1 -> [1, 3, 132, 0, 28]
SEC_p128r2 -> [1, 3, 132, 0, 29]
SEC_p160k1 -> [1, 3, 132, 0, 9]
SEC_p160r1 -> [1, 3, 132, 0, 8]
SEC_p160r2 -> [1, 3, 132, 0, 30]
SEC_p192k1 -> [1, 3, 132, 0, 31]
SEC_p192r1 -> [1, 2, 840, 10045, 3, 1, 1]
SEC_p224k1 -> [1, 3, 132, 0, 32]
SEC_p224r1 -> [1, 3, 132, 0, 33]
SEC_p256k1 -> [1, 3, 132, 0, 10]
SEC_p256r1 -> [1, 2, 840, 10045, 3, 1, 7]
SEC_p384r1 -> [1, 3, 132, 0, 34]
SEC_p521r1 -> [1, 3, 132, 0, 35]
SEC_t113r1 -> [1, 3, 132, 0, 4]
SEC_t113r2 -> [1, 3, 132, 0, 5]
SEC_t131r1 -> [1, 3, 132, 0, 22]
SEC_t131r2 -> [1, 3, 132, 0, 23]
SEC_t163k1 -> [1, 3, 132, 0, 1]
SEC_t163r1 -> [1, 3, 132, 0, 2]
SEC_t163r2 -> [1, 3, 132, 0, 15]
SEC_t193r1 -> [1, 3, 132, 0, 24]
SEC_t193r2 -> [1, 3, 132, 0, 25]
SEC_t233k1 -> [1, 3, 132, 0, 26]
SEC_t233r1 -> [1, 3, 132, 0, 27]
SEC_t239k1 -> [1, 3, 132, 0, 3]
SEC_t283k1 -> [1, 3, 132, 0, 16]
SEC_t283r1 -> [1, 3, 132, 0, 17]
SEC_t409k1 -> [1, 3, 132, 0, 36]
SEC_t409r1 -> [1, 3, 132, 0, 37]
SEC_t571k1 -> [1, 3, 132, 0, 38]
SEC_t571r1 -> [1, 3, 132, 0, 39]
toASN1 (PrivKeyEC_Prime d a b p g o c s) = (++)
[ Start Sequence, IntVal 1, OctetString (i2osp d)
, Start (Container Context 0), Start Sequence, IntVal 1
, Start Sequence, OID [1, 2, 840, 10045, 1, 1], IntVal p, End Sequence
, Start Sequence, OctetString a', OctetString b', BitString s'
, End Sequence, OctetString g' , IntVal o, IntVal c, End Sequence
, End (Container Context 0), End Sequence
]
where
a' = i2osp a
b' = i2osp b
SerializedPoint g' = g
s' = BitArray (8 * fromIntegral (B.length bytes)) bytes
where
bytes = i2osp s
------------------------------------------------------------------------------
asn1ToPEM :: String -> [ASN1] -> PEM
asn1ToPEM name = PEM name [] . encodeASN1' DER
------------------------------------------------------------------------------
pemToASN1 :: PEM -> Either String [ASN1]
pemToASN1 (PEM _ _ content) = first show $ decodeASN1' BER content
------------------------------------------------------------------------------
asn1ObjectToPEM :: ASN1Object a => String -> a -> PEM
asn1ObjectToPEM name = asn1ToPEM name . flip toASN1 []
------------------------------------------------------------------------------
pemToASN1Object :: ASN1Object a => PEM -> Either String a
pemToASN1Object pem = fmap fst $ pemToASN1 pem >>= fromASN1
------------------------------------------------------------------------------
readASN1ObjectBytes :: ASN1Object a => ByteString -> [a]
readASN1ObjectBytes = either (const []) (rights . fmap pemToASN1Object)
. pemParseBS
------------------------------------------------------------------------------
readASN1ObjectBytesLazy :: ASN1Object a => L.ByteString -> [a]
readASN1ObjectBytesLazy = either (const []) (rights . fmap pemToASN1Object)
. pemParseLBS
------------------------------------------------------------------------------
readASN1ObjectFile :: ASN1Object a => FilePath -> IO [a]
readASN1ObjectFile = fmap readASN1ObjectBytes . B.readFile
------------------------------------------------------------------------------
writeASN1ObjectBytes :: ASN1Object a => String -> [a] -> ByteString
writeASN1ObjectBytes name = foldMap (pemWriteBS . asn1ObjectToPEM name)
------------------------------------------------------------------------------
writeASN1ObjectBytesLazy :: ASN1Object a => String -> [a] -> L.ByteString
writeASN1ObjectBytesLazy name = foldMap (pemWriteLBS . asn1ObjectToPEM name)
------------------------------------------------------------------------------
writeASN1ObjectFile :: ASN1Object a => String -> FilePath -> [a] -> IO ()
writeASN1ObjectFile name path = L.writeFile path
. writeASN1ObjectBytesLazy name
------------------------------------------------------------------------------
signedObjectToPEM :: (ASN1Object a, Eq a, Show a)
=> String -> SignedExact a -> PEM
signedObjectToPEM name = PEM name [] . encodeSignedObject
------------------------------------------------------------------------------
pemToSignedObject :: (ASN1Object a, Eq a, Show a)
=> PEM -> Either String (SignedExact a)
pemToSignedObject (PEM _ _ content) = decodeSignedObject content
------------------------------------------------------------------------------
readSignedObjectBytes :: (Eq a, Show a, ASN1Object a)
=> ByteString -> [SignedExact a]
readSignedObjectBytes = either (const []) (rights . fmap pemToSignedObject)
. pemParseBS
------------------------------------------------------------------------------
readSignedObjectBytesLazy :: (Eq a, Show a, ASN1Object a)
=> L.ByteString -> [SignedExact a]
readSignedObjectBytesLazy = either (const []) (rights . fmap pemToSignedObject)
. pemParseLBS
------------------------------------------------------------------------------
readSignedObjectFile :: (Eq a, Show a, ASN1Object a)
=> FilePath -> IO [SignedExact a]
readSignedObjectFile = fmap readSignedObjectBytes . B.readFile
------------------------------------------------------------------------------
writeSignedObjectBytes :: (Eq a, Show a, ASN1Object a)
=> String -> [SignedExact a] -> ByteString
writeSignedObjectBytes = foldMap . (pemWriteBS .) . signedObjectToPEM
------------------------------------------------------------------------------
writeSignedObjectBytesLazy :: (Eq a, Show a, ASN1Object a)
=> String -> [SignedExact a] -> L.ByteString
writeSignedObjectBytesLazy = foldMap . (pemWriteLBS .) . signedObjectToPEM
------------------------------------------------------------------------------
writeSignedObjectFile :: (Eq a, Show a, ASN1Object a)
=> String -> FilePath -> [SignedExact a] -> IO ()
writeSignedObjectFile name path = L.writeFile path
. writeSignedObjectBytesLazy name
------------------------------------------------------------------------------
pemToKey :: PEM -> Either String PrivKey
pemToKey pem@(PEM "PRIVATE KEY" _ _) = pemToASN1Object pem
pemToKey pem@(PEM "RSA PRIVATE KEY" _ _) = PrivKeyRSA <$> pemToASN1Object pem
pemToKey pem@(PEM "DSA PRIVATE KEY" _ _) = PrivKeyDSA <$> pemToASN1Object pem
pemToKey pem@(PEM "EC PRIVATE KEY" _ _) = PrivKeyEC <$> pemToASN1Object pem
pemToKey (PEM name _ _) = Left $ "Not a key: " ++ name
------------------------------------------------------------------------------
keyToPEM :: PrivKey -> PEM
keyToPEM (PrivKeyRSA key) = asn1ObjectToPEM "RSA PRIVATE KEY" key
keyToPEM (PrivKeyDSA key) = asn1ObjectToPEM "DSA PRIVATE KEY" key
keyToPEM (PrivKeyEC key) = asn1ObjectToPEM "EC PRIVATE KEY" key
keyToPEM key = asn1ObjectToPEM "PRIVATE KEY" key
------------------------------------------------------------------------------
readKeyBytes :: ByteString -> [PrivKey]
readKeyBytes = either (const []) (rights . fmap pemToKey) . pemParseBS
------------------------------------------------------------------------------
readKeyBytesLazy :: L.ByteString -> [PrivKey]
readKeyBytesLazy = either (const []) (rights . fmap pemToKey) . pemParseLBS
------------------------------------------------------------------------------
readKeyFile :: FilePath -> IO [PrivKey]
readKeyFile = fmap readKeyBytes . B.readFile
------------------------------------------------------------------------------
writeKeyBytes :: [PrivKey] -> ByteString
writeKeyBytes = foldMap (pemWriteBS . keyToPEM)
------------------------------------------------------------------------------
writeKeyBytesLazy :: [PrivKey] -> L.ByteString
writeKeyBytesLazy = foldMap (pemWriteLBS . keyToPEM)
------------------------------------------------------------------------------
writeKeyFile :: FilePath -> [PrivKey] -> IO ()
writeKeyFile path = L.writeFile path . writeKeyBytesLazy
------------------------------------------------------------------------------
readCertificateBytes :: ByteString -> [SignedCertificate]
readCertificateBytes = readSignedObjectBytes
------------------------------------------------------------------------------
readCertificateBytesLazy :: L.ByteString -> [SignedCertificate]
readCertificateBytesLazy = readSignedObjectBytesLazy
------------------------------------------------------------------------------
readCertificateFile :: FilePath -> IO [SignedCertificate]
readCertificateFile = readSignedObjectFile
------------------------------------------------------------------------------
writeCertificateBytes :: [SignedCertificate] -> ByteString
writeCertificateBytes = writeSignedObjectBytes "CERTIFICATE"
------------------------------------------------------------------------------
writeCertificateBytesLazy :: [SignedCertificate] -> L.ByteString
writeCertificateBytesLazy = writeSignedObjectBytesLazy "CERTIFICATE"
------------------------------------------------------------------------------
writeCertificateFile :: FilePath -> [SignedCertificate] -> IO ()
writeCertificateFile = writeSignedObjectFile "CERTIFICATE"
------------------------------------------------------------------------------
spanEnd :: Word -> [ASN1] -> ([ASN1], [ASN1])
spanEnd = go id
where
go dlist n (a@(Start _) : as) = go (dlist . (a :)) (n + 1) as
go dlist 0 (End _ : as) = (dlist [], as)
go dlist n (a@(End _) : as) = go (dlist . (a :)) (n - 1) as
go dlist n (a : as) = go (dlist . (a :)) n as
go dlist _ [] = (dlist [], [])
------------------------------------------------------------------------------
spanTag :: Int -> [ASN1] -> ([ASN1], [ASN1])
spanTag a (Start (Container _ b) : as) | a == b = spanEnd 0 as
spanTag _ as = ([], as)