Skip to content

Commit

Permalink
Remove calls to head
Browse files Browse the repository at this point in the history
  • Loading branch information
ocheron committed Aug 27, 2023
1 parent 94a13b4 commit afbbc19
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Crypto/Store/CMS/Signed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,13 @@ certSigner :: MonadRandom m
-> Maybe [Attribute]
-> [Attribute]
-> ProducerOfSI m
certSigner alg priv (CertificateChain chain) sAttrsM uAttrs ct msg =
certSigner _ _ (CertificateChain []) _ _ _ _ =
pure $ Left (InvalidInput "Empty certificate chain")
certSigner alg priv (CertificateChain chain@(cert:_)) sAttrsM uAttrs ct msg =
fmap build <$> generate
where
md = digest dig msg
def = DigestAlgorithm Crypto.Store.CMS.Algorithms.SHA256
cert = head chain
obj = signedObject (getSigned cert)
isn = IssuerAndSerialNumber (certIssuerDN obj) (certSerial obj)
pub = certPubKey obj
Expand Down
9 changes: 5 additions & 4 deletions src/Crypto/Store/PKCS12.hs
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,10 @@ fromCredential' :: ([Attribute] -> [Attribute])
-> ProtectionPassword
-> (X509.CertificateChain, X509.PrivKey)
-> Either StoreError PKCS12
fromCredential' trans algChain algKey pwd (X509.CertificateChain certs, key)
| null certs = Left (InvalidInput "Empty certificate chain")
| otherwise = (<>) <$> pkcs12Chain <*> pkcs12Key
fromCredential' _ _ _ _ (X509.CertificateChain [], _) =
Left (InvalidInput "Empty certificate chain")
fromCredential' trans algChain algKey pwd (X509.CertificateChain certs@(leaf:_), key) =
(<>) <$> pkcs12Chain <*> pkcs12Key
where
pkcs12Key = unencrypted <$> scKeyOrError
pkcs12Chain =
Expand All @@ -689,7 +690,7 @@ fromCredential' trans algChain algKey pwd (X509.CertificateChain certs, key)
wrap shrouded = SafeContents [Bag (PKCS8ShroudedKeyBag shrouded) attrs]
encodedKey = encodeASN1Object (FormattedKey PKCS8Format key)

X509.Fingerprint keyId = X509.getFingerprint (head certs) X509.HashSHA1
X509.Fingerprint keyId = X509.getFingerprint leaf X509.HashSHA1
attrs = trans (setLocalKeyId keyId [])

-- Standard attributes
Expand Down
2 changes: 1 addition & 1 deletion tests/X509/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ keyTests name prefix count =
writeSignedObjectToMemory objs @?= bs
, testCase "write public key" $ do
bs <- B.readFile fKey
let key = head (readPubKeyFileFromMemory bs)
let (key : _) = readPubKeyFileFromMemory bs
assertBool "first key differs" $
writePubKeyFileToMemory [key] `B.isPrefixOf` bs
]
Expand Down

0 comments on commit afbbc19

Please sign in to comment.