Skip to content

Commit acee9d4

Browse files
committed
misc: name change of KeyDefinitions.cs fields
1 parent 9c3c41d commit acee9d4

26 files changed

+243
-160
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AsnPrivateKeyReader.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static IPrivateKeyParameters CreateKeyParameters(ReadOnlyMemory<byte> pkc
3838
string oidAlgorithm = seqAlgorithmIdentifier.ReadObjectIdentifier();
3939
switch (oidAlgorithm)
4040
{
41-
case KeyDefinitions.CryptoOids.RSA:
41+
case KeyDefinitions.Oids.RSA:
4242
{
4343
if (seqAlgorithmIdentifier.HasData)
4444
{
@@ -49,13 +49,13 @@ public static IPrivateKeyParameters CreateKeyParameters(ReadOnlyMemory<byte> pkc
4949
var rsaParameters = CreateRSAParameters(pkcs8EncodedKey);
5050
return RSAPrivateKeyParameters.CreateFromParameters(rsaParameters);
5151
}
52-
case KeyDefinitions.CryptoOids.ECDSA:
52+
case KeyDefinitions.Oids.ECDSA:
5353
{
5454
var ecParams = CreateECParameters(pkcs8EncodedKey);
5555
return ECPrivateKeyParameters.CreateFromParameters(ecParams);
5656
}
57-
case KeyDefinitions.CryptoOids.X25519:
58-
case KeyDefinitions.CryptoOids.Ed25519:
57+
case KeyDefinitions.Oids.X25519:
58+
case KeyDefinitions.Oids.Ed25519:
5959
{
6060
return Curve25519PrivateKeyParameters.CreateFromPkcs8(pkcs8EncodedKey);
6161
}
@@ -84,7 +84,7 @@ public static ECParameters CreateECParameters(ReadOnlyMemory<byte> pkcs8EncodedK
8484

8585
var seqAlgorithmIdentifier = seqPrivateKeyInfo.ReadSequence();
8686
string oidAlgorithm = seqAlgorithmIdentifier.ReadObjectIdentifier();
87-
if (oidAlgorithm != KeyDefinitions.CryptoOids.ECDSA)
87+
if (oidAlgorithm != KeyDefinitions.Oids.ECDSA)
8888
{
8989
throw new InvalidOperationException(
9090
string.Format(
@@ -94,9 +94,9 @@ public static ECParameters CreateECParameters(ReadOnlyMemory<byte> pkcs8EncodedK
9494

9595
string curveOid = seqAlgorithmIdentifier.ReadObjectIdentifier();
9696
if (curveOid is not (
97-
KeyDefinitions.CryptoOids.P256 or
98-
KeyDefinitions.CryptoOids.P384 or
99-
KeyDefinitions.CryptoOids.P521))
97+
KeyDefinitions.Oids.P256 or
98+
KeyDefinitions.Oids.P384 or
99+
KeyDefinitions.Oids.P521))
100100
{
101101
throw new InvalidOperationException(
102102
string.Format(
@@ -186,7 +186,7 @@ public static RSAParameters CreateRSAParameters(ReadOnlyMemory<byte> pkcs8Encode
186186

187187
var seqAlgorithmIdentifier = seqPrivateKeyInfo.ReadSequence();
188188
string oidAlgorithm = seqAlgorithmIdentifier.ReadObjectIdentifier();
189-
if (oidAlgorithm != KeyDefinitions.CryptoOids.RSA)
189+
if (oidAlgorithm != KeyDefinitions.Oids.RSA)
190190
{
191191
throw new InvalidOperationException(
192192
string.Format(

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AsnPrivateKeyWriter.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public static byte[] EncodeToPkcs8(
3737
{
3838
return keyType switch
3939
{
40-
KeyType.P256 => EncodeECKey(privateKey, KeyDefinitions.CryptoOids.P256, publicPoint),
41-
KeyType.P384 => EncodeECKey(privateKey, KeyDefinitions.CryptoOids.P384, publicPoint),
42-
KeyType.P521 => EncodeECKey(privateKey, KeyDefinitions.CryptoOids.P521, publicPoint),
43-
KeyType.X25519 => EncodeCurve25519Key(privateKey.Span, KeyDefinitions.CryptoOids.X25519),
44-
KeyType.Ed25519 => EncodeCurve25519Key(privateKey.Span, KeyDefinitions.CryptoOids.Ed25519),
40+
KeyType.P256 => EncodeECKey(privateKey, KeyDefinitions.Oids.P256, publicPoint),
41+
KeyType.P384 => EncodeECKey(privateKey, KeyDefinitions.Oids.P384, publicPoint),
42+
KeyType.P521 => EncodeECKey(privateKey, KeyDefinitions.Oids.P521, publicPoint),
43+
KeyType.X25519 => EncodeCurve25519Key(privateKey.Span, KeyDefinitions.Oids.X25519),
44+
KeyType.Ed25519 => EncodeCurve25519Key(privateKey.Span, KeyDefinitions.Oids.Ed25519),
4545
_ => throw new NotSupportedException($"Key type {keyType} is not supported for encoding.")
4646
};
4747
}
@@ -100,7 +100,7 @@ public static byte[] EncodeToPkcs8(RSAParameters parameters)
100100
writer.WriteInteger(0);
101101

102102
_ = writer.PushSequence();
103-
writer.WriteObjectIdentifier(KeyDefinitions.CryptoOids.RSA);
103+
writer.WriteObjectIdentifier(KeyDefinitions.Oids.RSA);
104104
writer.WriteNull();
105105
writer.PopSequence();
106106

@@ -185,7 +185,7 @@ private static byte[] EncodeECKey(
185185

186186
// Algorithm Identifier SEQUENCE
187187
_ = writer.PushSequence();
188-
writer.WriteObjectIdentifier(KeyDefinitions.CryptoOids.ECDSA);
188+
writer.WriteObjectIdentifier(KeyDefinitions.Oids.ECDSA);
189189
writer.WriteObjectIdentifier(curveOid);
190190
writer.PopSequence();
191191

@@ -208,12 +208,12 @@ private static byte[] EncodeCurve25519Key(ReadOnlySpan<byte> privateKey, string
208208
throw new ArgumentException("Curve OID is null.");
209209
}
210210

211-
if (curveOid is not (KeyDefinitions.CryptoOids.X25519 or KeyDefinitions.CryptoOids.Ed25519))
211+
if (curveOid is not (KeyDefinitions.Oids.X25519 or KeyDefinitions.Oids.Ed25519))
212212
{
213213
throw new ArgumentException("Curve OID is not supported.", nameof(curveOid));
214214
}
215215

216-
if (curveOid == KeyDefinitions.CryptoOids.X25519)
216+
if (curveOid == KeyDefinitions.Oids.X25519)
217217
{
218218
AsnUtilities.VerifyX25519PrivateKey(privateKey);
219219
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AsnPublicKeyReader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IPublicKeyParameters CreateKeyParameters(ReadOnlyMemory<byte> pkcs
3636

3737
switch (oidAlgorithm)
3838
{
39-
case KeyDefinitions.CryptoOids.RSA:
39+
case KeyDefinitions.Oids.RSA:
4040
{
4141
if (seqAlgorithmIdentifier.HasData)
4242
{
@@ -46,16 +46,16 @@ public static IPublicKeyParameters CreateKeyParameters(ReadOnlyMemory<byte> pkcs
4646

4747
return CreateRSAPublicKeyParameters(subjectPublicKey);
4848
}
49-
case KeyDefinitions.CryptoOids.ECDSA:
49+
case KeyDefinitions.Oids.ECDSA:
5050
{
5151
string oidCurve = seqAlgorithmIdentifier.ReadObjectIdentifier();
5252
return CreateECPublicKeyParameters(oidCurve, subjectPublicKey);
5353
}
54-
case KeyDefinitions.CryptoOids.X25519:
54+
case KeyDefinitions.Oids.X25519:
5555
{
5656
return Curve25519PublicKeyParameters.CreateFromValue(subjectPublicKey, KeyType.X25519);
5757
}
58-
case KeyDefinitions.CryptoOids.Ed25519:
58+
case KeyDefinitions.Oids.Ed25519:
5959
{
6060
return Curve25519PublicKeyParameters.CreateFromValue(subjectPublicKey, KeyType.Ed25519);
6161
}
@@ -89,8 +89,8 @@ private static RSAPublicKeyParameters CreateRSAPublicKeyParameters(byte[] subjec
8989

9090
private static ECPublicKeyParameters CreateECPublicKeyParameters(string curveOid, byte[] subjectPublicKey)
9191
{
92-
if (curveOid is not (KeyDefinitions.CryptoOids.P256 or KeyDefinitions.CryptoOids.P384
93-
or KeyDefinitions.CryptoOids.P521))
92+
if (curveOid is not (KeyDefinitions.Oids.P256 or KeyDefinitions.Oids.P384
93+
or KeyDefinitions.Oids.P521))
9494
{
9595
throw new NotSupportedException(
9696
string.Format(

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AsnPublicKeyWriter.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public static byte[] EncodeToSubjectPublicKeyInfo(ReadOnlyMemory<byte> publicPoi
3535
int coordinateLength = keyDefinition.LengthInBytes;
3636
return keyType switch
3737
{
38-
KeyType.P256 => CreateEcEncodedKey(publicPoint, KeyDefinitions.CryptoOids.P256, coordinateLength),
39-
KeyType.P384 => CreateEcEncodedKey(publicPoint, KeyDefinitions.CryptoOids.P384, coordinateLength),
40-
KeyType.P521 => CreateEcEncodedKey(publicPoint, KeyDefinitions.CryptoOids.P521, coordinateLength),
38+
KeyType.P256 => CreateEcEncodedKey(publicPoint, KeyDefinitions.Oids.P256, coordinateLength),
39+
KeyType.P384 => CreateEcEncodedKey(publicPoint, KeyDefinitions.Oids.P384, coordinateLength),
40+
KeyType.P521 => CreateEcEncodedKey(publicPoint, KeyDefinitions.Oids.P521, coordinateLength),
4141
KeyType.X25519 => CreateCurve25519ToSpki(publicPoint, keyType),
4242
KeyType.Ed25519 => CreateCurve25519ToSpki(publicPoint, keyType),
4343
_ => throw new NotSupportedException($"Key type {keyType} is not supported for encoding.")
@@ -83,7 +83,7 @@ public static byte[] EncodeToSubjectPublicKeyInfo(
8383

8484
// Algorithm Identifier SEQUENCE
8585
_ = writer.PushSequence();
86-
writer.WriteObjectIdentifier(KeyDefinitions.CryptoOids.RSA);
86+
writer.WriteObjectIdentifier(KeyDefinitions.Oids.RSA);
8787
writer.WriteNull();
8888
writer.PopSequence();
8989

@@ -158,7 +158,7 @@ public static byte[] EncodeToSubjectPublicKeyInfo(ECParameters parameters)
158158

159159
// Algorithm Identifier SEQUENCE
160160
_ = writer.PushSequence();
161-
writer.WriteObjectIdentifier(KeyDefinitions.CryptoOids.ECDSA);
161+
writer.WriteObjectIdentifier(KeyDefinitions.Oids.ECDSA);
162162
writer.WriteObjectIdentifier(curveOid);
163163
writer.PopSequence();
164164

@@ -179,8 +179,8 @@ private static byte[] CreateCurve25519ToSpki(ReadOnlyMemory<byte> publicKey, Key
179179
throw new ArgumentException("Curve OID is null.");
180180
}
181181

182-
if (keyDefinition.AlgorithmOid != KeyDefinitions.CryptoOids.X25519 &&
183-
keyDefinition.AlgorithmOid != KeyDefinitions.CryptoOids.Ed25519)
182+
if (keyDefinition.AlgorithmOid != KeyDefinitions.Oids.X25519 &&
183+
keyDefinition.AlgorithmOid != KeyDefinitions.Oids.Ed25519)
184184
{
185185
throw new ArgumentException("Invalid curve OID.");
186186
}
@@ -233,7 +233,7 @@ private static byte[] CreateEcEncodedKey(ReadOnlyMemory<byte> publicPoint, strin
233233

234234
// Algorithm Identifier SEQUENCE
235235
_ = writer.PushSequence();
236-
writer.WriteObjectIdentifier(KeyDefinitions.CryptoOids.ECDSA);
236+
writer.WriteObjectIdentifier(KeyDefinitions.Oids.ECDSA);
237237
writer.WriteObjectIdentifier(curveOid);
238238
writer.PopSequence();
239239

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/Curve25519PrivateKeyParameters.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private Curve25519PrivateKeyParameters(
3131
KeyType keyType)
3232
{
3333
var keyDefinition = keyType.GetKeyDefinition();
34-
if (keyDefinition.AlgorithmOid == KeyDefinitions.CryptoOids.X25519)
34+
if (keyDefinition.AlgorithmOid == KeyDefinitions.Oids.X25519)
3535
{
3636
AsnUtilities.VerifyX25519PrivateKey(privateKey.Span);
3737
}
@@ -84,12 +84,12 @@ public static Curve25519PrivateKeyParameters CreateFromPkcs8(ReadOnlyMemory<byte
8484

8585
var seqAlgorithmIdentifier = seqPrivateKeyInfo.ReadSequence();
8686
string algorithmOid = seqAlgorithmIdentifier.ReadObjectIdentifier();
87-
if (algorithmOid != KeyDefinitions.CryptoOids.X25519 &&
88-
algorithmOid != KeyDefinitions.CryptoOids.Ed25519)
87+
if (algorithmOid != KeyDefinitions.Oids.X25519 &&
88+
algorithmOid != KeyDefinitions.Oids.Ed25519)
8989
{
9090
throw new ArgumentException(
91-
"Invalid curve OID. Must be: " + KeyDefinitions.CryptoOids.X25519 + " or " +
92-
KeyDefinitions.CryptoOids.Ed25519);
91+
"Invalid curve OID. Must be: " + KeyDefinitions.Oids.X25519 + " or " +
92+
KeyDefinitions.Oids.Ed25519);
9393
}
9494

9595
using var privateKeyDataHandle = new ZeroingMemoryHandle(seqPrivateKeyInfo.ReadOctetString());

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/ECPrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static ECPrivateKeyParameters CreateFromValue(
120120
KeyType keyType)
121121
{
122122
var keyDefinition = keyType.GetKeyDefinition();
123-
if (keyDefinition.AlgorithmOid is not KeyDefinitions.CryptoOids.ECDSA)
123+
if (keyDefinition.AlgorithmOid is not KeyDefinitions.Oids.ECDSA)
124124
{
125125
throw new ArgumentException("Only P-256, P-384 and P-521 are supported.", nameof(keyType));
126126
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/ECPublicKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public ECPublicKeyParameters(ECDsa ecdsa)
116116
public static IPublicKeyParameters CreateFromValue(ReadOnlyMemory<byte> publicPoint, KeyType keyType)
117117
{
118118
var keyDefinition = KeyDefinitions.GetByKeyType(keyType);
119-
if (keyDefinition.AlgorithmOid is not KeyDefinitions.CryptoOids.ECDSA)
119+
if (keyDefinition.AlgorithmOid is not KeyDefinitions.Oids.ECDSA)
120120
{
121121
throw new ArgumentException("Only P-256, P-384 and P-521 are supported.", nameof(keyType));
122122
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/EcdsaVerify.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,15 @@ private static string GetOidByLength(int encodedPointLength)
349349
{
350350
if (encodedPointLength == (KeyDefinitions.P256.LengthInBytes * 2) + 1)
351351
{
352-
return KeyDefinitions.CryptoOids.P256;
352+
return KeyDefinitions.Oids.P256;
353353
}
354354
if (encodedPointLength == (KeyDefinitions.P384.LengthInBytes * 2) + 1)
355355
{
356-
return KeyDefinitions.CryptoOids.P384;
356+
return KeyDefinitions.Oids.P384;
357357
}
358358
if (encodedPointLength == (KeyDefinitions.P521.LengthInBytes * 2) + 1)
359359
{
360-
return KeyDefinitions.CryptoOids.P521;
360+
return KeyDefinitions.Oids.P521;
361361
}
362362

363363
throw new ArgumentException(ExceptionMessages.UnsupportedAlgorithm);
@@ -458,10 +458,10 @@ private static string GetOidByAlgorithm(CoseAlgorithmIdentifier algorithm)
458458
{
459459
return algorithm switch
460460
{
461-
CoseAlgorithmIdentifier.ES256 => KeyDefinitions.CryptoOids.P256,
462-
CoseAlgorithmIdentifier.ECDHwHKDF256 => KeyDefinitions.CryptoOids.P256,
463-
CoseAlgorithmIdentifier.ES384 => KeyDefinitions.CryptoOids.P384,
464-
CoseAlgorithmIdentifier.ES512 => KeyDefinitions.CryptoOids.P521,
461+
CoseAlgorithmIdentifier.ES256 => KeyDefinitions.Oids.P256,
462+
CoseAlgorithmIdentifier.ECDHwHKDF256 => KeyDefinitions.Oids.P256,
463+
CoseAlgorithmIdentifier.ES384 => KeyDefinitions.Oids.P384,
464+
CoseAlgorithmIdentifier.ES512 => KeyDefinitions.Oids.P521,
465465
_ => throw new NotSupportedException(ExceptionMessages.UnsupportedAlgorithm)
466466
};
467467
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2024 Yubico AB
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License").
4+
// You may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
17+
namespace Yubico.YubiKey.Cryptography;
18+
19+
internal class EmptyPrivateKeyParameters : IPrivateKeyParameters
20+
{
21+
public KeyDefinition KeyDefinition { get; } = new();
22+
public KeyType KeyType { get; }
23+
public byte[] ExportPkcs8PrivateKey() => Array.Empty<byte>();
24+
25+
public static ReadOnlyMemory<byte> PrivateKey => Array.Empty<byte>();
26+
public void Clear() { }
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2024 Yubico AB
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License").
4+
// You may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
17+
namespace Yubico.YubiKey.Cryptography;
18+
19+
internal class EmptyPublicKeyParameters : IPublicKeyParameters
20+
{
21+
public KeyDefinition KeyDefinition { get; } = new();
22+
public KeyType KeyType { get; }
23+
public static ReadOnlyMemory<byte> PublicPoint => Array.Empty<byte>();
24+
public byte[] ExportSubjectPublicKeyInfo() => Array.Empty<byte>();
25+
}

0 commit comments

Comments
 (0)