/
PemEncoding.cs
548 lines (480 loc) · 22.1 KB
/
PemEncoding.cs
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace System.Security.Cryptography
{
/// <summary>
/// Provides methods for reading and writing the IETF RFC 7468
/// subset of PEM (Privacy-Enhanced Mail) textual encodings.
/// This class cannot be inherited.
/// </summary>
public static class PemEncoding
{
private const string PreEBPrefix = "-----BEGIN ";
private const string PostEBPrefix = "-----END ";
private const string Ending = "-----";
private const int EncodedLineLength = 64;
/// <summary>
/// Finds the first PEM-encoded data.
/// </summary>
/// <param name="pemData">
/// The text containing the PEM-encoded data.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="pemData"/> does not contain a well-formed PEM-encoded value.
/// </exception>
/// <returns>
/// A value that specifies the location, label, and data location of
/// the encoded data.
/// </returns>
/// <remarks>
/// IETF RFC 7468 permits different decoding rules. This method
/// always uses lax rules.
/// </remarks>
public static PemFields Find(ReadOnlySpan<char> pemData)
{
if (!TryFind(pemData, out PemFields fields))
{
throw new ArgumentException(SR.Argument_PemEncoding_NoPemFound, nameof(pemData));
}
return fields;
}
/// <summary>
/// Attempts to find the first PEM-encoded data.
/// </summary>
/// <param name="pemData">
/// The text containing the PEM-encoded data.
/// </param>
/// <param name="fields">
/// When this method returns, contains a value
/// that specifies the location, label, and data location of the encoded data;
/// or that specifies those locations as empty if no PEM-encoded data is found.
/// This parameter is treated as uninitialized.
/// </param>
/// <returns>
/// <c>true</c> if PEM-encoded data was found; otherwise <c>false</c>.
/// </returns>
/// <remarks>
/// IETF RFC 7468 permits different decoding rules. This method
/// always uses lax rules.
/// </remarks>
public static bool TryFind(ReadOnlySpan<char> pemData, out PemFields fields)
{
// Check for the minimum possible encoded length of a PEM structure
// and exit early if there is no way the input could contain a well-formed
// PEM.
if (pemData.Length < PreEBPrefix.Length + Ending.Length * 2 + PostEBPrefix.Length)
{
fields = default;
return false;
}
const int PostebStackBufferSize = 256;
Span<char> postebStackBuffer = stackalloc char[PostebStackBufferSize];
int areaOffset = 0;
int preebIndex;
while ((preebIndex = pemData.IndexOfByOffset(PreEBPrefix, areaOffset)) >= 0)
{
int labelStartIndex = preebIndex + PreEBPrefix.Length;
// If there are any previous characters, the one prior to the PreEB
// must be a white space character.
if (preebIndex > 0 && !IsWhiteSpaceCharacter(pemData[preebIndex - 1]))
{
areaOffset = labelStartIndex;
continue;
}
int preebEndIndex = pemData.IndexOfByOffset(Ending, labelStartIndex);
// There is no ending sequence, -----, in the remainder of
// the document. Therefore, there can never be a complete PreEB
// and we can exit.
if (preebEndIndex < 0)
{
fields = default;
return false;
}
Range labelRange = labelStartIndex..preebEndIndex;
ReadOnlySpan<char> label = pemData[labelRange];
// There could be a preeb that is valid after this one if it has an invalid
// label, so move from there.
if (!IsValidLabel(label))
{
goto NextAfterLabel;
}
int contentStartIndex = preebEndIndex + Ending.Length;
int postebLength = PostEBPrefix.Length + label.Length + Ending.Length;
Span<char> postebBuffer = postebLength > PostebStackBufferSize
? new char[postebLength]
: postebStackBuffer;
ReadOnlySpan<char> posteb = WritePostEB(label, postebBuffer);
int postebStartIndex = pemData.IndexOfByOffset(posteb, contentStartIndex);
if (postebStartIndex < 0)
{
goto NextAfterLabel;
}
int pemEndIndex = postebStartIndex + postebLength;
// The PostEB must either end at the end of the string, or
// have at least one white space character after it.
if (pemEndIndex < pemData.Length - 1 &&
!IsWhiteSpaceCharacter(pemData[pemEndIndex]))
{
goto NextAfterLabel;
}
Range contentRange = contentStartIndex..postebStartIndex;
if (!TryCountBase64(pemData[contentRange], out int base64start, out int base64end, out int decodedSize))
{
goto NextAfterLabel;
}
Range pemRange = preebIndex..pemEndIndex;
Range base64range = (contentStartIndex + base64start)..(contentStartIndex + base64end);
fields = new PemFields(labelRange, base64range, pemRange, decodedSize);
return true;
NextAfterLabel:
if (preebEndIndex <= areaOffset)
{
// We somehow ended up in a situation where we will advance
// backward or not at all, which means we'll probably end up here again,
// advancing backward, in a loop. To avoid getting stuck,
// detect this situation and return.
fields = default;
return false;
}
areaOffset = preebEndIndex;
}
fields = default;
return false;
static ReadOnlySpan<char> WritePostEB(ReadOnlySpan<char> label, Span<char> destination)
{
int size = PostEBPrefix.Length + label.Length + Ending.Length;
Debug.Assert(destination.Length >= size);
PostEBPrefix.CopyTo(destination);
label.CopyTo(destination.Slice(PostEBPrefix.Length));
Ending.CopyTo(destination.Slice(PostEBPrefix.Length + label.Length));
return destination.Slice(0, size);
}
}
private static int IndexOfByOffset(this ReadOnlySpan<char> str, ReadOnlySpan<char> value, int startPosition)
{
Debug.Assert(startPosition <= str.Length);
int index = str.Slice(startPosition).IndexOf(value);
return index == -1 ? -1 : index + startPosition;
}
private static bool IsValidLabel(ReadOnlySpan<char> data)
{
static bool IsLabelChar(char c) => (uint)(c - 0x21u) <= 0x5du && c != '-';
// Empty labels are permitted per RFC 7468.
if (data.IsEmpty)
return true;
// The first character must be a labelchar, so initialize to false
bool previousIsLabelChar = false;
for (int index = 0; index < data.Length; index++)
{
char c = data[index];
if (IsLabelChar(c))
{
previousIsLabelChar = true;
continue;
}
bool isSpaceOrHyphen = c == ' ' || c == '-';
// IETF RFC 7468 states that every character in a label must
// be a labelchar, and each labelchar may have zero or one
// preceding space or hyphen, except the first labelchar.
// If this character is not a space or hyphen, then this characer
// is invalid.
// If it is a space or hyphen, and the previous character was
// also not a labelchar (another hyphen or space), then we have
// two consecutive spaces or hyphens which is invalid.
if (!isSpaceOrHyphen || !previousIsLabelChar)
{
return false;
}
previousIsLabelChar = false;
}
// The last character must also be a labelchar. It cannot be a
// hyphen or space since these are only allowed to precede
// a labelchar.
return previousIsLabelChar;
}
private static bool TryCountBase64(
ReadOnlySpan<char> str,
out int base64Start,
out int base64End,
out int base64DecodedSize)
{
// Trim starting and ending allowed white space characters
int start = 0;
int end = str.Length - 1;
for (; start < str.Length && IsWhiteSpaceCharacter(str[start]); start++);
for (; end > start && IsWhiteSpaceCharacter(str[end]); end--);
// Validate that the remaining characters are valid base-64 encoded data.
if (Base64.IsValid(str.Slice(start, end + 1 - start), out base64DecodedSize))
{
base64Start = start;
base64End = end + 1;
return true;
}
base64Start = 0;
base64End = 0;
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsWhiteSpaceCharacter(char ch)
{
// Match white space characters from Convert.Base64
return ch is ' ' or '\t' or '\n' or '\r';
}
/// <summary>
/// Determines the length of a PEM-encoded value, in characters,
/// given the length of a label and binary data.
/// </summary>
/// <param name="labelLength">
/// The length of the label, in characters.
/// </param>
/// <param name="dataLength">
/// The length of the data, in bytes.
/// </param>
/// <returns>
/// The number of characters in the encoded PEM.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="labelLength"/> is a negative value.
/// <para>
/// -or-
/// </para>
/// <paramref name="dataLength"/> is a negative value.
/// <para>
/// -or-
/// </para>
/// <paramref name="labelLength"/> exceeds the maximum possible label length.
/// <para>
/// -or-
/// </para>
/// <paramref name="dataLength"/> exceeds the maximum possible encoded data length.
/// </exception>
/// <exception cref="ArgumentException">
/// The length of the PEM-encoded value is larger than <see cref="int.MaxValue"/>.
/// </exception>
public static int GetEncodedSize(int labelLength, int dataLength)
{
// The largest possible label is MaxLabelSize - when included in the posteb
// and preeb lines new lines, assuming the base64 content is empty.
// -----BEGIN {char * MaxLabelSize}-----\n
// -----END {char * MaxLabelSize}-----
const int MaxLabelSize = 1_073_741_808;
// The largest possible binary value to fit in a padded base64 string
// is 1,610,612,733 bytes. RFC 7468 states:
// Generators MUST wrap the base64-encoded lines so that each line
// consists of exactly 64 characters except for the final line
// We need to account for new line characters, every 64 characters.
// This works out to 1,585,834,053 maximum bytes in data when wrapping
// is accounted for assuming an empty label.
const int MaxDataLength = 1_585_834_053;
ArgumentOutOfRangeException.ThrowIfNegative(labelLength);
ArgumentOutOfRangeException.ThrowIfNegative(dataLength);
if (labelLength > MaxLabelSize)
throw new ArgumentOutOfRangeException(nameof(labelLength), SR.Argument_PemEncoding_EncodedSizeTooLarge);
if (dataLength > MaxDataLength)
throw new ArgumentOutOfRangeException(nameof(dataLength), SR.Argument_PemEncoding_EncodedSizeTooLarge);
int preebLength = PreEBPrefix.Length + labelLength + Ending.Length;
int postebLength = PostEBPrefix.Length + labelLength + Ending.Length;
int totalEncapLength = preebLength + postebLength + 1; //Add one for newline after preeb
// dataLength is already known to not overflow here
int encodedDataLength = ((dataLength + 2) / 3) << 2;
int lineCount = Math.DivRem(encodedDataLength, EncodedLineLength, out int remainder);
if (remainder > 0)
lineCount++;
int encodedDataLengthWithBreaks = encodedDataLength + lineCount;
if (int.MaxValue - encodedDataLengthWithBreaks < totalEncapLength)
throw new ArgumentException(SR.Argument_PemEncoding_EncodedSizeTooLarge);
return encodedDataLengthWithBreaks + totalEncapLength;
}
/// <summary>
/// Tries to write the provided data and label as PEM-encoded data into
/// a provided buffer.
/// </summary>
/// <param name="label">
/// The label to write.
/// </param>
/// <param name="data">
/// The data to write.
/// </param>
/// <param name="destination">
/// The buffer to receive the PEM-encoded text.
/// </param>
/// <param name="charsWritten">
/// When this method returns, this parameter contains the number of characters
/// written to <paramref name="destination"/>. This parameter is treated
/// as uninitialized.
/// </param>
/// <returns>
/// <c>true</c> if <paramref name="destination"/> is large enough to contain
/// the PEM-encoded text, otherwise <c>false</c>.
/// </returns>
/// <remarks>
/// This method always wraps the base-64 encoded text to 64 characters, per the
/// recommended wrapping of IETF RFC 7468. Unix-style line endings are used for line breaks.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="label"/> exceeds the maximum possible label length.
/// <para>
/// -or-
/// </para>
/// <paramref name="data"/> exceeds the maximum possible encoded data length.
/// </exception>
/// <exception cref="ArgumentException">
/// The resulting PEM-encoded text is larger than <see cref="int.MaxValue"/>.
/// <para>
/// - or -
/// </para>
/// <paramref name="label"/> contains invalid characters.
/// </exception>
public static bool TryWrite(ReadOnlySpan<char> label, ReadOnlySpan<byte> data, Span<char> destination, out int charsWritten)
{
if (!IsValidLabel(label))
throw new ArgumentException(SR.Argument_PemEncoding_InvalidLabel, nameof(label));
int encodedSize = GetEncodedSize(label.Length, data.Length);
if (destination.Length < encodedSize)
{
charsWritten = 0;
return false;
}
charsWritten = WriteCore(label, data, destination);
Debug.Assert(encodedSize == charsWritten);
return true;
}
private static int WriteCore(ReadOnlySpan<char> label, ReadOnlySpan<byte> data, Span<char> destination)
{
static int Write(ReadOnlySpan<char> str, Span<char> dest, int offset)
{
str.CopyTo(dest.Slice(offset));
return str.Length;
}
static int WriteBase64(ReadOnlySpan<byte> bytes, Span<char> dest, int offset)
{
bool success = Convert.TryToBase64Chars(bytes, dest.Slice(offset), out int base64Written);
if (!success)
{
Debug.Fail("Convert.TryToBase64Chars failed with a pre-sized buffer");
throw new ArgumentException(null, nameof(destination));
}
return base64Written;
}
const string NewLine = "\n";
const int BytesPerLine = 48;
int charsWritten = 0;
charsWritten += Write(PreEBPrefix, destination, charsWritten);
charsWritten += Write(label, destination, charsWritten);
charsWritten += Write(Ending, destination, charsWritten);
charsWritten += Write(NewLine, destination, charsWritten);
ReadOnlySpan<byte> remainingData = data;
while (remainingData.Length >= BytesPerLine)
{
charsWritten += WriteBase64(remainingData.Slice(0, BytesPerLine), destination, charsWritten);
charsWritten += Write(NewLine, destination, charsWritten);
remainingData = remainingData.Slice(BytesPerLine);
}
Debug.Assert(remainingData.Length < BytesPerLine);
if (remainingData.Length > 0)
{
charsWritten += WriteBase64(remainingData, destination, charsWritten);
charsWritten += Write(NewLine, destination, charsWritten);
}
charsWritten += Write(PostEBPrefix, destination, charsWritten);
charsWritten += Write(label, destination, charsWritten);
charsWritten += Write(Ending, destination, charsWritten);
return charsWritten;
}
/// <summary>
/// Creates an encoded PEM with the given label and data.
/// </summary>
/// <param name="label">
/// The label to encode.
/// </param>
/// <param name="data">
/// The data to encode.
/// </param>
/// <returns>
/// A character array of the encoded PEM.
/// </returns>
/// <remarks>
/// This method always wraps the base-64 encoded text to 64 characters, per the
/// recommended wrapping of RFC-7468. Unix-style line endings are used for line breaks.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="label"/> exceeds the maximum possible label length.
/// <para>
/// -or-
/// </para>
/// <paramref name="data"/> exceeds the maximum possible encoded data length.
/// </exception>
/// <exception cref="ArgumentException">
/// The resulting PEM-encoded text is larger than <see cref="int.MaxValue"/>.
/// <para>
/// - or -
/// </para>
/// <paramref name="label"/> contains invalid characters.
/// </exception>
public static char[] Write(ReadOnlySpan<char> label, ReadOnlySpan<byte> data)
{
if (!IsValidLabel(label))
throw new ArgumentException(SR.Argument_PemEncoding_InvalidLabel, nameof(label));
int encodedSize = GetEncodedSize(label.Length, data.Length);
char[] buffer = new char[encodedSize];
int charsWritten = WriteCore(label, data, buffer);
Debug.Assert(charsWritten == encodedSize);
return buffer;
}
/// <summary>
/// Creates an encoded PEM with the given label and data.
/// </summary>
/// <param name="label">
/// The label to encode.
/// </param>
/// <param name="data">
/// The data to encode.
/// </param>
/// <returns>
/// A string of the encoded PEM.
/// </returns>
/// <remarks>
/// This method always wraps the base-64 encoded text to 64 characters, per the
/// recommended wrapping of RFC-7468. Unix-style line endings are used for line breaks.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="label"/> exceeds the maximum possible label length.
///
/// -or-
///
/// <paramref name="data"/> exceeds the maximum possible encoded data length.
/// </exception>
/// <exception cref="ArgumentException">
/// The resulting PEM-encoded text is larger than <see cref="int.MaxValue"/>.
///
/// - or -
///
/// <paramref name="label"/> contains invalid characters.
/// </exception>
public static unsafe string WriteString(ReadOnlySpan<char> label, ReadOnlySpan<byte> data)
{
if (!IsValidLabel(label))
throw new ArgumentException(SR.Argument_PemEncoding_InvalidLabel, nameof(label));
int encodedSize = GetEncodedSize(label.Length, data.Length);
#pragma warning disable CS8500 // takes address of managed type
return string.Create(
encodedSize,
(LabelPointer: (IntPtr)(&label), DataPointer: (IntPtr)(&data)),
static (destination, state) =>
{
ReadOnlySpan<char> label = *(ReadOnlySpan<char>*)state.LabelPointer;
ReadOnlySpan<byte> data = *(ReadOnlySpan<byte>*)state.DataPointer;
int charsWritten = WriteCore(label, data, destination);
if (charsWritten != destination.Length)
{
Debug.Fail("WriteCore wrote the wrong amount of data");
throw new CryptographicException();
}
});
#pragma warning restore CS8500
}
}
}