(key, passphrase string)
| 413 | } |
| 414 | |
| 415 | func decodeRSAPrivateKey(key, passphrase string) (*rsa.PrivateKey, error) { |
| 416 | block, _ := pem.Decode([]byte(key)) |
| 417 | if block == nil { |
| 418 | return nil, errors.Errorf("failed to get private key PEM block from key") |
| 419 | } |
| 420 | switch block.Type { |
| 421 | case "PRIVATE KEY": |
| 422 | privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) |
| 423 | if err != nil { |
| 424 | return nil, errors.Wrapf(err, "failed to parse pkcs8 private key") |
| 425 | } |
| 426 | rsaKey, ok := privateKey.(*rsa.PrivateKey) |
| 427 | if !ok { |
| 428 | return nil, errors.Errorf("expected RSA private key, got %T", privateKey) |
| 429 | } |
| 430 | return rsaKey, nil |
| 431 | case "ENCRYPTED PRIVATE KEY": |
| 432 | // NOTE: As of Jun 2024, Golang's official library does not support passcode-encrypted PKCS8 private key. And |
| 433 | // Snowflake do not introduce an external library to achieve this due to the security purposes. |
| 434 | // We introduce https://pkg.go.dev/github.com/youmark/pkcs8 to help us to achieve this goal. |
| 435 | pk, err := pkcs8.ParsePKCS8PrivateKeyRSA(block.Bytes, []byte(passphrase)) |
| 436 | if err != nil { |
| 437 | return nil, errors.Wrapf(err, "failed to parse pkcs8 private key to rsa private key with passphrase") |
| 438 | } |
| 439 | return pk, nil |
| 440 | default: |
| 441 | return nil, errors.Errorf("unsupported pem block type: %s", block.Type) |
| 442 | } |
| 443 | } |
no test coverage detected