|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/x509" |
| 7 | + "encoding/base64" |
| 8 | + "encoding/pem" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "errors" |
| 12 | + |
| 13 | + "golang.org/x/crypto/ssh" |
| 14 | +) |
| 15 | + |
| 16 | +// generateRSAKeys generates an RSA public/private key pair |
| 17 | +// and returns them as PEM encoded strings. |
| 18 | +func generateRSAKeys() (string, string, error) { |
| 19 | + // Generate the private key |
| 20 | + privateKey, err := rsa.GenerateKey(rand.Reader, 2048*2) |
| 21 | + if err != nil { |
| 22 | + return "", "", err |
| 23 | + } |
| 24 | + |
| 25 | + // Marshal the private key to DER format |
| 26 | + privateKeyDER := x509.MarshalPKCS1PrivateKey(privateKey) |
| 27 | + |
| 28 | + // PEM encode the private key |
| 29 | + privateKeyBlock := &pem.Block{ |
| 30 | + Type: "RSA PRIVATE KEY", |
| 31 | + Bytes: privateKeyDER, |
| 32 | + } |
| 33 | + privateKeyPEM := string(pem.EncodeToMemory(privateKeyBlock)) |
| 34 | + |
| 35 | + // Generate the public key |
| 36 | + publicKey := &privateKey.PublicKey |
| 37 | + |
| 38 | + // Marshal the public key to DER format |
| 39 | + publicKeyDER, err := x509.MarshalPKIXPublicKey(publicKey) |
| 40 | + if err != nil { |
| 41 | + return "", "", err |
| 42 | + } |
| 43 | + |
| 44 | + // PEM encode the public key |
| 45 | + publicKeyBlock := &pem.Block{ |
| 46 | + Type: "PUBLIC KEY", |
| 47 | + Bytes: publicKeyDER, |
| 48 | + } |
| 49 | + publicKeyPEM := string(pem.EncodeToMemory(publicKeyBlock)) |
| 50 | + publicKeyOpenSSH, err := PublicPEMtoOpenSSH([]byte(publicKeyPEM)) |
| 51 | + if err != nil { |
| 52 | + return "", "", err |
| 53 | + } |
| 54 | + |
| 55 | + return publicKeyOpenSSH, privateKeyPEM, nil |
| 56 | +} |
| 57 | + |
| 58 | +// Converts PEM public key to OpenSSH format to be used in authorized_keys file |
| 59 | +// Similar to: "ssh-keygen", "-i", "-m", "pkcs8", "-f", auth_keys_new_path |
| 60 | +func PublicPEMtoOpenSSH(pemBytes []byte) (string, error) { |
| 61 | + // Decode and get the first block in the PEM file. |
| 62 | + // In our case it should be the Public key block. |
| 63 | + pemBlock, rest := pem.Decode(pemBytes) |
| 64 | + if pemBlock == nil { |
| 65 | + return "", errors.New("invalid PEM public key passed, pem.Decode() did not find a public key") |
| 66 | + } |
| 67 | + if len(rest) > 0 { |
| 68 | + return "", errors.New("PEM block contains more than just public key") |
| 69 | + } |
| 70 | + |
| 71 | + // Confirm we got the PUBLIC KEY block type |
| 72 | + if pemBlock.Type != "PUBLIC KEY" { |
| 73 | + return "", fmt.Errorf("ssh: unsupported key type %q", pemBlock.Type) |
| 74 | + } |
| 75 | + |
| 76 | + // Convert to rsa |
| 77 | + rsaPubKey, err := x509.ParsePKIXPublicKey(pemBlock.Bytes) |
| 78 | + if err != nil { |
| 79 | + return "", fmt.Errorf("x509.parse pki public key: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + // Confirm we got an rsa public key. Returned value is an interface{} |
| 83 | + sshKey, ok := rsaPubKey.(*rsa.PublicKey) |
| 84 | + if !ok { |
| 85 | + return "", fmt.Errorf("invalid PEM passed in from user: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + // Generate the ssh public key |
| 89 | + pub, err := ssh.NewPublicKey(sshKey) |
| 90 | + if err != nil { |
| 91 | + return "", fmt.Errorf("new ssh public key from rsa: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + // Encode to store to file |
| 95 | + sshPubKey := base64.StdEncoding.EncodeToString(pub.Marshal()) |
| 96 | + |
| 97 | + return "ssh-rsa " + sshPubKey, nil |
| 98 | +} |
0 commit comments