| 42 | } |
| 43 | |
| 44 | func generateRSAKey(rsaKeySize int) (PrivateAndPublicKeys, error) { |
| 45 | privateKey, err := rsa.GenerateKey(rand.Reader, rsaKeySize) |
| 46 | if err != nil { |
| 47 | return PrivateAndPublicKeys{}, errors.Wrap(err, "unable to generate RSA private key") |
| 48 | } |
| 49 | |
| 50 | if err = privateKey.Validate(); err != nil { |
| 51 | return PrivateAndPublicKeys{}, err |
| 52 | } |
| 53 | |
| 54 | block := &pem.Block{ |
| 55 | Type: rsaPEMBlockType, |
| 56 | Bytes: x509.MarshalPKCS1PrivateKey(privateKey), |
| 57 | } |
| 58 | |
| 59 | publicKey, err := sshlib.NewPublicKey(&privateKey.PublicKey) |
| 60 | if err != nil { |
| 61 | return PrivateAndPublicKeys{}, errors.Wrap(err, "unable to generate RSA public key") |
| 62 | } |
| 63 | |
| 64 | return PrivateAndPublicKeys{ |
| 65 | PrivateKey: pem.EncodeToMemory(block), |
| 66 | PublicKey: sshlib.MarshalAuthorizedKey(publicKey), |
| 67 | }, nil |
| 68 | } |
| 69 | |
| 70 | func generateECDSAKey() (PrivateAndPublicKeys, error) { |
| 71 | // Curve size currently not configurable, using the NIST recommendation. |