Debian Codegraph @

cmd/tlskeypair/main.go

raw
1 package main
2
3 import (
4 "bytes"
5 "crypto"
6 "crypto/ecdsa"
7 "crypto/elliptic"
8 "crypto/rsa"
9 "crypto/x509"
10 "encoding/pem"
11 "errors"
12 "flag"
13 "fmt"
14 "io/ioutil"
15 "log"
16 "os"
17
18 "github.com/kisom/goutils/die"
19 )
20
21 var validPEMs = map[string]bool{
22 "PRIVATE KEY": true,
23 "RSA PRIVATE KEY": true,
24 "EC PRIVATE KEY": true,
25 }
26
27 const (
28 curveInvalid = iota // any invalid curve
29 curveRSA // indicates key is an RSA key, not an EC key
30 curveP256
31 curveP384
32 curveP521
33 )
34
35 func getECCurve(pub interface{}) int {
36 switch pub := pub.(type) {
37 case *ecdsa.PublicKey:
38 switch pub.Curve {
39 case elliptic.P256():
40 return curveP256
41 case elliptic.P384():
42 return curveP384
43 case elliptic.P521():
44 return curveP521
45 default:
46 return curveInvalid
47 }
48 case *rsa.PublicKey:
49 return curveRSA
50 default:
51 return curveInvalid
52 }
53 }
54
55 func loadKey(path string) (crypto.Signer, error) {
56 in, err := ioutil.ReadFile(path)
57 if err != nil {
58 return nil, err
59 }
60
61 in = bytes.TrimSpace(in)
62 p, _ := pem.Decode(in)
63 if p != nil {
64 if !validPEMs[p.Type] {
65 return nil, errors.New("invalid private key file type " + p.Type)
66 }
67 in = p.Bytes
68 }
69
70 priv, err := x509.ParsePKCS8PrivateKey(in)
71 if err != nil {
72 priv, err = x509.ParsePKCS1PrivateKey(in)
73 if err != nil {
74 priv, err = x509.ParseECPrivateKey(in)
75 if err != nil {
76 return nil, err
77 }
78 }
79 }
80
81 switch priv.(type) {
82 case *rsa.PrivateKey:
83 return priv.(*rsa.PrivateKey), nil
84 case *ecdsa.PrivateKey:
85 return priv.(*ecdsa.PrivateKey), nil
86 }
87
88 // should never reach here
89 return nil, errors.New("invalid private key")
90
91 }
92
93 func main() {
94 var keyFile, certFile string
95 flag.StringVar(&keyFile, "k", "", "TLS private `key` file")
96 flag.StringVar(&certFile, "c", "", "TLS `certificate` file")
97 flag.Parse()
98
99 in, err := ioutil.ReadFile(certFile)
100 die.If(err)
101
102 p, _ := pem.Decode(in)
103 if p != nil {
104 if p.Type != "CERTIFICATE" {
105 die.With("invalid certificate (type is %s)", p.Type)
106 }
107 in = p.Bytes
108 }
109 cert, err := x509.ParseCertificate(in)
110 die.If(err)
111
112 priv, err := loadKey(keyFile)
113 die.If(err)
114
115 switch pub := priv.Public().(type) {
116 case *rsa.PublicKey:
117 switch certPub := cert.PublicKey.(type) {
118 case *rsa.PublicKey:
119 if pub.N.Cmp(certPub.N) != 0 || pub.E != certPub.E {
120 fmt.Println("No match (public keys don't match).")
121 os.Exit(1)
122 }
123 fmt.Println("Match.")
124 return
125 case *ecdsa.PublicKey:
126 fmt.Println("No match (RSA private key, EC public key).")
127 os.Exit(1)
128 }
129 case *ecdsa.PublicKey:
130 privCurve := getECCurve(pub)
131 certCurve := getECCurve(cert.PublicKey)
132 log.Printf("priv: %d\tcert: %d\n", privCurve, certCurve)
133
134 if certCurve == curveRSA {
135 fmt.Println("No match (private key is EC, certificate is RSA).")
136 os.Exit(1)
137 } else if privCurve == curveInvalid {
138 fmt.Println("No match (invalid private key curve).")
139 os.Exit(1)
140 } else if privCurve != certCurve {
141 fmt.Println("No match (EC curves don't match).")
142 os.Exit(1)
143 }
144
145 certPub := cert.PublicKey.(*ecdsa.PublicKey)
146 if pub.X.Cmp(certPub.X) != 0 {
147 fmt.Println("No match (public keys don't match).")
148 os.Exit(1)
149 }
150
151 if pub.Y.Cmp(certPub.Y) != 0 {
152 fmt.Println("No match (public keys don't match).")
153 os.Exit(1)
154 }
155
156 fmt.Println("Match.")
157 default:
158 fmt.Printf("Unrecognised private key type: %T\n", priv.Public())
159 os.Exit(1)
160 }
161 }