Skip to content

Instantly share code, notes, and snippets.

@nsheridan
Created June 3, 2016 19:52
Show Gist options
  • Save nsheridan/e67bd4d26860b6db46f9d8caa0fbaad9 to your computer and use it in GitHub Desktop.
Save nsheridan/e67bd4d26860b6db46f9d8caa0fbaad9 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"fmt"
"net"
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
func main() {
// rsa key + cert
pr, _ := rsa.GenerateKey(rand.Reader, 2048)
r, _ := ssh.NewPublicKey(&pr.PublicKey)
cr := &ssh.Certificate{
Key: r,
CertType: ssh.UserCert,
ValidBefore: ssh.CertTimeInfinity,
}
signer, _ := ssh.NewSignerFromKey(pr)
cr.SignCert(rand.Reader, signer)
// ecdsa key + cert
pe, _ := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
e, _ := ssh.NewPublicKey(&pe.PublicKey)
ce := &ssh.Certificate{
Key: e,
CertType: ssh.UserCert,
ValidBefore: ssh.CertTimeInfinity,
}
ce.SignCert(rand.Reader, signer)
soc, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
fmt.Printf("Error connecting to agent: %v\n", err)
os.Exit(1)
}
local := agent.NewClient(soc)
local.Add(agent.AddedKey{
Certificate: cr,
PrivateKey: pr,
LifetimeSecs: 1,
})
local.Add(agent.AddedKey{
Certificate: ce,
PrivateKey: pe,
LifetimeSecs: 1,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment