pluto-src 0.1.1+0.10.4

Sources of Pluto (Lua 5.4 dialect) and logic to build it.
Documentation
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include "Asn1Sequence.hpp"

#include <sstream>

#include "Asn1Type.hpp"
#include "Bigint.hpp"
//#include "Exception.hpp"
#include "MemoryRefReader.hpp"
#include "Oid.hpp"
#include "string.hpp"
#include "time.hpp"

NAMESPACE_SOUP
{
	Asn1Sequence::Asn1Sequence() SOUP_EXCAL
		: std::vector<Asn1Element>()
	{
	}

	Asn1Sequence::Asn1Sequence(const std::string& data) SOUP_EXCAL
		: Asn1Sequence()
	{
		MemoryRefReader r{ data };
		while (r.hasMore())
		{
			auto id = readIdentifier(r);
			auto len = readLength(r);
			/*SOUP_IF_UNLIKELY (len > 10000)
			{
				SOUP_THROW(Exception("Asn1Element is unreasonably long"));
			}*/
			std::string buf;
			r.str(len, buf);
			emplace_back(Asn1Element{ std::move(id), std::move(buf) });
		}
	}

	Asn1Sequence Asn1Sequence::fromDer(const std::string& str) SOUP_EXCAL
	{
		MemoryRefReader r{ str };
		return fromDer(r);
	}

	Asn1Sequence Asn1Sequence::fromDer(const char* data, size_t size)
	{
		MemoryRefReader r(data, size);
		return fromDer(r);
	}

	Asn1Sequence Asn1Sequence::fromDer(Reader& r) SOUP_EXCAL
	{
		SOUP_IF_UNLIKELY (readIdentifier(r).type != ASN1_SEQUENCE)
		{
			return {};
		}
		auto len = readLength(r);
		SOUP_IF_UNLIKELY (len > 10000)
		{
			return {};
		}
		std::string buf;
		r.str(len, buf);
		return Asn1Sequence{ std::move(buf) };
	}

	size_t Asn1Sequence::countChildren() const
	{
		return size();
	}

	const Asn1Identifier& Asn1Sequence::getChildType(const size_t child_idx) const
	{
		return at(child_idx).identifier;
	}

	std::string& Asn1Sequence::getString(const size_t child_idx)
	{
		return at(child_idx).data;
	}

	const std::string& Asn1Sequence::getString(const size_t child_idx) const
	{
		return at(child_idx).data;
	}

	Asn1Sequence Asn1Sequence::getSeq(const size_t child_idx) const
	{
		return Asn1Sequence{ getString(child_idx) };
	}

	Bigint Asn1Sequence::getInt(const size_t child_idx) const
	{
		return Bigint::fromBinary(getString(child_idx));
	}

	Oid Asn1Sequence::getOid(const size_t child_idx) const
	{
		return Oid::fromBinary(getString(child_idx));
	}

	std::time_t Asn1Sequence::getUtctime(const size_t child_idx) const
	{
		auto utctime = getString(child_idx);
		SOUP_IF_UNLIKELY (utctime.size() < 12)
		{
			return 0;
		}
		int year = std::stol(utctime.substr(0, 2)) + 2000; // what happens in 2100? probably something really funny. too bad I won't be around for that.
		int month = std::stol(utctime.substr(2, 2));
		int day = std::stol(utctime.substr(4, 2));
		int hour = std::stol(utctime.substr(6, 2));
		int minute = std::stol(utctime.substr(8, 2));
		int second = std::stol(utctime.substr(10, 2));
		return time::toUnix(year, month, day, hour, minute, second);
	}

	void Asn1Sequence::addInt(const Bigint& val)
	{
		std::string bin = val.toBinary();
		if (bin.empty())
		{
			bin = std::string(1, '\0');
		}
		else if ((uint8_t)bin.at(0) & 0x80) // A proper decoder might think the value is negative :'(
		{
			bin.insert(0, 1, '\0');
		}
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, false, ASN1_INTEGER },
			std::move(bin)
		});
	}

	void Asn1Sequence::addOid(const Oid& val)
	{
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, false, ASN1_OID },
			val.toDer()
		});
	}

	void Asn1Sequence::addNull()
	{
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, false, ASN1_NULL },
			{}
		});
	}

	void Asn1Sequence::addBitString(std::string val)
	{
		val.insert(0, 1, '\0'); // idk why there's a preceeding 0 but there is so I guess we just do the same...
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, false, ASN1_BITSTRING },
			std::move(val)
		});
	}

	void Asn1Sequence::addUtf8String(std::string val)
	{
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, false, ASN1_UTF8STRING },
			std::move(val)
		});
	}

	void Asn1Sequence::addPrintableString(std::string val)
	{
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, false, ASN1_STRING_PRINTABLE },
			std::move(val)
		});
	}

	void Asn1Sequence::addSeq(const Asn1Sequence& seq)
	{
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, true, ASN1_SEQUENCE },
			seq.toDerNoPrefix()
		});
	}

	void Asn1Sequence::addSet(const Asn1Sequence& seq)
	{
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, true, ASN1_SET },
			seq.toDerNoPrefix()
		});
	}

	void Asn1Sequence::addName(const std::vector<std::pair<Oid, std::string>>& name)
	{
		Asn1Sequence set;
		for (const auto& e : name)
		{
			Asn1Sequence seq;
			seq.addOid(e.first);
			seq.addUtf8String(e.second);

			set.addSeq(seq);
		}

		Asn1Sequence seq;
		seq.addSet(set);

		addSeq(seq);
	}

	void Asn1Sequence::addUtctime(std::time_t t)
	{
		auto datetime = time::datetimeUtc(t);

		std::string str;
		str.append(std::to_string(datetime.year).substr(2, 2));
		str.append(string::lpad(std::to_string(datetime.month), 2, '0'));
		str.append(string::lpad(std::to_string(datetime.day), 2, '0'));
		str.append(string::lpad(std::to_string(datetime.hour), 2, '0'));
		str.append(string::lpad(std::to_string(datetime.minute), 2, '0'));
		str.append(string::lpad(std::to_string(datetime.second), 2, '0'));
		str.push_back('Z');
		
		emplace_back(Asn1Element{
			Asn1Identifier{ 0, false, ASN1_UTCTIME },
			std::move(str)
		});
	}

	std::string Asn1Sequence::toDer() const
	{
		std::string res = toDerNoPrefix();
		res.insert(0, encodeLength(res.size()));
		res.insert(0, Asn1Identifier{ 0, true, ASN1_SEQUENCE }.toDer());
		return res;
	}

	std::string Asn1Sequence::toDerNoPrefix() const
	{
		std::string res{};
		for (const auto& c : *this)
		{
			res.append(c.identifier.toDer());
			res.append(encodeLength(c.data.size()));
			res.append(c.data);
		}
		return res;
	}

	std::string Asn1Sequence::toString(const std::string& prefix) const
	{
		std::string ret{};
		size_t i = 0;
		for (const auto& c : *this)
		{
			ret.push_back('\n');
			ret.append(prefix);
			ret.push_back('[');
			ret.append(std::to_string(i++));
			ret.append("] ");
			ret.append(c.identifier.constructed ? "Constructed " : "Primitive ");
			if (c.identifier.m_class == 0)
			{
				switch (c.identifier.type)
				{
				default:
					ret.append(std::to_string(c.identifier.type));
					break;

				case ASN1_BOOLEAN:
					ret.append("BOOLEAN");
					break;

				case ASN1_INTEGER:
					ret.append("INTEGER");
					break;

				case ASN1_BITSTRING:
					ret.append("BIT STRING");
					break;

				case ASN1_STRING_OCTET:
					ret.append("OCTET STRING");
					break;

				case ASN1_UTF8STRING:
					ret.append("UTF8 STRING");
					break;

				case ASN1_NULL:
					ret.append("NULL");
					break;

				case ASN1_OID:
					ret.append("OID");
					break;

				case ASN1_SEQUENCE:
					ret.append("SEQUENCE");
					break;

				case ASN1_SET:
					ret.append("SET");
					break;

				case ASN1_STRING_PRINTABLE:
					ret.append("PrintableString");
					break;

				case ASN1_STRING_IA5:
					ret.append("IA5String");
					break;

				case ASN1_UTCTIME:
					ret.append("UTCTime");
					break;
				}
			}
			else
			{
				switch (c.identifier.m_class)
				{
				case 1:
					ret.append("Application-specific ");
					break;

				case 2:
					ret.append("Context-specific ");
					break;

				case 3:
					ret.append("Private ");
					break;
				}
				ret.append(std::to_string(c.identifier.type));
			}
			if (c.data.empty())
			{
				continue;
			}
			ret.push_back(':');
			if (c.identifier.m_class == 0
				&& (c.identifier.type == ASN1_SEQUENCE || c.identifier.type == ASN1_SET)
				)
			{
				ret.push_back('\n');
				std::string rp = prefix;
				rp.push_back('\t');
				ret.append(Asn1Sequence(c.data).toString(rp));
			}
			else
			{
				ret.push_back(' ');
				switch (c.identifier.type)
				{
				default:
					ret.append(string::bin2hex(c.data));
					break;

				case ASN1_INTEGER:
				//case ASN1_BITSTRING:
					ret.append(Bigint::fromBinary(c.data).toString());
					break;

				case ASN1_OID:
					ret.append(Oid::fromBinary(c.data).toString());
					break;

				case ASN1_UTF8STRING:
				case ASN1_STRING_PRINTABLE:
				case ASN1_STRING_IA5:
				case ASN1_UTCTIME: // yyMMddHHmmssZ
					ret.append(c.data);
					break;
				}
			}
		}
		if (!ret.empty())
		{
			ret.erase(0, 1);
		}
		return ret;
	}

	Asn1Identifier Asn1Sequence::readIdentifier(Reader& r)
	{
		Asn1Identifier ret{};
		uint8_t first;
		r.u8(first);
		ret.m_class = (first >> 6);
		ret.constructed = (first >> 5) & 1;
		ret.type = (first & 0b11111);
		if (ret.type > 30)
		{
			r.om<uint32_t>(ret.type);
		}
		return ret;
	}

	size_t Asn1Sequence::readLength(Reader& r)
	{
		uint8_t b;
		if (!r.u8(b))
		{
			return 0;
		}
		size_t length = b;
		if (length & 0x80)
		{
			uint8_t length_bytes = (length & 0x7F);
			length = 0;
			for (uint8_t i = 0; i != length_bytes; ++i)
			{
				if (!r.u8(b))
				{
					break;
				}
				length <<= 8;
				length |= b;
			}
		}
		return length;
	}

	std::string Asn1Sequence::encodeLength(size_t len)
	{
		std::string ret{};
		if (len <= 0x7F)
		{
			ret.push_back(static_cast<char>(len));
		}
		else
		{
			do
			{
				ret.insert(0, 1, static_cast<char>(static_cast<unsigned char>(len)));
			} while (len >>= 8, len != 0);
			ret.insert(0, 1, static_cast<char>(ret.size() | 0x80));
		}
		return ret;
	}
}