Skip to content

Commit f25c281

Browse files
committed
WIP update PIV samples to support curve25519
1 parent 5bccd24 commit f25c281

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

Yubico.YubiKey/examples/PivSampleCode/Run/PivSampleRun.Operations.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,12 @@ private bool GetAsymmetricAlgorithm(out PivAlgorithm algorithm)
712712
string[] menuItems = new string[] {
713713
"RSA 1024",
714714
"RSA 2048",
715+
"RSA 3076",
716+
"RSA 4096",
715717
"ECC P-256",
716-
"ECC P-384"
718+
"ECC P-384",
719+
"ECC Ed25519",
720+
"ECC X25519"
717721
};
718722

719723
int response = _menuObject.RunMenu("Which algorithm?", menuItems);
@@ -722,8 +726,12 @@ private bool GetAsymmetricAlgorithm(out PivAlgorithm algorithm)
722726
{
723727
0 => PivAlgorithm.Rsa1024,
724728
1 => PivAlgorithm.Rsa2048,
725-
2 => PivAlgorithm.EccP256,
726-
3 => PivAlgorithm.EccP384,
729+
2 => PivAlgorithm.Rsa3072,
730+
3 => PivAlgorithm.Rsa4096,
731+
4 => PivAlgorithm.EccP256,
732+
5 => PivAlgorithm.EccP384,
733+
6 => PivAlgorithm.EccEd25519,
734+
7 => PivAlgorithm.EccX25519,
727735
_ => PivAlgorithm.None,
728736
};
729737

Yubico.YubiKey/examples/PivSampleCode/SlotContents/SamplePivSlotContents.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Security.Cryptography.X509Certificates;
17+
using Yubico.YubiKey.Cryptography;
1718
using Yubico.YubiKey.Piv;
1819
using Yubico.YubiKey.Sample.SharedCode;
1920

@@ -32,6 +33,8 @@ public class SamplePivSlotContents
3233

3334
public PivPublicKey PublicKey { get; set; }
3435

36+
//public IPublicKeyParameters PublicKeyParameters { get; set; }
37+
3538
public CertificateRequest CertRequest { get; set; }
3639

3740
private byte[] _certRequestDer;
@@ -45,7 +48,14 @@ public SamplePivSlotContents()
4548

4649
public void PrintPublicKeyPem()
4750
{
48-
char[] pubKeyPem = KeyConverter.GetPemFromPivPublicKey(PublicKey);
51+
char[] pubKeyPem;
52+
53+
if (PublicKey.Algorithm.GetKeyType() == KeyType.X25519 || PublicKey.Algorithm.GetKeyType() == KeyType.Ed25519) {
54+
var publicKeyParameters = KeyParametersPivHelper.CreatePublicKeyParameters(PublicKey.PivEncodedPublicKey, PublicKey.Algorithm.GetKeyType());
55+
pubKeyPem = PemOperations.BuildPem("PUBLIC KEY", publicKeyParameters.ExportSubjectPublicKeyInfo());
56+
} else {
57+
pubKeyPem = KeyConverter.GetPemFromPivPublicKey(PublicKey);
58+
}
4959
SampleMenu.WriteMessage(MessageType.Title, 0, "\n" + new string(pubKeyPem) + "\n");
5060
}
5161

Yubico.YubiKey/examples/SharedSampleCode/SharedSampleCode.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ limitations under the License. -->
3232
</ItemGroup>-->
3333

3434
<ItemGroup>
35-
<PackageReference Include="Yubico.YubiKey" Version="1.*-*" />
35+
<!-- <PackageReference Include="Yubico.YubiKey" Version="1.*-*" /> -->
36+
<ProjectReference Include="..\..\src\Yubico.YubiKey.csproj" />
3637
</ItemGroup>
3738

3839
</Project>

Yubico.YubiKey/src/Yubico/YubiKey/Piv/Commands/GenerateKeyPairResponse.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,11 @@ public GenerateKeyPairResponse(
216216
// but in some times the caller is receiving a pivpublickey..
217217
Status switch
218218
{
219-
ResponseStatus.Success => PivPublicKey.Create(ResponseApdu.Data),
219+
#pragma warning disable CS0618 // Type or member is obsolete
220+
ResponseStatus.Success => PivPublicKey.Create(ResponseApdu.Data, Algorithm),
221+
#pragma warning restore CS0618 // Type or member is obsolete
220222
_ => throw new InvalidOperationException(StatusMessage),
221223
};
224+
222225
}
223226
}

Yubico.YubiKey/src/Yubico/YubiKey/Piv/KeyParametersPivExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public static Memory<byte> ToPivEncodedPublicKey(this IPublicKeyParameters param
4444
};
4545
}
4646

47+
public static PivPublicKey ToPivPublicKey(this IPublicKeyParameters parameters) {
48+
return PivPublicKey.Create(parameters.ToPivEncodedPublicKey(), parameters.KeyType.GetPivAlgorithm());
49+
}
50+
4751
private static Memory<byte> EncodeRSAPublicKeyParameters(RSAPublicKeyParameters parameters)
4852
{
4953
var rsaParameters = parameters.Parameters;

0 commit comments

Comments
 (0)