File: pubdump.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 384 kB
  • ctags: 331
  • sloc: makefile: 6
file content (55 lines) | stat: -rw-r--r-- 959 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main

import (
	"crypto/ecdsa"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"flag"
	"fmt"
	"io/ioutil"
	"log"

	"github.com/kisom/goutils/die"
)

func main() {
	flag.Parse()

	for _, fileName := range flag.Args() {
		in, err := ioutil.ReadFile(fileName)
		die.If(err)

		if p, _ := pem.Decode(in); p != nil {
			if p.Type != "CERTIFICATE REQUEST" {
				log.Fatal("INVALID FILE TYPE")
			}
			in = p.Bytes
		}

		csr, err := x509.ParseCertificateRequest(in)
		die.If(err)

		out, err := x509.MarshalPKIXPublicKey(csr.PublicKey)
		die.If(err)

		var t string
		switch pub := csr.PublicKey.(type) {
		case *rsa.PublicKey:
			t = "RSA PUBLIC KEY"
		case *ecdsa.PublicKey:
			t = "EC PUBLIC KEY"
		default:
			die.With("unrecognised public key type %T", pub)
		}

		p := &pem.Block{
			Type:  t,
			Bytes: out,
		}

		err = ioutil.WriteFile(fileName+".pub", pem.EncodeToMemory(p), 0644)
		die.If(err)
		fmt.Printf("[+] wrote %s.\n", fileName+".pub")
	}
}