File: marshal_utils.go

package info (click to toggle)
golang-github-smallstep-crypto 0.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,800 kB
  • sloc: sh: 66; makefile: 50
file content (251 lines) | stat: -rw-r--r-- 5,778 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package x509util

import (
	"crypto/x509"
	"encoding/asn1"
	"encoding/json"
	"net"
	"net/url"
	"strconv"
	"strings"

	"github.com/pkg/errors"
)

// MultiString is a type used to unmarshal a JSON string or an array of strings
// into a []string.
type MultiString []string

// UnmarshalJSON implements the json.Unmarshaler interface for MultiString.
func (m *MultiString) UnmarshalJSON(data []byte) error {
	if s, ok := maybeString(data); ok {
		*m = MultiString([]string{s})
		return nil
	}

	var v []string
	if err := json.Unmarshal(data, &v); err != nil {
		return errors.Wrap(err, "error unmarshaling json")
	}
	*m = MultiString(v)
	return nil
}

// MultiIP is a type used to unmarshal a JSON string or an array of strings into
// a []net.IP.
type MultiIP []net.IP

// UnmarshalJSON implements the json.Unmarshaler interface for MultiIP.
func (m *MultiIP) UnmarshalJSON(data []byte) error {
	ms, err := unmarshalMultiString(data)
	if err != nil {
		return err
	}
	if ms != nil {
		ips := make([]net.IP, len(ms))
		for i, s := range ms {
			ip := net.ParseIP(s)
			if ip == nil {
				return errors.Errorf("error unmarshaling json: ip %s is not valid", s)
			}
			ips[i] = ip
		}

		*m = MultiIP(ips)
	}
	return nil
}

// MultiIPNet is a type used to unmarshal a JSON string or an array of strings
// into a []*net.IPNet.
type MultiIPNet []*net.IPNet

// MarshalJSON implements the json.Marshaler interface for MultiIPNet.
func (m MultiIPNet) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	ipNets := make([]string, len(m))
	for i, v := range m {
		ipNets[i] = v.String()
	}
	return json.Marshal(ipNets)
}

// UnmarshalJSON implements the json.Unmarshaler interface for MultiIPNet.
func (m *MultiIPNet) UnmarshalJSON(data []byte) error {
	ms, err := unmarshalMultiString(data)
	if err != nil {
		return err
	}
	if ms != nil {
		ipNets := make([]*net.IPNet, len(ms))
		for i, s := range ms {
			_, ipNet, err := net.ParseCIDR(s)
			if err != nil {
				return errors.Wrap(err, "error unmarshaling json")
			}
			ipNets[i] = ipNet
		}

		*m = MultiIPNet(ipNets)
	}
	return nil
}

// MultiURL is a type used to unmarshal a JSON string or an array of strings
// into a []*url.URL.
type MultiURL []*url.URL

// MarshalJSON implements the json.Marshaler interface for MultiURL.
func (m MultiURL) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	urls := make([]string, len(m))
	for i, u := range m {
		urls[i] = u.String()
	}
	return json.Marshal(urls)
}

// UnmarshalJSON implements the json.Unmarshaler interface for MultiURL.
func (m *MultiURL) UnmarshalJSON(data []byte) error {
	ms, err := unmarshalMultiString(data)
	if err != nil {
		return err
	}
	if ms != nil {
		urls := make([]*url.URL, len(ms))
		for i, s := range ms {
			u, err := url.Parse(s)
			if err != nil {
				return errors.Wrap(err, "error unmarshaling json")
			}
			urls[i] = u
		}

		*m = MultiURL(urls)
	}
	return nil
}

// MultiOID is a type used to unmarshal a JSON string or an array
// of strings into a []x509.OID.
type MultiOID []x509.OID

// MarshalJSON implements the json.Marshaler interface for MultiOID.
func (m MultiOID) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	oids := make([]string, len(m))
	for i, u := range m {
		oids[i] = u.String()
	}
	return json.Marshal(oids)
}

// UnmarshalJSON implements the json.Unmarshaler interface for
// MultiOID.
func (m *MultiOID) UnmarshalJSON(data []byte) error {
	ms, err := unmarshalMultiString(data)
	if err != nil {
		return err
	}
	if ms != nil {
		oids := make([]x509.OID, len(ms))
		for i, s := range ms {
			oid, err := x509.ParseOID(s)
			if err != nil {
				return err
			}
			oids[i] = oid
		}

		*m = MultiOID(oids)
	}
	return nil
}

// MultiObjectIdentifier is a type used to unmarshal a JSON string or an array
// of strings into a []asn1.ObjectIdentifier.
type MultiObjectIdentifier []asn1.ObjectIdentifier

// MarshalJSON implements the json.Marshaler interface for MultiObjectIdentifier.
func (m MultiObjectIdentifier) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	oids := make([]string, len(m))
	for i, u := range m {
		oids[i] = u.String()
	}
	return json.Marshal(oids)
}

// UnmarshalJSON implements the json.Unmarshaler interface for
// MultiObjectIdentifier.
func (m *MultiObjectIdentifier) UnmarshalJSON(data []byte) error {
	ms, err := unmarshalMultiString(data)
	if err != nil {
		return err
	}
	if ms != nil {
		oids := make([]asn1.ObjectIdentifier, len(ms))
		for i, s := range ms {
			oid, err := parseObjectIdentifier(s)
			if err != nil {
				return err
			}
			oids[i] = oid
		}

		*m = MultiObjectIdentifier(oids)
	}
	return nil
}

func maybeString(data []byte) (string, bool) {
	if len(data) > 0 && data[0] == '"' {
		var v string
		if err := json.Unmarshal(data, &v); err == nil {
			return v, true
		}
	}
	return "", false
}

func unmarshalString(data []byte) (string, error) {
	var v string
	if err := json.Unmarshal(data, &v); err != nil {
		return v, errors.Wrap(err, "error unmarshaling json")
	}
	return v, nil
}

func unmarshalMultiString(data []byte) ([]string, error) {
	var v MultiString
	if err := json.Unmarshal(data, &v); err != nil {
		return nil, errors.Wrap(err, "error unmarshaling json")
	}
	return []string(v), nil
}

func parseObjectIdentifier(oid string) (asn1.ObjectIdentifier, error) {
	if oid == "" {
		return asn1.ObjectIdentifier{}, nil
	}

	parts := strings.Split(oid, ".")
	oids := make([]int, len(parts))

	for i, s := range parts {
		n, err := strconv.Atoi(s)
		if err != nil {
			return asn1.ObjectIdentifier{}, errors.Errorf("error unmarshaling json: %s is not an ASN1 object identifier", oid)
		}
		oids[i] = n
	}
	return asn1.ObjectIdentifier(oids), nil
}