Skip to content

Instantly share code, notes, and snippets.

@tobowers
Created August 17, 2017 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobowers/8118cd1a668926c40909639a2b2af195 to your computer and use it in GitHub Desktop.
Save tobowers/8118cd1a668926c40909639a2b2af195 to your computer and use it in GitHub Desktop.
golang errors when just marshaling and then unmarshaling
package main
import (
"log"
"crypto/rand"
"crypto/rsa"
"golang.org/x/crypto/ssh"
)
func main() {
certPrivateKey, err := rsa.GenerateKey(rand.Reader, 512)
if err != nil {
log.Fatalf("Error generating cert key: %v", err)
}
certPublicKey, err := ssh.NewPublicKey(&certPrivateKey.PublicKey)
if err != nil {
log.Fatalf("Error generating ssh public key: %v", err)
}
cert := &ssh.Certificate{
Key: certPublicKey,
CertType: 1,
KeyId: "{requester: \"bob\"}",
ValidPrincipals: []string{"alice"},
}
signer, err := ssh.NewSignerFromKey(certPrivateKey)
if err != nil {
log.Fatalf("Error creating signer: %v", err)
}
err = cert.SignCert(rand.Reader, signer)
if err != nil {
log.Fatalf("Error signing certificate: %v", err)
}
marshaled := cert.Marshal()
parsedCert := &ssh.Certificate{}
err = ssh.Unmarshal(marshaled, parsedCert)
if err != nil {
log.Fatalf("error unmaarshaling: %v", err)
}
if err != nil {
log.Fatalf("error parsing cert: %v", err)
}
if parsedCert.CertType != cert.CertType {
log.Fatalf("Error, parsed certType did not match %s", cert.CertType)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment