summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Paul Weber <singpolyma@singpolyma.net>2011-08-01 16:36:15 -0500
committerStephen Paul Weber <singpolyma@singpolyma.net>2011-08-01 16:36:15 -0500
commit608b8430864306480af1eeac0dd326db4b187643 (patch)
treefb69bd9c8bd4b49f1a5deb4e2125003e50768dcd
parent502c7489e6c8cf4e5c39c0910fca232e6d9e7f63 (diff)
Refactor types, enums are Binary now
-rw-r--r--lib/OpenPGP.hs103
1 files changed, 61 insertions, 42 deletions
diff --git a/lib/OpenPGP.hs b/lib/OpenPGP.hs
index bf78af0..57f83c5 100644
--- a/lib/OpenPGP.hs
+++ b/lib/OpenPGP.hs
@@ -14,9 +14,6 @@ import qualified Codec.Compression.BZip as BZip2
14 14
15import qualified BaseConvert as BaseConvert 15import qualified BaseConvert as BaseConvert
16 16
17newtype Message = Message [Packet] deriving (Show, Read, Eq)
18newtype MPI = MPI Integer deriving (Show, Read, Eq, Ord)
19
20data Packet = 17data Packet =
21 OnePassSignaturePacket { 18 OnePassSignaturePacket {
22 version::Word8, 19 version::Word8,
@@ -45,35 +42,56 @@ data Packet =
45 UserIDPacket String 42 UserIDPacket String
46 deriving (Show, Read, Eq) 43 deriving (Show, Read, Eq)
47 44
48data HashAlgorithm = MD5 | SHA1 | RIPEMD160 | SHA256 | SHA384 | SHA512 | SHA224 deriving (Show, Read, Eq) 45data HashAlgorithm = MD5 | SHA1 | RIPEMD160 | SHA256 | SHA384 | SHA512 | SHA224
49data KeyAlgorithm = RSA | ELGAMAL | DSA | ECC | ECDSA | DH deriving (Show, Read, Eq) 46 deriving (Show, Read, Eq)
50data CompressionAlgorithm = Uncompressed | ZIP | ZLIB | BZip2 deriving (Show, Read, Eq) 47instance Binary HashAlgorithm where
51 48 get = do
52hash_algorithms :: (Num a) => a -> HashAlgorithm 49 tag <- get :: Get Word8
53hash_algorithms 1 = MD5 50 case tag of
54hash_algorithms 2 = SHA1 51 01 -> return MD5
55hash_algorithms 3 = RIPEMD160 52 02 -> return SHA1
56hash_algorithms 8 = SHA256 53 03 -> return RIPEMD160
57hash_algorithms 9 = SHA384 54 08 -> return SHA256
58hash_algorithms 10 = SHA512 55 09 -> return SHA384
59hash_algorithms 11 = SHA224 56 10 -> return SHA512
57 11 -> return SHA224
60 58
61key_algorithms :: (Num a) => a -> KeyAlgorithm 59data KeyAlgorithm = RSA | RSA_E | RSA_S | ELGAMAL | DSA | ECC | ECDSA | DH
62key_algorithms 1 = RSA 60 deriving (Show, Read, Eq)
63key_algorithms 2 = RSA 61instance Binary KeyAlgorithm where
64key_algorithms 3 = RSA 62 put RSA = put (01 :: Word8)
65key_algorithms 16 = ELGAMAL 63 put RSA_E = put (02 :: Word8)
66key_algorithms 17 = DSA 64 put RSA_S = put (03 :: Word8)
67key_algorithms 18 = ECC 65 put ELGAMAL = put (16 :: Word8)
68key_algorithms 19 = ECDSA 66 put DSA = put (17 :: Word8)
69key_algorithms 21 = DH 67 put ECC = put (18 :: Word8)
68 put ECDSA = put (19 :: Word8)
69 put DH = put (21 :: Word8)
70 get = do
71 tag <- get :: Get Word8
72 case tag of
73 01 -> return RSA
74 02 -> return RSA_E
75 03 -> return RSA_S
76 16 -> return ELGAMAL
77 17 -> return DSA
78 18 -> return ECC
79 19 -> return ECDSA
80 21 -> return DH
70 81
71public_key_fields :: KeyAlgorithm -> [Char] 82data CompressionAlgorithm = Uncompressed | ZIP | ZLIB | BZip2
72public_key_fields RSA = ['n', 'e'] 83 deriving (Show, Read, Eq)
73public_key_fields ELGAMAL = ['p', 'g', 'y'] 84instance Binary CompressionAlgorithm where
74public_key_fields DSA = ['p', 'q', 'g', 'y'] 85 get = do
86 tag <- get :: Get Word8
87 case tag of
88 0 -> return Uncompressed
89 1 -> return ZIP
90 2 -> return ZLIB
91 3 -> return BZip2
75 92
76-- A message is encoded as a list that takes the entire file 93-- A message is encoded as a list that takes the entire file
94newtype Message = Message [Packet] deriving (Show, Read, Eq)
77instance Binary Message where 95instance Binary Message where
78 put (Message []) = return () 96 put (Message []) = return ()
79 put (Message (x:xs)) = do 97 put (Message (x:xs)) = do
@@ -88,6 +106,7 @@ instance Binary Message where
88 (Message tail) <- get :: Get Message 106 (Message tail) <- get :: Get Message
89 return (Message (next_packet:tail)) 107 return (Message (next_packet:tail))
90 108
109newtype MPI = MPI Integer deriving (Show, Read, Eq, Ord)
91instance Binary MPI where 110instance Binary MPI where
92 put (MPI i) = do 111 put (MPI i) = do
93 put ((((fromIntegral (LZ.length bytes)) - 1) * 8) + (floor (logBase 2 (fromIntegral (bytes `LZ.index` 1)))) + 1 :: Word16) 112 put ((((fromIntegral (LZ.length bytes)) - 1) * 8) + (floor (logBase 2 (fromIntegral (bytes `LZ.index` 1)))) + 1 :: Word16)
@@ -147,15 +166,15 @@ parse_packet :: Word8 -> Get Packet
147parse_packet 4 = do 166parse_packet 4 = do
148 version <- get 167 version <- get
149 signature_type <- get 168 signature_type <- get
150 hash_algo <- get :: Get Word8 169 hash_algo <- get
151 key_algo <- get :: Get Word8 170 key_algo <- get
152 key_id <- get :: Get Word64 171 key_id <- get :: Get Word64
153 nested <- get 172 nested <- get
154 return (OnePassSignaturePacket { 173 return (OnePassSignaturePacket {
155 version = version, 174 version = version,
156 signature_type = signature_type, 175 signature_type = signature_type,
157 hash_algorithm = (hash_algorithms hash_algo), 176 hash_algorithm = hash_algo,
158 key_algorithm = (key_algorithms key_algo), 177 key_algorithm = key_algo,
159 key_id = (BaseConvert.toString 16 key_id), 178 key_id = (BaseConvert.toString 16 key_id),
160 nested = nested 179 nested = nested
161 }) 180 })
@@ -165,7 +184,7 @@ parse_packet 6 = do
165 case version of 184 case version of
166 4 -> do 185 4 -> do
167 timestamp <- get 186 timestamp <- get
168 algorithm <- fmap key_algorithms (get :: Get Word8) 187 algorithm <- get
169 key <- mapM (\f -> do 188 key <- mapM (\f -> do
170 mpi <- get :: Get MPI 189 mpi <- get :: Get MPI
171 return (f, mpi)) (public_key_fields algorithm) 190 return (f, mpi)) (public_key_fields algorithm)
@@ -177,27 +196,27 @@ parse_packet 6 = do
177 }) 196 })
178-- CompressedDataPacket, http://tools.ietf.org/html/rfc4880#section-5.6 197-- CompressedDataPacket, http://tools.ietf.org/html/rfc4880#section-5.6
179parse_packet 8 = do 198parse_packet 8 = do
180 algorithm <- get :: Get Word8 199 algorithm <- get
181 message <- getRemainingLazyByteString 200 message <- getRemainingLazyByteString
182 case algorithm of 201 case algorithm of
183 0 -> 202 Uncompressed ->
184 return (CompressedDataPacket { 203 return (CompressedDataPacket {
185 compressed_data_algorithm = Uncompressed, 204 compressed_data_algorithm = algorithm,
186 message = runGet (get :: Get Message) message 205 message = runGet (get :: Get Message) message
187 }) 206 })
188 1 -> 207 ZIP ->
189 return (CompressedDataPacket { 208 return (CompressedDataPacket {
190 compressed_data_algorithm = ZIP, 209 compressed_data_algorithm = algorithm,
191 message = runGet (get :: Get Message) (Zip.decompress message) 210 message = runGet (get :: Get Message) (Zip.decompress message)
192 }) 211 })
193 2 -> 212 ZLIB ->
194 return (CompressedDataPacket { 213 return (CompressedDataPacket {
195 compressed_data_algorithm = ZLIB, 214 compressed_data_algorithm = algorithm,
196 message = runGet (get :: Get Message) (Zlib.decompress message) 215 message = runGet (get :: Get Message) (Zlib.decompress message)
197 }) 216 })
198 3 -> 217 BZip2 ->
199 return (CompressedDataPacket { 218 return (CompressedDataPacket {
200 compressed_data_algorithm = BZip2, 219 compressed_data_algorithm = algorithm,
201 message = runGet (get :: Get Message) (BZip2.decompress message) 220 message = runGet (get :: Get Message) (BZip2.decompress message)
202 }) 221 })
203-- LiteralDataPacket, http://tools.ietf.org/html/rfc4880#section-5.9 222-- LiteralDataPacket, http://tools.ietf.org/html/rfc4880#section-5.9