|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + |
| 11 | + "github.com/ameshkov/gocurl/internal/config" |
| 12 | + "github.com/ameshkov/gocurl/internal/output" |
| 13 | + "github.com/chris-wood/ohttp-go" |
| 14 | +) |
| 15 | + |
| 16 | +// obliviousHTTPTransport is transport that uses Oblivious HTTP to encrypt |
| 17 | +// requests before sending them to a gateway. |
| 18 | +type obliviousHTTPTransport struct { |
| 19 | + base Transport |
| 20 | + gatewayURL *url.URL |
| 21 | + publicConfig ohttp.PublicConfig |
| 22 | + out *output.Output |
| 23 | +} |
| 24 | + |
| 25 | +// type check |
| 26 | +var _ Transport = (*obliviousHTTPTransport)(nil) |
| 27 | + |
| 28 | +// Conn returns the last established connection using this transport. |
| 29 | +func (t *obliviousHTTPTransport) Conn() (conn net.Conn) { |
| 30 | + return t.base.Conn() |
| 31 | +} |
| 32 | + |
| 33 | +// RoundTrip implements the http.RoundTripper interface for |
| 34 | +// *obliviousHTTPTransport. It encrypts the request using OHTTP and sends it to |
| 35 | +// the gateway. |
| 36 | +func (t *obliviousHTTPTransport) RoundTrip(r *http.Request) (resp *http.Response, err error) { |
| 37 | + // Create an OHTTP client with the public configuration. |
| 38 | + client := ohttp.NewDefaultClient(t.publicConfig) |
| 39 | + |
| 40 | + // Serialize the original request using BinaryRequest format. |
| 41 | + binaryReq := (*ohttp.BinaryRequest)(r) |
| 42 | + requestBytes, err := binaryReq.Marshal() |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("failed to serialize request: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + t.out.Debug("Encrypting request with OHTTP, original size: %d bytes", len(requestBytes)) |
| 48 | + |
| 49 | + // Encrypt the request using OHTTP. |
| 50 | + encapsulatedReq, encapContext, err := client.EncapsulateRequest(requestBytes) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("failed to encapsulate request: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Marshal the encapsulated request to bytes. |
| 56 | + encapsulatedReqBytes := encapsulatedReq.Marshal() |
| 57 | + t.out.Debug("Encrypted request size: %d bytes", len(encapsulatedReqBytes)) |
| 58 | + |
| 59 | + // Create a new HTTP POST request to the gateway with the encrypted payload. |
| 60 | + gatewayReq, err := http.NewRequest(http.MethodPost, t.gatewayURL.String(), bytes.NewReader(encapsulatedReqBytes)) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to create gateway request: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Set the content type for OHTTP requests. |
| 66 | + gatewayReq.Header.Set("Content-Type", "message/ohttp-req") |
| 67 | + |
| 68 | + t.out.Debug("Sending encrypted request to gateway: %s", t.gatewayURL.String()) |
| 69 | + |
| 70 | + // Send the encrypted request to the gateway. |
| 71 | + gatewayResp, err := t.base.RoundTrip(gatewayReq) |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("failed to send request to gateway: %w", err) |
| 74 | + } |
| 75 | + defer func() { |
| 76 | + _ = gatewayResp.Body.Close() |
| 77 | + }() |
| 78 | + |
| 79 | + // Verify the gateway response status. |
| 80 | + if gatewayResp.StatusCode != http.StatusOK { |
| 81 | + return nil, fmt.Errorf("gateway returned non-OK status: %s", gatewayResp.Status) |
| 82 | + } |
| 83 | + |
| 84 | + // Verify the gateway response content type. |
| 85 | + contentType := gatewayResp.Header.Get("Content-Type") |
| 86 | + if contentType != "message/ohttp-res" { |
| 87 | + t.out.Debug("Warning: unexpected Content-Type from gateway: %s (expected message/ohttp-res)", contentType) |
| 88 | + } |
| 89 | + |
| 90 | + // Read the encrypted response from the gateway. |
| 91 | + encapsulatedRespBytes, err := io.ReadAll(gatewayResp.Body) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("failed to read gateway response: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + t.out.Debug("Received encrypted response from gateway, size: %d bytes", len(encapsulatedRespBytes)) |
| 97 | + |
| 98 | + // Unmarshal the encapsulated response. |
| 99 | + encapsulatedResp, err := ohttp.UnmarshalEncapsulatedResponse(encapsulatedRespBytes) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed to unmarshal encapsulated response: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + // Decrypt the response using OHTTP. |
| 105 | + decryptedResp, err := encapContext.DecapsulateResponse(encapsulatedResp) |
| 106 | + if err != nil { |
| 107 | + return nil, fmt.Errorf("failed to decapsulate response: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + t.out.Debug("Decrypted response size: %d bytes", len(decryptedResp)) |
| 111 | + |
| 112 | + // Parse the decrypted response as an HTTP response using BinaryResponse |
| 113 | + // format. |
| 114 | + resp, err = ohttp.UnmarshalBinaryResponse(decryptedResp) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("failed to parse decrypted response: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + return resp, nil |
| 120 | +} |
| 121 | + |
| 122 | +func newRoundTripper( |
| 123 | + hostname string, |
| 124 | + cfg *config.Config, |
| 125 | + out *output.Output, |
| 126 | +) (rt http.RoundTripper, d *clientDialer, err error) { |
| 127 | + d, err = newDialer(hostname, cfg, out) |
| 128 | + if err != nil { |
| 129 | + return nil, nil, err |
| 130 | + } |
| 131 | + |
| 132 | + // Create transport for communicating with the hostname. |
| 133 | + rt, err = createHTTPTransport(d, cfg) |
| 134 | + if err != nil { |
| 135 | + return nil, nil, err |
| 136 | + } |
| 137 | + |
| 138 | + return rt, d, nil |
| 139 | +} |
| 140 | + |
| 141 | +// newObliviousHTTPTransport creates a new obliviousHTTPTransport. |
| 142 | +func newObliviousHTTPTransport( |
| 143 | + cfg *config.Config, |
| 144 | + out *output.Output, |
| 145 | +) (rt Transport, err error) { |
| 146 | + // Create base transport for requesting the KeyConfig. |
| 147 | + keyTransport, _, err := newRoundTripper(cfg.OHTTPKeysURL.Hostname(), cfg, out) |
| 148 | + if err != nil { |
| 149 | + return nil, fmt.Errorf("failed to create key transport: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + // Download the KeyConfig from the keys URL. |
| 153 | + out.Debug("Downloading OHTTP KeyConfig from: %s", cfg.OHTTPKeysURL.String()) |
| 154 | + |
| 155 | + keyReq, err := http.NewRequest(http.MethodGet, cfg.OHTTPKeysURL.String(), nil) |
| 156 | + if err != nil { |
| 157 | + return nil, fmt.Errorf("failed to create OHTTP KeyConfig request: %w", err) |
| 158 | + } |
| 159 | + |
| 160 | + keyResp, err := keyTransport.RoundTrip(keyReq) |
| 161 | + if err != nil { |
| 162 | + return nil, fmt.Errorf("failed to download OHTTP KeyConfig: %w", err) |
| 163 | + } |
| 164 | + defer func() { |
| 165 | + _ = keyResp.Body.Close() |
| 166 | + }() |
| 167 | + |
| 168 | + if keyResp.StatusCode != http.StatusOK { |
| 169 | + return nil, fmt.Errorf("failed to download OHTTP KeyConfig, status: %s", keyResp.Status) |
| 170 | + } |
| 171 | + |
| 172 | + keyConfigBytes, err := io.ReadAll(keyResp.Body) |
| 173 | + if err != nil { |
| 174 | + return nil, fmt.Errorf("failed to read OHTTP KeyConfig: %w", err) |
| 175 | + } |
| 176 | + |
| 177 | + out.Debug("Downloaded OHTTP KeyConfig, size: %d bytes", len(keyConfigBytes)) |
| 178 | + |
| 179 | + // Deserialize and validate the KeyConfig (PublicConfig). |
| 180 | + publicConfig, err := ohttp.UnmarshalPublicConfig(keyConfigBytes) |
| 181 | + if err != nil { |
| 182 | + return nil, fmt.Errorf("failed to deserialize OHTTP KeyConfig: %w", err) |
| 183 | + } |
| 184 | + |
| 185 | + out.Debug("OHTTP KeyConfig deserialized successfully, KeyID: %d", publicConfig.ID) |
| 186 | + |
| 187 | + // Create base transport for communicating with the gateway. |
| 188 | + gwTransport, d, err := newRoundTripper(cfg.OHTTPGatewayURL.Hostname(), cfg, out) |
| 189 | + if err != nil { |
| 190 | + return nil, fmt.Errorf("failed to create gateway transport: %w", err) |
| 191 | + } |
| 192 | + |
| 193 | + return &obliviousHTTPTransport{ |
| 194 | + base: &transport{d: d, base: gwTransport}, |
| 195 | + gatewayURL: cfg.OHTTPGatewayURL, |
| 196 | + publicConfig: publicConfig, |
| 197 | + out: out, |
| 198 | + }, nil |
| 199 | +} |
0 commit comments