Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions internal/websecure/ed25519_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package websecure

import (
"os"
"testing"
)

var (
fixtureEd25519Certificate = `-----BEGIN CERTIFICATE-----
MIIBQDCB86ADAgECAhQdB4qB6dV0/u1lwhJofQgkmjjV1zAFBgMrZXAwLzELMAkG
A1UEBhMCREUxIDAeBgNVBAMMF2VkMjU1MTktdGVzdC5qZXRrdm0uY29tMB4XDTI1
MDUyMzEyNTkyN1oXDTI3MDQyMzEyNTkyN1owLzELMAkGA1UEBhMCREUxIDAeBgNV
BAMMF2VkMjU1MTktdGVzdC5qZXRrdm0uY29tMCowBQYDK2VwAyEA9tLyoulJn7Ev
bf8kuD1ZGdA092773pCRjFEDKpXHonyjITAfMB0GA1UdDgQWBBRkmrVMfsLY57iy
r/0POP0S4QxCADAFBgMrZXADQQBfTRvqavLHDYQiKQTgbGod+Yn+fIq2lE584+1U
C4wh9peIJDFocLBEAYTQpEMKxa4s0AIRxD+a7aCS5oz0e/0I
-----END CERTIFICATE-----`

fixtureEd25519PrivateKey = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIKV08xUsLRHBfMXqZwxVRzIbViOp8G7aQGjPvoRFjujB
-----END PRIVATE KEY-----`

certStore *CertStore
certSigner *SelfSigner
)

func TestMain(m *testing.M) {
tlsStorePath, err := os.MkdirTemp("", "jktls.*")
if err != nil {
defaultLogger.Fatal().Err(err).Msg("failed to create temp directory")
}

certStore = NewCertStore(tlsStorePath, nil)
certStore.LoadCertificates()

certSigner = NewSelfSigner(
certStore,
nil,
"ci.jetkvm.com",
"JetKVM",
"JetKVM",
"JetKVM",
)

m.Run()

os.RemoveAll(tlsStorePath)
}

func TestSaveEd25519Certificate(t *testing.T) {
err, _ := certStore.ValidateAndSaveCertificate("ed25519-test.jetkvm.com", fixtureEd25519Certificate, fixtureEd25519PrivateKey, true)
if err != nil {
t.Fatalf("failed to save certificate: %v", err)
}
}
7 changes: 6 additions & 1 deletion internal/websecure/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package websecure

import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
Expand Down Expand Up @@ -37,11 +38,15 @@ func keyToFile(cert *tls.Certificate, filename string) error {
if e != nil {
return fmt.Errorf("failed to marshal EC private key: %v", e)
}

keyBlock = pem.Block{
Type: "EC PRIVATE KEY",
Bytes: b,
}
case ed25519.PrivateKey:
keyBlock = pem.Block{
Type: "ED25519 PRIVATE KEY",
Bytes: k,
Comment on lines +46 to +48
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider marshaling the ED25519 key using a standard encoding function such as x509.MarshalPKCS8PrivateKey for consistency with other key types and to ensure proper PEM formatting.

Suggested change
keyBlock = pem.Block{
Type: "ED25519 PRIVATE KEY",
Bytes: k,
b, e := x509.MarshalPKCS8PrivateKey(k)
if e != nil {
return fmt.Errorf("failed to marshal ED25519 private key: %v", e)
}
keyBlock = pem.Block{
Type: "PRIVATE KEY",
Bytes: b,

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@IDisposable IDisposable May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot you bungled the `Type: "PRIVATE KEY", losing the information that it is a ED25519 key.

}
default:
return fmt.Errorf("unknown private key type: %T", k)
}
Expand Down
Loading