fido.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //go:build !nofido
  2. package fido
  3. import (
  4. "bytes"
  5. "fmt"
  6. "github.com/keys-pub/go-libfido2"
  7. )
  8. type Device struct {
  9. Device *libfido2.Device
  10. Info *libfido2.DeviceInfo
  11. }
  12. func Setup(rpID, pin string, cdh []byte, device Device) ([]byte, error) {
  13. attest, err := device.Device.MakeCredential(
  14. cdh,
  15. libfido2.RelyingParty{
  16. ID: rpID,
  17. Name: "hmac-secret",
  18. },
  19. libfido2.User{
  20. ID: bytes.Repeat([]byte{0x01}, 16),
  21. Name: "hmac-secret",
  22. },
  23. libfido2.ES256,
  24. pin,
  25. &libfido2.MakeCredentialOpts{
  26. Extensions: []libfido2.Extension{libfido2.HMACSecretExtension},
  27. RK: libfido2.True,
  28. },
  29. )
  30. if err != nil {
  31. return []byte{}, err
  32. }
  33. return attest.CredentialID, nil
  34. }
  35. func GetHmacSecret(rpID, pin string, cdh, credentialID, salt []byte, device Device) ([]byte, error) {
  36. assertion, err := device.Device.Assertion(
  37. rpID,
  38. cdh,
  39. [][]byte{credentialID},
  40. pin,
  41. &libfido2.AssertionOpts{
  42. Extensions: []libfido2.Extension{libfido2.HMACSecretExtension},
  43. HMACSalt: salt,
  44. },
  45. )
  46. if err != nil {
  47. return []byte{}, err
  48. }
  49. return assertion.HMACSecret, nil
  50. }
  51. func GetPluggedDevices() ([]Device, error) {
  52. devices := []Device{}
  53. locs, err := libfido2.DeviceLocations()
  54. if err != nil {
  55. return devices, fmt.Errorf("while getting locations: %w", err)
  56. }
  57. if len(locs) == 0 {
  58. return devices, fmt.Errorf("no devices")
  59. }
  60. for _, loc := range locs {
  61. device, err := libfido2.NewDevice(loc.Path)
  62. if err != nil {
  63. return devices, fmt.Errorf("while getting device from path: %w", err)
  64. }
  65. info, err := device.Info()
  66. if err != nil {
  67. return devices, fmt.Errorf("while getting info: %w", err)
  68. }
  69. devices = append(devices, Device{
  70. Device: device,
  71. Info: info,
  72. })
  73. }
  74. return devices, nil
  75. }
  76. func GetUUID() ([]byte, error) {
  77. devices, err := GetPluggedDevices()
  78. if err != nil {
  79. return []byte{}, err
  80. }
  81. return devices[0].Info.AAGUID, nil
  82. }