2020-09-18 18:01:16 -04:00
|
|
|
/*
|
|
|
|
SSHSecure - a program to harden OpenSSH from defaults
|
|
|
|
Copyright (C) 2020 Brent Saner
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-09-18 04:04:39 -04:00
|
|
|
package moduli
|
2020-09-24 04:38:29 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
)
|
|
|
|
|
2020-09-27 03:23:58 -04:00
|
|
|
// getPregen gets the pregenerated moduli from upstream mirror.
|
|
|
|
func getPregen() (Moduli, error) {
|
|
|
|
m := Moduli{}
|
2020-09-24 04:38:29 -04:00
|
|
|
// get the pregenerated moduli
|
|
|
|
resp, err := http.Get(pregenURL)
|
|
|
|
if err != nil {
|
2020-09-27 03:23:58 -04:00
|
|
|
return m, err
|
2020-09-24 04:38:29 -04:00
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2020-09-27 03:23:58 -04:00
|
|
|
return m, errors.New(fmt.Sprintf("returned status code %v: %v", resp.StatusCode, resp.Status))
|
2020-09-24 04:38:29 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
b := make([]byte, resp.ContentLength)
|
|
|
|
if _, err = resp.Body.Read(b); err != nil {
|
2020-09-27 03:23:58 -04:00
|
|
|
return m, err
|
2020-09-24 04:38:29 -04:00
|
|
|
}
|
|
|
|
// and compare the SHA3-512 (NIST) checksum.
|
|
|
|
s := sha3.New512()
|
|
|
|
if _, err = s.Write(b); err != nil {
|
|
|
|
// TODO: return nil instead of b?
|
2020-09-27 03:23:58 -04:00
|
|
|
return m, err
|
2020-09-24 04:38:29 -04:00
|
|
|
}
|
|
|
|
goodCksum, err := hex.DecodeString(pregenCksum)
|
|
|
|
if err != nil {
|
2020-09-27 03:23:58 -04:00
|
|
|
return m, err
|
2020-09-24 04:38:29 -04:00
|
|
|
}
|
|
|
|
// We just compare the bytestrings.
|
|
|
|
if bytes.Compare(s.Sum(nil), goodCksum) != 0 {
|
2020-09-27 03:23:58 -04:00
|
|
|
return m, errors.New("checksums do not match")
|
2020-09-24 04:38:29 -04:00
|
|
|
}
|
2020-09-27 03:23:58 -04:00
|
|
|
if err := Unmarshal(b, m); err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
return m, nil
|
2020-09-24 04:38:29 -04:00
|
|
|
}
|