Skip to content

Instantly share code, notes, and snippets.

@tobowers tobowers/marshal.go
Created Aug 17, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.