Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import X509 certificate and collections from PEM. #38280

Merged
merged 10 commits into from
Jun 24, 2020
60 changes: 60 additions & 0 deletions src/libraries/Common/src/Internal/Cryptography/PemEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Security.Cryptography;

namespace Internal.Cryptography
{
internal readonly ref struct PemEnumerator
{
private readonly ReadOnlySpan<char> _contents;

public PemEnumerator(ReadOnlySpan<char> contents)
{
_contents = contents;
}

public Enumerator GetEnumerator() => new Enumerator(_contents);

internal ref struct Enumerator
{
private ReadOnlySpan<char> _contents;
private PemFields _pemFields;

public Enumerator(ReadOnlySpan<char> contents)
{
_contents = contents;
_pemFields = default;
}

public PemFieldItem Current => new PemFieldItem(_contents, _pemFields);

public bool MoveNext()
{
_contents = _contents[_pemFields.Location.End..];
return PemEncoding.TryFind(_contents, out _pemFields);
}

internal readonly ref struct PemFieldItem
{
private readonly ReadOnlySpan<char> _contents;
private readonly PemFields _pemFields;

public PemFieldItem(ReadOnlySpan<char> contents, PemFields pemFields)
{
_contents = contents;
_pemFields = pemFields;
}

public void Deconstruct(out ReadOnlySpan<char> contents, out PemFields pemFields)
{
contents = _contents;
pemFields = _pemFields;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ internal static class PemLabels
internal const string RsaPublicKey = "RSA PUBLIC KEY";
internal const string RsaPrivateKey = "RSA PRIVATE KEY";
internal const string EcPrivateKey = "EC PRIVATE KEY";
internal const string X509Certificate = "CERTIFICATE";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public X509Certificate2(string fileName, string? password, System.Security.Crypt
public System.Security.Cryptography.X509Certificates.X500DistinguishedName SubjectName { get { throw null; } }
public string Thumbprint { get { throw null; } }
public int Version { get { throw null; } }
public static System.Security.Cryptography.X509Certificates.X509Certificate2 CreateFromEncryptedPem(System.ReadOnlySpan<char> certPem, System.ReadOnlySpan<char> keyPem, System.ReadOnlySpan<char> password) { throw null; }
public static System.Security.Cryptography.X509Certificates.X509Certificate2 CreateFromEncryptedPemFile(string certPemFilePath, System.ReadOnlySpan<char> password, string? keyPemFilePath = null) { throw null; }
public static System.Security.Cryptography.X509Certificates.X509Certificate2 CreateFromPem(System.ReadOnlySpan<char> certPem, System.ReadOnlySpan<char> keyPem) { throw null; }
public static System.Security.Cryptography.X509Certificates.X509Certificate2 CreateFromPemFile(string certPemFilePath, string? keyPemFilePath = null) { throw null; }
public static System.Security.Cryptography.X509Certificates.X509ContentType GetCertContentType(byte[] rawData) { throw null; }
public static System.Security.Cryptography.X509Certificates.X509ContentType GetCertContentType(string fileName) { throw null; }
public string GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType nameType, bool forIssuer) { throw null; }
Expand Down Expand Up @@ -269,6 +273,8 @@ public void Import(byte[] rawData) { }
public void Import(byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) { }
public void Import(string fileName) { }
public void Import(string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) { }
public void ImportFromPem(System.ReadOnlySpan<char> certPem) { }
public void ImportFromPemFile(string certPemFilePath) { }
public void Insert(int index, System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) { }
public void Remove(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) { }
public void RemoveRange(System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@
<data name="Cryptography_X509_StoreCannotCreate" xml:space="preserve">
<value>The platform does not have a definition for an X509 certificate store named '{0}' with a StoreLocation of '{1}', and does not support creating it.</value>
</data>
<data name="Cryptography_X509_NoPemCertificate" xml:space="preserve">
<value>The certificate contents do not contain a PEM with a CERTIFICATE label, or the content is malformed.</value>
</data>
<data name="Cryptography_X509_NoOrMismatchedPemKey" xml:space="preserve">
<value>The key contents do not contain a PEM, the content is malformed, or the key does not match the certificate algorithm.</value>
</data>
<data name="InvalidOperation_EnumNotStarted" xml:space="preserve">
<value>Enumeration has not started. Call MoveNext.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
Link="Common\System\Security\Cryptography\KeyBlobHelpers.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\KeySizeHelpers.cs"
Link="Common\System\Security\Cryptography\KeySizeHelpers.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\PemEnumerator.cs"
Link="Common\Internal\Cryptography\PemEnumerator.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\PemLabels.cs"
Link="Common\System\Security\Cryptography\PemLabels.cs" />
<AsnXml Include="$(CommonPath)System\Security\Cryptography\Asn1\AlgorithmIdentifierAsn.xml">
<Link>Common\System\Security\Cryptography\Asn1\AlgorithmIdentifierAsn.xml</Link>
</AsnXml>
Expand Down Expand Up @@ -679,6 +683,6 @@
<Reference Include="System.Console" Condition="'$(Configuration)' == 'Debug'" />
</ItemGroup>
<ItemGroup>
<None Include="@(AsnXml)" />
<None Include="@(AsnXml)" />
</ItemGroup>
</Project>
Loading