|
1 | 1 | package tls
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "crypto/rsa" |
4 | 5 | "crypto/tls"
|
5 | 6 | "crypto/x509"
|
6 | 7 | "encoding/pem"
|
7 | 8 | "errors"
|
8 | 9 | "fmt"
|
| 10 | + "math/big" |
9 | 11 | "os"
|
10 | 12 | "strings"
|
11 | 13 | "testing"
|
@@ -452,3 +454,74 @@ func TestLoadX509CertPool(t *testing.T) {
|
452 | 454 | require.Nil(t, p)
|
453 | 455 | })
|
454 | 456 | }
|
| 457 | + |
| 458 | +func TestEncodeX509KeyPair_InvalidRSAKey(t *testing.T) { |
| 459 | + t.Run("Nil RSA private key", func(t *testing.T) { |
| 460 | + cert := tls.Certificate{ |
| 461 | + Certificate: [][]byte{{0x30, 0x82}}, // minimal DER certificate bytes |
| 462 | + PrivateKey: (*rsa.PrivateKey)(nil), |
| 463 | + } |
| 464 | + certPEM, keyPEM := EncodeX509KeyPair(cert) |
| 465 | + assert.NotEmpty(t, certPEM) |
| 466 | + assert.Empty(t, keyPEM) |
| 467 | + }) |
| 468 | + |
| 469 | + t.Run("RSA private key that fails validation", func(t *testing.T) { |
| 470 | + // Create an RSA key with invalid parameters that will fail Validate() |
| 471 | + invalidKey := &rsa.PrivateKey{ |
| 472 | + PublicKey: rsa.PublicKey{ |
| 473 | + N: big.NewInt(1), // Too small modulus, will fail validation |
| 474 | + E: 65537, |
| 475 | + }, |
| 476 | + D: big.NewInt(1), // Invalid private exponent |
| 477 | + } |
| 478 | + cert := tls.Certificate{ |
| 479 | + Certificate: [][]byte{{0x30, 0x82}}, // minimal DER certificate bytes |
| 480 | + PrivateKey: invalidKey, |
| 481 | + } |
| 482 | + certPEM, keyPEM := EncodeX509KeyPair(cert) |
| 483 | + assert.NotEmpty(t, certPEM) |
| 484 | + assert.Empty(t, keyPEM) |
| 485 | + }) |
| 486 | + |
| 487 | + t.Run("RSA private key with inconsistent parameters", func(t *testing.T) { |
| 488 | + invalidKey := &rsa.PrivateKey{ |
| 489 | + PublicKey: rsa.PublicKey{ |
| 490 | + N: big.NewInt(35), |
| 491 | + E: 65537, |
| 492 | + }, |
| 493 | + D: big.NewInt(99999), |
| 494 | + } |
| 495 | + cert := tls.Certificate{ |
| 496 | + Certificate: [][]byte{{0x30, 0x82}}, // minimal DER certificate bytes |
| 497 | + PrivateKey: invalidKey, |
| 498 | + } |
| 499 | + certPEM, keyPEM := EncodeX509KeyPair(cert) |
| 500 | + assert.NotEmpty(t, certPEM) |
| 501 | + assert.Empty(t, keyPEM) |
| 502 | + }) |
| 503 | + |
| 504 | + t.Run("Unsupported private key type", func(t *testing.T) { |
| 505 | + // Use a type that's not *rsa.PrivateKey or *ecdsa.PrivateKey |
| 506 | + cert := tls.Certificate{ |
| 507 | + Certificate: [][]byte{{0x30, 0x82}}, // minimal DER certificate bytes |
| 508 | + PrivateKey: "not a private key", // Unsupported type |
| 509 | + } |
| 510 | + certPEM, keyPEM := EncodeX509KeyPair(cert) |
| 511 | + assert.NotEmpty(t, certPEM) |
| 512 | + assert.Empty(t, keyPEM) |
| 513 | + }) |
| 514 | + |
| 515 | + t.Run("Valid RSA private key should work", func(t *testing.T) { |
| 516 | + // Generate a valid RSA key for testing |
| 517 | + opts := CertOptions{Hosts: []string{"localhost"}, Organization: "Test"} |
| 518 | + validCert, err := GenerateX509KeyPair(opts) |
| 519 | + require.NoError(t, err) |
| 520 | + |
| 521 | + certPEM, keyPEM := EncodeX509KeyPair(*validCert) |
| 522 | + assert.NotEmpty(t, certPEM) |
| 523 | + assert.NotEmpty(t, keyPEM) |
| 524 | + assert.Contains(t, string(keyPEM), "-----BEGIN RSA PRIVATE KEY-----") |
| 525 | + assert.Contains(t, string(keyPEM), "-----END RSA PRIVATE KEY-----") |
| 526 | + }) |
| 527 | +} |
0 commit comments