Skip to content

Commit db4fb36

Browse files
committed
docs(keys): improved docs surrounding IPublicKey, IPrivateKeys and their encoders/docers
1 parent 0008093 commit db4fb36

File tree

6 files changed

+71
-24
lines changed

6 files changed

+71
-24
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AsnPrivateKeyDecoder.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,23 @@
1919

2020
namespace Yubico.YubiKey.Cryptography;
2121

22+
/// <summary>
23+
/// A class that converts ASN.1 DER encoded private keys to parameters and values.
24+
/// </summary>
2225
internal class AsnPrivateKeyDecoder
2326
{
27+
/// <summary>
28+
/// Creates an instance of <see cref="IPrivateKey"/> from a PKCS#8
29+
/// ASN.1 DER-encoded private key.
30+
/// </summary>
31+
/// <param name="pkcs8EncodedKey">
32+
/// The ASN.1 DER-encoded private key.
33+
/// </param>
34+
/// <returns>
35+
/// A new instance of <see cref="IPrivateKey"/>.
36+
/// </returns>
37+
/// <exception cref="CryptographicException">Thrown if privateKey does not match expected format.</exception>
38+
/// <exception cref="InvalidOperationException">Thrown if the algorithm is not supported</exception>
2439
public static IPrivateKey CreatePrivateKey(ReadOnlyMemory<byte> pkcs8EncodedKey)
2540
{
2641
var reader = new AsnReader(pkcs8EncodedKey, AsnEncodingRules.DER);
@@ -66,8 +81,25 @@ public static IPrivateKey CreatePrivateKey(ReadOnlyMemory<byte> pkcs8EncodedKey)
6681
ExceptionMessages.UnsupportedAlgorithm));
6782
}
6883

69-
public static Curve25519PrivateKey CreateCurve25519Key(ReadOnlyMemory<byte> pkcs8EncodedKey) =>
70-
Curve25519PrivateKey.CreateFromPkcs8(pkcs8EncodedKey);
84+
/// <summary>
85+
/// Creates an instance of <see cref="Curve25519PrivateKey"/> from a PKCS#8
86+
/// ASN.1 DER-encoded private key.
87+
/// </summary>
88+
/// <param name="pkcs8EncodedKey">
89+
/// The ASN.1 DER-encoded private key.
90+
/// </param>
91+
/// <returns>
92+
/// A new instance of <see cref="Curve25519PrivateKey"/>.
93+
/// </returns>
94+
/// <exception cref="CryptographicException">Thrown if privateKey does not match expected format.</exception>
95+
/// <exception cref="ArgumentException">Thrown if the algorithm is not <see cref="Oids.X25519"/> or
96+
/// <see cref="Oids.Ed25519"/></exception>
97+
public static Curve25519PrivateKey CreateCurve25519Key(ReadOnlyMemory<byte> pkcs8EncodedKey)
98+
{
99+
(byte[] privateKey, var keyType) = GetCurve25519PrivateKeyData(pkcs8EncodedKey);
100+
using var privateKeyHandle = new ZeroingMemoryHandle(privateKey);
101+
return Curve25519PrivateKey.CreateFromValue(privateKeyHandle.Data, keyType);
102+
}
71103

