Add GetPubkey command support

This commit is contained in:
Hendrik Hofstadt 2018-09-08 20:43:08 +02:00
parent 727a1c5fe5
commit 30b3942a8d
2 changed files with 30 additions and 0 deletions

View File

@ -111,3 +111,15 @@ func CreateCloseSessionCommand() (*CommandMessage, error) {
return command, nil 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
}

View File

@ -38,6 +38,12 @@ type (
SignDataEddsaResponse struct { SignDataEddsaResponse struct {
Signature []byte 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. // 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) return parsePutAsymmetricKeyResponse(payload)
case CommandTypeCloseSession: case CommandTypeCloseSession:
return nil, nil return nil, nil
case CommandTypeGetPubKey:
return parseGetPubKeyResponse(payload)
case ErrorResponseCode: case ErrorResponseCode:
return nil, parseErrorResponse(payload) return nil, parseErrorResponse(payload)
default: default:
@ -151,6 +159,16 @@ func parsePutAsymmetricKeyResponse(payload []byte) (Response, error) {
}, nil }, 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 // Error formats a card error message into a human readable format
func (e *Error) Error() string { func (e *Error) Error() string {
message := "" message := ""