| 1 |
{-# LANGUAGE OverloadedStrings #-} |
| 2 |
-- ASCIIArmor/Decode.hs: OpenPGP (RFC4880) ASCII armor implementation |
| 3 |
-- Copyright © 2012-2018 Clint Adams |
| 4 |
-- This software is released under the terms of the Expat license. |
| 5 |
-- (See the LICENSE file). |
| 6 |
|
| 7 |
module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( |
| 8 |
parseArmor |
| 9 |
, decode |
| 10 |
, decodeLazy |
| 11 |
) where |
| 12 |
|
| 13 |
import Codec.Encryption.OpenPGP.ASCIIArmor.Types |
| 14 |
import Codec.Encryption.OpenPGP.ASCIIArmor.Utils |
| 15 |
import Control.Applicative (many, (<|>), (<$>), (<*), (<*>), (*>), optional) |
| 16 |
import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>)) |
| 17 |
import qualified Data.Attoparsec.ByteString as AS |
| 18 |
import qualified Data.Attoparsec.ByteString.Lazy as AL |
| 19 |
import Data.Attoparsec.ByteString.Char8 (isDigit_w8, anyChar) |
| 20 |
import Data.Bits (shiftL) |
| 21 |
import Data.ByteString.Lazy (ByteString) |
| 22 |
import qualified Data.ByteString as B |
| 23 |
import qualified Data.ByteString.Lazy as BL |
| 24 |
import qualified Data.ByteString.Char8 as BC8 |
| 25 |
import qualified Data.ByteString.Base64 as Base64 |
| 26 |
import Data.Digest.CRC24 (crc24) |
| 27 |
import Data.Binary.Get (Get, runGetOrFail, getWord8) |
| 28 |
import Data.Functor (($>)) |
| 29 |
import Data.String (IsString, fromString) |
| 30 |
import Data.Word (Word32) |
| 31 |
|
| 32 |
decode :: IsString e => B.ByteString -> Either e [Armor] |
| 33 |
decode bs = go (AS.parse parseArmors bs) |
| 34 |
where |
| 35 |
go (AS.Fail _ _ e) = Left (fromString e) |
| 36 |
go (AS.Partial cont) = go (cont B.empty) |
| 37 |
go (AS.Done _ r) = Right r |
| 38 |
|
| 39 |
decodeLazy :: IsString e => BL.ByteString -> Either e [Armor] |
| 40 |
decodeLazy bs = go (AL.parse parseArmors bs) |
| 41 |
where |
| 42 |
go (AL.Fail _ _ e) = Left (fromString e) |
| 43 |
go (AL.Done _ r) = Right r |
| 44 |
|
| 45 |
parseArmors :: Parser [Armor] |
| 46 |
parseArmors = many parseArmor |
| 47 |
|
| 48 |
parseArmor :: Parser Armor |
| 49 |
parseArmor = prefixed (clearsigned <|> armor) <?> "armor" |
| 50 |
|
| 51 |
clearsigned :: Parser Armor |
| 52 |
clearsigned = do |
| 53 |
_ <- string "-----BEGIN PGP SIGNED MESSAGE-----" <?> "clearsign header" |
| 54 |
_ <- lineEnding <?> "line ending" |
| 55 |
headers <- armorHeaders <?> "clearsign headers" |
| 56 |
_ <- blankishLine <?> "blank line" |
| 57 |
cleartext <- dashEscapedCleartext |
| 58 |
sig <- armor |
| 59 |
return $ ClearSigned headers cleartext sig |
| 60 |
|
| 61 |
armor :: Parser Armor |
| 62 |
armor = do |
| 63 |
atype <- beginLine <?> "begin line" |
| 64 |
headers <- armorHeaders <?> "headers" |
| 65 |
_ <- blankishLine <?> "blank line" |
| 66 |
payload <- base64Data <?> "base64 data" |
| 67 |
_ <- endLine atype <?> "end line" |
| 68 |
return $ Armor atype headers payload |
| 69 |
|
| 70 |
beginLine :: Parser ArmorType |
| 71 |
beginLine = do |
| 72 |
_ <- string "-----BEGIN PGP " <?> "leading minus-hyphens" |
| 73 |
atype <- pubkey <|> privkey <|> parts <|> message <|> signature |
| 74 |
_ <- string "-----" <?> "trailing minus-hyphens" |
| 75 |
_ <- many (satisfy (inClass " \t")) <?> "whitespace" |
| 76 |
_ <- lineEnding <?> "line ending" |
| 77 |
return atype |
| 78 |
where |
| 79 |
message = string "MESSAGE" $> ArmorMessage |
| 80 |
pubkey = string "PUBLIC KEY BLOCK" $> ArmorPublicKeyBlock |
| 81 |
privkey = string "PRIVATE KEY BLOCK" $> ArmorPrivateKeyBlock |
| 82 |
signature = string "SIGNATURE" $> ArmorSignature |
| 83 |
parts = string "MESSAGE, PART " *> (partsdef <|> partsindef) |
| 84 |
partsdef = do |
| 85 |
firstnum <- num |
| 86 |
_ <- word8 (fromIntegral . fromEnum $ '/') |
| 87 |
secondnum <- num |
| 88 |
return $ ArmorSplitMessage (BL.pack firstnum) (BL.pack secondnum) |
| 89 |
partsindef = ArmorSplitMessageIndefinite . BL.pack <$> num |
| 90 |
num = many1 (satisfy isDigit_w8) <?> "number" |
| 91 |
|
| 92 |
lineEnding :: Parser B.ByteString |
| 93 |
lineEnding = string "\n" <|> string "\r\n" |
| 94 |
|
| 95 |
armorHeaders :: Parser [(String, String)] |
| 96 |
armorHeaders = many armorHeader |
| 97 |
|
| 98 |
armorHeader :: Parser (String, String) |
| 99 |
armorHeader = do |
| 100 |
key <- many1 (satisfy (inClass "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) |
| 101 |
_ <- string ": " |
| 102 |
val <- many1 (satisfy (notInClass "\n\r")) |
| 103 |
_ <- lineEnding |
| 104 |
return (w8sToString key, w8sToString val) |
| 105 |
where |
| 106 |
w8sToString = BC8.unpack . B.pack |
| 107 |
|
| 108 |
blankishLine :: Parser B.ByteString |
| 109 |
blankishLine = many (satisfy (inClass " \t")) *> lineEnding |
| 110 |
|
| 111 |
endLine :: ArmorType -> Parser B.ByteString |
| 112 |
endLine atype = do |
| 113 |
_ <- string $ "-----END PGP " `B.append` aType atype `B.append` "-----" |
| 114 |
lineEnding |
| 115 |
|
| 116 |
aType :: ArmorType -> B.ByteString |
| 117 |
aType ArmorMessage = BC8.pack "MESSAGE" |
| 118 |
aType ArmorPublicKeyBlock = BC8.pack "PUBLIC KEY BLOCK" |
| 119 |
aType ArmorPrivateKeyBlock = BC8.pack "PRIVATE KEY BLOCK" |
| 120 |
aType (ArmorSplitMessage x y) = BC8.pack "MESSAGE, PART " `B.append` l2s x `B.append` BC8.singleton '/' `B.append` l2s y |
| 121 |
aType (ArmorSplitMessageIndefinite x) = BC8.pack "MESSAGE, PART " `B.append` l2s x |
| 122 |
aType ArmorSignature = BC8.pack "SIGNATURE" |
| 123 |
|
| 124 |
l2s :: BL.ByteString -> B.ByteString |
| 125 |
l2s = B.concat . BL.toChunks |
| 126 |
|
| 127 |
base64Data :: Parser ByteString |
| 128 |
base64Data = do |
| 129 |
ls <- many1 base64Line |
| 130 |
cksum <- checksumLine |
| 131 |
let payload = B.concat ls |
| 132 |
let ourcksum = crc24 payload |
| 133 |
case runGetOrFail d24 (BL.fromStrict cksum) of |
| 134 |
Left (_,_,err) -> fail err |
| 135 |
Right (_,_,theircksum) -> if theircksum == ourcksum then return (BL.fromStrict payload) else fail ("CRC24 mismatch: " ++ show (B.unpack cksum) ++ "/" ++ show theircksum ++ " vs. " ++ show ourcksum) |
| 136 |
where |
| 137 |
base64Line :: Parser B.ByteString |
| 138 |
base64Line = do |
| 139 |
b64 <- many1 (satisfy (inClass "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")) |
| 140 |
pad <- many (word8 (fromIntegral . fromEnum $ '=')) |
| 141 |
_ <- lineEnding |
| 142 |
let line = B.pack b64 `B.append` B.pack pad |
| 143 |
case Base64.decode line of |
| 144 |
Left err -> fail err |
| 145 |
Right bs -> return bs |
| 146 |
checksumLine :: Parser B.ByteString |
| 147 |
checksumLine = do |
| 148 |
_ <- word8 (fromIntegral . fromEnum $ '=') |
| 149 |
b64 <- many1 (satisfy (inClass "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")) |
| 150 |
_ <- lineEnding |
| 151 |
let line = B.pack b64 |
| 152 |
case Base64.decode line of |
| 153 |
Left err -> fail err |
| 154 |
Right bs -> return bs |
| 155 |
|
| 156 |
|
| 157 |
d24 :: Get Word32 |
| 158 |
d24 = do |
| 159 |
a <- getWord8 |
| 160 |
b <- getWord8 |
| 161 |
c <- getWord8 |
| 162 |
return $ shiftL (fromIntegral a :: Word32) 16 + shiftL (fromIntegral b :: Word32) 8 + (fromIntegral c :: Word32) |
| 163 |
|
| 164 |
prefixed :: Parser a -> Parser a |
| 165 |
prefixed end = end <|> anyChar *> prefixed end |
| 166 |
|
| 167 |
dashEscapedCleartext :: Parser ByteString |
| 168 |
dashEscapedCleartext = do |
| 169 |
ls <- many1 ((deLine <|> unescapedLine) <* lineEnding) |
| 170 |
return . BL.fromStrict $ crlfUnlines ls |
| 171 |
where |
| 172 |
deLine :: Parser B.ByteString |
| 173 |
deLine = B.pack <$> (string "- " *> many (satisfy (notInClass "\n\r"))) |
| 174 |
unescapedLine :: Parser B.ByteString |
| 175 |
unescapedLine = maybe B.empty B.pack <$> optional ((:) <$> satisfy (notInClass "-\n\r") <*> many (satisfy (notInClass "\n\r"))) |