|
| 1 | +// Copyright 2026 HAProxy Technologies |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +package storage |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "crypto/ecdsa" |
| 21 | + "crypto/elliptic" |
| 22 | + "crypto/rand" |
| 23 | + "crypto/x509" |
| 24 | + "crypto/x509/pkix" |
| 25 | + "encoding/pem" |
| 26 | + "io" |
| 27 | + "math/big" |
| 28 | + "os" |
| 29 | + "strings" |
| 30 | + "testing" |
| 31 | + "time" |
| 32 | + |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | +) |
| 35 | + |
| 36 | +// Certificate bundles include the private key: owner only. |
| 37 | +func TestSSLStorage_PrivateKeyNotWorldReadable(t *testing.T) { |
| 38 | + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 39 | + require.NoError(t, err) |
| 40 | + tmpl := &x509.Certificate{ |
| 41 | + SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "test.local"}, |
| 42 | + NotBefore: time.Now(), NotAfter: time.Now().Add(time.Hour), |
| 43 | + } |
| 44 | + der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key) |
| 45 | + require.NoError(t, err) |
| 46 | + kb, err := x509.MarshalECPrivateKey(key) |
| 47 | + require.NoError(t, err) |
| 48 | + var bundle bytes.Buffer |
| 49 | + require.NoError(t, pem.Encode(&bundle, &pem.Block{Type: "CERTIFICATE", Bytes: der})) |
| 50 | + require.NoError(t, pem.Encode(&bundle, &pem.Block{Type: "EC PRIVATE KEY", Bytes: kb})) |
| 51 | + |
| 52 | + pemData := bundle.String() |
| 53 | + s, err := New(t.TempDir(), SSLType) |
| 54 | + require.NoError(t, err) |
| 55 | + f, _, err := s.Create("site.pem", io.NopCloser(strings.NewReader(pemData))) |
| 56 | + require.NoError(t, err) |
| 57 | + fi, err := os.Stat(f) |
| 58 | + require.NoError(t, err) |
| 59 | + require.Zero(t, fi.Mode().Perm()&0o077, "created: private key readable by group/others: %v", fi.Mode().Perm()) |
| 60 | + |
| 61 | + f, err = s.Replace("site.pem", pemData) |
| 62 | + require.NoError(t, err) |
| 63 | + fi, err = os.Stat(f) |
| 64 | + require.NoError(t, err) |
| 65 | + require.Zero(t, fi.Mode().Perm()&0o077, "replaced: private key readable by group/others: %v", fi.Mode().Perm()) |
| 66 | +} |
0 commit comments