Class SetPinCommand
Set the YubiKey's FIDO2 application to be PIN-protected.
public class SetPinCommand : IYubiKeyCommand<SetPinResponse>
- Inheritance
-
objectSetPinCommand
- Implements
Remarks
Upon manufacture, the YubiKey's FIDO2 application has no PIN set as there is no default PIN defined. Any FIDO2 operation is possible by simply inserting the YubiKey and possibly touching the contact.
However, it is possible to set the application to require a PIN to perform many of the operations. Use this command to set the PIN.
Note that this command is possible only if no PIN is currently set. To change a PIN, use the ChangePinCommand. To remove a PIN, reset the application. Note that resetting the application will mean losing all credentials as well as removing the PIN.
Constructors
SetPinCommand(PinUvAuthProtocolBase, ReadOnlyMemory<byte>)
Constructs a new instance of SetPinCommand.
public SetPinCommand(PinUvAuthProtocolBase pinProtocol, ReadOnlyMemory<byte> newPin)
Parameters
pinProtocol
PinUvAuthProtocolBaseAn object defining the PIN protocol the command will use. The Encapsulate(CoseKey) method must have been successfully executed before passing it to this constructor.
newPin
ReadOnlyMemory<byte>The PIN to set. This is a byte array with the PIN provided as the UTF-8 encoding of Unicode characters in Normalization Form C.
Remarks
The caller must specify which PIN protocol the command will use. This
is done by passing in a subclass of PinUvAuthProtocolBase.
This constructor requires the
Encapsulate(CoseKey) method to have been called
before passing it in. Note that the Encapsulate
method
requires the YubiKey's public key, which is obtained by executing the
GetKeyAgreementCommand.
In order to set the PIN, the caller must supply the new PIN at
construction. In this class, the PIN is supplied as
ReadOnlyMemory<byte>
. It is possible to pass a
byte[]
, because it will be automatically cast.
The standard specifies that the PIN must be "the UTF-8 representation of" the "Unicode characters in Normalization Form C." This constructor expects the PIN to already be in that representation. See the User's Manual entry on the FIDO2 PIN for more information on what this means and how to build the PIN into the appropriate form. While this constructor will verify that the PIN is not too long, it will not verify the PIN is in the correct form. If it is invalid, the YubiKey might reject it and the response will indicate a failure.
This class will encrypt the PIN and will not copy a reference. That means you can overwrite the PIN in your byte array after calling the constructor.
The PIN is at least 4 unicode code points. If the YubiKey supports the "Set Minimum PIN Length" feature, it is possible to change this minimum to a bigger number, but never smaller than 4.
Note that the minimum length is given in code points, not bytes. The PIN must be converted to a sequence of bytes representing the Unicode characters in Normalization Form C, then UTF-8 encoded.
The maximum length is 63 bytes. This limit is in bytes, not code points. The standard also specifies that the last byte cannot be zero. Because the PIN must be UTF-8 encoded, this should never be an issue.
Exceptions
- ArgumentNullException
The
pinProtocol
arg is null.- ArgumentException
The PIN is an incorrect length.
- ArgumentException
The
pinProtocol
is in a state indicatingEncapsulate
has not executed.
Properties
Application
Gets the YubiKeyApplication (e.g. PIV, OATH, etc.) to which this command applies.
public YubiKeyApplication Application { get; }
Property Value
- YubiKeyApplication
YubiKeyApplication.Otp, YubiKeyApplication.Piv, etc.
Methods
CreateCommandApdu()
Creates a well-formed CommandApdu to send to the YubiKey.
public CommandApdu CreateCommandApdu()
Returns
- CommandApdu
A valid CommandApdu that is ready to be sent to the YubiKey, or passed along to additional encoders for further processing.
Remarks
This method will first perform validation on all of the parameters and data provided to it. The CommandAPDU it creates should contain all of the data payload for the command, even if it exceeds 65,535 bytes as specified by the ISO 7816-4 specification. The APDU will be properly chained by the device connection prior to being sent to the YubiKey, and the responses will collapsed into a single result.
CreateResponseForApdu(ResponseApdu)
Creates the corresponding IYubiKeyResponse implementation for the current command.
public SetPinResponse CreateResponseForApdu(ResponseApdu responseApdu)
Parameters
responseApdu
ResponseApduThe ResponseApdu returned by the YubiKey.
Returns
- SetPinResponse
The implementation of IYubiKeyResponse that parses and presents ths response APDU.