File: tor_service_descriptor_test.go

package info (click to toggle)
golang-github-zmap-zcrypto 0.0~git20240512.0fef58d-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,856 kB
  • sloc: python: 567; sh: 124; makefile: 9
file content (253 lines) | stat: -rw-r--r-- 7,823 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
252
253
package x509

import (
	"fmt"
	"reflect"
	"testing"

	"github.com/zmap/zcrypto/encoding/asn1"
	"github.com/zmap/zcrypto/x509/pkix"
)

// TestParseTorServiceDescriptorSyntax tests that parsing certificates with the
// CAB Forum TorServiceDescriptorSyntax x509 extension works correctly.
func TestParseTorServiceDescriptorSyntax(t *testing.T) {
	// expected TorServiceDescriptorHash hash bytes from test certs.
	mockHashBytes := []byte{
		0xc7, 0x49, 0xf5, 0xb2, 0x49, 0x9c, 0x8f, 0x65,
		0x5c, 0x19, 0xb3, 0x3f, 0xf9, 0x3e, 0x03, 0x7b,
		0x7b, 0x7d, 0xbe, 0x47, 0x2a, 0xac, 0x62, 0x78,
		0x30, 0x71, 0xb0, 0x39, 0xb8, 0x66, 0x38, 0x5c,
	}
	mockAlgorithm := pkix.AlgorithmIdentifier{
		Algorithm: asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1},
	}
	// mustASN1 marshals a given object to its ASN1 bytes or panics.
	mustASN1 := func(value interface{}) []byte {
		result, err := asn1.Marshal(value)
		if err != nil {
			panic(fmt.Sprintf("err marshaling asn1 test data: %v", err))
		}
		return result
	}
	// sequence marshals a ASN1 SEQUENCE for the given bytes.
	sequence := func(bytes []byte) []byte {
		return mustASN1(asn1.RawValue{
			Tag:        asn1.TagSequence,
			Class:      asn1.ClassUniversal,
			IsCompound: true,
			Bytes:      bytes,
		})
	}
	// torServiceDescriptorHash constructs a marshaled SEQUENCE for
	// a TorServiceDescriptorHash with the given values.
	torServiceDescriptorHash := func(onion string, algorithm pkix.AlgorithmIdentifier, hash []byte, bits int) []byte {
		return sequence(
			append(mustASN1(asn1.RawValue{
				Tag:   asn1.TagUTF8String,
				Class: asn1.ClassUniversal,
				Bytes: []byte(onion),
			}),
				append(
					mustASN1(algorithm),
					mustASN1(asn1.BitString{
						Bytes:     hash,
						BitLength: bits,
					})...)...,
			))
	}
	testCases := []struct {
		Name                          string
		InputExtension                pkix.Extension
		ExpectedErrMsg                string
		ExpectedTorServiceDescriptors []*TorServiceDescriptorHash
	}{
		{
			Name: "empty Tor service descriptor extension",
			InputExtension: pkix.Extension{
				Value: nil,
			},
			ExpectedErrMsg: "asn1: syntax error: unable to unmarshal outer TorServiceDescriptor SEQUENCE",
		},
		{
			Name: "invalid outer SEQUENCE in service descriptor extension",
			InputExtension: pkix.Extension{
				Value: mustASN1(asn1.RawValue{}),
			},
			ExpectedErrMsg: "asn1: syntax error: invalid outer TorServiceDescriptor SEQUENCE",
		},
		{
			Name: "data trailing outer SEQUENCE in service descriptor extension",
			InputExtension: pkix.Extension{
				Value: append(
					sequence(nil),                // Outer SEQUENCE
					mustASN1(asn1.RawValue{})..., // Trailing data
				),
			},
			ExpectedErrMsg: "asn1: syntax error: trailing data after outer TorServiceDescriptor SEQUENCE",
		},
		{
			Name: "bad service descriptor onion URI field tag",
			InputExtension: pkix.Extension{
				Value: sequence( // Outer SEQUENCE
					sequence( // TorServiceDescriptorHash SEQUENCE
						mustASN1(asn1.RawValue{}), // Invalid Onion URI
					)),
			},
			ExpectedErrMsg: "asn1: syntax error: TorServiceDescriptorHash missing non-compound UTF8String tag",
		},
		{
			Name: "bad service descriptor algorithm field",
			InputExtension: pkix.Extension{
				Value: sequence( // Outer SEQUENCE
					sequence( // TorServiceDescriptorHash SEQUENCE
						mustASN1(asn1.RawValue{ // Onion URI
							Tag:   asn1.TagUTF8String,
							Class: asn1.ClassUniversal,
						}))),
				// No pkix.AlgorithmIdentifier algorithm field
			},
			ExpectedErrMsg: "asn1: syntax error: error unmarshaling TorServiceDescriptorHash algorithm",
		},
		{
			Name: "bad service descriptor hash field",
			InputExtension: pkix.Extension{
				Value: sequence( // Outer SEQUENCE
					sequence( // TorServiceDescriptorHash SEQUENCE
						append(mustASN1(asn1.RawValue{ // Onion URI
							Tag:   asn1.TagUTF8String,
							Class: asn1.ClassUniversal,
						}),
							mustASN1(mockAlgorithm)...), // Algorithm
					)),
				// No BitString hash field
			},
			ExpectedErrMsg: "asn1: syntax error: error unmarshaling TorServiceDescriptorHash Hash",
		},
		{
			Name: "data trailing inner TorServiceDescriptorHash SEQUENCE",
			InputExtension: pkix.Extension{
				Value: sequence(
					sequence( // Outer SEQUENCE
						append(mustASN1(asn1.RawValue{ // Onion URI
							Tag:   asn1.TagUTF8String,
							Class: asn1.ClassUniversal,
						}),
							append(
								append(
									mustASN1(mockAlgorithm), // Algorithm
									mustASN1(asn1.BitString{ // Hash
										Bytes:     []byte{0x00},
										BitLength: 1,
									})...,
								),
								mustASN1(asn1.RawValue{})..., // Trailing data
							)...,
						),
					)),
			},
			ExpectedErrMsg: "asn1: syntax error: trailing data after TorServiceDescriptorHash",
		},
		{
			Name: "valid service descriptor unknown hash algorithm",
			InputExtension: pkix.Extension{
				Value: sequence( // Outer SEQUENCE
					torServiceDescriptorHash(
						"https://zmap.onion",
						pkix.AlgorithmIdentifier{
							Algorithm: asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 99},
						},
						mockHashBytes,
						256),
				),
			},
			ExpectedTorServiceDescriptors: []*TorServiceDescriptorHash{
				{
					Onion:         "https://zmap.onion",
					AlgorithmName: "Unknown",
					Algorithm: pkix.AlgorithmIdentifier{
						Algorithm: asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 99},
					},
					HashBits: 256,
					Hash:     mockHashBytes,
				},
			},
		},
		{
			Name: "valid service descriptor extension",
			InputExtension: pkix.Extension{
				Value: sequence( // Outer SEQUENCE
					torServiceDescriptorHash(
						"https://zmap.onion",
						mockAlgorithm,
						mockHashBytes,
						256),
				)},
			ExpectedTorServiceDescriptors: []*TorServiceDescriptorHash{
				{
					Onion:         "https://zmap.onion",
					AlgorithmName: "SHA256",
					Algorithm:     mockAlgorithm,
					HashBits:      256,
					Hash:          mockHashBytes,
				},
			},
		},
		{
			Name: "valid service descriptor extension, multiple entries",
			InputExtension: pkix.Extension{
				Value: sequence( // Outer SEQUENCE
					append(torServiceDescriptorHash(
						"https://zmap.onion",
						mockAlgorithm,
						mockHashBytes,
						256),
						torServiceDescriptorHash(
							"https://other.onion",
							mockAlgorithm,
							mockHashBytes,
							256)...),
				)},
			ExpectedTorServiceDescriptors: []*TorServiceDescriptorHash{
				{
					Onion:         "https://zmap.onion",
					AlgorithmName: "SHA256",
					Algorithm:     mockAlgorithm,
					HashBits:      256,
					Hash:          mockHashBytes,
				},
				{
					Onion:         "https://other.onion",
					AlgorithmName: "SHA256",
					Algorithm:     mockAlgorithm,
					HashBits:      256,
					Hash:          mockHashBytes,
				},
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.Name, func(t *testing.T) {
			descs, err := parseTorServiceDescriptorSyntax(tc.InputExtension)
			if err != nil && tc.ExpectedErrMsg == "" {
				t.Errorf("expected no error, got %v", err)
			} else if err == nil && tc.ExpectedErrMsg != "" {
				t.Errorf("expected error %q, got nil", tc.ExpectedErrMsg)
			} else if err != nil && err.Error() != tc.ExpectedErrMsg {
				t.Errorf("expected error %q, got %q", tc.ExpectedErrMsg, err.Error())
			} else if err == nil && tc.ExpectedErrMsg == "" {
				if len(descs) != len(tc.ExpectedTorServiceDescriptors) {
					t.Errorf("expected %d TorServiceDescriptorHashes, got %d",
						len(tc.ExpectedTorServiceDescriptors), len(descs))
				}
				for i, servDesc := range descs {
					if !reflect.DeepEqual(servDesc, tc.ExpectedTorServiceDescriptors[i]) {
						t.Errorf("expected TorServiceDescriptors %#v in index %d, got %#v",
							tc.ExpectedTorServiceDescriptors[i], i, servDesc)
					}
				}
			}
		})
	}
}