|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto" |
| 7 | + "crypto/rsa" |
| 8 | + "crypto/x509" |
| 9 | + "encoding/hex" |
| 10 | + "encoding/pem" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + "log" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "strings" |
| 17 | + "sync" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "go.uber.org/zap" |
| 22 | +) |
| 23 | + |
| 24 | +func TestSoftHSMConcurrentSignRequests(t *testing.T) { |
| 25 | + hsmModule := os.Getenv("HSM_MODULE") |
| 26 | + if hsmModule == "" { |
| 27 | + t.Skip("HSM_MODULE environment variable is not set") |
| 28 | + } |
| 29 | + tokenLabel := os.Getenv("TOKEN_LABEL") |
| 30 | + if tokenLabel == "" { |
| 31 | + t.Skip("TOKEN_LABEL environment variable is not set") |
| 32 | + } |
| 33 | + keyLabel := os.Getenv("KEY_LABEL") |
| 34 | + if keyLabel == "" { |
| 35 | + t.Skip("KEY_LABEL environment variable is not set") |
| 36 | + } |
| 37 | + hsmPin := os.Getenv("HSM_PIN") |
| 38 | + if hsmPin == "" { |
| 39 | + t.Skip("HSM_PIN environment variable is not set") |
| 40 | + } |
| 41 | + |
| 42 | + const ( |
| 43 | + base = "http://localhost:8080" |
| 44 | + healthURL = base + "/healthz" |
| 45 | + url = base + "/sign/rsassa-pss?hashAlgorithm=sha256" |
| 46 | + bodyHex = "aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f" |
| 47 | + ) |
| 48 | + |
| 49 | + var ( |
| 50 | + headers = map[string]string{ |
| 51 | + "Content-Type": "text/plain", |
| 52 | + "Content-Encoding": "hex", |
| 53 | + "Accept": "application/x-pem-file", |
| 54 | + } |
| 55 | + ) |
| 56 | + |
| 57 | + l, err := zap.NewDevelopment() |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Failed to create logger: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + ctx, cancel := context.WithCancel(t.Context()) |
| 63 | + t.Cleanup(func() { |
| 64 | + cancel() |
| 65 | + }) |
| 66 | + |
| 67 | + go func() { |
| 68 | + if err := run(&Config{ |
| 69 | + HSMModule: strings.TrimSpace(hsmModule), |
| 70 | + HSMTokenLabel: tokenLabel, |
| 71 | + HSMSlot: -1, |
| 72 | + HSMKeyLabel: keyLabel, |
| 73 | + HSMPass: hsmPin, |
| 74 | + Port: "8080", |
| 75 | + DisableHTTPS: true, |
| 76 | + DisableAuth: true, |
| 77 | + MaxBodySizeBytes: 2048, |
| 78 | + Logger: l, |
| 79 | + RunServer: true, |
| 80 | + }); err != nil { |
| 81 | + cancel() |
| 82 | + t.Errorf("Failed to start signing server: %v", err) |
| 83 | + return |
| 84 | + } |
| 85 | + }() |
| 86 | + |
| 87 | + var wg sync.WaitGroup |
| 88 | + client := &http.Client{Timeout: 10 * time.Second} |
| 89 | + |
| 90 | +outer: |
| 91 | + for { |
| 92 | + select { |
| 93 | + case <-ctx.Done(): |
| 94 | + t.Fatal("Test context was cancelled before health check passed") |
| 95 | + default: |
| 96 | + log.Println("Waiting for signing server to be ready...") |
| 97 | + healthReq, err := http.NewRequest(http.MethodGet, healthURL, nil) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Health request creation failed: %v", err) |
| 100 | + } |
| 101 | + healthResp, _ := client.Do(healthReq) |
| 102 | + if healthResp != nil && healthResp.StatusCode == http.StatusOK { |
| 103 | + log.Println("Health check passed") |
| 104 | + break outer |
| 105 | + } |
| 106 | + time.Sleep(500 * time.Millisecond) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for i := 0; i < 10; i++ { |
| 111 | + wg.Add(1) |
| 112 | + |
| 113 | + go func(index int) { |
| 114 | + defer wg.Done() |
| 115 | + |
| 116 | + req, err := http.NewRequest(http.MethodPost, url, bytes.NewBufferString(bodyHex)) |
| 117 | + if err != nil { |
| 118 | + t.Errorf("Request %d creation failed: %v", index, err) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + for k, v := range headers { |
| 123 | + req.Header.Set(k, v) |
| 124 | + } |
| 125 | + |
| 126 | + resp, err := client.Do(req) |
| 127 | + if err != nil { |
| 128 | + t.Errorf("Request %d failed: %v", index, err) |
| 129 | + return |
| 130 | + } |
| 131 | + defer resp.Body.Close() |
| 132 | + |
| 133 | + body, err := io.ReadAll(resp.Body) |
| 134 | + if err != nil { |
| 135 | + t.Errorf("Request %d read body failed: %v", index, err) |
| 136 | + return |
| 137 | + } |
| 138 | + if resp.StatusCode != http.StatusOK { |
| 139 | + t.Errorf("Request %d failed with status %d: %s", index, resp.StatusCode, body) |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + block, _ := pem.Decode(body) |
| 144 | + if block == nil || block.Type != "SIGNATURE" { |
| 145 | + log.Fatal("Failed to parse PEM block") |
| 146 | + } |
| 147 | + |
| 148 | + hsmPublicKeyFile := os.Getenv("HSM_PUBLIC_KEY_FILE") |
| 149 | + if hsmPublicKeyFile == "" { |
| 150 | + log.Println("HSM_PUBLIC_KEY_FILE environment variable is not set, skipping public key verification") |
| 151 | + } else { |
| 152 | + hsmPublicKeyData, err := os.ReadFile(hsmPublicKeyFile) |
| 153 | + if err != nil { |
| 154 | + log.Fatalf("Failed to read public key file %s: %v", hsmPublicKeyFile, err) |
| 155 | + } |
| 156 | + publicKeyBlock, _ := pem.Decode(hsmPublicKeyData) |
| 157 | + if publicKeyBlock == nil || publicKeyBlock.Type != "PUBLIC KEY" { |
| 158 | + log.Fatal("Failed to parse PEM block for public key") |
| 159 | + } |
| 160 | + pub, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes) |
| 161 | + if err != nil { |
| 162 | + log.Fatalf("Failed to parse public key: %v", err) |
| 163 | + } |
| 164 | + rsaPubKey, ok := pub.(*rsa.PublicKey) |
| 165 | + if !ok { |
| 166 | + log.Fatal("Public key is not of type RSA") |
| 167 | + } |
| 168 | + rawDigest, err := hex.DecodeString(bodyHex) |
| 169 | + if err != nil { |
| 170 | + log.Fatalf("Failed to decode hex body: %v", err) |
| 171 | + } |
| 172 | + if err := rsa.VerifyPSS(rsaPubKey, crypto.SHA256, rawDigest, block.Bytes, nil); err != nil { |
| 173 | + log.Fatalf("Signature verification failed: %v", err) |
| 174 | + } else { |
| 175 | + log.Printf("Signature verification succeeded for request %d", index) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + fmt.Printf("Decoded signature: %x\n", block.Bytes) |
| 180 | + |
| 181 | + log.Printf("Response %d: %d - %.100q\n", index, resp.StatusCode, body) |
| 182 | + }(i) |
| 183 | + } |
| 184 | + |
| 185 | + wg.Wait() |
| 186 | +} |
0 commit comments