Skip to content

Commit 516e1c0

Browse files
authored
Merge pull request #108 from dirkmueller/fips140
Switch to sha256 from md5
2 parents a3f3e2f + 81fdca9 commit 516e1c0

6 files changed

Lines changed: 31 additions & 29 deletions

File tree

cmd/container-suseconnect/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func requestProducts() ([]cs.Product, error) {
126126
suseConnectData.Insecure = false
127127

128128
if cloudCfg.Ca != "" {
129-
regionsrv.SafeCAFile(cloudCfg.Ca)
129+
regionsrv.SaveCAFile(cloudCfg.Ca)
130130
}
131131

132132
regionsrv.UpdateHostsFile(cloudCfg.ServerFqdn, cloudCfg.ServerIP)

internal/regionsrv/ca.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
package regionsrv
1616

1717
import (
18-
"crypto/md5"
18+
"crypto/sha256"
1919
"io"
2020
"os"
2121
"os/exec"
2222
"strings"
2323
)
2424

2525
var (
26-
hashFilePath = "/etc/pki/containerbuild-regionsrv.md5"
27-
caFilePath = "/etc/pki/trust/anchors/containerbuild-regionsrv.pem"
26+
oldHashFilePath = "/etc/pki/containerbuild-regionsrv.md5"
27+
hashFilePath = "/etc/pki/containerbuild-regionsrv.sha256"
28+
caFilePath = "/etc/pki/trust/anchors/containerbuild-regionsrv.pem"
2829
)
2930

3031
// commander is a very simple interface that just implements the `Run` function,
@@ -34,7 +35,7 @@ type commander interface {
3435
Run() error
3536
}
3637

37-
// Returns true if the CA file has to be updated, false otherwise.
38+
// Returns true if the CA file needs an update, false otherwise.
3839
func updateNeeded(contents string) bool {
3940
if _, err := os.Stat(hashFilePath); os.IsNotExist(err) {
4041
return true
@@ -45,24 +46,25 @@ func updateNeeded(contents string) bool {
4546
return true
4647
}
4748

48-
hash := md5.New()
49+
hash := sha256.New()
4950
io.WriteString(hash, contents)
5051

5152
return strings.TrimSpace(string(data)) != string(hash.Sum(nil))
5253
}
5354

54-
// safeCAFile implements `SafeCAFile` by assuming a `commander` type will be
55+
// saveCAFile implements `SaveCAFile` by assuming a `commander` type will be
5556
// given.
56-
func safeCAFile(cmd commander, contents string) error {
57+
func saveCAFile(cmd commander, contents string) error {
5758
if !updateNeeded(contents) {
5859
return nil
5960
}
6061

6162
// Nuke everything before populating things back again.
63+
os.Remove(oldHashFilePath)
6264
os.Remove(hashFilePath)
6365
os.Remove(caFilePath)
6466

65-
// Safe the file
67+
// Save the file
6668
err := os.WriteFile(caFilePath, []byte(contents), 0o644)
6769
if err != nil {
6870
return err
@@ -73,18 +75,18 @@ func safeCAFile(cmd commander, contents string) error {
7375
return err
7476
}
7577

76-
// Safe the new checksum
77-
hash := md5.New()
78+
// Save the new checksum
79+
hash := sha256.New()
7880
io.WriteString(hash, contents)
7981
os.WriteFile(hashFilePath, hash.Sum(nil), 0o644)
8082

8183
return nil
8284
}
8385

84-
// SafeCAFile creates a certificate file into the right location if it isn't
86+
// SaveCAFile creates a certificate file into the right location if it isn't
8587
// already there. This function will call `update-ca-certificates` whenever the
8688
// CA file has been updated.
87-
func SafeCAFile(contents string) error {
89+
func SaveCAFile(contents string) error {
8890
cmd := exec.Command("update-ca-certificates")
89-
return safeCAFile(cmd, contents)
91+
return saveCAFile(cmd, contents)
9092
}

internal/regionsrv/ca_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package regionsrv
1616

1717
import (
18-
"crypto/md5"
18+
"crypto/sha256"
1919
"errors"
2020
"fmt"
2121
"io"
@@ -41,7 +41,7 @@ func (t testCommand) Run() error {
4141

4242
// Run this before each test to get the fixtures path right.
4343
func beforeTest() {
44-
hashFilePath = fixturesPath("valid.md5")
44+
hashFilePath = fixturesPath("valid.sha256")
4545
caFilePath = fixturesPath("valid.pem")
4646
}
4747

@@ -85,14 +85,14 @@ func TestUpdateIsNeededCouldNotReadFile(t *testing.T) {
8585
}
8686
}
8787

88-
func TestSafeCAFileBadWrite(t *testing.T) {
88+
func TestSaveCAFileBadWrite(t *testing.T) {
8989
beforeTest()
9090

91-
hashFilePath = fixturesPath(fmt.Sprintf("file%v.md5", rand.Int()))
91+
hashFilePath = fixturesPath(fmt.Sprintf("file%v.sha256", rand.Int()))
9292
caFilePath = "/wubalubadubdub"
9393
cmd := testCommand{shouldFail: false}
9494

95-
err := safeCAFile(cmd, "valid")
95+
err := saveCAFile(cmd, "valid")
9696
os.Remove(hashFilePath)
9797
os.Remove(caFilePath)
9898

@@ -101,14 +101,14 @@ func TestSafeCAFileBadWrite(t *testing.T) {
101101
}
102102
}
103103

104-
func TestSafeCAFileBadCommand(t *testing.T) {
104+
func TestSaveCAFileBadCommand(t *testing.T) {
105105
beforeTest()
106106

107-
hashFilePath = fixturesPath(fmt.Sprintf("file%v.md5", rand.Int()))
107+
hashFilePath = fixturesPath(fmt.Sprintf("file%v.sha256", rand.Int()))
108108
caFilePath = fixturesPath(fmt.Sprintf("file%v.pem", rand.Int()))
109109
cmd := testCommand{shouldFail: true}
110110

111-
err := safeCAFile(cmd, "valid")
111+
err := saveCAFile(cmd, "valid")
112112
os.Remove(hashFilePath)
113113
os.Remove(caFilePath)
114114

@@ -121,13 +121,13 @@ func TestSafeCAFileBadCommand(t *testing.T) {
121121
}
122122
}
123123

124-
func TestSafeCAFileSuccess(t *testing.T) {
124+
func TestSaveCAFileSuccess(t *testing.T) {
125125
beforeTest()
126126

127-
hashFilePath = fixturesPath("tmp.md5")
127+
hashFilePath = fixturesPath("tmp.sha256")
128128
cmd := testCommand{shouldFail: false}
129129

130-
err := safeCAFile(cmd, "valid")
130+
err := saveCAFile(cmd, "valid")
131131
if err != nil {
132132
os.Remove(hashFilePath)
133133
t.Fatalf("Expected error to be nil: %v\n", err)
@@ -136,7 +136,7 @@ func TestSafeCAFileSuccess(t *testing.T) {
136136
b, _ := os.ReadFile(hashFilePath)
137137
os.Remove(hashFilePath)
138138

139-
hash := md5.New()
139+
hash := sha256.New()
140140
io.WriteString(hash, "valid")
141141
if string(b) != string(hash.Sum(nil)) {
142142
t.Fatal("Bad checksum")

internal/regionsrv/fixtures/valid.md5

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ìeO¬•™ö.yâpj¾ò=û|À…ª†ÛM†•ð·ѳ

internal/regionsrv/zypper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func PrintResponse(params map[string]string) error {
7575
return errors.New("no credentials given")
7676
}
7777

78-
// Safe the contents of the CA file if it doesn't exist already.
79-
if err = SafeCAFile(cfg.Ca); err != nil {
78+
// Save the contents of the CA file if it doesn't exist already.
79+
if err = SaveCAFile(cfg.Ca); err != nil {
8080
return err
8181
}
8282

0 commit comments

Comments
 (0)