Skip to content

Instantly share code, notes, and snippets.

@tobowers
Created August 17, 2017 08:17
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/7df99bac5fb2cb30bb06e4ce4d540364 to your computer and use it in GitHub Desktop.
Save tobowers/7df99bac5fb2cb30bb06e4ce4d540364 to your computer and use it in GitHub Desktop.
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()
pubKey, err := ssh.ParsePublicKey(marshaled)
if err != nil {
log.Fatalf("error parsing public key")
}
parsedCert, ok := pubKey.(*ssh.Certificate)
if !ok {
log.Fatal("agent: bad RSA certificate")
}
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