Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 9f6fd9c

Browse files
committed
[CBR-470] Ensure 600 file permission on generated x509 certificates and keys
It is of the utmost importance that those files aren't readable by anyone but the user generating them. Files are generated upon start and used to secure the communication between cardano-sl and its clients (e.g. Daedalus) Note that, we do assume that users are able to secure their own environment from potential adversary services, so this is really about preventing one user to have access to certificates of another user.
1 parent f273cb7 commit 9f6fd9c

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

x509/cardano-sl-x509.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ library
3737
, optparse-applicative
3838
, text
3939
, universum
40+
, unix
4041
, unordered-containers
4142
, x509
4243
, x509-store

x509/src/Data/X509/Extra.hs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE FlexibleInstances #-}
23
{-# LANGUAGE LambdaCase #-}
34

5+
#if !defined(mingw32_HOST_OS)
6+
#define POSIX
7+
#endif
8+
49
--
510
-- | Cryptographic & Data.X509 specialized methods for RSA with SHA256
611
--
@@ -50,6 +55,11 @@ import Data.X509.CertificateStore (CertificateStore,
5055
import Data.X509.Validation
5156
import Net.IPv4 (IPv4 (..))
5257
import Net.IPv6 (IPv6 (..))
58+
#ifdef POSIX
59+
import System.Posix.Files (ownerReadMode, ownerWriteMode, setFileMode,
60+
unionFileModes)
61+
import System.Posix.Types (FileMode)
62+
#endif
5363

5464
import qualified Crypto.PubKey.RSA.Types as RSA
5565
import qualified Data.ByteString as BS
@@ -231,21 +241,20 @@ writeCredentials
231241
-> (PrivateKey, SignedCertificate)
232242
-> IO ()
233243
writeCredentials filename (key, cert) = do
234-
BS.writeFile (filename <> ".pem") (BS.concat [keyBytes, "\n", certBytes])
235-
BS.writeFile (filename <> ".key") keyBytes
236-
BS.writeFile (filename <> ".crt") certBytes
244+
writeFile600 (filename <> ".pem") (BS.concat [keyBytes, "\n", certBytes])
245+
writeFile600 (filename <> ".key") keyBytes
246+
writeFile600 (filename <> ".crt") certBytes
237247
where
238248
keyBytes = encodePEM key
239249
certBytes = encodePEM cert
240250

241-
242251
-- | Write a certificate to the given location
243252
writeCertificate
244253
:: FilePath
245254
-> SignedCertificate
246255
-> IO ()
247256
writeCertificate filename cert =
248-
BS.writeFile (filename <> ".crt") (encodePEM cert)
257+
writeFile600 (filename <> ".crt") (encodePEM cert)
249258

250259

251260
--
@@ -330,3 +339,18 @@ validateCertificateIP ip cert =
330339
[]
331340
else
332341
[NameMismatch $ B8.unpack ip]
342+
343+
344+
writeFile600 :: FilePath -> ByteString -> IO ()
345+
#ifdef POSIX
346+
writeFile600 filepath bytes = do
347+
BS.writeFile filepath mempty
348+
setFileMode filepath mode600
349+
BS.appendFile filepath bytes
350+
where
351+
mode600 :: FileMode
352+
mode600 = unionFileModes ownerReadMode ownerWriteMode
353+
#else
354+
writeFile600 _ _ =
355+
return ()
356+
#endif

0 commit comments

Comments
 (0)