Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:jpmorganchase/constellation
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmn committed Jun 5, 2017
2 parents 2b6cef9 + 67707bb commit 9d83d41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
22 changes: 19 additions & 3 deletions Constellation/Enclave/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,34 @@ import Control.Monad.Trans.Either (EitherT(EitherT), runEitherT)
import qualified Crypto.Saltine.Class as S
import qualified Crypto.Saltine.Core.Box as Box
import qualified Data.Aeson as AE
import qualified Data.ByteString.Base64.Lazy as B64L
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T

import Constellation.Enclave.Types (PublicKey(PublicKey), mkPublicKey)
import Constellation.Enclave.Types
(PublicKey(PublicKey, unPublicKey), mkPublicKey)
import Constellation.Util.ByteString (b64TextDecodeBs)
import Constellation.Util.Either (fromShowRight, flattenEithers, maybeToEitherT)
import Constellation.Util.Lockable (unlock, promptingUnlock)
import Constellation.Util.Lockable
(Lockable(Unlocked), lock, promptingUnlock, unlock)

newKeyPair :: IO (PublicKey, Box.SecretKey)
newKeyPair = do
(priv, pub) <- Box.newKeypair
return (PublicKey pub, priv)

b64EncodePublicKey :: PublicKey -> BL.ByteString
b64EncodePublicKey = B64L.encode . BL.fromStrict . S.encode . unPublicKey

-- | Optionally takes a password to lock the private key.
jsonEncodePrivateKey :: Maybe String -> Box.SecretKey -> IO BL.ByteString
jsonEncodePrivateKey mpwd priv = AE.encode <$> mkLockable
where
mkLockable = case mpwd of
Nothing -> return $ Unlocked (S.encode priv)
Just "" -> return $ Unlocked (S.encode priv)
Just pwd -> lock pwd (S.encode priv)

loadKeyPair :: (FilePath, FilePath, Maybe String)
-> IO (Either String (PublicKey, Box.SecretKey))
loadKeyPair (pubPath, privPath, mpwd) = runEitherT $ do
Expand All @@ -33,7 +49,7 @@ loadKeyPair (pubPath, privPath, mpwd) = runEitherT $ do
Just pwd -> return $ unlock pwd locked
Nothing -> promptingUnlock locked
liftIO $ putStrLn $ "Unlocked " ++ privPath
(pub,) <$> maybeToEitherT "Failed to S.encode privBs" (S.decode privBs)
(pub,) <$> maybeToEitherT "Failed to S.decode privBs" (S.decode privBs)

loadPublicKey :: FilePath -> IO (Either String PublicKey)
loadPublicKey pubPath = runEitherT $ do
Expand Down
18 changes: 5 additions & 13 deletions Constellation/Enclave/Keygen/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ import ClassyPrelude hiding (getArgs, writeFile)
import System.Console.Haskeline (runInputT, defaultSettings, getPassword)
import System.Environment (getArgs, getProgName)
import Text.Printf (printf)
import qualified Crypto.Saltine.Class as S
import qualified Data.Aeson as AE
import qualified Data.ByteString.Base64.Lazy as B64L
import qualified Data.ByteString.Lazy as BL

import Constellation.Enclave.Key (newKeyPair)
import Constellation.Enclave.Types (PublicKey(unPublicKey))
import Constellation.Util.Lockable (Lockable(Unlocked), lock)
import Constellation.Enclave.Key
(newKeyPair, b64EncodePublicKey, jsonEncodePrivateKey)
import Constellation.Util.Text (tformat)

defaultMain :: IO ()
Expand All @@ -28,13 +24,9 @@ generateKeyPair name = do
mpwd <- runInputT defaultSettings $
getPassword (Just '*') (printf "Lock key pair %s with password [none]: " name)
(pub, priv) <- newKeyPair
BL.writeFile (name ++ ".pub") $ B64L.encode $ BL.fromStrict $ S.encode $
unPublicKey pub
k <- case mpwd of
Nothing -> return $ Unlocked (S.encode priv)
Just "" -> return $ Unlocked (S.encode priv)
Just pwd -> lock pwd (S.encode priv)
BL.writeFile (name ++ ".key") $ AE.encode k
BL.writeFile (name ++ ".pub") $ b64EncodePublicKey pub
json <- jsonEncodePrivateKey mpwd priv
BL.writeFile (name ++ ".key") json

usage :: IO ()
usage = getProgName >>= \progName ->
Expand Down

0 comments on commit 9d83d41

Please sign in to comment.