|
| 1 | +//go:build ooni_userauth |
| 2 | + |
| 3 | +package userauth |
| 4 | + |
| 5 | +// #cgo CFLAGS: -I${SRCDIR}/ffi |
| 6 | +// #cgo linux,amd64 LDFLAGS: -L${SRCDIR}/lib/linux/amd64 -looniprobe_userauth -ldl -lm -lpthread |
| 7 | +// #cgo linux,arm64 LDFLAGS: -L${SRCDIR}/lib/linux/arm64 -looniprobe_userauth -ldl -lm -lpthread |
| 8 | +// #cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/darwin/amd64 -looniprobe_userauth -framework CoreFoundation -framework Security |
| 9 | +// #cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/darwin/arm64 -looniprobe_userauth -framework CoreFoundation -framework Security |
| 10 | +// #cgo windows,amd64 LDFLAGS: -L${SRCDIR}/lib/windows/amd64 -looniprobe_userauth -lws2_32 -luserenv -lbcrypt |
| 11 | +// #include <stdlib.h> |
| 12 | +// #include "ooniprobe_userauth.h" |
| 13 | +import "C" |
| 14 | + |
| 15 | +import ( |
| 16 | + "encoding/json" |
| 17 | + "errors" |
| 18 | + "unsafe" |
| 19 | +) |
| 20 | + |
| 21 | +// RegistrationResponse represents the result of user registration |
| 22 | +type RegistrationResponse struct { |
| 23 | + Credential string `json:"credential"` |
| 24 | + EmissionDay int16 `json:"emission_day"` |
| 25 | +} |
| 26 | + |
| 27 | +// SubmitResponse represents the result of submitting measurement |
| 28 | +type SubmitResponse struct { |
| 29 | + MeasurementUID string `json:"measurement_uid,omitempty"` |
| 30 | + IsVerified bool `json:"is_verified"` |
| 31 | + SubmitResponse string `json:"submit_response"` |
| 32 | +} |
| 33 | + |
| 34 | +// HTTPGet performs an HTTP GET request |
| 35 | +func HTTPGet(url string) (string, error) { |
| 36 | + cURL := C.CString(url) |
| 37 | + defer C.free(unsafe.Pointer(cURL)) |
| 38 | + |
| 39 | + resp := C.client_get(cURL) |
| 40 | + defer C.client_response_free(resp) |
| 41 | + |
| 42 | + return parseResponse(resp) |
| 43 | +} |
| 44 | + |
| 45 | +// HTTPPost performs an HTTP POST request |
| 46 | +func HTTPPost(url, payload string) (string, error) { |
| 47 | + cURL := C.CString(url) |
| 48 | + defer C.free(unsafe.Pointer(cURL)) |
| 49 | + |
| 50 | + cPayload := C.CString(payload) |
| 51 | + defer C.free(unsafe.Pointer(cPayload)) |
| 52 | + |
| 53 | + resp := C.client_post(cURL, cPayload) |
| 54 | + defer C.client_response_free(resp) |
| 55 | + |
| 56 | + return parseResponse(resp) |
| 57 | +} |
| 58 | + |
| 59 | +// Register registers a new user and obtains a credential |
| 60 | +func Register(url, publicParams, manifestVersion string) (*RegistrationResponse, error) { |
| 61 | + cURL := C.CString(url) |
| 62 | + defer C.free(unsafe.Pointer(cURL)) |
| 63 | + |
| 64 | + cPublicParams := C.CString(publicParams) |
| 65 | + defer C.free(unsafe.Pointer(cPublicParams)) |
| 66 | + |
| 67 | + cManifestVersion := C.CString(manifestVersion) |
| 68 | + defer C.free(unsafe.Pointer(cManifestVersion)) |
| 69 | + |
| 70 | + resp := C.userauth_register(cURL, cPublicParams, cManifestVersion) |
| 71 | + defer C.client_response_free(resp) |
| 72 | + |
| 73 | + jsonStr, err := parseResponse(resp) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + var result RegistrationResponse |
| 79 | + if err := json.Unmarshal([]byte(jsonStr), &result); err != nil { |
| 80 | + return nil, errors.New("failed to parse registration response: " + err.Error()) |
| 81 | + } |
| 82 | + |
| 83 | + return &result, nil |
| 84 | +} |
| 85 | + |
| 86 | +// Submit submits user credentials with measurement data |
| 87 | +func Submit(url, credentialB64, publicParams, probeCC, probeASN, manifestVersion string) (*SubmitResponse, error) { |
| 88 | + cURL := C.CString(url) |
| 89 | + defer C.free(unsafe.Pointer(cURL)) |
| 90 | + |
| 91 | + cCredentialB64 := C.CString(credentialB64) |
| 92 | + defer C.free(unsafe.Pointer(cCredentialB64)) |
| 93 | + |
| 94 | + cPublicParams := C.CString(publicParams) |
| 95 | + defer C.free(unsafe.Pointer(cPublicParams)) |
| 96 | + |
| 97 | + cProbeCC := C.CString(probeCC) |
| 98 | + defer C.free(unsafe.Pointer(cProbeCC)) |
| 99 | + |
| 100 | + cProbeASN := C.CString(probeASN) |
| 101 | + defer C.free(unsafe.Pointer(cProbeASN)) |
| 102 | + |
| 103 | + cManifestVersion := C.CString(manifestVersion) |
| 104 | + defer C.free(unsafe.Pointer(cManifestVersion)) |
| 105 | + |
| 106 | + resp := C.userauth_submit(cURL, cCredentialB64, cPublicParams, cProbeCC, cProbeASN, cManifestVersion) |
| 107 | + defer C.client_response_free(resp) |
| 108 | + |
| 109 | + jsonStr, err := parseResponse(resp) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + var result SubmitResponse |
| 115 | + if err := json.Unmarshal([]byte(jsonStr), &result); err != nil { |
| 116 | + return nil, errors.New("failed to parse submit response: " + err.Error()) |
| 117 | + } |
| 118 | + |
| 119 | + return &result, nil |
| 120 | +} |
| 121 | + |
| 122 | +// parseResponse converts a C ClientResponse to Go types |
| 123 | +func parseResponse(resp C.ClientResponse) (string, error) { |
| 124 | + if resp.error != nil { |
| 125 | + errStr := C.GoString(resp.error) |
| 126 | + return "", errors.New(errStr) |
| 127 | + } |
| 128 | + |
| 129 | + if resp.json != nil { |
| 130 | + return C.GoString(resp.json), nil |
| 131 | + } |
| 132 | + |
| 133 | + return "", errors.New("empty response from FFI") |
| 134 | +} |
0 commit comments