aboutsummaryrefslogtreecommitdiff
path: root/rfc4251.h
blob: b2f9658854fdd8ec9abb2d95f54795d6eb0bfac5 (plain)
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
/*
 * rfc4251.h -- implements types from RFC 4251, section 5
 *
 * rfc4251byte		byte
 * rfc4251bool		bool
 * rfc4251uint32	uint32
 * rfc4251uint64	uint64
 * rfc4251string	string, incl. mpint and name-list
 *
 * those structs contain the objects in their RFC 4251 representation,
 * conversions are provided via constructors and cast operators
 *
 * Copyright (C) 2013 Timo Weingärtner <timo@tiwe.de>
 *
 * This file is part of ssh-agent-filter.
 *
 * ssh-agent-filter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ssh-agent-filter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ssh-agent-filter.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <vector>
#include <string>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <arpa/inet.h>	// ntohl() / htonl()
#include <gmpxx.h>

struct rfc4251byte {
	union {
		uint8_t value;
		char buf[1];
	};

	rfc4251byte () = default;
	explicit rfc4251byte (uint8_t v) : value(v) {}

	operator uint8_t () const { return value; }
};

inline std::istream & operator>> (std::istream & is, rfc4251byte & x) {
	return is.read(x.buf, sizeof(x.buf));
}

inline std::ostream & operator<< (std::ostream & os, rfc4251byte const & x) {
	return os.write(x.buf, sizeof(x.buf));;
}


struct rfc4251bool {
	union {
		bool value;
		char buf[1];
	};

	rfc4251bool () = default;
	explicit rfc4251bool (uint8_t v) : value(v) {}

	operator uint8_t () const { return value; }
};

inline std::istream & operator>> (std::istream & is, rfc4251bool & x) {
	return is.read(x.buf, sizeof(x.buf));
}

inline std::ostream & operator<< (std::ostream & os, rfc4251bool const & x) {
	return os.write(x.buf, sizeof(x.buf));;
}


struct rfc4251uint32 {
	union {
		uint32_t value;
		char buf[4];
	};

	rfc4251uint32 () = default;
	explicit rfc4251uint32 (uint32_t v) { value = htonl(v); }

	operator uint32_t () const { return ntohl(value); }
};

inline std::istream & operator>> (std::istream & is, rfc4251uint32 & x) {
	return is.read(x.buf, sizeof(x.buf));
}

inline std::ostream & operator<< (std::ostream & os, rfc4251uint32 const & x) {
	return os.write(x.buf, sizeof(x.buf));;
}


struct rfc4251uint64 {
	union {
		uint64_t value;
		char buf[8];
	};

	rfc4251uint64 () = default;
	inline explicit rfc4251uint64 (uint64_t v);

	inline explicit operator uint64_t () const;
};

inline rfc4251uint64::rfc4251uint64 (uint64_t v) {
	for (int_fast8_t i{7}; i >= 0; --i) {
		buf[i] = v & 0xff;
		v >>= 8;
	}
}

inline rfc4251uint64::operator uint64_t () const {
	uint64_t ret{0};
	for (uint_fast8_t i{0}; i < 8; ++i) {
		ret |= buf[i];
		ret <<= 8;
	}
	return ret;
}

inline std::istream & operator>> (std::istream & is, rfc4251uint64 & x) {
	return is.read(x.buf, sizeof(x.buf));
}

inline std::ostream & operator<< (std::ostream & os, rfc4251uint64 const & x) {
	return os.write(x.buf, sizeof(x.buf));;
}


struct rfc4251string {
	std::vector<char> value;
	
	rfc4251string () = default;
	inline explicit rfc4251string (char const *);
	inline explicit rfc4251string (char const *, size_t);
	explicit rfc4251string (std::string const & s) : rfc4251string{s.data(), s.size()} {}
	explicit rfc4251string (std::vector<std::string> const &);
	explicit rfc4251string (mpz_srcptr);
	explicit rfc4251string (mpz_class const & x) : rfc4251string{x.get_mpz_t()} {}

	operator std::string () const { return {value.begin(), value.end()}; }
	operator std::vector<std::string> () const;
	operator mpz_class () const;
};

inline rfc4251string::rfc4251string (char const * s) {
	auto len = ntohl(*reinterpret_cast<uint32_t const *>(s));
	value.insert(value.begin(), s + 4, s + 4 + len);
}

inline rfc4251string::rfc4251string (char const * s, size_t l) {
	if (l > std::numeric_limits<uint32_t>::max())
		throw std::length_error{"32-bit limit for rfc4251string exceeded"};
	value.insert(value.end(), s, s + l);
}

rfc4251string::rfc4251string (std::vector<std::string> const & v) {
	for (auto it = v.begin(); it != v.end();) {
		if (it->size() == 0)
			throw std::length_error{"name of zero length"};
		if (value.size() + it->size() > std::numeric_limits<uint32_t>::max())
			throw std::length_error{"32-bit limit for rfc4251string exceeded"};
		value.insert(value.end(), it->data(), it->data() + it->size());
		++it;
		if (it == v.end())
			break;
		value.push_back(',');
	}
}

rfc4251string::operator std::vector<std::string> () const {
	std::vector<std::string> ret;
	auto name_start = value.begin();
	if (name_start != value.end())
		for (auto it = name_start; ; ++it) {
			if (it == value.end() or *it == ',') {
				if (it == name_start)
					throw std::length_error{"name of zero length"};
				ret.emplace_back(name_start, it);
				name_start = it + 1;
			}
			if (it == value.end())
				break;
		}
	return ret;
}

inline std::istream & operator>> (std::istream & is, rfc4251string & s) {
	s.value.clear();
	rfc4251uint32 len;
	if (is >> len) {
		s.value.resize(len);
		is.read(s.value.data(), len);
	}
	return is;
}

inline std::ostream & operator<< (std::ostream & os, rfc4251string const & s) {
	if (s.value.size() > std::numeric_limits<uint32_t>::max())
		throw std::length_error{"32-bit limit for rfc4251string exceeded"};
	if (os << rfc4251uint32{static_cast<uint32_t>(s.value.size())})
		os.write(s.value.data(), s.value.size());
	return os;
}

inline bool operator== (rfc4251string const & l, rfc4251string const & r) {
	return l.value == r.value;
}

inline bool operator< (rfc4251string const & l, rfc4251string const & r) {
	return l.value < r.value;
}