helper.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package cert
  2. import (
  3. "crypto/ecdsa"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "encoding/pem"
  7. "os"
  8. )
  9. func IsCertificate(pemStr string) bool {
  10. block, _ := pem.Decode([]byte(pemStr))
  11. if block == nil {
  12. return false
  13. }
  14. _, err := x509.ParseCertificate(block.Bytes)
  15. return err == nil
  16. }
  17. func IsPrivateKey(pemStr string) bool {
  18. block, _ := pem.Decode([]byte(pemStr))
  19. if block == nil {
  20. return false
  21. }
  22. _, errRSA := x509.ParsePKCS1PrivateKey(block.Bytes)
  23. if errRSA == nil {
  24. return true
  25. }
  26. _, errECDSA := x509.ParseECPrivateKey(block.Bytes)
  27. if errECDSA == nil {
  28. return true
  29. }
  30. _, errPKC := x509.ParsePKCS8PrivateKey(block.Bytes)
  31. return errPKC == nil
  32. }
  33. // IsCertificatePath checks if the file at the given path is a certificate or not exists.
  34. func IsCertificatePath(path string) bool {
  35. if path == "" {
  36. return false
  37. }
  38. _, err := os.Stat(path)
  39. if err != nil {
  40. if os.IsNotExist(err) {
  41. return true
  42. }
  43. return false
  44. }
  45. bytes, err := os.ReadFile(path)
  46. if err != nil {
  47. return false
  48. }
  49. return IsCertificate(string(bytes))
  50. }
  51. // IsPrivateKeyPath checks if the file at the given path is a private key or not exists.
  52. func IsPrivateKeyPath(path string) bool {
  53. if path == "" {
  54. return false
  55. }
  56. _, err := os.Stat(path)
  57. if err != nil {
  58. if os.IsNotExist(err) {
  59. return true
  60. }
  61. return false
  62. }
  63. bytes, err := os.ReadFile(path)
  64. if err != nil {
  65. return false
  66. }
  67. return IsPrivateKey(string(bytes))
  68. }
  69. // GetKeyType determines the key type from a PEM certificate string.
  70. // Returns "2048", "3072", "4096", "P256", "P384" or empty string.
  71. func GetKeyType(pemStr string) (string, error) {
  72. block, _ := pem.Decode([]byte(pemStr))
  73. if block == nil {
  74. return "", ErrCertDecode
  75. }
  76. cert, err := x509.ParseCertificate(block.Bytes)
  77. if err != nil {
  78. return "", ErrCertParse
  79. }
  80. switch cert.PublicKeyAlgorithm {
  81. case x509.RSA:
  82. rsaKey, ok := cert.PublicKey.(*rsa.PublicKey)
  83. if !ok {
  84. return "", nil
  85. }
  86. keySize := rsaKey.Size() * 8 // Size returns size in bytes, convert to bits
  87. switch keySize {
  88. case 2048:
  89. return "2048", nil
  90. case 3072:
  91. return "3072", nil
  92. case 4096:
  93. return "4096", nil
  94. default:
  95. return "", nil
  96. }
  97. case x509.ECDSA:
  98. ecKey, ok := cert.PublicKey.(*ecdsa.PublicKey)
  99. if !ok {
  100. return "", nil
  101. }
  102. curve := ecKey.Curve.Params().Name
  103. switch curve {
  104. case "P-256":
  105. return "P256", nil
  106. case "P-384":
  107. return "P384", nil
  108. default:
  109. return "", nil
  110. }
  111. default:
  112. return "", nil
  113. }
  114. }
  115. // GetKeyTypeFromPath determines the key type from a certificate file.
  116. // Returns "2048", "3072", "4096", "P256", "P384" or empty string.
  117. func GetKeyTypeFromPath(path string) (string, error) {
  118. if path == "" {
  119. return "", ErrCertPathIsEmpty
  120. }
  121. bytes, err := os.ReadFile(path)
  122. if err != nil {
  123. return "", err
  124. }
  125. return GetKeyType(string(bytes))
  126. }