diff --git a/README.md b/README.md index bfdc2c1..05aa9a2 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ messages is depleted. Currently the following commands are implemented: + * DeviceInfo * Reset * GenerateAsymmetricKey * SignDataEddsa diff --git a/commands/constructors.go b/commands/constructors.go index c303b06..9b95a4f 100644 --- a/commands/constructors.go +++ b/commands/constructors.go @@ -9,6 +9,15 @@ import ( "github.com/certusone/yubihsm-go/authkey" ) + +func CreateDeviceInfoCommand() (*CommandMessage, error) { + command := &CommandMessage{ + CommandType: CommandTypeDeviceInfo, + } + + return command, nil +} + func CreateCreateSessionCommand(keySetID uint16, hostChallenge []byte) (*CommandMessage, error) { command := &CommandMessage{ CommandType: CommandTypeCreateSession, diff --git a/commands/response.go b/commands/response.go index bd34727..87bcaee 100644 --- a/commands/response.go +++ b/commands/response.go @@ -15,6 +15,16 @@ type ( Code ErrorCode } + DeviceInfoResponse struct { + MajorVersion uint8 + MinorVersion uint8 + BuildVersion uint8 + SerialNumber uint32 + LogTotal uint8 + LogUsed uint8 + SupportedAlgorithms []Algorithm + } + CreateSessionResponse struct { SessionID uint8 CardChallenge []byte @@ -141,6 +151,8 @@ func ParseResponse(data []byte) (Response, error) { } switch transactionType { + case CommandTypeDeviceInfo: + return parseDeviceInfoResponse(payload) case CommandTypeCreateSession: return parseCreateSessionResponse(payload) case CommandTypeAuthenticateSession: @@ -214,6 +226,28 @@ func parseSessionMessage(payload []byte) (Response, error) { }, nil } +func parseDeviceInfoResponse(payload []byte) (Response, error) { + var serialNumber uint32 + err := binary.Read(bytes.NewReader(payload[3:7]), binary.BigEndian, &serialNumber) + if err != nil { + return nil, err + } + + var supportedAlgorithms []Algorithm + for _, alg := range payload[9:] { + supportedAlgorithms = append(supportedAlgorithms, Algorithm(alg)) + } + + return &DeviceInfoResponse{ + MajorVersion: payload[0], + MinorVersion: payload[1], + BuildVersion: payload[2], + SerialNumber: serialNumber, + LogTotal: payload[7], + LogUsed: payload[8], + SupportedAlgorithms: supportedAlgorithms, + }, nil +} func parseCreateSessionResponse(payload []byte) (Response, error) { if len(payload) != 17 { return nil, errors.New("invalid response payload length")