-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathohttp.patch
1990 lines (1951 loc) · 76.9 KB
/
ohttp.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/components/cronet/BUILD.gn b/components/cronet/BUILD.gn
index 92350cf3b4b77..cf15c3fda0faf 100644
--- a/components/cronet/BUILD.gn
+++ b/components/cronet/BUILD.gn
@@ -275,6 +275,8 @@ if (is_android) {
deps = [
"//components/cronet",
"//components/cronet/native:cronet_native_headers",
+ "//third_party/boringssl",
+ "//third_party/ohttp",
]
if ((is_linux || is_chromeos) && !is_component_build) {
public_configs = [ "//build/config/gcc:rpath_for_built_shared_libraries" ]
diff --git a/components/cronet/native/sample/main.cc b/components/cronet/native/sample/main.cc
index 007d5bbab90d7..64e0b7453ec6d 100644
--- a/components/cronet/native/sample/main.cc
+++ b/components/cronet/native/sample/main.cc
@@ -3,55 +3,32 @@
// found in the LICENSE file.
#include <iostream>
+#include <vector>
+#include <string>
+#include <cstdint>
#include "cronet_c.h"
+#include "openssl/evp.h"
#include "sample_executor.h"
#include "sample_url_request_callback.h"
+#include "third_party/boringssl/src/include/openssl/hpke.h"
+#include "third_party/ohttp/ohttp.h"
-Cronet_EnginePtr CreateCronetEngine() {
- Cronet_EnginePtr cronet_engine = Cronet_Engine_Create();
- Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
- Cronet_EngineParams_user_agent_set(engine_params, "CronetSample/1");
- Cronet_EngineParams_envoy_url_set(engine_params,
- "https://example.com/enovy_path/");
- Cronet_EngineParams_envoy_url_set(
- engine_params,
- "envoy://"
- "?url=https%3A%2F%2Fexample.com%2Fenvoy_path%2F%3Fk1%3Dv1&header_Host="
- "subdomain.example.com&resolve=MAP%20example.com%201.2.3.4");
- // only MAP url-host to address
- Cronet_EngineParams_envoy_url_set(
- engine_params,
- "envoy://"
- "?url=https%3A%2F%2Fexample.com%2Fenvoy_path%2F%3Fk1%3Dv1&header_Host="
- "subdomain.example.com&address=1.2.3.4");
- Cronet_EngineParams_envoy_url_set(
- engine_params,
- "envoy://"
- "?url=https%3A%2F%2Fexample.com%2Fenvoy_path%2F%3Fk1%3Dv1&header_Host="
- "subdomain.example.com&address=1.2.3.4&disabled_cipher_suites=0xc024,0xc02f");
- Cronet_EngineParams_envoy_url_set(
- engine_params,
- "envoy://"
- "?url=https%3A%2F%2Fexample.com%2Fenvoy_path%2F%3Fk1%3Dv1&header_Host="
- "subdomain.example.com&address=1.2.3.4&disabled_cipher_suites=0xc024,0xc02f");
- Cronet_EngineParams_envoy_url_set(engine_params, "socks5://127.0.0.1:1080");
- // proxy URL and SOCKS5 together (for true PTs)
- Cronet_EngineParams_envoy_url_set(
- engine_params,
- "envoy://"
- "?url=https%3A%2F%2Frayon.example.com%2Fwikipedia%2F&address=142.65.13.41"
- "&header_Host=abc.example.com&socks5=socks5%3A%2F%2Flocalhost%3A8192");
-
- Cronet_EngineParams_enable_quic_set(engine_params, true);
- Cronet_Engine_StartWithParams(cronet_engine, engine_params);
- Cronet_EngineParams_Destroy(engine_params);
- return cronet_engine;
+// Function to base64 encode
+std::string base64_encode(const std::string& input) {
+ size_t output_length = 4 * ((input.size() + 2) / 3);
+ std::string encoded(output_length, '\0');
+
+ int actual_length = EVP_EncodeBlock(reinterpret_cast<unsigned char*>(&encoded[0]),
+ reinterpret_cast<const unsigned char*>(input.data()),
+ input.size());
+ encoded.resize(actual_length); // Trim to actual length
+ return encoded;
}
-void PerformRequest(Cronet_EnginePtr cronet_engine,
+std::string GetResponseBody(Cronet_EnginePtr cronet_engine,
const std::string& url,
Cronet_ExecutorPtr executor) {
SampleUrlRequestCallback url_request_callback;
@@ -68,8 +45,42 @@ void PerformRequest(Cronet_EnginePtr cronet_engine,
url_request_callback.WaitForDone();
Cronet_UrlRequest_Destroy(request);
- std::cout << "Response Data:" << std::endl
- << url_request_callback.response_as_string() << std::endl;
+ return url_request_callback.response_as_string();
+}
+
+Cronet_EnginePtr CreateCronetEngine() {
+ Cronet_EnginePtr cronet_engine = Cronet_Engine_Create();
+ Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
+ Cronet_EngineParams_user_agent_set(engine_params, "CronetSample/1");
+
+ // Get the OHTTP config for use in our envoy URL.
+ Cronet_Engine_StartWithParams(cronet_engine, engine_params);
+ SampleExecutor executor;
+ std::string config = GetResponseBody(cronet_engine, "https://ohttp-gateway.jthess.com/gog/ohttp-keys", executor.GetExecutor());
+ std::string config_as_base64 = base64_encode(config);
+ Cronet_Engine_Shutdown(cronet_engine);
+
+ std::string envoy_url = "envoy://"
+ "?url=https%3A%2F%2Fohttp-relay.jthess.com" // Relay
+ "&ohttp=1" // Flag to indicate this is an ohttp relay
+ "&ohttp_gateway=ohttp-gateway.jthess.com/gog/gateway"
+ "&ohttp_config=";
+
+ envoy_url = envoy_url + config_as_base64;
+ Cronet_EngineParams_envoy_url_set(engine_params, envoy_url.c_str());
+ Cronet_EngineParams_enable_quic_set(engine_params, true);
+
+ Cronet_Engine_StartWithParams(cronet_engine, engine_params);
+ Cronet_EngineParams_Destroy(engine_params);
+ // Use that engine to get the OHTTP config.
+ return cronet_engine;
+}
+
+void PerformRequest(Cronet_EnginePtr cronet_engine,
+ const std::string& url,
+ Cronet_ExecutorPtr executor) {
+ std::string response = GetResponseBody(cronet_engine, url, executor);
+ std::cout << "Response to caller: " << std::endl << std::endl << response << std::endl;
}
// Download a resource from the Internet. Optional argument must specify
@@ -77,11 +88,11 @@ void PerformRequest(Cronet_EnginePtr cronet_engine,
int main(int argc, const char* argv[]) {
std::cout << "Hello from Cronet!\n";
Cronet_EnginePtr cronet_engine = CreateCronetEngine();
- std::cout << "Cronet version: "
+ std::cout << " Cronet version: "
<< Cronet_Engine_GetVersionString(cronet_engine) << std::endl;
- std::string url(argc > 1 ? argv[1] : "https://www.google.com/generate_204");
- std::cout << "URL: " << url << std::endl;
+ std::string url(argc > 1 ? argv[1] : "https://www.example.com");
+ std::cout << " GETting URL: " << url << " via OHTTP" << std::endl;
SampleExecutor executor;
PerformRequest(cronet_engine, url, executor.GetExecutor());
diff --git a/components/cronet/native/sample/sample_url_request_callback.cc b/components/cronet/native/sample/sample_url_request_callback.cc
index 396e27dbd27d2..6e3ca7f3752ca 100644
--- a/components/cronet/native/sample/sample_url_request_callback.cc
+++ b/components/cronet/native/sample/sample_url_request_callback.cc
@@ -29,17 +29,17 @@ void SampleUrlRequestCallback::OnRedirectReceived(
Cronet_UrlRequestPtr request,
Cronet_UrlResponseInfoPtr info,
Cronet_String newLocationUrl) {
- std::cout << "OnRedirectReceived called: " << newLocationUrl << std::endl;
+ // std::cout << "SampleUrlRequestCallback::OnRedirectReceived called: " << newLocationUrl << std::endl;
Cronet_UrlRequest_FollowRedirect(request);
}
void SampleUrlRequestCallback::OnResponseStarted(
Cronet_UrlRequestPtr request,
Cronet_UrlResponseInfoPtr info) {
- std::cout << "OnResponseStarted called." << std::endl;
- std::cout << "HTTP Status: "
- << Cronet_UrlResponseInfo_http_status_code_get(info) << " "
- << Cronet_UrlResponseInfo_http_status_text_get(info) << std::endl;
+ // std::cout << "SampleUrlRequestCallback::OnResponseStarted called." << std::endl;
+ // std::cout << "HTTP Status to Caller: " << std::dec
+ // << Cronet_UrlResponseInfo_http_status_code_get(info) << " "
+ // << Cronet_UrlResponseInfo_http_status_text_get(info) << std::endl;
// Create and allocate 32kb buffer.
Cronet_BufferPtr buffer = Cronet_Buffer_Create();
Cronet_Buffer_InitWithAlloc(buffer, 32 * 1024);
@@ -51,8 +51,10 @@ void SampleUrlRequestCallback::OnReadCompleted(Cronet_UrlRequestPtr request,
Cronet_UrlResponseInfoPtr info,
Cronet_BufferPtr buffer,
uint64_t bytes_read) {
- std::cout << "OnReadCompleted called: " << bytes_read << " bytes read."
- << std::endl;
+ // std::cout << "SampleUrlRequestCallback::OnReadCompleted called: " << bytes_read << " bytes read."
+ // << std::endl;
+ // It's possible that the data's not ready yet so it hasn't been decapsulated.
+ // If that's true, we need to deal with that.
std::string last_read_data(
reinterpret_cast<char*>(Cronet_Buffer_GetData(buffer)), bytes_read);
response_as_string_ += last_read_data;
@@ -62,22 +64,22 @@ void SampleUrlRequestCallback::OnReadCompleted(Cronet_UrlRequestPtr request,
void SampleUrlRequestCallback::OnSucceeded(Cronet_UrlRequestPtr request,
Cronet_UrlResponseInfoPtr info) {
- std::cout << "OnSucceeded called." << std::endl;
+ // std::cout << "SampleUrlRequestCallback::OnSucceeded called." << std::endl;
SignalDone(true);
}
void SampleUrlRequestCallback::OnFailed(Cronet_UrlRequestPtr request,
Cronet_UrlResponseInfoPtr info,
Cronet_ErrorPtr error) {
- std::cout << "OnFailed called: " << Cronet_Error_message_get(error)
- << std::endl;
+ // std::cout << "SampleUrlRequestCallback::OnFailed called: " << Cronet_Error_message_get(error)
+ // << std::endl;
last_error_message_ = Cronet_Error_message_get(error);
SignalDone(false);
}
void SampleUrlRequestCallback::OnCanceled(Cronet_UrlRequestPtr request,
Cronet_UrlResponseInfoPtr info) {
- std::cout << "OnCanceled called." << std::endl;
+ // std::cout << "OnCanceled called." << std::endl;
SignalDone(false);
}
diff --git a/components/cronet/native/sample/test/sample_test.cc b/components/cronet/native/sample/test/sample_test.cc
index 7831b69f72a4a..ee0e6167ada44 100644
--- a/components/cronet/native/sample/test/sample_test.cc
+++ b/components/cronet/native/sample/test/sample_test.cc
@@ -44,7 +44,7 @@ TEST(SampleTest, TestConnectionRefused) {
EXPECT_NE(std::string::npos, sample_out.find("net::ERR_INVALID_URL"));
}
-} // namespace
+}
int main(int argc, char** argv) {
s_test_app_path = argv[0];
diff --git a/net/BUILD.gn b/net/BUILD.gn
index e12e5b6936b8d..bbd05f79d0072 100644
--- a/net/BUILD.gn
+++ b/net/BUILD.gn
@@ -1078,6 +1078,7 @@ component("net") {
"//net/dns:mdns_client",
"//net/dns/public",
"//net/third_party/quiche",
+ "//third_party/ohttp",
]
allow_circular_includes_from = [
diff --git a/net/url_request/url_request_context_builder.cc b/net/url_request/url_request_context_builder.cc
index b3b5b9da8f5ae..552acc549a6f4 100644
--- a/net/url_request/url_request_context_builder.cc
+++ b/net/url_request/url_request_context_builder.cc
@@ -55,6 +55,7 @@
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_job_factory.h"
#include "net/url_request/url_request_throttler_manager.h"
+#include "third_party/ohttp/ohttp.h"
#include "url/url_constants.h"
#if BUILDFLAG(ENABLE_REPORTING)
@@ -286,8 +287,10 @@ std::unique_ptr<URLRequestContext> URLRequestContextBuilder::Build() {
context->set_require_network_isolation_key(require_network_isolation_key_);
context->set_network_quality_estimator(network_quality_estimator_);
- if (!envoy_url_.empty())
+ // Give the context the Envoy URL
+ if (!envoy_url_.empty()) {
context->set_envoy_url(envoy_url_);
+ }
if (http_user_agent_settings_) {
context->set_http_user_agent_settings(std::move(http_user_agent_settings_));
} else {
@@ -380,6 +383,7 @@ std::unique_ptr<URLRequestContext> URLRequestContextBuilder::Build() {
// TODO assert value
auto url = GURL(value);
+ // Set resolve/address/MAP, if specified.
if (GetValueForKeyInQuery(envoy_url, "resolve", &value)) {
std::unique_ptr<net::MappedHostResolver> remapped_resolver(
new net::MappedHostResolver(std::move(host_resolver_)));
@@ -399,6 +403,7 @@ std::unique_ptr<URLRequestContext> URLRequestContextBuilder::Build() {
} else {
SSLContextConfig ssl_context_config;
std::vector<uint16_t> disabled_ciphers;
+ // Disable cipher suites, if specified
if (GetValueForKeyInQuery(envoy_url, "disabled_cipher_suites", &value)) {
auto cipher_strings = base::SplitString(value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
// see net::ParseCipherSuites(cipher_strings);
diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc
index da6ee48808da0..f3816c2146bc3 100644
--- a/net/url_request/url_request_http_job.cc
+++ b/net/url_request/url_request_http_job.cc
@@ -36,6 +36,7 @@
#include "base/values.h"
#include "build/build_config.h"
#include "base/strings/escape.h"
+#include "net/base/elements_upload_data_stream.h"
#include "net/base/features.h"
#include "net/base/host_port_pair.h"
#include "net/base/http_user_agent_settings.h"
@@ -47,6 +48,7 @@
#include "net/base/privacy_mode.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/base/trace_constants.h"
+#include "net/base/upload_bytes_element_reader.h"
#include "net/base/url_util.h"
#include "net/cert/cert_status_flags.h"
#include "net/cert/ct_policy_status.h"
@@ -93,7 +95,9 @@
#include "net/url_request/url_request_redirect_job.h"
#include "net/url_request/url_request_throttler_manager.h"
#include "net/url_request/websocket_handshake_userdata_key.h"
+#include "openssl/evp.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
+#include "third_party/ohttp/ohttp.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_constants.h"
@@ -189,6 +193,16 @@ GURL UpgradeSchemeToCryptographic(const GURL& insecure_url) {
return secure_url;
}
+// For parsing b64 OHTTP config in Envoy URL
+std::vector<uint8_t> base64_decode(const std::string& input) {
+ uint8_t* output = new uint8_t[input.size()];
+ int actual_length = EVP_DecodeBlock(output,
+ reinterpret_cast<const unsigned char*>(input.data()),
+ input.size());
+ std::vector<uint8_t> decoded(output, output + actual_length);
+ return decoded;
+}
+
} // namespace
namespace net {
@@ -538,7 +552,6 @@ void URLRequestHttpJob::MaybeStartTransactionInternal(int result) {
void URLRequestHttpJob::StartTransactionInternal() {
DCHECK(!override_response_headers_);
-
// NOTE: This method assumes that request_info_ is already setup properly.
// If we already have a transaction, then we should restart the transaction
@@ -560,6 +573,48 @@ void URLRequestHttpJob::StartTransactionInternal() {
} else {
DCHECK(request_->context()->http_transaction_factory());
+ // refactor this with the same conditional below
+ if (!(request_info_.load_flags & LOAD_BYPASS_PROXY)) {
+ if (request_->context()->envoy_url().rfind("http://", 0) == 0 ||
+ request_->context()->envoy_url().rfind("https://", 0) == 0 ||
+ request_->context()->envoy_url().rfind("envoy://", 0) == 0) {
+ size_t pkR_len = 32;
+ // Get the OHTTP Config from the Envoy URL
+ size_t ohttp_config_start = request_->context()->envoy_url().find("ohttp_config=");
+ DCHECK(ohttp_config_start != std::string::npos);
+ ohttp_config_start += 13;
+ size_t ohttp_config_end = request_->context()->envoy_url().find("&", ohttp_config_start);
+ if (ohttp_config_end == std::string::npos) {
+ ohttp_config_end = request_->context()->envoy_url().size();
+ }
+ std::string ohttp_config = request_->context()->envoy_url().substr(ohttp_config_start, ohttp_config_end - ohttp_config_start);
+ // base64 decode the envoy config
+ std::vector<uint8_t> envoy_config_decoded = base64_decode(ohttp_config);
+ std::vector<uint8_t> pkR_vec = ohttp::get_public_key(envoy_config_decoded);
+ DCHECK(pkR_vec.size() == pkR_len);
+ uint8_t pkR[pkR_len];
+ std::copy(pkR_vec.begin(), pkR_vec.end(), pkR);
+ sender_context = ohttp::createHpkeContext();
+ byte_data = ohttp::get_encapsulated_request(
+ sender_context,
+ request_info_.method,
+ request_info_.url.scheme(),
+ request_info_.url.host(),
+ request_info_.url.path(),
+ "", // TODO: read the provided upload_data_stream
+ client_enc, &client_enc_len,
+ pkR, pkR_len);
+ const char* request_data = reinterpret_cast<const char*>(byte_data.data());
+ std::vector<std::unique_ptr<UploadElementReader>> readers;
+ readers.push_back(
+ std::make_unique<net::UploadBytesElementReader>(request_data, byte_data.size()));
+ std::unique_ptr<UploadDataStream> upload_stream =
+ std::make_unique<ElementsUploadDataStream>(std::move(readers), 0);
+ SetUpload(upload_stream.release());
+ }
+ }
+
+ // This sets transaction_, so the upload must be changed before this point
rv = request_->context()->http_transaction_factory()->CreateTransaction(
priority_, &transaction_);
@@ -575,6 +630,7 @@ void URLRequestHttpJob::StartTransactionInternal() {
}
if (rv == OK) {
+
transaction_->SetConnectedCallback(base::BindRepeating(
&URLRequestHttpJob::NotifyConnectedCallback, base::Unretained(this)));
transaction_->SetRequestHeadersCallback(request_headers_callback_);
@@ -587,6 +643,9 @@ void URLRequestHttpJob::StartTransactionInternal() {
// Don't use the Envoy proxy when LOAD_BYPASS_PROXY is set
// Specifically, we don't want to use Envoy for DoH requests
+ // Why don't we use it for DoH?
+ // How does Envoy allow proxying only some requests?
+ // This section actually routes our request through the proxy.
if (!(request_info_.load_flags & LOAD_BYPASS_PROXY)) {
if (request_->context()->envoy_url().rfind("http://", 0) == 0 ||
@@ -607,20 +666,27 @@ void URLRequestHttpJob::StartTransactionInternal() {
auto value = it.GetUnescapedValue();
if (key.compare("url") == 0) {
// see GetUnescapedValue, TODO check is_valid() before set
- request_info_.url =
- GURL(base::UnescapeURLComponent(value, base::UnescapeRule::NORMAL));
- } else if (key.compare("salt") == 0) {
- salt = value;
+ request_info_.url = GURL(base::UnescapeURLComponent(
+ value, base::UnescapeRule::NORMAL));
+ } else if (key.compare("salt") == 0) {
+ salt = value;
} else if (key.rfind(headerPrefix, 0) == 0 &&
- key.size() > headerPrefixLength) {
+ key.size() > headerPrefixLength) {
+ request_info_.extra_headers.SetHeader(
+ key.substr(headerPrefixLength),
+ value); // check for header Host, add :authority for
+ // http2; :path for http2
+ } else if (key.compare("ohttp") == 0) {
+ request_info_.method = "POST";
request_info_.extra_headers.SetHeader(
- key.substr(headerPrefixLength), value); // check for header Host, add :authority for http2; :path for http2
+ "Content-Type", "message/ohttp-req");
}
}
}
-
// count for cache key
+ // So why do we use the digest? It gets passed to the proxy, but not to the
+ // target server (that's good).
auto digest = crypto::SHA256HashString(request_->url().spec() + salt);
request_info_.url =
AppendQueryParameter(request_info_.url, "_digest", digest);
@@ -1561,22 +1627,61 @@ bool URLRequestHttpJob::ShouldFixMismatchedContentLength(int rv) const {
int URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size) {
DCHECK_NE(buf_size, 0);
DCHECK(!read_in_progress_);
+ // This should be modified
+ // It should not give the caller data if the transaction is not done
+ // When the transaction *is* done, it should ohttp decapsulate it
+ // if necessary, and then give it to the caller
+ int rv;
+ if (!byte_data.empty()) {
+ scoped_refptr<IOBufferWithSize> tbuf = base::MakeRefCounted<IOBufferWithSize>(buf_size);
+ rv = transaction_->Read(tbuf.get(), buf_size,
+ base::BindOnce(&URLRequestHttpJob::OnReadCompleted,
+ base::Unretained(this)));
+ // Read the contents of the temporary buffer into response data vector
+ if (rv > 0) {
+ response_data.insert(response_data.end(), tbuf->data(), tbuf->data() + rv);
+ size_t max_drequest_len = response_data.size();
+ uint8_t dresponse[max_drequest_len];
+ size_t dresponse_len;
+ ohttp::DecapsulationErrorCode decap_result = decapsulate_response(
+ sender_context,
+ client_enc,
+ client_enc_len,
+ response_data,
+ dresponse,
+ &dresponse_len,
+ max_drequest_len);
+ if (decap_result != ohttp::DecapsulationErrorCode::SUCCESS) {
+ return ERR_FAILED; // Why did we fail? Are we not done reading from the network? Handle that differently.
+ }
+ std::vector<uint8_t> dresponse_as_vec;
+ dresponse_as_vec.insert(dresponse_as_vec.end(), dresponse, dresponse + dresponse_len);
+ std::string body = ohttp::get_body_from_binary_response(dresponse_as_vec);
- int rv =
- transaction_->Read(buf, buf_size,
- base::BindOnce(&URLRequestHttpJob::OnReadCompleted,
- base::Unretained(this)));
-
- if (ShouldFixMismatchedContentLength(rv))
- rv = OK;
-
- if (rv == 0 || (rv < 0 && rv != ERR_IO_PENDING))
- DoneWithRequest(FINISHED);
-
- if (rv == ERR_IO_PENDING)
- read_in_progress_ = true;
+ // Copy the decapsulated response into the buffer
+ memcpy(buf->data(), body.c_str(), body.size());
+ return dresponse_len;
+ } else {
+ DoneWithRequest(FINISHED);
+ return rv;
+ }
+ } else {
+ // usual control flow
+ rv =
+ transaction_->Read(buf, buf_size,
+ base::BindOnce(&URLRequestHttpJob::OnReadCompleted,
+ base::Unretained(this)));
+ if (ShouldFixMismatchedContentLength(rv))
+ rv = OK;
- return rv;
+ if (rv == 0 || (rv < 0 && rv != ERR_IO_PENDING)) {
+ DoneWithRequest(FINISHED);
+ }
+ if (rv == ERR_IO_PENDING) {
+ read_in_progress_ = true;
+ }
+ return rv;
+ }
}
int64_t URLRequestHttpJob::GetTotalReceivedBytes() const {
diff --git a/net/url_request/url_request_http_job.h b/net/url_request/url_request_http_job.h
index 866c8287a9b95..aba86e500b2db 100644
--- a/net/url_request/url_request_http_job.h
+++ b/net/url_request/url_request_http_job.h
@@ -31,6 +31,7 @@
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_throttler_entry_interface.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
+#include "third_party/ohttp/ohttp.h"
namespace net {
@@ -60,7 +61,11 @@ class NET_EXPORT_PRIVATE URLRequestHttpJob : public URLRequestJob {
void SetEarlyResponseHeadersCallback(
ResponseHeadersCallback callback) override;
void SetResponseHeadersCallback(ResponseHeadersCallback callback) override;
-
+ std::vector<uint8_t> byte_data;
+ std::vector<uint8_t> response_data;
+ ohttp::OHTTP_HPKE_CTX* sender_context;
+ uint8_t client_enc[65]; // TODO: Don't hardcode this. However, variables not permitted.
+ size_t client_enc_len;
protected:
URLRequestHttpJob(URLRequest* request,
const HttpUserAgentSettings* http_user_agent_settings);
diff --git a/net/url_request/url_request_job.cc b/net/url_request/url_request_job.cc
index 0127bbcf86499..a856f21a9a2b1 100644
--- a/net/url_request/url_request_job.cc
+++ b/net/url_request/url_request_job.cc
@@ -4,6 +4,7 @@
#include "net/url_request/url_request_job.h"
+#include <iostream>
#include <utility>
#include "base/bind.h"
@@ -688,7 +689,6 @@ int URLRequestJob::ReadRawDataHelper(IOBuffer* buf,
// TODO(xunjieli): Make ReadRawData take in a callback rather than requiring
// subclass to call ReadRawDataComplete upon asynchronous completion.
int result = ReadRawData(buf, buf_size);
-
if (result != ERR_IO_PENDING) {
// If the read completes synchronously, either success or failure, invoke
// GatherRawReadStats so we can account for the completed read.
diff --git a/third_party/ohttp/BUILD.gn b/third_party/ohttp/BUILD.gn
new file mode 100644
index 0000000000000..e0cf6234938bc
--- /dev/null
+++ b/third_party/ohttp/BUILD.gn
@@ -0,0 +1,21 @@
+import("//testing/test.gni")
+
+source_set("ohttp") {
+ sources = [
+ "ohttp.cc",
+ ]
+ public = [
+ "ohttp.h"
+ ]
+ public_deps = [
+ "//third_party/boringssl",
+ ]
+}
+
+test("ohttp_test") {
+ sources = [ "ohttp_test.cc" ]
+ deps = [
+ ":ohttp",
+ "//testing/gtest:gtest",
+ ]
+}
\ No newline at end of file
diff --git a/third_party/ohttp/ohttp.cc b/third_party/ohttp/ohttp.cc
new file mode 100644
index 0000000000000..b88ca0d3e69e9
--- /dev/null
+++ b/third_party/ohttp/ohttp.cc
@@ -0,0 +1,864 @@
+#include <vector>
+#include <iostream>
+#include <stdexcept>
+
+#include "ohttp.h"
+#include "openssl/hkdf.h"
+#include "openssl/hpke.h"
+#include "openssl/err.h"
+#include "openssl/rand.h"
+
+namespace ohttp {
+
+ struct OHTTP_HPKE_CTX {
+ EVP_HPKE_CTX* internal_ctx;
+ };
+
+ OHTTP_HPKE_CTX* createHpkeContext() {
+ OHTTP_HPKE_CTX* ctx = new OHTTP_HPKE_CTX();
+ ctx->internal_ctx = EVP_HPKE_CTX_new();
+ return ctx;
+ }
+
+ void destroyHpkeContext(OHTTP_HPKE_CTX* ctx) {
+ EVP_HPKE_CTX_free(ctx->internal_ctx);
+ delete ctx;
+ }
+
+ struct OHTTP_HPKE_KEY {
+ EVP_HPKE_KEY* internal_key;
+ };
+
+ OHTTP_HPKE_KEY* createHpkeKey() {
+ OHTTP_HPKE_KEY* key = new OHTTP_HPKE_KEY();
+ key->internal_key = EVP_HPKE_KEY_new();
+ return key;
+ }
+
+ void destroyHpkeKey(OHTTP_HPKE_KEY* key) {
+ EVP_HPKE_KEY_free(key->internal_key);
+ delete key;
+ }
+
+ struct OHTTP_HPKE_KEM {
+ const EVP_HPKE_KEM* internal_kem;
+ };
+
+ OHTTP_HPKE_KEM* createHpkeKem() {
+ OHTTP_HPKE_KEM* kem = new OHTTP_HPKE_KEM();
+ kem->internal_kem = EVP_hpke_x25519_hkdf_sha256();
+ return kem;
+ }
+
+ void destroyHpkeKem(OHTTP_HPKE_KEM* kem) {
+ delete kem;
+ }
+
+ int OHTTP_HPKE_KEY_generate(OHTTP_HPKE_KEY* key, const OHTTP_HPKE_KEM* kem) {
+ return EVP_HPKE_KEY_generate(key->internal_key, kem->internal_kem);
+ }
+
+ bool OHTTP_HPKE_KEY_public_key(OHTTP_HPKE_KEY* key, uint8_t* out, size_t* out_len, size_t max_out) {
+ return EVP_HPKE_KEY_public_key(key->internal_key, out, out_len, max_out);
+ }
+
+ const int OHTTP_HPKE_MAX_PUBLIC_KEY_LENGTH = EVP_HPKE_MAX_PUBLIC_KEY_LENGTH;
+ const int OHTTP_HPKE_MAX_ENC_LENGTH = EVP_HPKE_MAX_ENC_LENGTH;
+
+ // Generates a config for a single keypair.
+ std::vector<uint8_t> generate_key_config(OHTTP_HPKE_KEY *keypair) {
+ // HPKE Symmetric Algorithms {
+ // HPKE KDF ID (16),
+ // HPKE AEAD ID (16),
+ // }
+
+ // Key Config {
+ // Key Identifier (8),
+ // HPKE KEM ID (16),
+ // HPKE Public Key (Npk * 8),
+ // HPKE Symmetric Algorithms Length (16) = 4..65532,
+ // HPKE Symmetric Algorithms (32) ...,
+ // }
+
+ std::vector<uint8_t> config;
+
+ // Key Identifier is always 0
+ config.push_back(0);
+
+ // KEM_ID
+ const EVP_HPKE_KEM *kem = EVP_HPKE_KEY_kem(keypair->internal_key);
+ const uint16_t kem_id = EVP_HPKE_KEM_id(kem);
+ const uint8_t kem_high_byte = (kem_id >> 8) & 0xFF;
+ const uint8_t kem_low_byte = kem_id & 0xFF;
+ config.push_back(kem_high_byte);
+ config.push_back(kem_low_byte);
+
+ // HPKE Public Key
+ uint8_t public_key[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH];
+ size_t public_key_len;
+ EVP_HPKE_KEY* internal_key = keypair->internal_key;
+ if (!EVP_HPKE_KEY_public_key(internal_key, public_key, &public_key_len, sizeof(public_key))) {
+ config.clear();
+ return config;
+ }
+ std::cout << "Public key length: " << public_key_len << std::endl;
+ config.insert(config.end(), public_key, public_key + public_key_len);
+
+ // Symmetric Algorithms Length
+ config.push_back(0);
+ config.push_back(4);
+
+ // Hardcoded KDF and AEAD IDs
+ config.push_back(0x00);
+ config.push_back(0x01);
+ config.push_back(0x00);
+ config.push_back(0x01);
+
+ // Each encoded configuration is prefixed with a 2-byte integer in
+ // network byte order that indicates the length of the key configuration
+ // in bytes. The length-prefixed encodings are concatenated to form a
+ // list.
+ const uint16_t length = config.size();
+ const uint8_t high_byte_length = (length >> 8) & 0xFF;
+ const uint8_t low_byte_length = length & 0xFF;
+ config.insert(config.begin(), low_byte_length);
+ config.insert(config.begin(), high_byte_length);
+
+ return config;
+ }
+
+ std::vector<uint8_t> get_public_key(std::vector<uint8_t> key_config) {
+ // Extract the public key from the key configuration.
+ // The public key starts at the 5th byte of the key configuration and is 32 bytes long.
+ std::vector<uint8_t> public_key(key_config.begin() + 5, key_config.begin() + 37);
+ return public_key;
+ }
+
+ // Helper to encode a string as a binary vector (length-prefixed)
+ std::vector<uint8_t> encode_string(const std::string& str) {
+ std::vector<uint8_t> result;
+
+ // WARNING: Cloudflare/Wood implementation uses just one byte
+ // where we would expect to see 2 per RFC 9292.
+
+ uint16_t length = str.size();
+ // TODO: Support QUIC-style multi-byte length encoding.
+ // see https://www.rfc-editor.org/rfc/rfc9000#section-16
+ // for now, just support short 6-bit lengths.
+ assert(length < 64); // 2^6
+ result.push_back(length & 0xFF);
+ result.insert(result.end(), str.begin(), str.end());
+ return result;
+ }
+
+ OhttpParseErrorCode get_next_encoded_string(std::vector<uint8_t>& input, int offset, std::vector<uint8_t>& out, int& bytes_used) {
+ // Assume length is the first byte.
+ // TODO: Deal with QUIQ-style multi-byte length encoding.
+ int len_bytes = 1;
+ int start_content = offset + len_bytes;
+ int end_offset = start_content + input[offset];
+ if (end_offset > int(input.size())) {
+ return OhttpParseErrorCode::ERR_BAD_OFFSET;
+ }
+ bytes_used = end_offset - start_content + len_bytes;
+ for (int i = start_content; i < end_offset; i++) {
+ out.push_back(input[i]);
+ }
+ return OhttpParseErrorCode::SUCCESS;
+ }
+
+ // Function to encode the POST request in binary format per RFC 9292
+ std::vector<uint8_t> get_binary_request(const std::string& method, const std::string& scheme, const std::string& host, const std::string& path, const std::string& body) {
+ std::vector<uint8_t> binary_request;
+
+ // Known-Length Request {
+ // Framing Indicator (i) = 0,
+ // Request Control Data (..),
+ // Known-Length Field Section (..),
+ // Known-Length Content (..),
+ // Known-Length Field Section (..),
+ // Padding (..),
+ // }
+
+ // Framing indicator. 0 for fixed length request.
+ std::vector<uint8_t> framing_indicator = {0};
+ binary_request.insert(binary_request.end(), framing_indicator.begin(), framing_indicator.end());
+
+ // Control Data
+ // Request Control Data {
+ // Method Length (i),
+ // Method (..),
+ // Scheme Length (i),
+ // Scheme (..),
+ // Authority Length (i),
+ // Authority (..),
+ // Path Length (i),
+ // Path (..),
+ // }
+
+ // Method Length & Method
+ std::vector<uint8_t> method_value_field = encode_string(method);
+ binary_request.insert(binary_request.end(), method_value_field.begin(), method_value_field.end());
+
+ // Scheme Length & Scheme
+ std::vector<uint8_t> scheme_value_field = encode_string(scheme);
+ binary_request.insert(binary_request.end(), scheme_value_field.begin(), scheme_value_field.end());
+
+ // Authority Length & Authority
+ std::string authority_value = host;
+ std::vector<uint8_t> authority_value_field = encode_string(authority_value);
+ binary_request.insert(binary_request.end(), authority_value_field.begin(), authority_value_field.end());
+
+ // Path Length & Path
+ std::vector<uint8_t> path_value_field = encode_string(path);
+ binary_request.insert(binary_request.end(), path_value_field.begin(), path_value_field.end());
+
+ // Header section.
+ // Known-Length Field Section {
+ // Length (i),
+ // Field Line (..) ...,
+ // }
+
+ // Length & Field Line
+ std::vector<uint8_t> no_fields = encode_string("");
+ binary_request.insert(binary_request.end(), no_fields.begin(), no_fields.end());
+
+ // Known-Length Content {
+ // Content Length (i),
+ // Content (..),
+ // }
+
+ // Content Length & Content
+ std::vector<uint8_t> body_field = encode_string(body);
+ binary_request.insert(binary_request.end(), body_field.begin(), body_field.end());
+
+ // Trailer section
+ // Known-Length Field Section {
+ // Length (i),
+ // Field Line (..) ...,
+ // }
+
+ // WARNING: Another deviation from cloudflare implementation
+
+ // Length & Field Line
+ binary_request.insert(binary_request.end(), no_fields.begin(), no_fields.end());
+
+ // No zero bytes.
+ return binary_request;
+ }
+
+ std::vector<uint8_t> get_quic_integer_as_bytes(uint64_t in) {
+ std::vector<uint8_t> encoded;
+
+ if (in <= 63ULL) {
+ encoded.push_back(static_cast<uint8_t>(in | 0b00'000000));
+ } else if (in <= 16383ULL) {
+ encoded.push_back(static_cast<uint8_t>((in >> 8) | 0b01'000000));
+ encoded.push_back(static_cast<uint8_t>(in & 0xFF));
+ } else if (in <= 1073741823ULL) {
+ encoded.push_back(static_cast<uint8_t>((in >> 24) | 0b10'000000));
+ encoded.push_back(static_cast<uint8_t>((in >> 16) & 0xFF));
+ encoded.push_back(static_cast<uint8_t>((in >> 8) & 0xFF));
+ encoded.push_back(static_cast<uint8_t>(in & 0xFF));
+ } else if (in <= 4611686018427387903ULL) {
+ encoded.push_back(static_cast<uint8_t>(((in >> 56) & 0b00111111) | 0b11'000000));
+ encoded.push_back(static_cast<uint8_t>((in >> 48) & 0xFF));
+ encoded.push_back(static_cast<uint8_t>((in >> 40) & 0xFF));
+ encoded.push_back(static_cast<uint8_t>((in >> 32) & 0xFF));
+ encoded.push_back(static_cast<uint8_t>((in >> 24) & 0xFF));
+ encoded.push_back(static_cast<uint8_t>((in >> 16) & 0xFF));
+ encoded.push_back(static_cast<uint8_t>((in >> 8) & 0xFF));
+ encoded.push_back(static_cast<uint8_t>(in & 0xFF));
+ }
+ return encoded;
+ }
+
+ uint64_t get_quic_integer_from_bytes(std::vector<uint8_t>& in) {
+ if (in.size() == 0) {
+ return -1;
+ }
+ uint64_t result = 0;
+ if (in.size() >= 1 && (in[0] & 0b11'000000) == 0b00'000000) {
+ result = in[0] & 0b00'111111;
+ in.erase(in.begin());
+ } else if (in.size() >= 2 && (in[0] & 0b11'000000) == 0b01'000000) {
+ result = (in[0] & 0b00'111111) << 8;
+ result |= in[1];
+ in.erase(in.begin(), in.begin() + 2);
+ } else if (in.size() >= 4 && (in[0] & 0b11'000000) == 0b10'000000) {
+ result = (in[0] & 0b00'111111) << 24;
+ result |= in[1] << 16;
+ result |= in[2] << 8;
+ result |= in[3];
+ in.erase(in.begin(), in.begin() + 4);
+ } else if (in.size() >= 8 && (in[0] & 0b11'000000) == 0b11'000000) {
+ result = static_cast<uint64_t>(in[0] & 0b00'111111) << 56;
+ result |= static_cast<uint64_t>(in[1]) << 48;
+ result |= static_cast<uint64_t>(in[2]) << 40;
+ result |= static_cast<uint64_t>(in[3]) << 32;
+ result |= static_cast<uint64_t>(in[4]) << 24;
+ result |= static_cast<uint64_t>(in[5]) << 16;
+ result |= static_cast<uint64_t>(in[6]) << 8;
+ result |= static_cast<uint64_t>(in[7]);
+ in.erase(in.begin(), in.begin() + 8);
+ } else {
+ return -1;
+ }
+ return result;
+ }
+
+ std::vector<uint8_t> get_binary_response(const int response_code, const std::vector<uint8_t>& content) {
+ // Known-Length Response {
+ std::vector<uint8_t> binary_response;
+ // Framing Indicator (i) = 1,
+ binary_response.push_back(1); // Known-Length Informational Response
+ // Known-Length Informational Response (..) ...,
+ // Final Response Control Data {
+ // Status Code (i) = 200..599,
+ std::vector<uint8_t> response_code_bytes = get_quic_integer_as_bytes(uint64_t(response_code));
+ binary_response.insert(binary_response.end(), response_code_bytes.begin(), response_code_bytes.end());
+ // }
+ // Known-Length Field Section {
+ binary_response.push_back(0);
+ // Length (i),
+ // Field Line (..) ...,
+ // }
+ // Known-Length Content {
+ // Content Length (i),
+ std::vector<uint8_t> content_size_bytes = get_quic_integer_as_bytes(uint64_t(content.size()));
+ binary_response.insert(binary_response.end(), content_size_bytes.begin(), content_size_bytes.end());
+ // Content (..),
+ binary_response.insert(binary_response.end(), content.begin(), content.end());
+ // }
+ // Known-Length Field Section {
+ binary_response.push_back(0);
+ // Length (i),
+ // Field Line (..) ...,
+ // }
+ // Padding (..),
+ // }
+ return binary_response;
+ }
+
+ std::string get_url_from_binary_request(const std::vector<uint8_t>& binary_request) {
+ std::string url;
+ int offset = 0;
+ offset += 1; // Skip framing indicator
+ offset += 1 + binary_request[offset]; // Skip method
+ size_t scheme_length = binary_request[offset];
+ // Add Scheme to URL
+ for (size_t i = 0; i < scheme_length; i++) {
+ url.push_back(static_cast<char>(binary_request[offset + 1 + i]));
+ }
+ url += "://";
+ offset += 1 + scheme_length; // Skip scheme length and scheme
+ size_t authority_length = binary_request[offset];
+ for (size_t i = 0; i < authority_length; i++) {
+ url.push_back(static_cast<char>(binary_request[offset + 1 + i]));
+ }
+ offset += 1 + authority_length; // Skip authority length and authority
+ size_t path_length = binary_request[offset];
+ for (size_t i = 0; i < path_length; i++) {
+ url.push_back(static_cast<char>(binary_request[offset + 1 + i]));
+ }
+ return url;
+ }
+
+ std::string get_method_from_binary_request(const std::vector<uint8_t>& binary_request) {
+ std::string method;
+ size_t method_len = binary_request[1];
+ for (size_t i = 2; i < 2 + method_len; i++) {
+ method.push_back(static_cast<char>(binary_request[i]));
+ }
+ return method;
+ }
+
+ std::string get_body_from_binary_request(const std::vector<uint8_t>& binary_request) {
+ std::string body;
+ int offset = 0;
+ offset += 1; // Skip framing indicator
+ offset += 1 + binary_request[offset]; // Skip method
+ offset += 1 + binary_request[offset]; // Skip scheme
+ offset += 1 + binary_request[offset]; // Skip authority
+ offset += 1 + binary_request[offset]; // Skip path
+ offset += 1 + binary_request[offset]; // Skip header section
+ size_t body_length = binary_request[offset];
+ for (size_t i = 1; i <= body_length; i++) {
+ body.push_back(static_cast<char>(binary_request[offset + i]));
+ }
+ return body;
+ }
+
+ std::string get_body_from_binary_response(const std::vector<uint8_t>& binary_response) {
+ // Known-Length Response {
+ // Framing Indicator (i) = 1,
+ // Known-Length Informational Response (..) ...,
+ // Final Response Control Data (..),
+ // Known-Length Field Section (..),
+ // Known-Length Content (..),
+ // Known-Length Field Section (..),
+ // Padding (..),
+ // }
+ std::vector<uint8_t> consumable_resp(binary_response.size());
+ std::copy(binary_response.begin(), binary_response.end(), consumable_resp.begin());
+ // Skip framing indicator
+ consumable_resp.erase(consumable_resp.begin());
+ // Assume no informational responses.