2018-10-07 23:07:43 +00:00
|
|
|
package dhparam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-04 15:59:29 +00:00
|
|
|
"context"
|
2018-10-07 23:07:43 +00:00
|
|
|
"fmt"
|
2018-10-08 09:45:02 +00:00
|
|
|
"os"
|
2018-10-07 23:07:43 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-05-04 15:59:29 +00:00
|
|
|
"time"
|
2018-10-07 23:07:43 +00:00
|
|
|
)
|
|
|
|
|
2024-09-20 08:08:32 +00:00
|
|
|
//nolint:errcheck,gosec,revive
|
2018-10-08 10:27:44 +00:00
|
|
|
func opensslOutput(r GeneratorResult) {
|
|
|
|
switch r {
|
|
|
|
case GeneratorFoundPossiblePrime:
|
|
|
|
os.Stderr.WriteString(".")
|
|
|
|
case GeneratorFirstConfirmation:
|
|
|
|
os.Stderr.WriteString("+")
|
|
|
|
case GeneratorSafePrimeFound:
|
|
|
|
os.Stderr.WriteString("*\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 09:33:53 +00:00
|
|
|
func execGeneratorIntegration(t *testing.T, bitsize int, generator Generator) {
|
2018-10-08 10:27:44 +00:00
|
|
|
dh, err := Generate(bitsize, generator, opensslOutput)
|
2018-10-07 23:07:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to generate DH params: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pem, err := dh.ToPEM()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to generate PEM encoded version: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
2023-02-10 15:45:24 +00:00
|
|
|
f, err := os.CreateTemp("", "dhparam.*")
|
2018-10-08 09:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempfile: %s", err)
|
|
|
|
}
|
2024-09-20 08:08:32 +00:00
|
|
|
defer os.Remove(f.Name()) //nolint:errcheck
|
2018-10-08 09:45:02 +00:00
|
|
|
|
|
|
|
if _, err = f.Write(pem); err != nil {
|
|
|
|
t.Fatalf("Unable to write tempfile: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-09-20 08:08:32 +00:00
|
|
|
f.Close() //nolint:errcheck,gosec,revive
|
2018-10-08 09:45:02 +00:00
|
|
|
|
2024-09-20 08:08:32 +00:00
|
|
|
cmd := exec.Command("openssl", "dhparam", "-inform", "PEM", "-in", f.Name(), "-check", "-noout", "-text") //#nosec:G204 // Only for tests
|
2018-10-07 23:07:43 +00:00
|
|
|
cmd.Stdin = bytes.NewReader(pem)
|
|
|
|
cmd.Stdout = buf
|
|
|
|
cmd.Stderr = buf
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Errorf("Validation command was not successful: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := buf.String()
|
|
|
|
fullOutput := false
|
|
|
|
|
|
|
|
for _, expect := range []string{
|
|
|
|
fmt.Sprintf("DH Parameters: (%d bit)", bitsize),
|
|
|
|
"DH parameters appear to be ok.",
|
2023-02-10 15:50:23 +00:00
|
|
|
fmt.Sprintf("G: %d (0x%x)", generator, generator),
|
2018-10-07 23:07:43 +00:00
|
|
|
} {
|
|
|
|
if !strings.Contains(result, expect) {
|
|
|
|
t.Errorf("Did not find expected OpenSSL output: %q", expect)
|
|
|
|
fullOutput = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fullOutput {
|
|
|
|
t.Logf("Received OpenSSL output:\n%s", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerator512bit(t *testing.T) {
|
2018-10-08 09:33:53 +00:00
|
|
|
execGeneratorIntegration(t, 512, GeneratorTwo)
|
2018-10-07 23:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerator1024bit(t *testing.T) {
|
2018-10-08 09:33:53 +00:00
|
|
|
execGeneratorIntegration(t, 1024, GeneratorTwo)
|
2018-10-07 23:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerator2048bit(t *testing.T) {
|
2018-10-08 09:33:53 +00:00
|
|
|
execGeneratorIntegration(t, 2048, GeneratorTwo)
|
2018-10-07 23:07:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 15:59:29 +00:00
|
|
|
func TestGeneratorInterrupt(t *testing.T) {
|
|
|
|
start := time.Now()
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond)
|
|
|
|
dh, err := GenerateWithContext(ctx, 4096, GeneratorTwo, nil)
|
|
|
|
cancel()
|
|
|
|
duration := time.Since(start)
|
|
|
|
if duration > 1*time.Second {
|
|
|
|
t.Fatal("Function was not canceled early")
|
|
|
|
}
|
|
|
|
if err != context.DeadlineExceeded {
|
|
|
|
t.Fatal("Expected error to be context.DeadlineExceeded")
|
|
|
|
}
|
|
|
|
if dh != nil {
|
|
|
|
t.Fatal("Expected result to be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:07:43 +00:00
|
|
|
func TestGenerator5(t *testing.T) {
|
2018-10-08 09:33:53 +00:00
|
|
|
execGeneratorIntegration(t, 512, GeneratorFive)
|
2018-10-07 23:07:43 +00:00
|
|
|
}
|