Skip to content

Commit

Permalink
Create one-shot PEM reader and writer
Browse files Browse the repository at this point in the history
The reader and writer use the restricted form of PEM from IETF RFC 7468.

The reader uses the `laxtextualmsg` form, with a restricted subset of allowed whitespace (no vertical tab or form feed).

The writer writes according to the `stricttextualmsg` form.
  • Loading branch information
vcsjones committed Mar 22, 2020
1 parent 24c4cb1 commit 11c8c80
Show file tree
Hide file tree
Showing 9 changed files with 1,392 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public static void AtLeastOneEquals<T>(T expected1, T expected2, T value)
public delegate void AssertThrowsAction<T>(Span<T> span);

// Cannot use standard Assert.Throws() when testing Span - Span and closures don't get along.
public static void AssertThrows<E, T>(ReadOnlySpan<T> span, AssertThrowsActionReadOnly<T> action) where E : Exception
public static E AssertThrows<E, T>(ReadOnlySpan<T> span, AssertThrowsActionReadOnly<T> action) where E : Exception
{
Exception exception;

Expand All @@ -404,18 +404,18 @@ public static void AtLeastOneEquals<T>(T expected1, T expected2, T value)
exception = ex;
}

if (exception == null)
switch(exception)
{
throw new ThrowsException(typeof(E));
}

if (exception.GetType() != typeof(E))
{
throw new ThrowsException(typeof(E), exception);
case null:
throw new ThrowsException(typeof(E));
case E ex when (ex.GetType() == typeof(E)):
return ex;
default:
throw new ThrowsException(typeof(E), exception);
}
}

public static void AssertThrows<E, T>(Span<T> span, AssertThrowsAction<T> action) where E : Exception
public static E AssertThrows<E, T>(Span<T> span, AssertThrowsAction<T> action) where E : Exception
{
Exception exception;

Expand All @@ -429,15 +429,31 @@ public static void AtLeastOneEquals<T>(T expected1, T expected2, T value)
exception = ex;
}

if (exception == null)
switch(exception)
{
throw new ThrowsException(typeof(E));
case null:
throw new ThrowsException(typeof(E));
case E ex when (ex.GetType() == typeof(E)):
return ex;
default:
throw new ThrowsException(typeof(E), exception);
}
}

if (exception.GetType() != typeof(E))
{
throw new ThrowsException(typeof(E), exception);
}
public static E Throws<E, T>(string expectedParamName, ReadOnlySpan<T> span, AssertThrowsActionReadOnly<T> action)
where E : ArgumentException
{
E exception = AssertThrows<E, T>(span, action);
Assert.Equal(expectedParamName, exception.ParamName);
return exception;
}

public static E Throws<E, T>(string expectedParamName, Span<T> span, AssertThrowsAction<T> action)
where E : ArgumentException
{
E exception = AssertThrows<E, T>(span, action);
Assert.Equal(expectedParamName, exception.ParamName);
return exception;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ public enum OidGroup
Template = 9,
KeyDerivationFunction = 10,
}
public static partial class PemEncoding
{
public static System.Security.Cryptography.PemFields Find(System.ReadOnlySpan<char> pemData) { throw null; }
public static int GetEncodedSize(int labelLength, int dataLength) { throw null; }
public static bool TryFind(System.ReadOnlySpan<char> pemData, out System.Security.Cryptography.PemFields fields) { throw null; }
public static bool TryWrite(System.ReadOnlySpan<char> label, System.ReadOnlySpan<byte> data, System.Span<char> destination, out int charsWritten) { throw null; }
public static char[] Write(System.ReadOnlySpan<char> label, System.ReadOnlySpan<byte> data) { throw null; }
}
public readonly partial struct PemFields
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public System.Range Base64Data { get { throw null; } }
public int DecodedDataLength { get { throw null; } }
public System.Range Label { get { throw null; } }
public System.Range Location { get { throw null; } }
}
public partial class ToBase64Transform : System.IDisposable, System.Security.Cryptography.ICryptoTransform
{
public ToBase64Transform() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@
<data name="Argument_InvalidValue" xml:space="preserve">
<value>Value was invalid.</value>
</data>
<data name="Argument_PemEncoding_NoPemFound" xml:space="preserve">
<value>No PEM encoded data found.</value>
</data>
<data name="Argument_PemEncoding_InvalidLabel" xml:space="preserve">
<value>The specified label is not valid.</value>
</data>
<data name="Argument_PemEncoding_EncodedSizeTooLarge" xml:space="preserve">
<value>The encoded PEM size is too large to represent as a signed 32-bit integer.</value>
</data>
<data name="ArgumentOutOfRange_NeedPositiveNumber" xml:space="preserve">
<value>A positive number is required.</value>
</data>
<data name="ObjectDisposed_Generic" xml:space="preserve">
<value>Cannot access a disposed object.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<Compile Include="System\Security\Cryptography\OidCollection.cs" />
<Compile Include="System\Security\Cryptography\OidEnumerator.cs" />
<Compile Include="System\Security\Cryptography\OidGroup.cs" />
<Compile Include="System\Security\Cryptography\PemFields.cs" />
<Compile Include="System\Security\Cryptography\PemEncoding.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\Helpers.cs">
<Link>Internal\Cryptography\Helpers.cs</Link>
</Compile>
Expand Down Expand Up @@ -147,4 +149,4 @@
<Reference Include="System.Runtime.Numerics" />
<Reference Include="System.Threading" />
</ItemGroup>
</Project>
</Project>
Loading

0 comments on commit 11c8c80

Please sign in to comment.