aboutsummaryrefslogtreecommitdiff
path: root/pem.go
blob: bbe2a478f53b519d2bbedc381387323cc733c317 (plain) (blame)
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
package main

// handle the pem decoding of files

import (
	"encoding/pem"
	"fmt"
	"io"
	"io/ioutil"

	"github.com/gibheer/pki"
)

type (
	pemMap map[string][][]byte
)

// Return the content of a section from the pem part.
//
// To get this working, the section must only be contained one time and nothing
// but the wanted section must exist.
func getSectionFromPem(pems pemMap, label string) ([]byte, error) {
	if res, found := pems[label]; !found {
		return []byte{}, fmt.Errorf("could not find section '%s'", label)
	} else if len(res) > 1 {
		return []byte{}, fmt.Errorf("too many entries of type '%s'", label)
	} else {
		return res[0], nil
	}
}

// parse the content of a file into a map of pem decoded bodies
func parseFile(file io.Reader) (pemMap, error) {
	raw, err := ioutil.ReadAll(file)
	if err != nil {
		return nil, err
	}
	return parsePem(raw)
}

// parse a pem encoded payload into a lookup map
//
// Returns a map of labels and content and the overall number of found items.
func parsePem(payload []byte) (pemMap, error) {
	res := pemMap{}
	rest := payload
	rest_len := len(rest)
	for len(rest) > 0 {
		var block *pem.Block
		block, rest = pem.Decode(rest)
		if block == nil && len(rest) == rest_len {
			return nil, fmt.Errorf("no pem encoding found")
		}
		res[block.Type] = append(res[block.Type], block.Bytes)
		rest_len = len(rest)
	}
	return res, nil
}

func writePem(o pki.Pemmer, w io.Writer) error {
	marsh_pem, err := o.MarshalPem()
	if err != nil {
		return err
	}

	_, err = marsh_pem.WriteTo(w)
	if err != nil {
		return err
	}
	return nil
}