forked from Linaro/lite_bootstrap_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_cert_cbor.go
84 lines (69 loc) · 1.42 KB
/
get_cert_cbor.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//go:build never
// +build never
// This program reads the CBOR response to the api/v1/cr request, and
// outputs the retrieved certificate.
//
// Assuming the request was read with
//
// wget --ca-certificate=SERVER.crt \
// --post-file USER.cbor \
// https://localhost:1443/api/v1/cr \
// -O USER.rsp
//
// This program can be used to extract the certificate:
//
// go run get_cert_cbor.go -in USER.rsp -out USER.crt
package main
import (
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/fxamacker/cbor/v2"
"github.com/Linaro/lite_bootstrap_server/protocol"
)
var (
inFile = flag.String("in", "USER.cbor", "Name of input cbor file")
outFile = flag.String("out", "USER.crt", "Name of output certificate")
)
func main() {
flag.Parse()
err := run()
if err != nil {
fmt.Printf("Failure: %v\n", err)
os.Exit(1)
}
}
func run() error {
inp, err := os.Open(*inFile)
if err != nil {
return err
}
defer inp.Close()
raw, err := ioutil.ReadAll(inp)
if err != nil {
return err
}
var rsp protocol.CSRResponse
err = cbor.Unmarshal(raw, &rsp)
if err != nil {
return err
}
if rsp.Status != 0 {
return fmt.Errorf("Returned status was not 0")
}
var pdata pem.Block
pdata.Type = "CERTIFICATE"
pdata.Bytes = rsp.Cert
outp, err := os.Create(*outFile)
if err != nil {
return err
}
defer outp.Close()
err = pem.Encode(outp, &pdata)
if err != nil {
return err
}
return nil
}