| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ecdsa" |
| 5 |
"crypto/rsa" |
| 6 |
"crypto/x509" |
| 7 |
"encoding/pem" |
| 8 |
"flag" |
| 9 |
"fmt" |
| 10 |
"io/ioutil" |
| 11 |
"log" |
| 12 |
|
| 13 |
"github.com/kisom/goutils/die" |
| 14 |
) |
| 15 |
|
| 16 |
func main() { |
| 17 |
flag.Parse() |
| 18 |
|
| 19 |
for _, fileName := range flag.Args() { |
| 20 |
in, err := ioutil.ReadFile(fileName) |
| 21 |
die.If(err) |
| 22 |
|
| 23 |
if p, _ := pem.Decode(in); p != nil { |
| 24 |
if p.Type != "CERTIFICATE REQUEST" { |
| 25 |
log.Fatal("INVALID FILE TYPE") |
| 26 |
} |
| 27 |
in = p.Bytes |
| 28 |
} |
| 29 |
|
| 30 |
csr, err := x509.ParseCertificateRequest(in) |
| 31 |
die.If(err) |
| 32 |
|
| 33 |
out, err := x509.MarshalPKIXPublicKey(csr.PublicKey) |
| 34 |
die.If(err) |
| 35 |
|
| 36 |
var t string |
| 37 |
switch pub := csr.PublicKey.(type) { |
| 38 |
case *rsa.PublicKey: |
| 39 |
t = "RSA PUBLIC KEY" |
| 40 |
case *ecdsa.PublicKey: |
| 41 |
t = "EC PUBLIC KEY" |
| 42 |
default: |
| 43 |
die.With("unrecognised public key type %T", pub) |
| 44 |
} |
| 45 |
|
| 46 |
p := &pem.Block{ |
| 47 |
Type: t, |
| 48 |
Bytes: out, |
| 49 |
} |
| 50 |
|
| 51 |
err = ioutil.WriteFile(fileName+".pub", pem.EncodeToMemory(p), 0644) |
| 52 |
die.If(err) |
| 53 |
fmt.Printf("[+] wrote %s.\n", fileName+".pub") |
| 54 |
} |
| 55 |
} |