| 28 | } |
| 29 | |
| 30 | func readPrivateKeyWithContent(ctx context.Context, path string, privateKeyContent []byte) (ssh.Signer, error) { |
| 31 | // We parse the private key on our own first so that we can |
| 32 | // show a nicer error if the private key has a password. |
| 33 | block, _ := pem.Decode(privateKeyContent) |
| 34 | if block == nil { |
| 35 | return nil, fmt.Errorf("Failed to read key '%s': is not in the PEM format", path) |
| 36 | } |
| 37 | |
| 38 | privateKey := &PrivateKey{Block: block, Path: path} |
| 39 | |
| 40 | signer, err := privateKey.signer(ctx) |
| 41 | if err != nil { |
| 42 | if err, ok := err.(asn1.StructuralError); ok && strings.HasPrefix(err.Msg, "tags don't match") || err.Msg == "length too large" { |
| 43 | return nil, errors.Newf(ctx, "Fail to decrypt SSH key, invalid password.") |
| 44 | } |
| 45 | if err, ok := err.(asn1.SyntaxError); ok && err.Msg == "trailing data" { |
| 46 | return nil, errors.Newf(ctx, "The password was OK, but something went wrong.\n"+ |
| 47 | "Please re-run the command with the environment variable DEBUG=1 "+ |
| 48 | "and create an issue with the command output: https://github.com/Scalingo/cli/issues") |
| 49 | } |
| 50 | return nil, errors.Newf(ctx, "Invalid SSH key or password: %v", err) |
| 51 | } |
| 52 | |
| 53 | return signer, nil |
| 54 | } |