Skip to content

Commit

Permalink
Remove hardcoded algo name for certificate string (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Holmes <jholmes@cloudflare.com>
  • Loading branch information
Justin-Holmes and Justin Holmes authored Jan 20, 2022
1 parent 623ac9f commit dff6165
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
7 changes: 6 additions & 1 deletion sshcert.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ func (c *CA) SetName(name string) {
// String will output the SSH certificate in a format that can be used
// with an ssh client.
func (c *Cert) String() string {
return fmt.Sprintf("%s %s", ssh.CertAlgoECDSA256v01, base64.StdEncoding.EncodeToString(c.Certificate.Marshal()))
return fmt.Sprintf("%s %s", c.Type(), base64.StdEncoding.EncodeToString(c.Certificate.Marshal()))
}

// Type returns the certificate's algorithm name.
func (c *Cert) Type() string {
return c.Certificate.Type()
}

// NewSigningArguments will create a default SigningArguments type with the
Expand Down
27 changes: 20 additions & 7 deletions sshcert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,27 @@ func ExampleParsePublicKey() {
}

func TestSignCert(t *testing.T) {
ca, _ := NewCA()
pubBytes, _ := ioutil.ReadFile("testfiles/testkeys.pub")
pub, _ := ParsePublicKey(string(pubBytes))
signArgs := NewSigningArguments([]string{"root"})
tests := []struct {
algo string
fileName string
}{
{algo: "ecdsa-sha2-nistp256-cert-v01@openssh.com", fileName: "testkeys.pub"},
{algo: "ssh-ed25519-cert-v01@openssh.com", fileName: "ed25519_test_key.pub"},
}

_, err := ca.SignCert(pub, signArgs)
if err != nil {
t.Fatalf("Could not sign cert: %s", err)
for _, tc := range tests {
ca, _ := NewCA()
pubBytes, _ := ioutil.ReadFile(fmt.Sprintf("testfiles/%s", tc.fileName))
pub, _ := ParsePublicKey(string(pubBytes))
signArgs := NewSigningArguments([]string{"root"})

c, err := ca.SignCert(pub, signArgs)
if err != nil {
t.Fatalf("Could not sign cert: %s", err)
}
if c.Type() != tc.algo {
t.Fatalf("Certificate and public key type do not match: %s != %s", c.Type(), tc.algo)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions testfiles/ed25519_test_key.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAf48cHMj3/Vlbq69mHh4vSyRyBnwjWkYlH4BBWegNIa

0 comments on commit dff6165

Please sign in to comment.