diff --git a/commands/constructors.go b/commands/constructors.go index 6e14898..7fabcd9 100644 --- a/commands/constructors.go +++ b/commands/constructors.go @@ -111,3 +111,15 @@ func CreateCloseSessionCommand() (*CommandMessage, error) { return command, nil } + +func CreateGetPubKeyCommand(keyID uint16) (*CommandMessage, error) { + command := &CommandMessage{ + CommandType: CommandTypeGetPubKey, + } + + payload := bytes.NewBuffer([]byte{}) + binary.Write(payload, binary.BigEndian, keyID) + command.Data = payload.Bytes() + + return command, nil +} diff --git a/commands/response.go b/commands/response.go index 7541dd7..092309a 100644 --- a/commands/response.go +++ b/commands/response.go @@ -38,6 +38,12 @@ type ( SignDataEddsaResponse struct { Signature []byte } + + GetPubKeyResponse struct { + Algorithm Algorithm + // KeyData can contain different formats depending on the algorithm according to the YubiHSM2 documentation. + KeyData []byte + } ) // ParseResponse parses the binary response from the card to the relevant Response type. @@ -76,6 +82,8 @@ func ParseResponse(data []byte) (Response, error) { return parsePutAsymmetricKeyResponse(payload) case CommandTypeCloseSession: return nil, nil + case CommandTypeGetPubKey: + return parseGetPubKeyResponse(payload) case ErrorResponseCode: return nil, parseErrorResponse(payload) default: @@ -151,6 +159,16 @@ func parsePutAsymmetricKeyResponse(payload []byte) (Response, error) { }, nil } +func parseGetPubKeyResponse(payload []byte) (Response, error) { + if len(payload) < 1 { + return nil, errors.New("invalid response payload length") + } + return &GetPubKeyResponse{ + Algorithm: Algorithm(payload[0]), + KeyData: payload[1:], + }, nil +} + // Error formats a card error message into a human readable format func (e *Error) Error() string { message := ""