-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Hackage security library -- @package hackage-security @version 0.1.0.0 -- | Checked exceptions module Hackage.Security.Util.Checked -- | Checked exceptions class Throws e unthrow :: proxy e -> (Throws e => a) -> a -- | Throw a checked exception throwChecked :: (Exception e, Throws e) => e -> IO a -- | Catch a checked exception catchChecked :: Exception e => (Throws e => IO a) -> (e -> IO a) -> IO a -- | catchChecked with the arguments reversed handleChecked :: Exception e => (e -> IO a) -> (Throws e => IO a) -> IO a -- | Like try, but for checked exceptions tryChecked :: Exception e => (Throws e => IO a) -> IO (Either e a) -- | Rethrow IO exceptions as checked exceptions checkIO :: Throws IOException => IO a -> IO a -- | Throw an unchecked exception -- -- This is just an alias for throw, but makes it evident that -- this is a very intentional use of an unchecked exception. throwUnchecked :: Exception e => e -> IO a -- | Variation on throwUnchecked for internal errors internalError :: String -> IO a instance [incoherent] Throws (Catch e) -- | Producing human-reaadable strings module Hackage.Security.Util.Pretty -- | Produce a human-readable string class Pretty a pretty :: Pretty a => a -> String -- | A more type-safe version of file paths -- -- This module is intended to replace imports of System.FilePath, and -- additionally exports thin wrappers around common IO functions. To -- facilitate importing this module unqualified we also re-export some -- definitions from System.IO (importing both would likely lead to name -- clashes). -- -- Note that his module does not import any other modules from -- Hackage.Security; everywhere else we use Path instead of FilePath -- directly. module Hackage.Security.Util.Path -- | Path fragments -- -- Path fragments must be non-empty and not contain any path delimiters. data Fragment mkFragment :: String -> Fragment unFragment :: Fragment -> String -- | Paths -- -- A path consists of an optional root and a list of fragments. -- Alternatively, think of it as a list with two kinds of -- nil-constructors. data Path a -- | Unrooted paths -- -- Unrooted paths need a root before they can be interpreted. data Unrooted -- | Rooted paths -- -- The a parameter is a phantom argument; Rooted is -- effectively a proxy. data Rooted a Rooted :: Rooted a type UnrootedPath = Path Unrooted class IsRoot root showRoot :: IsRoot root => Rooted root -> String fragment :: Fragment -> UnrootedPath -- | For convenience: combine fragment and mkFragment -- -- This can therefore throw the same runtime errors as mkFragment. fragment' :: String -> UnrootedPath () :: Path a -> UnrootedPath -> Path a rootPath :: Rooted root -> UnrootedPath -> Path (Rooted root) unrootPath :: Path (Rooted root) -> (Rooted root, UnrootedPath) unrootPath' :: Path a -> UnrootedPath -- | Reinterpret the root of a path castRoot :: Path (Rooted root) -> Path (Rooted root') joinFragments :: [Fragment] -> UnrootedPath splitFragments :: UnrootedPath -> [Fragment] toUnrootedFilePath :: UnrootedPath -> FilePath fromUnrootedFilePath :: FilePath -> UnrootedPath isPathPrefixOf :: UnrootedPath -> UnrootedPath -> Bool takeDirectory :: Path a -> Path a takeFileName :: Path a -> Fragment (<.>) :: Path a -> String -> Path a splitExtension :: Path a -> (Path a, String) -- | A file system root can be interpreted as an (absolute) FilePath class IsRoot root => IsFileSystemRoot root data Relative data Absolute data HomeDir type AbsolutePath = Path (Rooted Absolute) type RelativePath = Path (Rooted Relative) -- | Abstract over a file system root -- -- see fromFilePath data FileSystemPath FileSystemPath :: Path (Rooted root) -> FileSystemPath toFilePath :: AbsolutePath -> FilePath fromFilePath :: FilePath -> FileSystemPath makeAbsolute :: FileSystemPath -> IO AbsolutePath toAbsoluteFilePath :: IsFileSystemRoot root => Path (Rooted root) -> IO FilePath fromAbsoluteFilePath :: FilePath -> AbsolutePath -- | Wrapper around openBinaryTempFileWithDefaultPermissions openTempFile :: IsFileSystemRoot root => Path (Rooted root) -> String -> IO (AbsolutePath, Handle) -- | Open a file in read mode -- -- We don't wrap the general withFile to encourage using atomic -- file ops. withFileInReadMode :: IsFileSystemRoot root => Path (Rooted root) -> (Handle -> IO r) -> IO r readLazyByteString :: IsFileSystemRoot root => Path (Rooted root) -> IO ByteString readStrictByteString :: IsFileSystemRoot root => Path (Rooted root) -> IO ByteString createDirectoryIfMissing :: IsFileSystemRoot root => Bool -> Path (Rooted root) -> IO () doesDirectoryExist :: IsFileSystemRoot root => Path (Rooted root) -> IO Bool doesFileExist :: IsFileSystemRoot root => Path (Rooted root) -> IO Bool getCurrentDirectory :: IO AbsolutePath -- | Return the immediate children of a directory -- -- Filters out "." and "..". getDirectoryContents :: IsFileSystemRoot root => Path (Rooted root) -> IO [UnrootedPath] -- | Recursive traverse a directory structure -- -- Returns a set of paths relative to the directory specified. TODO: Not -- sure about the memory behaviour with large file systems. getRecursiveContents :: IsFileSystemRoot root => Path (Rooted root) -> IO [UnrootedPath] getTemporaryDirectory :: IO AbsolutePath removeFile :: IsFileSystemRoot root => Path (Rooted root) -> IO () renameFile :: (IsFileSystemRoot root, IsFileSystemRoot root1) => Path (Rooted root) -> Path (Rooted root1) -> IO () data TarballRoot type TarballPath = Path (Rooted TarballRoot) tarIndexLookup :: TarIndex -> TarballPath -> Maybe TarIndexEntry tarAppend :: (IsFileSystemRoot root, IsFileSystemRoot root') => Path (Rooted root) -> Path (Rooted root') -> [TarballPath] -> IO () data WebRoot type URIPath = Path (Rooted WebRoot) uriPath :: URI -> URIPath modifyUriPath :: URI -> (URIPath -> URIPath) -> URI -- | See openFile data IOMode :: * ReadMode :: IOMode WriteMode :: IOMode AppendMode :: IOMode ReadWriteMode :: IOMode -- | Three kinds of buffering are supported: line-buffering, -- block-buffering or no-buffering. These modes have the following -- effects. For output, items are written out, or flushed, from -- the internal buffer according to the buffer mode: -- -- -- -- An implementation is free to flush the buffer more frequently, but not -- less frequently, than specified above. The output buffer is emptied as -- soon as it has been written out. -- -- Similarly, input occurs according to the buffer mode for the handle: -- -- -- -- The default buffering mode when a handle is opened is -- implementation-dependent and may depend on the file system object -- which is attached to that handle. For most implementations, physical -- files will normally be block-buffered and terminals will normally be -- line-buffered. data BufferMode :: * -- | buffering is disabled if possible. NoBuffering :: BufferMode -- | line-buffering should be enabled if possible. LineBuffering :: BufferMode -- | block-buffering should be enabled if possible. The size of the buffer -- is n items if the argument is Just n and is -- otherwise implementation-dependent. BlockBuffering :: Maybe Int -> BufferMode -- | Haskell defines operations to read and write characters from and to -- files, represented by values of type Handle. Each value of -- this type is a handle: a record used by the Haskell run-time -- system to manage I/O with file system objects. A handle has at -- least the following properties: -- -- -- -- Most handles will also have a current I/O position indicating where -- the next input or output operation will occur. A handle is -- readable if it manages only input or both input and output; -- likewise, it is writable if it manages only output or both -- input and output. A handle is open when first allocated. Once -- it is closed it can no longer be used for either input or output, -- though an implementation cannot re-use its storage while references -- remain to it. Handles are in the Show and Eq classes. -- The string produced by showing a handle is system dependent; it should -- include enough information to identify the handle for debugging. A -- handle is equal according to == only to itself; no attempt is -- made to compare the internal state of different handles for equality. data Handle :: * -- | Computation hSetBuffering hdl mode sets the mode of -- buffering for handle hdl on subsequent reads and writes. -- -- If the buffer mode is changed from BlockBuffering or -- LineBuffering to NoBuffering, then -- -- -- -- This operation may fail with: -- -- hSetBuffering :: Handle -> BufferMode -> IO () -- | Computation hClose hdl makes handle hdl -- closed. Before the computation finishes, if hdl is writable -- its buffer is flushed as for hFlush. Performing hClose -- on a handle that has already been closed has no effect; doing so is -- not an error. All other operations on a closed handle will fail. If -- hClose fails for any reason, any further operations (apart from -- hClose) on the handle will still fail as if hdl had -- been successfully closed. hClose :: Handle -> IO () -- | For a handle hdl which attached to a physical file, -- hFileSize hdl returns the size of that file in 8-bit -- bytes. hFileSize :: Handle -> IO Integer instance Show (Path a) instance Show Fragment instance Eq Fragment instance Ord Fragment instance Show (Rooted a) instance Show (Rooted TarballRoot) instance IsFileSystemRoot HomeDir instance IsFileSystemRoot Absolute instance IsFileSystemRoot Relative instance IsRoot HomeDir instance IsRoot Absolute instance IsRoot Relative instance IsRoot root => Pretty (Path (Rooted root)) instance Ord (Path a) instance Eq (Path a) instance Pretty Fragment module Hackage.Security.Util.IO -- | Create a short-lived temporary file -- -- Creates the directory where the temp file should live if it does not -- exist. withTempFile :: IsFileSystemRoot root => Path (Rooted root) -> String -> (AbsolutePath -> Handle -> IO a) -> IO a getFileSize :: IsFileSystemRoot root => Path (Rooted root) -> IO Integer handleDoesNotExist :: IO a -> IO (Maybe a) -- | Copy a file atomically -- -- If both files live in the same directory, we call renameFile. -- Otherwise we read the source file and call atomicWriteFile -- (because only when the two files live in the same directory can be -- sure that the two locations are on the same physical device). atomicCopyFile :: AbsolutePath -> AbsolutePath -> IO () -- | Atomically write a bytestring -- -- We write to a temporary file in the destination folder and then -- rename. atomicWriteFile :: AbsolutePath -> ByteString -> IO () -- | Like 'withFile .. WriteMode', but overwrite the destination -- atomically. -- -- We open a handle to a temporary file in the same directory as the -- final location, then call the callback, and only when there are no -- exceptions finally rename the temporary file to the final destination. atomicWithFile :: AbsolutePath -> (Handle -> IO a) -> IO a -- | Minimal implementation of Canonical JSON. -- -- http://wiki.laptop.org/go/Canonical_JSON -- -- A "canonical JSON" format is provided in order to provide meaningful -- and repeatable hashes of JSON-encoded data. Canonical JSON is parsable -- with any full JSON parser, but security-conscious applications will -- want to verify that input is in canonical form before authenticating -- any hash or signature on that input. -- -- This implementation is derived from the json parser from the json -- package, with simplifications to meet the Canonical JSON grammar. module Text.JSON.Canonical data JSValue JSNull :: JSValue JSBool :: !Bool -> JSValue JSNum :: !Int -> JSValue JSString :: String -> JSValue JSArray :: [JSValue] -> JSValue JSObject :: [(String, JSValue)] -> JSValue parseCanonicalJSON :: ByteString -> Either String JSValue renderCanonicalJSON :: JSValue -> ByteString instance Show JSValue instance Read JSValue instance Eq JSValue instance Ord JSValue -- | Some very simple lens definitions (to avoid further dependencies) -- -- Intended to be double-imported > import Hackage.Security.Util.Lens -- (Lens) > import qualified Hackage.Security.Util.Lens as Lens module Hackage.Security.Util.Lens -- | Polymorphic lens type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t -- | Monomorphic lens type Lens' s a = Lens s s a a get :: Lens' s a -> s -> a modify :: Lens s t a b -> (a -> b) -> s -> t set :: Lens s t a b -> b -> s -> t lookupM :: (Eq a, Monoid b) => a -> Lens' [(a, b)] b -- | Hiding existentials module Hackage.Security.Util.Some data Some f Some :: (f a) -> Some f data DictEq a DictEq :: DictEq a -- | Type f satisfies SomeEq f if f a satisfies -- Eq independent of a class SomeEq f someEq :: SomeEq f => DictEq (f a) data DictShow a DictShow :: DictShow a -- | Type f satisfies SomeShow f if f a -- satisfies Show independent of a class SomeShow f someShow :: SomeShow f => DictShow (f a) typecheckSome :: Typed f => Some f -> Some (TypeOf f) -> Bool instance Typeable Some instance SomeShow f => Show (Some f) instance (Typed f, SomeEq f) => Eq (Some f) module Hackage.Security.Key.Env -- | A key environment is a mapping from key IDs to the corresponding keys. -- -- It should satisfy the invariant that these key IDs actually match the -- keys; see checkKeyEnvInvariant. data KeyEnv keyEnvMap :: KeyEnv -> Map KeyId (Some PublicKey) fromPublicKeys :: [Some PublicKey] -> KeyEnv fromKeys :: [Some Key] -> KeyEnv empty :: KeyEnv null :: KeyEnv -> Bool insert :: Some PublicKey -> KeyEnv -> KeyEnv lookup :: KeyId -> KeyEnv -> Maybe (Some PublicKey) union :: KeyEnv -> KeyEnv -> KeyEnv instance Show KeyEnv instance ReportSchemaErrors m => FromJSON m KeyEnv instance Monad m => ToJSON m KeyEnv -- | Hackage-specific wrappers around the Util.JSON module module Hackage.Security.JSON data DeserializationError -- | Malformed JSON has syntax errors in the JSON itself (i.e., we cannot -- even parse it to a JSValue) DeserializationErrorMalformed :: String -> DeserializationError -- | Invalid JSON has valid syntax but invalid structure -- -- The string gives a hint about what we expected instead DeserializationErrorSchema :: String -> DeserializationError -- | The JSON file contains a key ID of an unknown key DeserializationErrorUnknownKey :: KeyId -> DeserializationError -- | Some verification step failed DeserializationErrorValidation :: String -> DeserializationError -- | Wrong file type -- -- Records actual and expected types. DeserializationErrorFileType :: String -> String -> DeserializationError validate :: MonadError DeserializationError m => String -> Bool -> m () verifyType :: (ReportSchemaErrors m, MonadError DeserializationError m) => JSValue -> String -> m () -- | MonadReader-like monad, specialized to key environments class (ReportSchemaErrors m, MonadError DeserializationError m) => MonadKeys m localKeys :: MonadKeys m => (KeyEnv -> KeyEnv) -> m a -> m a askKeys :: MonadKeys m => m KeyEnv addKeys :: MonadKeys m => KeyEnv -> m a -> m a withKeys :: MonadKeys m => KeyEnv -> m a -> m a lookupKey :: MonadKeys m => KeyId -> m (Some PublicKey) readKeyAsId :: MonadKeys m => JSValue -> m (Some PublicKey) data ReadJSON_Keys_Layout a data ReadJSON_Keys_NoLayout a data ReadJSON_NoKeys_NoLayout a runReadJSON_Keys_Layout :: KeyEnv -> RepoLayout -> ReadJSON_Keys_Layout a -> Either DeserializationError a runReadJSON_Keys_NoLayout :: KeyEnv -> ReadJSON_Keys_NoLayout a -> Either DeserializationError a runReadJSON_NoKeys_NoLayout :: ReadJSON_NoKeys_NoLayout a -> Either DeserializationError a parseJSON_Keys_Layout :: FromJSON ReadJSON_Keys_Layout a => KeyEnv -> RepoLayout -> ByteString -> Either DeserializationError a parseJSON_Keys_NoLayout :: FromJSON ReadJSON_Keys_NoLayout a => KeyEnv -> ByteString -> Either DeserializationError a parseJSON_NoKeys_NoLayout :: FromJSON ReadJSON_NoKeys_NoLayout a => ByteString -> Either DeserializationError a readJSON_Keys_Layout :: (IsFileSystemRoot root, FromJSON ReadJSON_Keys_Layout a) => KeyEnv -> RepoLayout -> Path (Rooted root) -> IO (Either DeserializationError a) readJSON_Keys_NoLayout :: (IsFileSystemRoot root, FromJSON ReadJSON_Keys_NoLayout a) => KeyEnv -> Path (Rooted root) -> IO (Either DeserializationError a) readJSON_NoKeys_NoLayout :: (IsFileSystemRoot root, FromJSON ReadJSON_NoKeys_NoLayout a) => Path (Rooted root) -> IO (Either DeserializationError a) data WriteJSON a runWriteJSON :: RepoLayout -> WriteJSON a -> a -- | Render to canonical JSON format renderJSON :: ToJSON WriteJSON a => RepoLayout -> a -> ByteString -- | Variation on renderJSON for files that don't require the repo -- layout renderJSON_NoLayout :: ToJSON Identity a => a -> ByteString writeJSON :: ToJSON WriteJSON a => RepoLayout -> AbsolutePath -> a -> IO () writeJSON_NoLayout :: ToJSON Identity a => AbsolutePath -> a -> IO () writeKeyAsId :: Some PublicKey -> JSValue class ToJSON m a toJSON :: ToJSON m a => a -> m JSValue class FromJSON m a fromJSON :: FromJSON m a => JSValue -> m a -- | Used in the ToJSON instance for Map class ToObjectKey m a toObjectKey :: ToObjectKey m a => a -> m String -- | Used in the FromJSON instance for Map class FromObjectKey m a fromObjectKey :: FromObjectKey m a => String -> m a -- | Monads in which we can report schema errors class (Applicative m, Monad m) => ReportSchemaErrors m expected :: ReportSchemaErrors m => Expected -> Maybe Got -> m a type Expected = String type Got = String expected' :: ReportSchemaErrors m => Expected -> JSValue -> m a fromJSObject :: ReportSchemaErrors m => JSValue -> m [(String, JSValue)] -- | Extract a field from a JSON object fromJSField :: (ReportSchemaErrors m, FromJSON m a) => JSValue -> String -> m a fromJSOptField :: (ReportSchemaErrors m, FromJSON m a) => JSValue -> String -> m (Maybe a) mkObject :: Monad m => [(String, m JSValue)] -> m JSValue data JSValue JSNull :: JSValue JSBool :: !Bool -> JSValue JSNum :: !Int -> JSValue JSString :: String -> JSValue JSArray :: [JSValue] -> JSValue JSObject :: [(String, JSValue)] -> JSValue instance Typeable DeserializationError instance Functor ReadJSON_Keys_Layout instance Applicative ReadJSON_Keys_Layout instance Monad ReadJSON_Keys_Layout instance MonadError DeserializationError ReadJSON_Keys_Layout instance Functor ReadJSON_Keys_NoLayout instance Applicative ReadJSON_Keys_NoLayout instance Monad ReadJSON_Keys_NoLayout instance MonadError DeserializationError ReadJSON_Keys_NoLayout instance Functor ReadJSON_NoKeys_NoLayout instance Applicative ReadJSON_NoKeys_NoLayout instance Monad ReadJSON_NoKeys_NoLayout instance MonadError DeserializationError ReadJSON_NoKeys_NoLayout instance Functor WriteJSON instance Applicative WriteJSON instance Monad WriteJSON instance MonadReader RepoLayout WriteJSON instance MonadKeys ReadJSON_Keys_NoLayout instance MonadKeys ReadJSON_Keys_Layout instance MonadReader RepoLayout ReadJSON_Keys_Layout instance ReportSchemaErrors ReadJSON_NoKeys_NoLayout instance ReportSchemaErrors ReadJSON_Keys_NoLayout instance ReportSchemaErrors ReadJSON_Keys_Layout instance Pretty DeserializationError instance Exception DeserializationError instance Show DeserializationError -- | Information about files -- -- Intended to be double imported -- --
--   import Hackage.Security.TUF.FileMap (FileMap)
--   import qualified Hackage.Security.TUF.FileMap as FileMap
--   
module Hackage.Security.TUF.FileMap -- | Mapping from paths to file info -- -- File maps are used in target files; the paths are relative to the -- location of the target files containing the file map. data FileMap -- | Entries in FileMap either talk about the repository or the -- index data TargetPath TargetPathRepo :: RepoPath -> TargetPath TargetPathIndex :: IndexPath -> TargetPath empty :: FileMap lookup :: TargetPath -> FileMap -> Maybe FileInfo (!) :: FileMap -> TargetPath -> FileInfo insert :: TargetPath -> FileInfo -> FileMap -> FileMap fromList :: [(TargetPath, FileInfo)] -> FileMap lookupM :: Monad m => FileMap -> TargetPath -> m FileInfo data FileChange -- | File got added or modified; we record the new file info FileChanged :: FileInfo -> FileChange -- | File got deleted FileDeleted :: FileChange fileMapChanges :: FileMap -> FileMap -> Map TargetPath FileChange instance Show TargetPath instance Eq TargetPath instance Ord TargetPath instance Show FileMap instance Show FileChange instance ReportSchemaErrors m => FromObjectKey m TargetPath instance Monad m => ToObjectKey m TargetPath instance ReportSchemaErrors m => FromJSON m FileMap instance Monad m => ToJSON m FileMap instance Pretty TargetPath -- | Abstract definition of a Repository -- -- Most clients should only need to import this module if they wish to -- define their own Repository implementations. module Hackage.Security.Client.Repository -- | Abstract definition of files we might have to download -- -- RemoteFile is parametrized by the type of the formats that we -- can accept from the remote repository. -- -- NOTE: Haddock lacks GADT support so constructors have only regular -- comments. data RemoteFile :: * -> * RemoteTimestamp :: RemoteFile (FormatUn :- ()) RemoteRoot :: Maybe (Trusted FileInfo) -> RemoteFile (FormatUn :- ()) RemoteSnapshot :: Trusted FileInfo -> RemoteFile (FormatUn :- ()) RemoteMirrors :: Trusted FileInfo -> RemoteFile (FormatUn :- ()) RemoteIndex :: HasFormat fs FormatGz -> Formats fs (Trusted FileInfo) -> RemoteFile fs RemotePkgTarGz :: PackageIdentifier -> Trusted FileInfo -> RemoteFile (FormatGz :- ()) -- | Files that we might request from the local cache data CachedFile -- | Timestamp metadata (timestamp.json) CachedTimestamp :: CachedFile -- | Root metadata (root.json) CachedRoot :: CachedFile -- | Snapshot metadata (snapshot.json) CachedSnapshot :: CachedFile -- | Mirrors list (mirrors.json) CachedMirrors :: CachedFile -- | Files that we might request from the index -- -- TODO: We should also provide a way to extract preferred versions info -- from the tarball. After all, this is a security sensitive, as it might -- be used for rollback/freeze attacks. Until we have author signing -- however this is not a strict necessity, as the preferred versions -- comes from the index which is itself signed. data IndexFile -- | Package-specific metadata (targets.json) IndexPkgMetadata :: PackageIdentifier -> IndexFile -- | Cabal file for a package IndexPkgCabal :: PackageIdentifier -> IndexFile -- | Default format for each file type -- -- For most file types we don't have a choice; for the index the -- repository is only required to offer the GZip-compressed format so -- that is the default. remoteFileDefaultFormat :: RemoteFile fs -> Some (HasFormat fs) -- | Default file info (see also remoteFileDefaultFormat) remoteFileDefaultInfo :: RemoteFile fs -> Maybe (Trusted FileInfo) -- | Repository -- -- This is an abstract representation of a repository. It simply provides -- a way to download metafiles and target files, without specifying how -- this is done. For instance, for a local repository this could just be -- doing a file read, whereas for remote repositories this could be using -- any kind of HTTP client. data Repository Repository :: (forall a fs. (Throws VerificationError, Throws SomeRemoteError) => IsRetry -> RemoteFile fs -> (forall f. HasFormat fs f -> TempPath -> IO a) -> IO a) -> (CachedFile -> IO (Maybe AbsolutePath)) -> IO AbsolutePath -> IO () -> (IndexFile -> IO (Maybe ByteString)) -> (forall a. Maybe [Mirror] -> IO a -> IO a) -> (LogMessage -> IO ()) -> RepoLayout -> String -> Repository -- | Get a file from the server -- -- Responsibilies of repWithRemote: -- -- -- -- Responsibilities of the callback: -- -- -- -- NOTE: Calls to repWithRemote should _always_ be in the scope of -- repWithMirror. repWithRemote :: Repository -> forall a fs. (Throws VerificationError, Throws SomeRemoteError) => IsRetry -> RemoteFile fs -> (forall f. HasFormat fs f -> TempPath -> IO a) -> IO a -- | Get a cached file (if available) repGetCached :: Repository -> CachedFile -> IO (Maybe AbsolutePath) -- | Get the cached root -- -- This is a separate method only because clients must ALWAYS have root -- information available. repGetCachedRoot :: Repository -> IO AbsolutePath -- | Clear all cached data -- -- In particular, this should remove the snapshot and the timestamp. It -- would also be okay, but not required, to delete the index. repClearCache :: Repository -> IO () -- | Get a file from the index -- -- The use of a strict bytestring here is intentional: it means the -- Repository is free to keep the index open and just seek the handle for -- different files. Since we only extract small files, having the entire -- extracted file in memory is not an issue. repGetFromIndex :: Repository -> IndexFile -> IO (Maybe ByteString) -- | Mirror selection -- -- The purpose of repWithMirror is to scope mirror selection. The -- idea is that if we have -- --
--   repWithMirror mirrorList $
--     someCallback
--   
-- -- then the repository may pick a mirror before calling -- someCallback, catch exceptions thrown by -- someCallback, and potentially try the callback again with a -- different mirror. -- -- The list of mirrors may be Nothing if we haven't yet -- downloaded the list of mirrors from the repository, or when our cached -- list of mirrors is invalid. Of course, if we did download it, then the -- list of mirrors may still be empty. In this case the repository must -- fall back to its primary download mechanism. -- -- Mirrors as currently defined (in terms of a "base URL") are inherently -- a HTTP (or related) concept, so in repository implementations such as -- the local-repo repWithMirrors is probably just an identity -- operation (see ignoreMirrors). Conversely, HTTP -- implementations of repositories may have other, out-of-band -- information (for example, coming from a cabal config file) that they -- may use to influence mirror selection. repWithMirror :: Repository -> forall a. Maybe [Mirror] -> IO a -> IO a -- | Logging repLog :: Repository -> LogMessage -> IO () -- | Layout of this repository repLayout :: Repository -> RepoLayout -- | Description of the repository (used in the show instance) repDescription :: Repository -> String -- | Path to temporary file type TempPath = AbsolutePath -- | Are we requesting this information because of a previous validation -- error? -- -- Clients can take advantage of this to tell caches to revalidate files. data IsRetry FirstAttempt :: IsRetry AfterVerificationError :: IsRetry -- | Log messages -- -- We use a RemoteFile rather than a RepoPath here because -- we might not have a RepoPath for the file that we were trying -- to download (that is, for example if the server does not provide an -- uncompressed tarball, it doesn't make much sense to list the path to -- that non-existing uncompressed tarball). data LogMessage -- | Root information was updated -- -- This message is issued when the root information is updated as part of -- the normal check for updates procedure. If the root information is -- updated because of a verification error WarningVerificationError is -- issued instead. LogRootUpdated :: LogMessage -- | A verification error -- -- Verification errors can be temporary, and may be resolved later; hence -- these are just warnings. (Verification errors that cannot be resolved -- are thrown as exceptions.) LogVerificationError :: VerificationError -> LogMessage -- | Download a file from a repository LogDownloading :: (Some RemoteFile) -> LogMessage -- | Incrementally updating a file from a repository LogUpdating :: (Some RemoteFile) -> LogMessage -- | Selected a particular mirror LogSelectedMirror :: MirrorDescription -> LogMessage -- | Updating a file failed (we will try again by downloading it whole) LogUpdateFailed :: (Some RemoteFile) -> UpdateFailure -> LogMessage -- | We got an exception with a particular mirror (we will try with a -- different mirror if any are available) LogMirrorFailed :: MirrorDescription -> SomeException -> LogMessage -- | Records why we are downloading a file rather than updating it. data UpdateFailure -- | Server only provides compressed form of the file UpdateImpossibleOnlyCompressed :: UpdateFailure -- | Likewise, it's possible that client _wants_ the compressed form of the -- file, in which case downloading the uncompressed form is not useful. UpdateNotUsefulWantsCompressed :: UpdateFailure -- | Server does not support incremental downloads UpdateImpossibleUnsupported :: UpdateFailure -- | We don't have a local copy of the file to update UpdateImpossibleNoLocalCopy :: UpdateFailure -- | Updating the local file would actually mean downloading MORE data then -- doing a regular download. UpdateTooLarge :: UpdateFailure -- | Update failed UpdateFailed :: SomeException -> UpdateFailure -- | Repository-specific exceptions -- -- For instance, for repositories using HTTP this might correspond to a -- 404; for local repositories this might correspond to file-not-found, -- etc. data SomeRemoteError :: * SomeRemoteError :: e -> SomeRemoteError -- | Helper function to implement repWithMirrors. mirrorsUnsupported :: Maybe [Mirror] -> IO a -> IO a remoteRepoPath :: RepoLayout -> RemoteFile fs -> Formats fs RepoPath remoteRepoPath' :: RepoLayout -> RemoteFile fs -> HasFormat fs f -> RepoPath indexFilePath :: IndexLayout -> IndexFile -> IndexPath -- | Is a particular remote file cached? data IsCached -- | This remote file should be cached, and we ask for it by name CacheAs :: CachedFile -> IsCached -- | We don't cache this remote file -- -- This doesn't mean a Repository should not feel free to cache the file -- if desired, but it does mean the generic algorithms will never ask for -- this file from the cache. DontCache :: IsCached -- | The index is somewhat special: it should be cached, but we never ask -- for it directly. -- -- Instead, we will ask the Repository for files _from_ the index, which -- it can serve however it likes. For instance, some repositories might -- keep the index in uncompressed form, others in compressed form; some -- might keep an index tarball index for quick access, others may scan -- the tarball linearly, etc. CacheIndex :: IsCached -- | Which remote files should we cache locally? mustCache :: RemoteFile fs -> IsCached instance Typeable SomeRemoteError instance Show (RemoteFile fs) instance Eq CachedFile instance Ord CachedFile instance Show CachedFile instance Show IndexFile instance Eq IsCached instance Ord IsCached instance Show IsCached instance Pretty UpdateFailure instance Pretty LogMessage instance Pretty SomeRemoteError instance Show SomeRemoteError instance Exception SomeRemoteError instance Show Repository instance Pretty IndexFile instance Pretty (RemoteFile fs) -- | Main entry point into the Hackage Security framework for clients module Hackage.Security.Client -- | Generic logic for checking if there are updates -- -- This implements the logic described in Section 5.1, "The client -- application", of the TUF spec. It checks which of the server metadata -- has changed, and downloads all changed metadata to the local cache. -- (Metadata here refers both to the TUF security metadata as well as the -- Hackage packge index.) checkForUpdates :: (Throws VerificationError, Throws SomeRemoteError) => Repository -> CheckExpiry -> IO HasUpdates -- | Should we check expiry dates? data CheckExpiry -- | Yes, check expiry dates CheckExpiry :: CheckExpiry -- | No, don't check expiry dates. -- -- This should ONLY be used in exceptional circumstances (such as when -- the main server is down for longer than the expiry dates used in the -- timestamp files on mirrors). DontCheckExpiry :: CheckExpiry data HasUpdates HasUpdates :: HasUpdates NoUpdates :: HasUpdates -- | Download a package -- -- It is the responsibility of the callback to move the package from its -- temporary location to a permanent location (if desired). The callback -- will only be invoked once the chain of trust has been verified. -- -- NOTE: Unlike the check for updates, downloading a package never -- triggers an update of the root information (even if verification of -- the package fails). downloadPackage :: (Throws SomeRemoteError, Throws VerificationError, Throws InvalidPackageException) => Repository -> PackageIdentifier -> (TempPath -> IO a) -> IO a -- | Get a cabal file from the index -- -- This does currently not do any verification (bcause the cabal file -- comes from the index, and the index itself is verified). Once we -- introduce author signing this needs to be adapted. -- -- Should be called only once a local index is available (i.e., after -- checkForUpdates). -- -- Throws an InvalidPackageException if there is no cabal file for -- the specified package in the index. getCabalFile :: Throws InvalidPackageException => Repository -> PackageIdentifier -> IO ByteString -- | Check if we need to bootstrap (i.e., if we have root info) requiresBootstrap :: Repository -> IO Bool -- | Bootstrap the chain of trust -- -- New clients might need to obtain a copy of the root metadata. This -- however represents a chicken-and-egg problem: how can we verify the -- root metadata we downloaded? The only possibility is to be provided -- with a set of an out-of-band set of root keys and an appropriate -- threshold. -- -- Clients who provide a threshold of 0 can do an initial "unsafe" update -- of the root information, if they wish. -- -- The downloaded root information will _only_ be verified against the -- provided keys, and _not_ against previously downloaded root info (if -- any). It is the responsibility of the client to call bootstrap -- only when this is the desired behaviour. bootstrap :: (Throws SomeRemoteError, Throws VerificationError) => Repository -> [KeyId] -> KeyThreshold -> IO () -- | File length -- -- Having verified file length information means we can protect against -- endless data attacks and similar. newtype FileLength FileLength :: Int -> FileLength fileLength :: FileLength -> Int -- | File hash newtype Hash Hash :: String -> Hash -- | Key threshold -- -- The key threshold is the minimum number of keys a document must be -- signed with. Key thresholds are specified in RoleSpec or -- DelegationsSpec. newtype KeyThreshold KeyThreshold :: Int -> KeyThreshold -- | File information -- -- This intentionally does not have an Eq instance; see -- knownFileInfoEqual and verifyFileInfo instead. -- -- NOTE: Throughout we compute file information always over the raw -- bytes. For example, when timestamp.json lists the hash of -- snapshot.json, this hash is computed over the actual -- snapshot.json file (as opposed to the canonical form of the -- embedded JSON). This brings it in line with the hash computed over -- target files, where that is the only choice available. data FileInfo FileInfo :: FileLength -> Map HashFn Hash -> FileInfo fileInfoLength :: FileInfo -> FileLength fileInfoHashes :: FileInfo -> Map HashFn Hash data HashFn HashFnSHA256 :: HashFn -- | File hash newtype Hash Hash :: String -> Hash -- | Compute FileInfo -- -- TODO: Currently this will load the entire input bytestring into -- memory. We need to make this incremental, by computing the length and -- all hashes in a single traversal over the input. However, the precise -- way to do that will depend on the hashing package we will use, and we -- have yet to pick that package. fileInfo :: ByteString -> FileInfo -- | Compute FileInfo computeFileInfo :: IsFileSystemRoot root => Path (Rooted root) -> IO FileInfo -- | Compare known file info -- -- This should be used only when the FileInfo is already known. If we -- want to compare known FileInfo against a file on disk we should delay -- until we know have confirmed that the file lengths don't match (see -- verifyFileInfo). knownFileInfoEqual :: FileInfo -> FileInfo -> Bool class HasHeader a fileExpires :: HasHeader a => Lens' a FileExpires fileVersion :: HasHeader a => Lens' a FileVersion -- | File version -- -- The file version is a flat integer which must monotonically increase -- on every file update. -- -- Show and Read instance are defined in terms of the -- underlying Int (this is use for example by hackage during the -- backup process). newtype FileVersion FileVersion :: Int -> FileVersion -- | File expiry date -- -- A Nothing value here means no expiry. That makes it possible to -- set some files to never expire. (Note that not having the Maybe in the -- type here still allows that, because you could set an expiry date 2000 -- years into the future. By having the Maybe here we avoid the _need_ -- for such encoding issues.) newtype FileExpires FileExpires :: (Maybe UTCTime) -> FileExpires -- | Occassionally it is useful to read only a header from a file. -- -- HeaderOnly intentionally only has a FromJSON instance -- (no ToJSON). data Header Header :: FileExpires -> FileVersion -> Header headerExpires :: Header -> FileExpires headerVersion :: Header -> FileVersion expiresInDays :: UTCTime -> Integer -> FileExpires expiresNever :: FileExpires isExpired :: UTCTime -> FileExpires -> Bool versionInitial :: FileVersion versionIncrement :: FileVersion -> FileVersion -- | The root of the repository -- -- Repository roots can be anchored at a remote URL or a local directory. -- -- Note that even for remote repos RepoRoot is (potentially) -- different from WebRoot -- for a repository located at, say, -- http://hackage.haskell.org they happen to coincide, -- but for one location at -- http://example.com/some/subdirectory they do not. data RepoRoot -- | Paths relative to the root of the repository type RepoPath = Path (Rooted RepoRoot) -- | Layout of a repository data RepoLayout RepoLayout :: RepoPath -> RepoPath -> RepoPath -> RepoPath -> RepoPath -> RepoPath -> (PackageIdentifier -> RepoPath) -> IndexLayout -> RepoLayout -- | TUF root metadata repoLayoutRoot :: RepoLayout -> RepoPath -- | TUF timestamp repoLayoutTimestamp :: RepoLayout -> RepoPath -- | TUF snapshot repoLayoutSnapshot :: RepoLayout -> RepoPath -- | TUF mirrors list repoLayoutMirrors :: RepoLayout -> RepoPath -- | Compressed index tarball repoLayoutIndexTarGz :: RepoLayout -> RepoPath -- | Uncompressed index tarball repoLayoutIndexTar :: RepoLayout -> RepoPath -- | Path to the package tarball repoLayoutPkgTarGz :: RepoLayout -> PackageIdentifier -> RepoPath -- | Layout of the index -- -- Since the repository hosts the index, the layout of the index is not -- independent of the layout of the repository. repoIndexLayout :: RepoLayout -> IndexLayout -- | The layout used on Hackage hackageRepoLayout :: RepoLayout -- | Layout used by cabal for ("legacy") local repos -- -- Obviously, such repos do not normally contain any of the TUF files, so -- their location is more or less arbitrary here. cabalLocalRepoLayout :: RepoLayout anchorRepoPathLocally :: IsFileSystemRoot root => Path (Rooted root) -> RepoPath -> Path (Rooted root) anchorRepoPathRemotely :: URIPath -> RepoPath -> URIPath -- | The root of the index tarball data IndexRoot -- | Paths relative to the root of the index tarball type IndexPath = Path (Rooted RepoRoot) -- | Layout of the files within the index tarball data IndexLayout IndexLayout :: (PackageIdentifier -> IndexPath) -> (PackageIdentifier -> IndexPath) -> IndexLayout -- | TUF metadata for a package indexLayoutPkgMetadata :: IndexLayout -> PackageIdentifier -> IndexPath -- | Package .cabal file indexLayoutPkgCabal :: IndexLayout -> PackageIdentifier -> IndexPath -- | The layout of the index as maintained on Hackage hackageIndexLayout :: IndexLayout -- | The cache directory data CacheRoot type CachePath = Path (Rooted CacheRoot) -- | Location of the various files we cache -- -- Although the generic TUF algorithms do not care how we organize the -- cache, we nonetheless specity this here because as long as there are -- tools which access files in the cache directly we need to define the -- cache layout. See also comments for defaultCacheLayout. data CacheLayout CacheLayout :: CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> Maybe CachePath -> CacheLayout -- | TUF root metadata cacheLayoutRoot :: CacheLayout -> CachePath -- | TUF timestamp cacheLayoutTimestamp :: CacheLayout -> CachePath -- | TUF snapshot cacheLayoutSnapshot :: CacheLayout -> CachePath -- | TUF mirrors list cacheLayoutMirrors :: CacheLayout -> CachePath -- | Uncompressed index tarball cacheLayoutIndexTar :: CacheLayout -> CachePath -- | Index to the uncompressed index tarball cacheLayoutIndexIdx :: CacheLayout -> CachePath -- | Compressed index tarball (if cached) cacheLayoutIndexTarGz :: CacheLayout -> Maybe CachePath -- | The cache layout cabal-install uses -- -- We cache the index as cache/00-index.tar; this is -- important because `cabal-install` expects to find it there (and does -- not currently go through the hackage-security library to get files -- from the index). cabalCacheLayout :: CacheLayout -- | Anchor a cache path to the location of the cache anchorCachePath :: IsFileSystemRoot root => Path (Rooted root) -> CachePath -> Path (Rooted root) data Mirrors Mirrors :: FileVersion -> FileExpires -> [Mirror] -> Mirrors mirrorsVersion :: Mirrors -> FileVersion mirrorsExpires :: Mirrors -> FileExpires mirrorsMirrors :: Mirrors -> [Mirror] -- | Definition of a mirror -- -- NOTE: Unlike the TUF specification, we require that all mirrors must -- have the same format. That is, we omit metapath and -- targetspath. data Mirror Mirror :: URI -> MirrorContent -> Mirror mirrorUrlBase :: Mirror -> URI mirrorContent :: Mirror -> MirrorContent -- | Full versus partial mirrors -- -- The TUF spec explicitly allows for partial mirrors, with the mirrors -- file specifying (through patterns) what is available from partial -- mirrors. -- -- For now we only support full mirrors; if we wanted to add partial -- mirrors, we would add a second MirrorPartial constructor here -- with arguments corresponding to TUF's metacontent and -- targetscontent fields. data MirrorContent MirrorFull :: MirrorContent type MirrorDescription = String -- | Give a human-readable description of a particular mirror -- -- (for use in error messages) describeMirror :: Mirror -> MirrorDescription type FileName = String type Directory = String type Extension = String type BaseName = String -- | Structured patterns over paths -- -- The type argument indicates what kind of function we expect when the -- pattern matches. For example, we have the pattern "*/*.txt": -- --
--   PathPatternDirAny (PathPatternFileExt ".txt")
--     :: PathPattern (Directory :- BaseName :- ())
--   
-- -- TODOs (see README.md): -- -- -- -- Currently this is a proof of concept more than anything else; the -- right structure is here, but it needs updating. However, until we add -- author signing (or out-of-tarball targets) we don't actually use this -- yet. -- -- NOTE: Haddock lacks GADT support so constructors have only regular -- comments. data Pattern a PatFileConst :: FileName -> Pattern () PatFileExt :: Extension -> Pattern (BaseName :- ()) PatFileAny :: Pattern (FileName :- ()) PatDirConst :: Directory -> Pattern a -> Pattern a PatDirAny :: Pattern a -> Pattern (Directory :- a) -- | Replacement patterns -- -- These constructors match the ones in Pattern: wildcards must be -- used in the same order as they appear in the pattern, but they don't -- all have to be used (that's why the base constructors are polymorphic -- in the stack tail). data Replacement a RepFileConst :: FileName -> Replacement a RepFileExt :: Extension -> Replacement (BaseName :- a) RepFileAny :: Replacement (FileName :- a) RepDirConst :: Directory -> Replacement a -> Replacement a RepDirAny :: Replacement a -> Replacement (Directory :- a) -- | A delegation -- -- A delegation is a pair of a pattern and a replacement. -- -- See match for an example. data Delegation Delegation :: (Pattern a) -> (Replacement a) -> Delegation -- | The identity replacement replaces a matched pattern with itself identityReplacement :: Pattern typ -> Replacement typ matchDelegation :: Delegation -> String -> Maybe String parseDelegation :: String -> String -> Either String Delegation -- | Quasi-quoter for delegations to make them easier to write in code -- -- This allows to write delegations as -- --
--   $(qqd "targets/*/*/*.cabal" "targets/*/*/revisions.json")
--   
-- -- (The alternative syntax which actually uses a quasi-quoter doesn't -- work very well because the /* bits confuse CPP: "unterminated -- comment") qqd :: String -> String -> Q Exp -- | The root metadata -- -- NOTE: We must have the invariant that ALL keys (apart from delegation -- keys) must be listed in rootKeys. (Delegation keys satisfy a -- similar invariant, see Targets.) data Root Root :: FileVersion -> FileExpires -> KeyEnv -> RootRoles -> Root rootVersion :: Root -> FileVersion rootExpires :: Root -> FileExpires rootKeys :: Root -> KeyEnv rootRoles :: Root -> RootRoles data RootRoles RootRoles :: RoleSpec Root -> RoleSpec Snapshot -> RoleSpec Targets -> RoleSpec Timestamp -> RoleSpec Mirrors -> RootRoles rootRolesRoot :: RootRoles -> RoleSpec Root rootRolesSnapshot :: RootRoles -> RoleSpec Snapshot rootRolesTargets :: RootRoles -> RoleSpec Targets rootRolesTimestamp :: RootRoles -> RoleSpec Timestamp rootRolesMirrors :: RootRoles -> RoleSpec Mirrors -- | Role specification -- -- The phantom type indicates what kind of type this role is meant to -- verify. data RoleSpec a RoleSpec :: [Some PublicKey] -> KeyThreshold -> RoleSpec a roleSpecKeys :: RoleSpec a -> [Some PublicKey] roleSpecThreshold :: RoleSpec a -> KeyThreshold data Signed a Signed :: a -> Signatures -> Signed a signed :: Signed a -> a signatures :: Signed a -> Signatures -- | A list of signatures -- -- Invariant: each signature must be made with a different key. We -- enforce this invariant for incoming untrusted data -- (fromPreSignatures) but not for lists of signatures that we -- create in code. newtype Signatures Signatures :: [Signature] -> Signatures data Signature Signature :: ByteString -> Some PublicKey -> Signature signature :: Signature -> ByteString signatureKey :: Signature -> Some PublicKey -- | Create a new document without any signatures unsigned :: a -> Signed a -- | Sign a document withSignatures :: ToJSON WriteJSON a => RepoLayout -> [Some Key] -> a -> Signed a -- | Variation on withSignatures that doesn't need the repo layout withSignatures' :: ToJSON Identity a => [Some Key] -> a -> Signed a -- | Construct signatures for already rendered value signRendered :: [Some Key] -> ByteString -> Signatures verifySignature :: ByteString -> Signature -> Bool -- | General FromJSON instance for signed datatypes -- -- We don't give a general FromJSON instance for Signed because for some -- datatypes we need to do something special (datatypes where we need to -- read key environments); for instance, see the "Signed Root" instance. signedFromJSON :: (MonadKeys m, FromJSON m a) => JSValue -> m (Signed a) -- | Signature verification -- -- NOTES: 1. By definition, the signature must be verified against the -- canonical JSON format. This means we _must_ parse and then pretty -- print (as we do here) because the document as stored may or may not be -- in canonical format. 2. However, it is important that we NOT translate -- from the JSValue to whatever internal datatype we are using and then -- back to JSValue, because that may not roundtrip: we must allow for -- additional fields in the JSValue that we ignore (and would therefore -- lose when we attempt to roundtrip). 3. We verify that all signatures -- are valid, but we cannot verify (here) that these signatures are -- signed with the right key, or that we have a sufficient number of -- signatures. This will be the responsibility of the calling code. verifySignatures :: JSValue -> Signatures -> Bool -- | File with uninterpreted signatures -- -- Sometimes we want to be able to read a file without interpreting the -- signatures (that is, resolving the key IDs) or doing any kind of -- checks on them. One advantage of this is that this allows us to read -- many file types without any key environment at all, which is sometimes -- useful. data UninterpretedSignatures a UninterpretedSignatures :: a -> [PreSignature] -> UninterpretedSignatures a uninterpretedSigned :: UninterpretedSignatures a -> a uninterpretedSignatures :: UninterpretedSignatures a -> [PreSignature] -- | A signature with a key ID (rather than an actual key) -- -- This corresponds precisely to the TUF representation of a signature. data PreSignature PreSignature :: ByteString -> Some KeyType -> KeyId -> PreSignature presignature :: PreSignature -> ByteString presigMethod :: PreSignature -> Some KeyType presigKeyId :: PreSignature -> KeyId -- | Convert a pre-signature to a signature -- -- Verifies that the key type matches the advertised method. fromPreSignature :: MonadKeys m => PreSignature -> m Signature -- | Convert a list of PreSignatures to a list of Signatures -- -- This verifies the invariant that all signatures are made with -- different keys. We do this on the presignatures rather than the -- signatures so that we can do the check on key IDs, rather than keys -- (the latter don't have an Ord instance). fromPreSignatures :: MonadKeys m => [PreSignature] -> m Signatures -- | Convert signature to pre-signature toPreSignature :: Signature -> PreSignature -- | Convert list of pre-signatures to a list of signatures toPreSignatures :: Signatures -> [PreSignature] data Snapshot Snapshot :: FileVersion -> FileExpires -> FileInfo -> FileInfo -> FileInfo -> Maybe FileInfo -> Snapshot snapshotVersion :: Snapshot -> FileVersion snapshotExpires :: Snapshot -> FileExpires -- | File info for the root metadata -- -- We list this explicitly in the snapshot so that we can check if we -- need to update the root metadata without first having to download the -- entire index tarball. snapshotInfoRoot :: Snapshot -> FileInfo -- | File info for the mirror metadata snapshotInfoMirrors :: Snapshot -> FileInfo -- | Compressed index tarball snapshotInfoTarGz :: Snapshot -> FileInfo -- | Uncompressed index tarball -- -- Repositories are not required to provide this. snapshotInfoTar :: Snapshot -> Maybe FileInfo -- | Target metadata -- -- Most target files do not need expiry dates because they are not -- subject to change (and hence attacks like freeze attacks are not a -- concern). data Targets Targets :: FileVersion -> FileExpires -> FileMap -> Maybe Delegations -> Targets targetsVersion :: Targets -> FileVersion targetsExpires :: Targets -> FileExpires targetsTargets :: Targets -> FileMap targetsDelegations :: Targets -> Maybe Delegations -- | Delegations -- -- Much like the Root datatype, this must have an invariant that ALL used -- keys (apart from the global keys, which are in the root key -- environment) must be listed in delegationsKeys. data Delegations Delegations :: KeyEnv -> [DelegationSpec] -> Delegations delegationsKeys :: Delegations -> KeyEnv delegationsRoles :: Delegations -> [DelegationSpec] -- | Delegation specification -- -- NOTE: This is a close analogue of RoleSpec. data DelegationSpec DelegationSpec :: [Some PublicKey] -> KeyThreshold -> Delegation -> DelegationSpec delegationSpecKeys :: DelegationSpec -> [Some PublicKey] delegationSpecThreshold :: DelegationSpec -> KeyThreshold delegation :: DelegationSpec -> Delegation -- | A delegation -- -- A delegation is a pair of a pattern and a replacement. -- -- See match for an example. data Delegation Delegation :: (Pattern a) -> (Replacement a) -> Delegation targetsLookup :: TargetPath -> Targets -> Maybe FileInfo data Timestamp Timestamp :: FileVersion -> FileExpires -> FileInfo -> Timestamp timestampVersion :: Timestamp -> FileVersion timestampExpires :: Timestamp -> FileExpires timestampInfoSnapshot :: Timestamp -> FileInfo data Ed25519 data Key a KeyEd25519 :: PublicKey -> SecretKey -> Key Ed25519 data PublicKey a PublicKeyEd25519 :: PublicKey -> PublicKey Ed25519 data PrivateKey a PrivateKeyEd25519 :: SecretKey -> PrivateKey Ed25519 data KeyType typ KeyTypeEd25519 :: KeyType Ed25519 somePublicKey :: Some Key -> Some PublicKey somePublicKeyType :: Some PublicKey -> Some KeyType someKeyId :: HasKeyId key => Some key -> KeyId publicKey :: Key a -> PublicKey a privateKey :: Key a -> PrivateKey a createKey :: KeyType key -> IO (Key key) createKey' :: KeyType key -> IO (Some Key) -- | The key ID of a key, by definition, is the hexdigest of the SHA-256 -- hash of the canonical JSON form of the key where the private object -- key is excluded. -- -- NOTE: The FromJSON and ToJSON instances for KeyId are ntentially -- omitted. Use writeKeyAsId instead. newtype KeyId KeyId :: String -> KeyId keyIdString :: KeyId -> String -- | Compute the key ID of a key class HasKeyId key keyId :: HasKeyId key => key typ -> KeyId -- | Sign a bytestring and return the signature -- -- TODO: It is unfortunate that we have to convert to a strict bytestring -- for ed25519 sign :: PrivateKey typ -> ByteString -> ByteString verify :: PublicKey typ -> ByteString -> ByteString -> Bool -- | Repository -- -- This is an abstract representation of a repository. It simply provides -- a way to download metafiles and target files, without specifying how -- this is done. For instance, for a local repository this could just be -- doing a file read, whereas for remote repositories this could be using -- any kind of HTTP client. data Repository -- | Repository-specific exceptions -- -- For instance, for repositories using HTTP this might correspond to a -- 404; for local repositories this might correspond to file-not-found, -- etc. data SomeRemoteError :: * SomeRemoteError :: e -> SomeRemoteError -- | Log messages -- -- We use a RemoteFile rather than a RepoPath here because -- we might not have a RepoPath for the file that we were trying -- to download (that is, for example if the server does not provide an -- uncompressed tarball, it doesn't make much sense to list the path to -- that non-existing uncompressed tarball). data LogMessage -- | Root information was updated -- -- This message is issued when the root information is updated as part of -- the normal check for updates procedure. If the root information is -- updated because of a verification error WarningVerificationError is -- issued instead. LogRootUpdated :: LogMessage -- | A verification error -- -- Verification errors can be temporary, and may be resolved later; hence -- these are just warnings. (Verification errors that cannot be resolved -- are thrown as exceptions.) LogVerificationError :: VerificationError -> LogMessage -- | Download a file from a repository LogDownloading :: (Some RemoteFile) -> LogMessage -- | Incrementally updating a file from a repository LogUpdating :: (Some RemoteFile) -> LogMessage -- | Selected a particular mirror LogSelectedMirror :: MirrorDescription -> LogMessage -- | Updating a file failed (we will try again by downloading it whole) LogUpdateFailed :: (Some RemoteFile) -> UpdateFailure -> LogMessage -- | We got an exception with a particular mirror (we will try with a -- different mirror if any are available) LogMirrorFailed :: MirrorDescription -> SomeException -> LogMessage -- | Re-throw all exceptions thrown by the client API as unchecked -- exceptions uncheckClientErrors :: ((Throws VerificationError, Throws SomeRemoteError, Throws InvalidPackageException) => IO a) -> IO a -- | Errors thrown during role validation data VerificationError -- | Not enough signatures signed with the appropriate keys VerificationErrorSignatures :: TargetPath -> VerificationError -- | The file is expired VerificationErrorExpired :: TargetPath -> VerificationError -- | The file version is less than the previous version VerificationErrorVersion :: TargetPath -> VerificationError -- | File information mismatch VerificationErrorFileInfo :: TargetPath -> VerificationError -- | We tried to lookup file information about a particular target file, -- but the information wasn't in the corresponding targets.json -- file. VerificationErrorUnknownTarget :: TargetPath -> VerificationError -- | The file we requested from the server was larger than expected -- (potential endless data attack) VerificationErrorFileTooLarge :: TargetPath -> VerificationError -- | The spec stipulates that if a verification error occurs during the -- check for updates, we must download new root information and start -- over. However, we limit how often we attempt this. -- -- We record all verification errors that occurred before we gave up. VerificationErrorLoop :: VerificationHistory -> VerificationError data InvalidPackageException InvalidPackageException :: PackageIdentifier -> InvalidPackageException data InvalidFileInIndex InvalidFileInIndex :: IndexFile -> DeserializationError -> InvalidFileInIndex data LocalFileCorrupted LocalFileCorrupted :: DeserializationError -> LocalFileCorrupted instance Typeable InvalidPackageException instance Typeable LocalFileCorrupted instance Typeable InvalidFileInIndex instance Show CheckExpiry instance Show HasUpdates instance Pretty InvalidFileInIndex instance Pretty LocalFileCorrupted instance Pretty InvalidPackageException instance Exception InvalidFileInIndex instance Exception LocalFileCorrupted instance Exception InvalidPackageException instance Show InvalidFileInIndex instance Show LocalFileCorrupted instance Show InvalidPackageException -- | Abstracting over HTTP libraries module Hackage.Security.Client.Repository.HttpLib -- | Abstraction over HTTP clients -- -- This avoids insisting on a particular implementation (such as the HTTP -- package) and allows for other implementations (such as a conduit based -- one). -- -- NOTE: Library-specific exceptions MUST be wrapped in -- SomeRemoteError. data HttpLib HttpLib :: (forall a. Throws SomeRemoteError => [HttpRequestHeader] -> URI -> ([HttpResponseHeader] -> BodyReader -> IO a) -> IO a) -> (forall a. Throws SomeRemoteError => [HttpRequestHeader] -> URI -> (Int, Int) -> ([HttpResponseHeader] -> BodyReader -> IO a) -> IO a) -> HttpLib -- | Download a file httpGet :: HttpLib -> forall a. Throws SomeRemoteError => [HttpRequestHeader] -> URI -> ([HttpResponseHeader] -> BodyReader -> IO a) -> IO a -- | Download a byte range -- -- Range is starting and (exclusive) end offset in bytes. httpGetRange :: HttpLib -> forall a. Throws SomeRemoteError => [HttpRequestHeader] -> URI -> (Int, Int) -> ([HttpResponseHeader] -> BodyReader -> IO a) -> IO a -- | Additional request headers -- -- Since different libraries represent headers differently, here we just -- abstract over the few request headers that we might want to set data HttpRequestHeader -- | Set Cache-Control: max-age=0 HttpRequestMaxAge0 :: HttpRequestHeader -- | Set Cache-Control: no-transform HttpRequestNoTransform :: HttpRequestHeader -- | Request transport compression (Accept-Encoding: gzip) -- -- It is the responsibility of the HttpLib to do compression (and -- report whether the original server reply was compressed or not). -- -- NOTE: Clients should NOT allow for compression unless explicitly -- requested (since decompression happens before signature verification, -- it is a potential security concern). HttpRequestContentCompression :: HttpRequestHeader -- | Response headers -- -- Since different libraries represent headers differently, here we just -- abstract over the few response headers that we might want to know -- about. data HttpResponseHeader -- | Server accepts byte-range requests (Accept-Ranges: bytes) HttpResponseAcceptRangesBytes :: HttpResponseHeader -- | Original server response was compressed (the HttpLib however -- must do decompression) HttpResponseContentCompression :: HttpResponseHeader -- | Proxy configuration -- -- Although actually setting the proxy is the purview of the -- initialization function for individual HttpLib implementations -- and therefore outside the scope of this module, we offer this -- ProxyConfiguration type here as a way to uniformly configure -- proxies across all HttpLibs. data ProxyConfig a -- | Don't use a proxy ProxyConfigNone :: ProxyConfig a -- | Use this specific proxy -- -- Individual HTTP backends use their own types for specifying proxies. ProxyConfigUse :: a -> ProxyConfig a -- | Use automatic proxy settings -- -- What precisely automatic means is HttpLib specific, though -- typically it will involve looking at the HTTP_PROXY -- environment variable or the (Windows) registry. ProxyConfigAuto :: ProxyConfig a -- | An IO action that represents an incoming response body coming -- from the server. -- -- The action gets a single chunk of data from the response body, or an -- empty bytestring if no more data is available. -- -- This definition is copied from the http-client package. type BodyReader = IO ByteString -- | Construct a Body reader from a lazy bytestring -- -- This is appropriate if the lazy bytestring is constructed, say, by -- calling hGetContents on a network socket, and the chunks of -- the bytestring correspond to the chunks as they are returned from the -- OS network layer. -- -- If the lazy bytestring needs to be re-chunked this function is NOT -- suitable. bodyReaderFromBS :: ByteString -> IO BodyReader instance Eq HttpRequestHeader instance Ord HttpRequestHeader instance Show HttpRequestHeader instance Eq HttpResponseHeader instance Ord HttpResponseHeader instance Show HttpResponseHeader -- | The files we cache from the repository -- -- Both the Local and the Remote repositories make use of this module. module Hackage.Security.Client.Repository.Cache -- | Location and layout of the local cache data Cache Cache :: AbsolutePath -> CacheLayout -> Cache cacheRoot :: Cache -> AbsolutePath cacheLayout :: Cache -> CacheLayout -- | Get a cached file (if available) getCached :: Cache -> CachedFile -> IO (Maybe AbsolutePath) -- | Get the cached root -- -- Calling getCachedRoot without root info available is a -- programmer error and will result in an unchecked exception. See -- requiresBootstrap. getCachedRoot :: Cache -> IO AbsolutePath -- | Get the cached index (if available) getCachedIndex :: Cache -> IO (Maybe AbsolutePath) -- | Delete a previously downloaded remote file clearCache :: Cache -> IO () -- | Get a file from the index getFromIndex :: Cache -> IndexLayout -> IndexFile -> IO (Maybe ByteString) -- | Cache a previously downloaded remote file cacheRemoteFile :: Cache -> TempPath -> Format f -> IsCached -> IO () -- | Local repository module Hackage.Security.Client.Repository.Local -- | Location of the repository -- -- Note that we regard the local repository as immutable; we cache files -- just like we do for remote repositories. type LocalRepo = Path (Rooted Absolute) -- | Initialize the repository (and cleanup resources afterwards) -- -- Like a remote repository, a local repository takes a RepoLayout as -- argument; but where the remote repository interprets this RepoLayout -- relative to a URL, the local repository interprets it relative to a -- local directory. -- -- It uses the same cache as the remote repository. withRepository :: LocalRepo -> Cache -> RepoLayout -> (LogMessage -> IO ()) -> (Repository -> IO a) -> IO a -- | An implementation of Repository that talks to repositories over HTTP. -- -- This implementation is itself parameterized over a -- HttpClient, so that it it not tied to a specific library; for -- instance, HttpClient can be implemented with the -- HTTP library, the http-client libary, or others. -- -- It would also be possible to give _other_ Repository implementations -- that talk to repositories over HTTP, if you want to make other design -- decisions than we did here, in particular: -- -- module Hackage.Security.Client.Repository.Remote -- | Initialize the repository (and cleanup resources afterwards) -- -- We allow to specify multiple mirrors to initialize the repository. -- These are mirrors that can be found "out of band" (out of the scope of -- the TUF protocol), for example in a cabal.config file. The -- TUF protocol itself will specify that any of these mirrors can serve a -- mirrors.json file that itself contains mirrors; we consider -- these as _additional_ mirrors to the ones that are passed here. -- -- NOTE: The list of mirrors should be non-empty (and should typically -- include the primary server). -- -- TODO: In the future we could allow finer control over precisely which -- mirrors we use (which combination of the mirrors that are passed as -- arguments here and the mirrors that we get from mirrors.json) -- as well as indicating mirror preferences. withRepository :: HttpLib -> [URI] -> AllowContentCompression -> WantCompressedIndex -> Cache -> RepoLayout -> (LogMessage -> IO ()) -> (Repository -> IO a) -> IO a -- | Should we allow HTTP content compression? -- -- Since content compression happens before signature verification, users -- who are concerned about potential exploits of the decompression -- algorithm may prefer to disallow content compression. data AllowContentCompression AllowContentCompression :: AllowContentCompression DisallowContentCompression :: AllowContentCompression -- | Do we want to a copy of the compressed index? -- -- This is important for mirroring clients only. data WantCompressedIndex WantCompressedIndex :: WantCompressedIndex DontNeedCompressedIndex :: WantCompressedIndex data FileSize -- | For most files we download we know the exact size beforehand (because -- this information comes from the snapshot or delegated info) FileSizeExact :: Int -> FileSize -- | For some files we might not know the size beforehand, but we might be -- able to provide an upper bound (timestamp, root info) FileSizeBound :: Int -> FileSize fileSizeWithinBounds :: Int -> FileSize -> Bool -- | Main entry point into the Hackage Security framework for clients module Hackage.Security.Server data Ed25519 data Key a KeyEd25519 :: PublicKey -> SecretKey -> Key Ed25519 data PublicKey a PublicKeyEd25519 :: PublicKey -> PublicKey Ed25519 data PrivateKey a PrivateKeyEd25519 :: SecretKey -> PrivateKey Ed25519 data KeyType typ KeyTypeEd25519 :: KeyType Ed25519 somePublicKey :: Some Key -> Some PublicKey somePublicKeyType :: Some PublicKey -> Some KeyType someKeyId :: HasKeyId key => Some key -> KeyId publicKey :: Key a -> PublicKey a privateKey :: Key a -> PrivateKey a createKey :: KeyType key -> IO (Key key) createKey' :: KeyType key -> IO (Some Key) -- | The key ID of a key, by definition, is the hexdigest of the SHA-256 -- hash of the canonical JSON form of the key where the private object -- key is excluded. -- -- NOTE: The FromJSON and ToJSON instances for KeyId are ntentially -- omitted. Use writeKeyAsId instead. newtype KeyId KeyId :: String -> KeyId keyIdString :: KeyId -> String -- | Compute the key ID of a key class HasKeyId key keyId :: HasKeyId key => key typ -> KeyId -- | Sign a bytestring and return the signature -- -- TODO: It is unfortunate that we have to convert to a strict bytestring -- for ed25519 sign :: PrivateKey typ -> ByteString -> ByteString verify :: PublicKey typ -> ByteString -> ByteString -> Bool -- | File length -- -- Having verified file length information means we can protect against -- endless data attacks and similar. newtype FileLength FileLength :: Int -> FileLength fileLength :: FileLength -> Int -- | File hash newtype Hash Hash :: String -> Hash -- | Key threshold -- -- The key threshold is the minimum number of keys a document must be -- signed with. Key thresholds are specified in RoleSpec or -- DelegationsSpec. newtype KeyThreshold KeyThreshold :: Int -> KeyThreshold -- | File information -- -- This intentionally does not have an Eq instance; see -- knownFileInfoEqual and verifyFileInfo instead. -- -- NOTE: Throughout we compute file information always over the raw -- bytes. For example, when timestamp.json lists the hash of -- snapshot.json, this hash is computed over the actual -- snapshot.json file (as opposed to the canonical form of the -- embedded JSON). This brings it in line with the hash computed over -- target files, where that is the only choice available. data FileInfo FileInfo :: FileLength -> Map HashFn Hash -> FileInfo fileInfoLength :: FileInfo -> FileLength fileInfoHashes :: FileInfo -> Map HashFn Hash data HashFn HashFnSHA256 :: HashFn -- | File hash newtype Hash Hash :: String -> Hash -- | Compute FileInfo -- -- TODO: Currently this will load the entire input bytestring into -- memory. We need to make this incremental, by computing the length and -- all hashes in a single traversal over the input. However, the precise -- way to do that will depend on the hashing package we will use, and we -- have yet to pick that package. fileInfo :: ByteString -> FileInfo -- | Compute FileInfo computeFileInfo :: IsFileSystemRoot root => Path (Rooted root) -> IO FileInfo -- | Compare known file info -- -- This should be used only when the FileInfo is already known. If we -- want to compare known FileInfo against a file on disk we should delay -- until we know have confirmed that the file lengths don't match (see -- verifyFileInfo). knownFileInfoEqual :: FileInfo -> FileInfo -> Bool class HasHeader a fileExpires :: HasHeader a => Lens' a FileExpires fileVersion :: HasHeader a => Lens' a FileVersion -- | File version -- -- The file version is a flat integer which must monotonically increase -- on every file update. -- -- Show and Read instance are defined in terms of the -- underlying Int (this is use for example by hackage during the -- backup process). newtype FileVersion FileVersion :: Int -> FileVersion -- | File expiry date -- -- A Nothing value here means no expiry. That makes it possible to -- set some files to never expire. (Note that not having the Maybe in the -- type here still allows that, because you could set an expiry date 2000 -- years into the future. By having the Maybe here we avoid the _need_ -- for such encoding issues.) newtype FileExpires FileExpires :: (Maybe UTCTime) -> FileExpires -- | Occassionally it is useful to read only a header from a file. -- -- HeaderOnly intentionally only has a FromJSON instance -- (no ToJSON). data Header Header :: FileExpires -> FileVersion -> Header headerExpires :: Header -> FileExpires headerVersion :: Header -> FileVersion expiresInDays :: UTCTime -> Integer -> FileExpires expiresNever :: FileExpires isExpired :: UTCTime -> FileExpires -> Bool versionInitial :: FileVersion versionIncrement :: FileVersion -> FileVersion -- | The root of the repository -- -- Repository roots can be anchored at a remote URL or a local directory. -- -- Note that even for remote repos RepoRoot is (potentially) -- different from WebRoot -- for a repository located at, say, -- http://hackage.haskell.org they happen to coincide, -- but for one location at -- http://example.com/some/subdirectory they do not. data RepoRoot -- | Paths relative to the root of the repository type RepoPath = Path (Rooted RepoRoot) -- | Layout of a repository data RepoLayout RepoLayout :: RepoPath -> RepoPath -> RepoPath -> RepoPath -> RepoPath -> RepoPath -> (PackageIdentifier -> RepoPath) -> IndexLayout -> RepoLayout -- | TUF root metadata repoLayoutRoot :: RepoLayout -> RepoPath -- | TUF timestamp repoLayoutTimestamp :: RepoLayout -> RepoPath -- | TUF snapshot repoLayoutSnapshot :: RepoLayout -> RepoPath -- | TUF mirrors list repoLayoutMirrors :: RepoLayout -> RepoPath -- | Compressed index tarball repoLayoutIndexTarGz :: RepoLayout -> RepoPath -- | Uncompressed index tarball repoLayoutIndexTar :: RepoLayout -> RepoPath -- | Path to the package tarball repoLayoutPkgTarGz :: RepoLayout -> PackageIdentifier -> RepoPath -- | Layout of the index -- -- Since the repository hosts the index, the layout of the index is not -- independent of the layout of the repository. repoIndexLayout :: RepoLayout -> IndexLayout -- | The layout used on Hackage hackageRepoLayout :: RepoLayout -- | Layout used by cabal for ("legacy") local repos -- -- Obviously, such repos do not normally contain any of the TUF files, so -- their location is more or less arbitrary here. cabalLocalRepoLayout :: RepoLayout anchorRepoPathLocally :: IsFileSystemRoot root => Path (Rooted root) -> RepoPath -> Path (Rooted root) anchorRepoPathRemotely :: URIPath -> RepoPath -> URIPath -- | The root of the index tarball data IndexRoot -- | Paths relative to the root of the index tarball type IndexPath = Path (Rooted RepoRoot) -- | Layout of the files within the index tarball data IndexLayout IndexLayout :: (PackageIdentifier -> IndexPath) -> (PackageIdentifier -> IndexPath) -> IndexLayout -- | TUF metadata for a package indexLayoutPkgMetadata :: IndexLayout -> PackageIdentifier -> IndexPath -- | Package .cabal file indexLayoutPkgCabal :: IndexLayout -> PackageIdentifier -> IndexPath -- | The layout of the index as maintained on Hackage hackageIndexLayout :: IndexLayout -- | The cache directory data CacheRoot type CachePath = Path (Rooted CacheRoot) -- | Location of the various files we cache -- -- Although the generic TUF algorithms do not care how we organize the -- cache, we nonetheless specity this here because as long as there are -- tools which access files in the cache directly we need to define the -- cache layout. See also comments for defaultCacheLayout. data CacheLayout CacheLayout :: CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> Maybe CachePath -> CacheLayout -- | TUF root metadata cacheLayoutRoot :: CacheLayout -> CachePath -- | TUF timestamp cacheLayoutTimestamp :: CacheLayout -> CachePath -- | TUF snapshot cacheLayoutSnapshot :: CacheLayout -> CachePath -- | TUF mirrors list cacheLayoutMirrors :: CacheLayout -> CachePath -- | Uncompressed index tarball cacheLayoutIndexTar :: CacheLayout -> CachePath -- | Index to the uncompressed index tarball cacheLayoutIndexIdx :: CacheLayout -> CachePath -- | Compressed index tarball (if cached) cacheLayoutIndexTarGz :: CacheLayout -> Maybe CachePath -- | The cache layout cabal-install uses -- -- We cache the index as cache/00-index.tar; this is -- important because `cabal-install` expects to find it there (and does -- not currently go through the hackage-security library to get files -- from the index). cabalCacheLayout :: CacheLayout -- | Anchor a cache path to the location of the cache anchorCachePath :: IsFileSystemRoot root => Path (Rooted root) -> CachePath -> Path (Rooted root) data Mirrors Mirrors :: FileVersion -> FileExpires -> [Mirror] -> Mirrors mirrorsVersion :: Mirrors -> FileVersion mirrorsExpires :: Mirrors -> FileExpires mirrorsMirrors :: Mirrors -> [Mirror] -- | Definition of a mirror -- -- NOTE: Unlike the TUF specification, we require that all mirrors must -- have the same format. That is, we omit metapath and -- targetspath. data Mirror Mirror :: URI -> MirrorContent -> Mirror mirrorUrlBase :: Mirror -> URI mirrorContent :: Mirror -> MirrorContent -- | Full versus partial mirrors -- -- The TUF spec explicitly allows for partial mirrors, with the mirrors -- file specifying (through patterns) what is available from partial -- mirrors. -- -- For now we only support full mirrors; if we wanted to add partial -- mirrors, we would add a second MirrorPartial constructor here -- with arguments corresponding to TUF's metacontent and -- targetscontent fields. data MirrorContent MirrorFull :: MirrorContent type MirrorDescription = String -- | Give a human-readable description of a particular mirror -- -- (for use in error messages) describeMirror :: Mirror -> MirrorDescription type FileName = String type Directory = String type Extension = String type BaseName = String -- | Structured patterns over paths -- -- The type argument indicates what kind of function we expect when the -- pattern matches. For example, we have the pattern "*/*.txt": -- --
--   PathPatternDirAny (PathPatternFileExt ".txt")
--     :: PathPattern (Directory :- BaseName :- ())
--   
-- -- TODOs (see README.md): -- -- -- -- Currently this is a proof of concept more than anything else; the -- right structure is here, but it needs updating. However, until we add -- author signing (or out-of-tarball targets) we don't actually use this -- yet. -- -- NOTE: Haddock lacks GADT support so constructors have only regular -- comments. data Pattern a PatFileConst :: FileName -> Pattern () PatFileExt :: Extension -> Pattern (BaseName :- ()) PatFileAny :: Pattern (FileName :- ()) PatDirConst :: Directory -> Pattern a -> Pattern a PatDirAny :: Pattern a -> Pattern (Directory :- a) -- | Replacement patterns -- -- These constructors match the ones in Pattern: wildcards must be -- used in the same order as they appear in the pattern, but they don't -- all have to be used (that's why the base constructors are polymorphic -- in the stack tail). data Replacement a RepFileConst :: FileName -> Replacement a RepFileExt :: Extension -> Replacement (BaseName :- a) RepFileAny :: Replacement (FileName :- a) RepDirConst :: Directory -> Replacement a -> Replacement a RepDirAny :: Replacement a -> Replacement (Directory :- a) -- | A delegation -- -- A delegation is a pair of a pattern and a replacement. -- -- See match for an example. data Delegation Delegation :: (Pattern a) -> (Replacement a) -> Delegation -- | The identity replacement replaces a matched pattern with itself identityReplacement :: Pattern typ -> Replacement typ matchDelegation :: Delegation -> String -> Maybe String parseDelegation :: String -> String -> Either String Delegation -- | Quasi-quoter for delegations to make them easier to write in code -- -- This allows to write delegations as -- --
--   $(qqd "targets/*/*/*.cabal" "targets/*/*/revisions.json")
--   
-- -- (The alternative syntax which actually uses a quasi-quoter doesn't -- work very well because the /* bits confuse CPP: "unterminated -- comment") qqd :: String -> String -> Q Exp -- | The root metadata -- -- NOTE: We must have the invariant that ALL keys (apart from delegation -- keys) must be listed in rootKeys. (Delegation keys satisfy a -- similar invariant, see Targets.) data Root Root :: FileVersion -> FileExpires -> KeyEnv -> RootRoles -> Root rootVersion :: Root -> FileVersion rootExpires :: Root -> FileExpires rootKeys :: Root -> KeyEnv rootRoles :: Root -> RootRoles data RootRoles RootRoles :: RoleSpec Root -> RoleSpec Snapshot -> RoleSpec Targets -> RoleSpec Timestamp -> RoleSpec Mirrors -> RootRoles rootRolesRoot :: RootRoles -> RoleSpec Root rootRolesSnapshot :: RootRoles -> RoleSpec Snapshot rootRolesTargets :: RootRoles -> RoleSpec Targets rootRolesTimestamp :: RootRoles -> RoleSpec Timestamp rootRolesMirrors :: RootRoles -> RoleSpec Mirrors -- | Role specification -- -- The phantom type indicates what kind of type this role is meant to -- verify. data RoleSpec a RoleSpec :: [Some PublicKey] -> KeyThreshold -> RoleSpec a roleSpecKeys :: RoleSpec a -> [Some PublicKey] roleSpecThreshold :: RoleSpec a -> KeyThreshold data Signed a Signed :: a -> Signatures -> Signed a signed :: Signed a -> a signatures :: Signed a -> Signatures -- | A list of signatures -- -- Invariant: each signature must be made with a different key. We -- enforce this invariant for incoming untrusted data -- (fromPreSignatures) but not for lists of signatures that we -- create in code. newtype Signatures Signatures :: [Signature] -> Signatures data Signature Signature :: ByteString -> Some PublicKey -> Signature signature :: Signature -> ByteString signatureKey :: Signature -> Some PublicKey -- | Create a new document without any signatures unsigned :: a -> Signed a -- | Sign a document withSignatures :: ToJSON WriteJSON a => RepoLayout -> [Some Key] -> a -> Signed a -- | Variation on withSignatures that doesn't need the repo layout withSignatures' :: ToJSON Identity a => [Some Key] -> a -> Signed a -- | Construct signatures for already rendered value signRendered :: [Some Key] -> ByteString -> Signatures verifySignature :: ByteString -> Signature -> Bool -- | General FromJSON instance for signed datatypes -- -- We don't give a general FromJSON instance for Signed because for some -- datatypes we need to do something special (datatypes where we need to -- read key environments); for instance, see the "Signed Root" instance. signedFromJSON :: (MonadKeys m, FromJSON m a) => JSValue -> m (Signed a) -- | Signature verification -- -- NOTES: 1. By definition, the signature must be verified against the -- canonical JSON format. This means we _must_ parse and then pretty -- print (as we do here) because the document as stored may or may not be -- in canonical format. 2. However, it is important that we NOT translate -- from the JSValue to whatever internal datatype we are using and then -- back to JSValue, because that may not roundtrip: we must allow for -- additional fields in the JSValue that we ignore (and would therefore -- lose when we attempt to roundtrip). 3. We verify that all signatures -- are valid, but we cannot verify (here) that these signatures are -- signed with the right key, or that we have a sufficient number of -- signatures. This will be the responsibility of the calling code. verifySignatures :: JSValue -> Signatures -> Bool -- | File with uninterpreted signatures -- -- Sometimes we want to be able to read a file without interpreting the -- signatures (that is, resolving the key IDs) or doing any kind of -- checks on them. One advantage of this is that this allows us to read -- many file types without any key environment at all, which is sometimes -- useful. data UninterpretedSignatures a UninterpretedSignatures :: a -> [PreSignature] -> UninterpretedSignatures a uninterpretedSigned :: UninterpretedSignatures a -> a uninterpretedSignatures :: UninterpretedSignatures a -> [PreSignature] -- | A signature with a key ID (rather than an actual key) -- -- This corresponds precisely to the TUF representation of a signature. data PreSignature PreSignature :: ByteString -> Some KeyType -> KeyId -> PreSignature presignature :: PreSignature -> ByteString presigMethod :: PreSignature -> Some KeyType presigKeyId :: PreSignature -> KeyId -- | Convert a pre-signature to a signature -- -- Verifies that the key type matches the advertised method. fromPreSignature :: MonadKeys m => PreSignature -> m Signature -- | Convert a list of PreSignatures to a list of Signatures -- -- This verifies the invariant that all signatures are made with -- different keys. We do this on the presignatures rather than the -- signatures so that we can do the check on key IDs, rather than keys -- (the latter don't have an Ord instance). fromPreSignatures :: MonadKeys m => [PreSignature] -> m Signatures -- | Convert signature to pre-signature toPreSignature :: Signature -> PreSignature -- | Convert list of pre-signatures to a list of signatures toPreSignatures :: Signatures -> [PreSignature] data Snapshot Snapshot :: FileVersion -> FileExpires -> FileInfo -> FileInfo -> FileInfo -> Maybe FileInfo -> Snapshot snapshotVersion :: Snapshot -> FileVersion snapshotExpires :: Snapshot -> FileExpires -- | File info for the root metadata -- -- We list this explicitly in the snapshot so that we can check if we -- need to update the root metadata without first having to download the -- entire index tarball. snapshotInfoRoot :: Snapshot -> FileInfo -- | File info for the mirror metadata snapshotInfoMirrors :: Snapshot -> FileInfo -- | Compressed index tarball snapshotInfoTarGz :: Snapshot -> FileInfo -- | Uncompressed index tarball -- -- Repositories are not required to provide this. snapshotInfoTar :: Snapshot -> Maybe FileInfo -- | Target metadata -- -- Most target files do not need expiry dates because they are not -- subject to change (and hence attacks like freeze attacks are not a -- concern). data Targets Targets :: FileVersion -> FileExpires -> FileMap -> Maybe Delegations -> Targets targetsVersion :: Targets -> FileVersion targetsExpires :: Targets -> FileExpires targetsTargets :: Targets -> FileMap targetsDelegations :: Targets -> Maybe Delegations -- | Delegations -- -- Much like the Root datatype, this must have an invariant that ALL used -- keys (apart from the global keys, which are in the root key -- environment) must be listed in delegationsKeys. data Delegations Delegations :: KeyEnv -> [DelegationSpec] -> Delegations delegationsKeys :: Delegations -> KeyEnv delegationsRoles :: Delegations -> [DelegationSpec] -- | Delegation specification -- -- NOTE: This is a close analogue of RoleSpec. data DelegationSpec DelegationSpec :: [Some PublicKey] -> KeyThreshold -> Delegation -> DelegationSpec delegationSpecKeys :: DelegationSpec -> [Some PublicKey] delegationSpecThreshold :: DelegationSpec -> KeyThreshold delegation :: DelegationSpec -> Delegation -- | A delegation -- -- A delegation is a pair of a pattern and a replacement. -- -- See match for an example. data Delegation Delegation :: (Pattern a) -> (Replacement a) -> Delegation targetsLookup :: TargetPath -> Targets -> Maybe FileInfo data Timestamp Timestamp :: FileVersion -> FileExpires -> FileInfo -> Timestamp timestampVersion :: Timestamp -> FileVersion timestampExpires :: Timestamp -> FileExpires timestampInfoSnapshot :: Timestamp -> FileInfo