SSHKeygenParsePublicKey extracts key type and length using ssh-keygen.
(key, keyTestPath, keygenPath string)
| 190 | |
| 191 | // SSHKeygenParsePublicKey extracts key type and length using ssh-keygen. |
| 192 | func SSHKeygenParsePublicKey(key, keyTestPath, keygenPath string) (string, int, error) { |
| 193 | tmpName, err := writeTmpKeyFile(key, keyTestPath) |
| 194 | if err != nil { |
| 195 | return "", 0, errors.Newf("writeTmpKeyFile: %v", err) |
| 196 | } |
| 197 | defer os.Remove(tmpName) |
| 198 | |
| 199 | stdout, stderr, err := process.Exec("SSHKeygenParsePublicKey", keygenPath, "-lf", tmpName) |
| 200 | if err != nil { |
| 201 | return "", 0, errors.Newf("fail to parse public key: %s - %s", err, stderr) |
| 202 | } |
| 203 | if strings.Contains(stdout, "is not a public key file") { |
| 204 | return "", 0, ErrKeyUnableVerify{stdout} |
| 205 | } |
| 206 | |
| 207 | fields := strings.Split(stdout, " ") |
| 208 | if len(fields) < 4 { |
| 209 | return "", 0, errors.Newf("invalid public key line: %s", stdout) |
| 210 | } |
| 211 | |
| 212 | keyType := strings.Trim(fields[len(fields)-1], "()\r\n") |
| 213 | return strings.ToLower(keyType), com.StrTo(fields[0]).MustInt(), nil |
| 214 | } |
| 215 | |
| 216 | // SSHNativeParsePublicKey extracts the key type and length using the golang SSH library. |
| 217 | func SSHNativeParsePublicKey(keyLine string) (string, int, error) { |