-
Notifications
You must be signed in to change notification settings - Fork 60
Adding support for deleting and moving keys between slots #103
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
fe2a406
Add support for moving PIV keys.
DennisDyallo 4455be9
Relaxed config for tests projects on discard operator warnings
DennisDyallo 4505b31
Added exception for invalid slot combinations and documentation
DennisDyallo 02e3801
All keys but attestation can be moved.
DennisDyallo f6ba974
Early exit to reduce nesting
DennisDyallo f913949
Adding refs to base classes
DennisDyallo 200c800
Formatting
DennisDyallo 3f00423
Rewording
DennisDyallo 56cb144
Add delete functionality
DennisDyallo 3e66b51
Fortified move tests, added comments
DennisDyallo 47db5ee
Add GetKeyConverter
DennisDyallo da74300
Fix formatting
DennisDyallo d1808a9
Asserting moved key is moved
DennisDyallo 43a22cd
Use constant
DennisDyallo 27fc673
Add default constructor
DennisDyallo f1c3303
Move feature together with other Piv features
DennisDyallo 86665a9
dotnet format
DennisDyallo f1d4eae
formnat SampleKeyPairs.cs
DennisDyallo 5d24f37
Fix typo from 'delete to move', move private methods and reformat.
DennisDyallo 86633a7
Add missing piv algorithms
DennisDyallo 9e6c853
Reformat
DennisDyallo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,823 changes: 523 additions & 1,300 deletions
1,823
Yubico.YubiKey/src/Resources/ExceptionMessages.Designer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
Yubico.YubiKey/src/Yubico/YubiKey/Piv/Commands/DeleteKeyCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2024 Yubico AB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Yubico.Core.Iso7816; | ||
|
||
namespace Yubico.YubiKey.Piv.Commands | ||
{ | ||
/// <summary> | ||
/// The <see cref="DeleteKeyCommand"/> is used to Delete a PIV key from the target slot | ||
/// <remarks> | ||
/// Any key, including the attestation key can be deleted. | ||
/// </remarks> | ||
/// </summary> | ||
public class DeleteKeyCommand : IYubiKeyCommand<DeleteKeyResponse> | ||
{ | ||
/// <summary> | ||
/// The Yubikey slot of the key you want to delete. | ||
/// </summary> | ||
public byte SlotToClear { get; set; } | ||
|
||
/// <summary> | ||
/// Constructor for the <see cref="DeleteKeyCommand"/> which is used to delete a PIV key from a slot. | ||
/// </summary> | ||
/// <param name="slotToClear">The Yubikey slot of the key you want to clear.</param> | ||
public DeleteKeyCommand(byte slotToClear) | ||
{ | ||
SlotToClear = slotToClear; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the YubiKeyApplication to which this command belongs. For this command it's PIV. | ||
/// </summary> | ||
/// <value> | ||
/// YubiKeyApplication.Piv | ||
/// </value> | ||
public YubiKeyApplication Application => YubiKeyApplication.Piv; | ||
|
||
/// <summary> | ||
/// Constructs a <see cref="CommandApdu"/> for the Delete-operation. | ||
/// </summary> | ||
/// <returns> | ||
/// The <see cref="CommandApdu"/> that targets the Delete-operation with the correct parameters. | ||
/// </returns> | ||
public CommandApdu CreateCommandApdu() => | ||
new CommandApdu | ||
{ | ||
Ins = 0xF6, | ||
P1 = 0xFF, | ||
P2 = SlotToClear, | ||
}; | ||
|
||
/// <summary> | ||
/// Creates the <see cref="DeleteKeyResponse"/> from the <see cref="ResponseApdu"/> data. | ||
/// </summary> | ||
/// <param name="responseApdu">The return data with which the Yubikey responded to the | ||
/// <see cref="DeleteKeyCommand"/> | ||
/// </param> | ||
/// <returns> | ||
/// The <see cref="DeleteKeyResponse"/> for the <see cref="DeleteKeyCommand"/> | ||
/// </returns> | ||
public DeleteKeyResponse CreateResponseForApdu(ResponseApdu responseApdu) => | ||
new DeleteKeyResponse(responseApdu); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Yubico.YubiKey/src/Yubico/YubiKey/Piv/Commands/DeleteKeyResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2024 Yubico AB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Yubico.Core.Iso7816; | ||
|
||
namespace Yubico.YubiKey.Piv.Commands | ||
{ | ||
/// <summary> | ||
/// The <see cref="DeleteKeyResponse"/> for the corresponding <see cref="DeleteKeyCommand"/> | ||
/// <seealso cref="PivResponse"/> | ||
/// <seealso cref="YubiKeyResponse"/> | ||
/// </summary> | ||
public class DeleteKeyResponse : PivResponse | ||
{ | ||
/// <summary> | ||
/// The constructor for the <see cref="DeleteKeyResponse"/> | ||
/// </summary> | ||
/// <param name="responseApdu">The return data with which the Yubikey responded | ||
/// to the <see cref="DeleteKeyCommand"/></param> | ||
/// <seealso cref="DeleteKeyCommand"/> | ||
public DeleteKeyResponse(ResponseApdu responseApdu) : base(responseApdu) | ||
{ | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
Yubico.YubiKey/src/Yubico/YubiKey/Piv/Commands/MoveKeyCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2024 Yubico AB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Globalization; | ||
using Yubico.Core.Iso7816; | ||
|
||
namespace Yubico.YubiKey.Piv.Commands | ||
{ | ||
/// <summary> | ||
/// The <see cref="MoveKeyCommand"/> is used to move a PIV key from one slot to another. | ||
/// The source slot must not be the <see cref="PivSlot.Attestation"/>-slot and the destination slot must be empty. | ||
/// </summary> | ||
public class MoveKeyCommand : IYubiKeyCommand<MoveKeyResponse> | ||
{ | ||
/// <summary> | ||
/// The Yubikey slot of the key you want to move. This must be a valid slot number. | ||
/// </summary> | ||
public byte SourceSlot { get; set; } | ||
|
||
/// <summary> | ||
/// The target Yubikey slot for the key you want to move. This must be a valid slot number. | ||
/// </summary> | ||
public byte DestinationSlot { get; set; } | ||
|
||
/// <summary> | ||
/// Constructor for the <see cref="MoveKeyCommand"/> which is used to move a PIV key from one slot to another. | ||
/// The source slot must not be the <see cref="PivSlot.Attestation"/>-slot and the destination slot must be empty. | ||
/// </summary> | ||
/// <param name="sourceSlot">The Yubikey slot of the key you want to move. This must be a valid slot number.</param> | ||
/// <param name="destinationSlot">The target Yubikey slot for the key you want to move. This must be a valid slot number.</param> | ||
public MoveKeyCommand(byte sourceSlot, byte destinationSlot) | ||
{ | ||
SourceSlot = sourceSlot; | ||
DestinationSlot = destinationSlot; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the YubiKeyApplication to which this command belongs. For this | ||
/// command it's PIV. | ||
/// </summary> | ||
/// <value> | ||
/// YubiKeyApplication.Piv | ||
/// </value> | ||
public YubiKeyApplication Application => YubiKeyApplication.Piv; | ||
|
||
/// <summary> | ||
/// This will create and validate the <see cref="CommandApdu"/>. | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">An exception will be thrown upon invalid slot usage. | ||
/// Either one of the slots were the <see cref="PivSlot.Attestation"/> or the source and destination slot were the same.</exception> | ||
/// <returns>The <see cref="CommandApdu"/> that targets the Move-operation with the correct parameters</returns> | ||
public CommandApdu CreateCommandApdu() | ||
{ | ||
ValidateSlots(SourceSlot, DestinationSlot); | ||
|
||
return new CommandApdu | ||
{ | ||
Ins = 0xF6, | ||
P1 = DestinationSlot, | ||
P2 = SourceSlot, | ||
}; | ||
} | ||
|
||
private static void ValidateSlots(byte sourceSlot, byte destinationSlot) | ||
{ | ||
if (sourceSlot == destinationSlot) | ||
{ | ||
throw new InvalidOperationException(string.Format( | ||
CultureInfo.CurrentCulture, | ||
ExceptionMessages.InvalidSlotsSameSourceAndDestinationSlotsCannotBeTheSame)); | ||
} | ||
|
||
ValidateSlot(sourceSlot); | ||
ValidateSlot(destinationSlot); | ||
} | ||
|
||
private static void ValidateSlot(byte slot) | ||
{ | ||
if (slot == PivSlot.Attestation) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format( | ||
CultureInfo.CurrentCulture, | ||
ExceptionMessages.InvalidSlot, | ||
slot)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates the <see cref="MoveKeyResponse"/> from the <see cref="ResponseApdu"/> data. | ||
/// </summary> | ||
/// <param name="responseApdu">The return data with which the Yubikey responded | ||
/// to the <see cref="MoveKeyCommand"/></param> | ||
/// <returns> | ||
/// The <see cref="MoveKeyResponse"/> for the <see cref="MoveKeyCommand"/> | ||
/// </returns> | ||
public MoveKeyResponse CreateResponseForApdu(ResponseApdu responseApdu) => new MoveKeyResponse(responseApdu); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Yubico.YubiKey/src/Yubico/YubiKey/Piv/Commands/MoveKeyResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2024 Yubico AB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Yubico.Core.Iso7816; | ||
|
||
namespace Yubico.YubiKey.Piv.Commands | ||
{ | ||
/// <summary> | ||
/// The <see cref="MoveKeyResponse"/> for the corresponding <see cref="MoveKeyCommand"/> | ||
/// <seealso cref="PivResponse"/> | ||
/// <seealso cref="YubiKeyResponse"/> | ||
/// </summary> | ||
public class MoveKeyResponse : PivResponse | ||
{ | ||
/// <summary> | ||
/// The constructor for the <see cref="MoveKeyResponse"/> | ||
/// </summary> | ||
/// <param name="responseApdu">The return data with which the Yubikey responded | ||
/// to the <see cref="MoveKeyCommand"/></param> | ||
/// <seealso cref="MoveKeyCommand"/> | ||
public MoveKeyResponse(ResponseApdu responseApdu) : base(responseApdu) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.