72104
public static (byte[] privateKey, KeyType keyType) GetCurve25519PrivateKeyData(ReadOnlyMemory<byte> pkcs8EncodedKey)
73105
{

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AsnPublicKeyDecoder.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,26 @@
1919

2020
namespace Yubico.YubiKey.Cryptography;
2121

22+
/// <summary>
23+
/// A class that converts ASN.1 DER encoded X.509 SubjectPublicKeyInfo to instances of IPublicKey.
24+
/// </summary>
2225
internal class AsnPublicKeyDecoder
2326
{
24-
public static IPublicKey CreatePublicKey(ReadOnlyMemory<byte> pkcs8EncodedKey)
27+
/// <summary>
28+
/// Creates an instance of <see cref="IPublicKey"/> from a
29+
/// ASN.1 DER-encoded SubjectPublicKeyInfo.
30+
/// </summary>
31+
/// <param name="subjectPublicKeyInfo">
32+
/// The ASN.1 DER-encoded SubjectPublicKeyInfo.
33+
/// </param>
34+
/// <returns>
35+
/// A new instance of <see cref="IPublicKey"/>.
36+
/// </returns>
37+
/// <exception cref="CryptographicException">Thrown if public key does not match expected format.</exception>
38+
/// <exception cref="InvalidOperationException">Thrown if the algorithm is not supported</exception>
39+
public static IPublicKey CreatePublicKey(ReadOnlyMemory<byte> subjectPublicKeyInfo)
2540
{
26-
var reader = new AsnReader(pkcs8EncodedKey, AsnEncodingRules.DER);
41+
var reader = new AsnReader(subjectPublicKeyInfo, AsnEncodingRules.DER);
2742
var seqSubjectPublicKeyInfo = reader.ReadSequence();
2843
var seqAlgorithmIdentifier = seqSubjectPublicKeyInfo.ReadSequence();
2944

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/Curve25519PrivateKey.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ public override byte[] ExportPkcs8PrivateKey()
7878

7979
/// <summary>
8080
/// Creates an instance of <see cref="Curve25519PrivateKey"/> from a PKCS#8
81-
/// DER-encoded private key.
81+
/// ASN.1 DER-encoded private key.
8282
/// </summary>
8383
/// <param name="pkcs8EncodedKey">
84-
/// The DER-encoded private key.
84+
/// The ASN.1 DER-encoded private key.
8585
/// </param>
8686
/// <returns>
8787
/// A new instance of <see cref="Curve25519PrivateKey"/>.

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/Curve25519PublicKey.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ public override byte[] ExportSubjectPublicKeyInfo() =>
7070
AsnPublicKeyEncoder.EncodeToSubjectPublicKeyInfo(_publicPoint, KeyDefinition.KeyType);
7171

7272
/// <summary>
73-
/// Creates a new instance of <see cref="Curve25519PublicKey"/> from a DER-encoded public key.
73+
/// Creates a new instance of <see cref="Curve25519PublicKey"/> from a DER-encoded SubjectPublicKeyInfo.
7474
/// </summary>
7575
/// <param name="subjectPublicKeyInfo">
76-
/// The DER-encoded public key.
76+
/// The DER-encoded SubjectPublicKeyInfo.
7777
/// </param>
7878
/// <returns>
7979
/// A new instance of <see cref="Curve25519PublicKey"/>.
8080
/// </returns>
8181
/// <exception cref="CryptographicException">
82-
/// Thrown if the public key is invalid.
82+
/// Thrown if the subjectPublicKeyInfo is invalid.
8383
/// </exception>
8484
public static Curve25519PublicKey CreateFromSubjectPublicKeyInfo(ReadOnlyMemory<byte> subjectPublicKeyInfo) =>
8585
AsnPublicKeyDecoder
@@ -111,6 +111,6 @@ public static Curve25519PublicKey CreateFromValue(ReadOnlyMemory<byte> publicPoi
111111
}
112112

113113
[Obsolete("Use CreateFromSubjectPublicKeyInfo instead", false)]
114-
public static Curve25519PublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
115-
CreateFromSubjectPublicKeyInfo(encodedKey);
114+
public static Curve25519PublicKey CreateFromPkcs8(ReadOnlyMemory<byte> subjectPublicKeyInfo) =>
115+
CreateFromSubjectPublicKeyInfo(subjectPublicKeyInfo);
116116
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/ECPublicKey.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,19 @@ public static ECPublicKey CreateFromValue(ReadOnlyMemory<byte> publicPoint, KeyT
141141
}
142142

143143
/// <summary>
144-
/// Creates an instance of <see cref="ECPublicKey"/> from a DER-encoded public key.
144+
/// Creates an instance of <see cref="ECPublicKey"/> from a DER-encoded SubjectPublicKeyInfo.
145145
/// </summary>
146-
/// <param name="encodedKey">The DER-encoded public key.</param>
146+
/// <param name="subjectPublicKeyInfo">The DER-encoded SubjectPublicKeyInfo.</param>
147147
/// <returns>An instance of <see cref="IPublicKey"/>.</returns>
148148
/// <exception cref="CryptographicException">
149-
/// Thrown if the public key is invalid.
149+
/// Thrown if the subjectPublicKeyInfo is invalid.
150150
/// </exception>
151-
public static ECPublicKey CreateFromSubjectPublicKeyInfo(ReadOnlyMemory<byte> encodedKey) =>
151+
public static ECPublicKey CreateFromSubjectPublicKeyInfo(ReadOnlyMemory<byte> subjectPublicKeyInfo) =>
152152
AsnPublicKeyDecoder
153-
.CreatePublicKey(encodedKey)
153+
.CreatePublicKey(subjectPublicKeyInfo)
154154
.Cast<ECPublicKey>();
155155

156156
[Obsolete("Use CreateFromSubjectPublicKeyInfo instead", false)]
157-
public static ECPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
158-
CreateFromSubjectPublicKeyInfo(encodedKey);
157+
public static ECPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> subjectPublicKeyInfo) =>
158+
CreateFromSubjectPublicKeyInfo(subjectPublicKeyInfo);
159159
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/RSAPublicKey.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,20 @@ public static RSAPublicKey CreateFromParameters(RSAParameters parameters)
9797
}
9898

9999
/// <summary>
100-
/// Creates a new instance of <see cref="IPublicKey"/> from a DER-encoded public key.
100+
/// Creates a new instance of <see cref="IPublicKey"/> from ASN.1 DER-encoded SubjectPublicKeyInfo.
101101
/// </summary>
102-
/// <param name="encodedKey">The DER-encoded public key.</param>
102+
/// <param name="subjectPublicKeyInfo">The DER-encoded SubjectPublicKeyInfo.</param>
103103
/// <returns>A new instance of <see cref="IPublicKey"/>.</returns>
104104
/// <exception cref="CryptographicException">
105105
/// Thrown if the public key is invalid.
106106
/// </exception>
107-
public static RSAPublicKey CreateFromSubjectPublicKeyInfo(ReadOnlyMemory<byte> encodedKey) =>
107+
public static RSAPublicKey CreateFromSubjectPublicKeyInfo(ReadOnlyMemory<byte> subjectPublicKeyInfo) =>
108108
AsnPublicKeyDecoder
109-
.CreatePublicKey(encodedKey)
109+
.CreatePublicKey(subjectPublicKeyInfo)
110110
.Cast<RSAPublicKey>();
111111

112112

113113
[Obsolete("Use CreateFromSubjectPublicKeyInfo instead", false)]
114-
public static RSAPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
115-
CreateFromSubjectPublicKeyInfo(encodedKey);
114+
public static RSAPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> subjectPublicKeyInfo) =>
115+
CreateFromSubjectPublicKeyInfo(subjectPublicKeyInfo);
116116
}

0 commit comments

Comments
 (0)