/
config.ml
2335 lines (2174 loc) · 83.3 KB
/
config.ml
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
module Log =
(val Logs.(
src_log
@@ Src.create ~doc:"Miragevpn library's configuration module"
"ovpn.config")
: Logs.LOG)
open Angstrom
module A = Angstrom
(* When adding a new constructor please update the sample
test/sample-configuration-files/inline-all.conf *)
type inlineable =
[ `Auth_user_pass
| `Ca
| `Connection
| `Pkcs12
| `Tls_auth of [ `Incoming | `Outgoing ] option
| `Tls_cert
| `Tls_key
| `Secret of [ `Incoming | `Outgoing ] option
| `Tls_crypt
| `Tls_crypt_v2 of bool
| `Peer_fingerprint ]
let string_of_inlineable = function
| `Auth_user_pass -> "auth-user-pass"
| `Ca -> "ca"
| `Connection -> "connection"
| `Pkcs12 -> "pkcs12"
| `Tls_auth _ -> "tls-auth"
| `Tls_cert -> "cert"
| `Tls_key -> "key"
| `Secret _ -> "secret"
| `Tls_crypt -> "tls-crypt"
| `Tls_crypt_v2 _ -> "tls-crypt-v2"
| `Peer_fingerprint -> "peer-fingerprint"
let default_inlineable_of_string = function
| "auth-user-pass" -> Some `Auth_user_pass
| "ca" -> Some `Ca
| "connection" -> Some `Connection
| "pkcs12" -> Some `Pkcs12
| "tls-auth" -> Some (`Tls_auth None)
| "cert" -> Some `Tls_cert
| "key" -> Some `Tls_key
| "secret" -> Some (`Secret None)
| "tls-crypt" -> Some `Tls_crypt
| "tls-crypt-v2" -> Some (`Tls_crypt_v2 false)
| "peer-fingerprint" -> Some `Peer_fingerprint
| _ -> None
type inline_or_path =
[ `Need_inline of inlineable | `Path of string * inlineable ]
let int_of_ping_interval = function
| `Not_configured -> 0
| `Seconds 0 ->
failwith
"Miragevpn.Config, internal invariant Ping_interval `Seconds <> 0 \
violated"
| `Seconds n -> n
let ( (cipher_to_cs : string -> Tls.Ciphersuite.ciphersuite),
(cs_to_cipher : Tls.Ciphersuite.ciphersuite -> string) ) =
let map =
[
( "TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384",
`ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 );
( "TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384",
`ECDHE_RSA_WITH_AES_256_GCM_SHA384 );
("TLS-DHE-RSA-WITH-AES-256-GCM-SHA384", `DHE_RSA_WITH_AES_256_GCM_SHA384);
( "TLS-ECDHE-ECDSA-WITH-CHACHA20-POLY1305-SHA256",
`ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 );
( "TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256",
`ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 );
( "TLS-DHE-RSA-WITH-CHACHA20-POLY1305-SHA256",
`DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 );
( "TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256",
`ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 );
( "TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256",
`ECDHE_RSA_WITH_AES_128_GCM_SHA256 );
("TLS-DHE-RSA-WITH-AES-128-GCM-SHA256", `DHE_RSA_WITH_AES_128_GCM_SHA256);
( "TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA384",
`ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 );
( "TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA384",
`ECDHE_RSA_WITH_AES_256_CBC_SHA384 );
("TLS-DHE-RSA-WITH-AES-256-CBC-SHA256", `DHE_RSA_WITH_AES_256_CBC_SHA256);
( "TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA256",
`ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 );
( "TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA256",
`ECDHE_RSA_WITH_AES_128_CBC_SHA256 );
("TLS-DHE-RSA-WITH-AES-128-CBC-SHA256", `DHE_RSA_WITH_AES_128_CBC_SHA256);
("TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA", `ECDHE_ECDSA_WITH_AES_256_CBC_SHA);
("TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA", `ECDHE_RSA_WITH_AES_256_CBC_SHA);
("TLS-DHE-RSA-WITH-AES-256-CBC-SHA", `DHE_RSA_WITH_AES_256_CBC_SHA);
("TLS-ECDHE-ECDSA-WITH-AES-128-CBC-SHA", `ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
("TLS-ECDHE-RSA-WITH-AES-128-CBC-SHA", `ECDHE_RSA_WITH_AES_128_CBC_SHA);
("TLS-DHE-RSA-WITH-AES-128-CBC-SHA", `DHE_RSA_WITH_AES_128_CBC_SHA);
]
in
let rev_map = List.map (fun (a, b) -> (b, a)) map in
( (fun s ->
match List.assoc_opt s map with
| None -> failwith ("unsupported tls-cipher " ^ s)
| Some c -> c),
fun c -> List.assoc c rev_map )
let ( (cipher13_to_cs13 : string -> Tls.Ciphersuite.ciphersuite13),
(cs13_to_cipher13 : Tls.Ciphersuite.ciphersuite13 -> string) ) =
let map =
[
("TLS_AES_256_GCM_SHA384", `AES_256_GCM_SHA384);
("TLS_CHACHA20_POLY1305_SHA256", `CHACHA20_POLY1305_SHA256);
("TLS_AES_128_GCM_SHA256", `AES_128_GCM_SHA256);
]
in
let rev_map = List.map (fun (a, b) -> (b, a)) map in
( (fun s ->
match List.assoc_opt s map with
| None -> failwith ("unsupported tls-ciphersuite " ^ s)
| Some c -> c),
fun c -> List.assoc c rev_map )
type aead_cipher = [ `AES_128_GCM | `AES_256_GCM | `CHACHA20_POLY1305 ]
type cipher = [ aead_cipher | `AES_256_CBC ]
let aead_cipher_to_string : aead_cipher -> string = function
| `AES_128_GCM -> "AES-128-GCM"
| `AES_256_GCM -> "AES-256-GCM"
| `CHACHA20_POLY1305 -> "CHACHA20-POLY1305"
let cipher_to_string : cipher -> string = function
| #aead_cipher as c -> aead_cipher_to_string c
| `AES_256_CBC -> "AES-256-CBC"
module Conf_map = struct
type flag = unit
(* Checklist when adding a new entry here:
- This file:
- Defaults.common
Declare default value as per `man openvpn`, if any.
- Conf_map.pp_b:
Enable config-serialization of the values
(must be in the format from `man openvpn`)
- a_config_entry:
Expose it to the parser
- Miragevpn.mli:Config 'a k:
Expose it in the mli interface.
Must add a docstring comment detailing which config entry the
key corresponds to, and its semantics if there can be any doubt.
*)
type 'a k =
| Auth : Mirage_crypto.Hash.hash k
| Auth_nocache : flag k
| Auth_retry : [ `Interact | `Nointeract | `None ] k
| Auth_user_pass : (string * string) k
| Auth_user_pass_verify : (string * [ `Via_env | `Via_file ]) k
| Bind
: (int option
* [ `Domain of [ `host ] Domain_name.t | `Ip of Ipaddr.t ] option)
option
k
| Ca : X509.Certificate.t list k
| Cipher
: [ `AES_256_CBC | `AES_128_GCM | `AES_256_GCM | `CHACHA20_POLY1305 ] k
| Comp_lzo : flag k
| Connect_retry : (int * int) k
| Connect_retry_max : [ `Unlimited | `Times of int ] k
| Connect_timeout : int k
| Data_ciphers : aead_cipher list k
| Dev : ([ `Tun | `Tap ] * string option) k
| Dhcp_disable_nbt : flag k
| Dhcp_dns : Ipaddr.t list k
| Dhcp_ntp : Ipaddr.t list k
| Dhcp_domain : [ `host ] Domain_name.t k
| Float : flag k
| Handshake_window : int k
| Ifconfig : (Ipaddr.t * Ipaddr.t) k
| Ifconfig_nowarn : flag k
| Key_derivation : [ `Tls_ekm ] k
| Link_mtu : int k
| Mssfix : int k
| Mute_replay_warnings : flag k
| Passtos : flag k
| Peer_fingerprint : Cstruct.t list k
| Persist_key : flag k
| Persist_tun : flag k
| Ping_interval : [ `Not_configured | `Seconds of int ] k
| Ping_timeout : [ `Restart of int | `Exit of int ] k
| Pkcs12 : X509.PKCS12.t k
| Port : int k
| Pull : flag k
| Proto
: ([ `Ipv6 | `Ipv4 ] option
* [ `Udp | `Tcp of [ `Server | `Client ] option ])
k
(* see socket.c:static const struct proto_names proto_names[] *)
| Remote
: ([ `Domain of [ `host ] Domain_name.t * [ `Ipv4 | `Ipv6 | `Any ]
| `Ip of Ipaddr.t ]
* int
* [ `Udp | `Tcp ])
list
k
| Remote_cert_tls : [ `Server | `Client ] k
| Remote_random : flag k
| Renegotiate_bytes : int k
| Renegotiate_packets : int k
| Renegotiate_seconds : int k
| Replay_window : (int * int) k
| Resolv_retry : [ `Infinite | `Seconds of int ] k
| Route
: ([ `Ip of Ipaddr.t | `Net_gateway | `Remote_host | `Vpn_gateway ]
* Ipaddr.Prefix.t option
* [ `Ip of Ipaddr.t | `Net_gateway | `Remote_host | `Vpn_gateway ]
option
* [ `Default | `Metric of int ])
list
k
| Route_delay : (int * int) k
| Route_gateway : [ `Ip of Ipaddr.t | `Default | `Dhcp ] k
| Route_metric : [ `Default | `Metric of int ] k
| Route_nopull : flag k
| Script_security : int k
| Secret
: ([ `Incoming | `Outgoing ] option
* Cstruct.t
* Cstruct.t
* Cstruct.t
* Cstruct.t)
k
| Server : Ipaddr.V4.Prefix.t k
| Tls_auth
: ([ `Incoming | `Outgoing ] option
* Cstruct.t
* Cstruct.t
* Cstruct.t
* Cstruct.t)
k
| Tls_cert : X509.Certificate.t k
| Tls_mode : [ `Client | `Server ] k
| Tls_key : X509.Private_key.t k
| Tls_timeout : int k
| Tls_version_min : (Tls.Core.tls_version * bool) k
| Tls_version_max : Tls.Core.tls_version k
| Tls_cipher : Tls.Ciphersuite.ciphersuite list k
| Tls_ciphersuite : Tls.Ciphersuite.ciphersuite13 list k
| Tls_crypt : Tls_crypt.t k
| Tls_crypt_v2_client : (Tls_crypt.t * Tls_crypt.Wrapped_key.t * bool) k
| Tls_crypt_v2_server : (Tls_crypt.V2_server.t * bool) k
| Topology : [ `Net30 | `P2p | `Subnet ] k
| Transition_window : int k
| Tun_mtu : int k
| Verb : int k
| Verify_client_cert : [ `None | `Optional | `Required ] k
| Verify_x509_name : [ `host ] Domain_name.t k
module K = struct
type 'a t = 'a k
let compare : type a b. a t -> b t -> (a, b) Gmap.Order.t =
fun a b ->
(* ensure they're plain variant tags: *)
let is_int x = Obj.(magic x |> is_int) in
assert (is_int a && is_int b);
match compare (Obj.magic a : int) (Obj.magic b) with
| 0 -> Obj.magic Gmap.Order.Eq (* GADT equality :-/ *)
| x when x < 0 -> Lt
| _ -> Gt
end
include Gmap.Make (K)
let is_valid_config t =
let open Result.Syntax in
let ensure_mem k err = if mem k t then Ok () else Error err in
(* let ensure_not k err = if not (mem k t) then Ok () else Error err in *)
if not (mem Tls_mode t) then
let* () =
Result.map_error
(fun err -> `Msg ("not a valid config: " ^ err))
(ensure_mem Cipher "config must specify 'cipher'")
in
if get Cipher t <> `AES_256_CBC then
Error (`Msg "only AES-256-CBC supported in static key mode")
else Ok ()
else if mem Ca t && mem Peer_fingerprint t then
Error
(`Msg
"While --ca and --peer-fingerprint are not mutually exclusive the \
semantics are unclear to us and thus not implemented")
else Ok ()
let cert_key_or_pkcs12 t =
match (find Tls_cert t, find Tls_key t, find Pkcs12 t) with
| Some cert, Some key, None ->
let cert_pubkey = X509.Certificate.public_key cert in
let key_pubkey = X509.Private_key.public key in
if
Cstruct.equal
(X509.Public_key.fingerprint cert_pubkey)
(X509.Public_key.fingerprint key_pubkey)
then Ok `Some
else Error "key and cert do not match"
| None, None, Some _ -> Ok `Some
| Some _, Some _, Some _ ->
Error "tls-cert, tls-key and pkcs12 are mutually exclusive"
| Some _, None, _ -> Error "missing tls-cert"
| None, Some _, _ -> Error "missing tls-key"
| None, None, None -> Ok `None
let is_valid_server_config t =
let ensure_mem k err = if mem k t then Ok () else Error err in
let open Result.Syntax in
let* () = is_valid_config t in
Result.map_error
(fun err -> `Msg ("not a valid server config: " ^ err))
(let* () = ensure_mem Bind "does not have a bind" in
let* () =
match find Tls_mode t with
| None | Some `Client -> Error "config must specify 'tls-server'"
| Some `Server -> Ok ()
in
let* _ = cert_key_or_pkcs12 t in
Ok ())
let is_valid_client_config t =
let open Result.Syntax in
let ensure_mem k err = if mem k t then Ok () else Error err in
let ensure_not k err = if not (mem k t) then Ok () else Error err in
let* () = is_valid_config t in
Result.map_error
(fun err -> `Msg ("not a valid client config: " ^ err))
(let* () = ensure_mem Remote "does not have a remote" in
let _todo = ensure_not in
let* () =
match (find Tls_mode t, find Secret t) with
| None, None | Some `Server, _ ->
Error "is a TLS server, not a TLS client"
| None, Some _ ->
Log.warn (fun m -> m "using non-forward secure static key mode");
Ok ()
| Some _, Some _ -> Error "both static secret and TLS mode specified"
| Some `Client, None -> (
match (find Auth_user_pass t, cert_key_or_pkcs12 t) with
| Some _, Ok _ | None, Ok `Some -> Ok ()
| None, Ok `None ->
Error
"config has neither user/password, nor TLS client \
certificate"
| _, (Error _ as e) -> e)
in
let* () =
if mem Remote_cert_tls t && get Remote_cert_tls t <> `Server then
Error "remote-cert-tls is not SERVER?!"
else Ok ()
in
ensure_not Tls_crypt_v2_server
"server tls-crypt-v2 key passed in tls-crypt-v2")
let pp_opt_direction ppf = function
| None -> ()
| Some `Incoming -> Fmt.pf ppf " 1"
| Some `Outgoing -> Fmt.pf ppf " 0"
let pp_key ppf (a, b, c, d) =
Fmt.pf ppf
"-----BEGIN OpenVPN Static key V1-----\n\
%a\n\
-----END OpenVPN Static key V1-----"
Fmt.(array ~sep:(any "\n") string)
(match Cstruct.concat [ a; b; c; d ] |> Hex.of_cstruct with
| `Hex h -> Array.init (256 / 16) (fun i -> String.sub h (i * 32) 32))
let pp_tls_crypt_client ppf key =
let lines = Tls_crypt.save_v1 key in
Seq.iter (Fmt.pf ppf "%s\n") lines
let pp_tls_crypt_v2_client ppf (key, wkc) =
let lines = Tls_crypt.save_tls_crypt_v2_client key wkc in
Seq.iter (Fmt.pf ppf "%s\n") lines
let pp_tls_crypt_v2_server ppf key =
let lines = Tls_crypt.V2_server.save key in
Seq.iter (Fmt.pf ppf "%s\n") lines
let pp_b ?(sep = Fmt.any "@.") ppf (b : b) =
let p () = Fmt.pf ppf in
let (B (k, v)) = b in
let pp_x509 ppf cert =
Fmt.pf ppf
"# CN: %S\n\
# Expiry: %a\n\
# Hosts: %a\n\
# Cert fingerprint (sha256): %a\n\
# Key fingerprint (sha256): %a\n\
%s"
(match
X509.(Distinguished_name.common_name (Certificate.subject cert))
with
| None -> "NO common name"
| Some x -> x)
Fmt.(pair ~sep:(any " -> ") Ptime.(pp_human ()) Ptime.(pp_human ()))
(X509.Certificate.validity cert)
X509.Host.Set.pp
(X509.Certificate.hostnames cert)
Hex.pp
(Hex.of_cstruct (X509.Certificate.fingerprint `SHA256 cert))
Hex.pp
(Hex.of_cstruct
(X509.Public_key.fingerprint ~hash:`SHA256
(X509.Certificate.public_key cert)))
(X509.Certificate.encode_pem cert |> Cstruct.to_string)
in
let pp_cert cert = p () "cert [inline]\n<cert>\n%a</cert>" pp_x509 cert in
let pp_ca certs =
p () "ca [inline]\n<ca>\n%a</ca>" Fmt.(list ~sep:(any "\n") pp_x509) certs
in
let pp_x509_private_key key =
p () "key [inline]\n<key>\n%s</key>"
(X509.Private_key.encode_pem key |> Cstruct.to_string)
in
let pp_tls_version ppf v =
Fmt.string ppf
(match v with
| `TLS_1_3 -> "1.3"
| `TLS_1_2 -> "1.2"
| `TLS_1_1 -> "1.1"
| `TLS_1_0 -> "1.0")
in
let pp_digest_algorithm ppf h =
Fmt.string ppf
(match h with
| `MD5 -> "MD5"
| `SHA1 -> "SHA1"
| `SHA224 -> "SHA224"
| `SHA256 -> "SHA256"
| `SHA384 -> "SHA384"
| `SHA512 -> "SHA512")
in
let pp_cipher ppf v = Fmt.string ppf (cipher_to_string (v :> cipher)) in
let pp_fingerprint ppf fp =
for i = 0 to Cstruct.length fp - 1 do
let a, b = Hex.of_char (Cstruct.get fp i) in
Fmt.pf ppf "%c%c" a b;
if i < Cstruct.length fp - 1 then Fmt.pf ppf ":"
done
in
match (k, v) with
| Auth, h -> p () "auth %a" pp_digest_algorithm h
| Auth_nocache, () -> p () "auth-nocache"
| Auth_retry, `None -> p () "auth-retry none"
| Auth_retry, `Nointeract -> p () "auth-retry nointeract"
| Auth_retry, `Interact -> p () "auth-retry interact"
| Auth_user_pass, (user, pass) ->
p ()
"auth-user-pass [inline]\n<auth-user-pass>\n%s\n%s\n</auth-user-pass>"
user pass
| Auth_user_pass_verify, (cmd, mode) ->
p () "auth-user-pass-verify %s %s" cmd
(match mode with `Via_file -> "via-file" | `Via_env -> "via-env")
| Bind, None -> p () "nobind"
| Bind, Some opts -> (
p () "bind";
(match fst opts with
| Some port -> p () "%alport %d" sep () port
| None -> ());
match snd opts with
| None -> ()
| Some (`Domain n) -> p () "%alocal %a" sep () Domain_name.pp n
| Some (`Ip ip) -> p () "%alocal %a" sep () Ipaddr.pp ip)
| Ca, ca -> pp_ca ca
| Cipher, cipher -> p () "cipher %a" pp_cipher cipher
| Comp_lzo, () -> p () "comp-lzo"
| Connect_retry, (low, high) -> p () "connect-retry %d %d" low high
| Connect_retry_max, `Unlimited -> p () "connect-retry-max unlimited"
| Connect_retry_max, `Times i -> p () "connect-retry-max %d" i
| Connect_timeout, seconds -> p () "connect-timeout %d" seconds
| Data_ciphers, cs ->
p () "data-ciphers %a" Fmt.(list ~sep:(any ":") pp_cipher) cs
| Dev, (`Tap, None) -> p () "dev tap"
| Dev, (`Tun, None) -> p () "dev tun"
| Dev, (typ, Some name) ->
p () "dev %s%adev-type %s" name sep ()
(match typ with `Tun -> "tun" | `Tap -> "tap")
| Dhcp_disable_nbt, () -> p () "dhcp-option disable-nbt"
| Dhcp_domain, n -> p () "dhcp-option domain %a" Domain_name.pp n
| Dhcp_ntp, ips ->
Fmt.(list ~sep @@ fun ppf -> pf ppf "dhcp-option ntp %a" Ipaddr.pp)
ppf ips
| Dhcp_dns, ips ->
Fmt.(list ~sep @@ fun ppf -> pf ppf "dhcp-option dns %a" Ipaddr.pp)
ppf ips
| Float, () -> p () "float"
| Handshake_window, seconds -> p () "hand-window %d" seconds
| Ifconfig, (local, remote) ->
p () "ifconfig %a %a" Ipaddr.pp local Ipaddr.pp remote
| Ifconfig_nowarn, () -> p () "ifconfig-nowarn"
| Key_derivation, `Tls_ekm -> p () "key-derivation tls-ekm"
| Link_mtu, i -> p () "link-mtu %d" i
| Ping_interval, n -> p () "ping %d" (int_of_ping_interval n)
| Ping_timeout, `Exit timeout -> p () "ping-exit %d" timeout
| Ping_timeout, `Restart timeout -> p () "ping-restart %d" timeout
| Mssfix, int -> p () "mssfix %d" int
| Mute_replay_warnings, () -> p () "mute-replay-warnings"
| Passtos, () -> p () "passtos"
| Peer_fingerprint, fps ->
p ()
"peer-fingerprint [inline]\n\
<peer-fingerprint>\n\
%a\n\
</peer-fingerprint>"
Fmt.(list ~sep:(any "\n") pp_fingerprint)
fps
| Persist_key, () -> p () "persist-key"
| Persist_tun, () -> p () "persist-tun"
| Pkcs12, p12 ->
p () "pkcs12 [inline]\n<pkcs12>\n%s\n</pkcs12>"
(Base64.encode_exn (Cstruct.to_string (X509.PKCS12.encode_der p12)))
| Port, port -> p () "port %u" port
| Proto, (ip_v, kind) ->
p () "proto %s%s%s"
(match kind with `Udp -> "udp" | `Tcp _ -> "tcp")
(match ip_v with Some `Ipv4 -> "4" | Some `Ipv6 -> "6" | None -> "")
(match kind with
| `Tcp (Some `Client) -> "-client"
| `Tcp (Some `Server) -> "-server"
| `Tcp None | `Udp -> "")
| Pull, () -> p () "pull"
| Remote, lst ->
p () "%a"
Fmt.(
list ~sep @@ fun ppf (endp, port, proto) ->
let pp_endpoint, ip_proto =
match endp with
| `Domain (name, prot) ->
( (fun ppf () -> Domain_name.pp ppf name),
match prot with `Ipv4 -> "4" | `Ipv6 -> "6" | `Any -> "" )
| `Ip ip ->
( (fun ppf () -> Ipaddr.pp ppf ip),
match ip with V4 _ -> "4" | V6 _ -> "6" )
in
pf ppf "remote %a %d %s%s" pp_endpoint () port
(match proto with `Udp -> "udp" | `Tcp -> "tcp")
ip_proto)
lst
| Remote_cert_tls, `Server -> p () "remote-cert-tls server"
| Remote_cert_tls, `Client -> p () "remote-cert-tls client"
| Remote_random, () -> p () "remote-random"
| Renegotiate_bytes, i -> p () "reneg-bytes %d" i
| Renegotiate_packets, i -> p () "reneg-pkts %d" i
| Renegotiate_seconds, i -> p () "reneg-sec %d" i
| Replay_window, (low, high) -> p () "replay-window %d %d" low high
| Resolv_retry, `Infinite -> p () "resolv-retry infinite"
| Resolv_retry, `Seconds i -> p () "resolv-retry %d" i
| Route, routes ->
routes
|> List.iter (fun (network, netmask, gateway, metric) ->
let pp_addr ppf v =
Fmt.pf ppf "%s"
(match v with
| `Ip ip -> Ipaddr.to_string ip
| `Net_gateway -> "net_gateway"
| `Remote_host -> "remote_host"
| `Vpn_gateway -> "vpn_gateway")
in
p () "route %a %a %a %s" pp_addr network
Fmt.(option ~none:(any "default") Ipaddr.Prefix.pp)
netmask
Fmt.(option ~none:(any "default") pp_addr)
gateway
(match metric with
| `Default -> "default"
| `Metric i -> string_of_int i))
| Route_delay, (n, w) -> p () "route-delay %d %d" n w
| Route_gateway, `Default -> p () "route-gateway default"
| Route_gateway, `Dhcp -> p () "route-gateway dhcp"
| Route_gateway, `Ip ip -> p () "route-gateway %a" Ipaddr.pp ip
| Route_nopull, () -> p () "route-nopull"
| Script_security, d -> p () "script-security %u" d
| Secret, (direction, a, b, c, d) ->
p () "secret [inline]%a\n<secret>\n%a\n</secret>" pp_opt_direction
direction pp_key (a, b, c, d)
| Server, cidr ->
p () "server %a %a" Ipaddr.V4.pp
(Ipaddr.V4.Prefix.address cidr)
Ipaddr.V4.pp
(Ipaddr.V4.Prefix.netmask cidr)
| Tls_auth, (direction, a, b, c, d) ->
p () "tls-auth [inline]%a\n<tls-auth>\n%a\n</tls-auth>" pp_opt_direction
direction pp_key (a, b, c, d)
| Route_metric, `Metric i -> p () "route-metric %d" i
| Route_metric, `Default -> p () "route-metric default"
| Tls_cert, cert -> pp_cert cert
| Tls_key, key -> pp_x509_private_key key
| Tls_version_min, (ver, or_highest) ->
p () "tls-version-min %a%s" pp_tls_version ver
(if or_highest then " or-highest" else "")
| Tls_version_max, ver -> p () "tls-version-max %a" pp_tls_version ver
| Tls_mode, `Server -> p () "tls-server"
| Tls_mode, `Client -> p () "tls-client"
| Tls_timeout, seconds -> p () "tls-timeout %d" seconds
| Tls_cipher, ciphers ->
p () "tls-cipher %a"
Fmt.(list ~sep:(any ":") string)
(List.map cs_to_cipher ciphers)
| Tls_ciphersuite, ciphers ->
p () "tls-ciphersuite %a"
Fmt.(list ~sep:(any ":") string)
(List.map cs13_to_cipher13 ciphers)
| Tls_crypt, key ->
p () "tls-crypt [inline]\n<tls-crypt>\n%a\n</tls-crypt>"
pp_tls_crypt_client key
| Tls_crypt_v2_client, (key, wkc, force_cookie) ->
p () "tls-crypt-v2 [inline] %s\n<tls-crypt-v2>\n%a\n</tls-crypt-v2>"
(if force_cookie then "force-cookie" else "allow-noncookie")
pp_tls_crypt_v2_client (key, wkc)
| Tls_crypt_v2_server, (key, force_cookie) ->
p () "tls-crypt-v2 [inline] %s<tls-crypt-v2>\n%a\n</tls-crypt-v2>"
(if force_cookie then "force-cookie" else "allow-noncookie")
pp_tls_crypt_v2_server key
| Topology, v ->
p () "topology %s"
(match v with
| `Net30 -> "net30"
| `P2p -> "p2p"
| `Subnet -> "subnet")
| Transition_window, seconds -> p () "tran-window %d" seconds
| Tun_mtu, int -> p () "tun-mtu %d" int
| Verb, int -> p () "verb %d" int
| Verify_client_cert, mode ->
p () "verify-client-cert %s"
(match mode with
| `None -> "none"
| `Optional -> "optional"
| `Required -> "require")
| Verify_x509_name, host ->
p () "verify-x509-name %a name" Domain_name.pp host
let pp_with_sep ?(sep = Fmt.any "@.") ppf t =
let minimized_t =
if find Tls_mode t = Some `Client && mem Pull t then (
Fmt.pf ppf "client%a" sep ();
remove Tls_mode t |> remove Pull)
else t
in
Fmt.(pf ppf "%a" (list ~sep pp_b) (bindings minimized_t))
let pp ppf t = pp_with_sep ppf t
end
module Defaults = struct
let common =
let open Conf_map in
empty
|> add Ping_interval `Not_configured
|> add Ping_timeout (`Restart 120)
|> add Renegotiate_seconds 3600
|> add Handshake_window 60 |> add Transition_window 3600
|> add Proto (None, `Udp)
|> add Auth `SHA1
|> add Data_ciphers [ `AES_128_GCM; `AES_256_GCM; `CHACHA20_POLY1305 ]
let client =
let open Conf_map in
common |> add Tls_timeout 2
|> add Bind (Some (None, None))
|> add Resolv_retry `Infinite |> add Auth_retry `None
|> add Connect_timeout 120
|> add Connect_retry_max `Unlimited
let server =
let open Conf_map in
common |> add Bind (Some (Some 1194, None))
end
open Conf_map
type block = [ `Inline of string * string ]
type non_block =
[ `Entries of b list
| `Entry of b
| `Comment of string
| `Ignored of string
| `Keepalive of int * int (* interval * timeout *)
| `Remote of
[ `Domain of [ `host ] Domain_name.t * [ `Ipv6 | `Ipv4 | `Any ]
| `Ip of Ipaddr.t ]
* [ `Port of int | `Default_rport ]
* [ `Udp | `Tcp ] option
| `Rport of int (* remote port number used by --remote option *)
| `Proto_force of [ `Tcp | `Udp ]
| `Socks_proxy of string * int * [ `Inline | `Path of string ]
| inline_or_path
| `Dev of string (* [dev name] stanza requiring a [dev-type]*)
| `Dev_type of [ `Tun | `Tap ] (* [dev-type] stanza requiring [dev name]*) ]
type line = [ block | non_block ]
let pp_line ppf (x : [< line ]) =
let v = Fmt.pf in
match x with
| `Remote (host, port, proto) ->
let pphost ppf () =
match host with
| `Domain (dom, _proto) -> Domain_name.pp ppf dom
| `Ip ip -> Ipaddr.pp ppf ip
in
v ppf "(remote %a, %s, %s)" pphost ()
(match port with
| `Default_rport -> "default"
| `Port i -> string_of_int i)
(match proto with
| None -> "any"
| Some `Tcp -> "tcp"
| Some `Udp -> "udp")
| `Entries bs -> v ppf "entries: @[<v>%a@]" Fmt.(list ~sep:(any "@,") pp_b) bs
| `Entry b -> v ppf "entry: %a" (fun v -> pp_b v) b
| `Comment s -> v ppf "# %s" s
| `Ignored s -> v ppf "# IGNORED: %s" s
| `Keepalive (interval, timeout) -> v ppf "keepalive %d %d" interval timeout
| `Rport d -> v ppf "rport %d" d
| `Proto_force `Tcp -> v ppf "proto-force tcp"
| `Proto_force `Udp -> v ppf "proto-force udp"
| `Socks_proxy _ -> v ppf "socks-proxy"
| `Inline (tag, content) -> v ppf "<%s>:%S" tag content
| `Path (fn, _) -> v ppf "inline-or-path: %s" fn
| `Need_inline _ -> v ppf "inline-or-path: Need_inline"
| `Dev name -> v ppf "dev %S" name
| `Dev_type `Tun -> v ppf "dev-type tun"
| `Dev_type `Tap -> v ppf "dev-type tap"
let a_comment =
(* MUST have # or ; at the first column of a line. *)
(char '#' <|> char ';')
*> skip_many (skip @@ function '\n' -> false | _ -> true)
let a_newline = choice [ string "\r\n" *> return (); char '\n' *> return () ]
let a_whitespace_unit = skip (function ' ' | '\t' -> true | _ -> false)
let a_single_param =
(* Handles single-quoted or double-quoted, using backslash as escaping char.*)
let rec escaped q acc : string Angstrom.t =
let again = escaped q in
let ret acc = return (String.concat "" (List.rev acc)) in
peek_char >>= function
| Some '\\' -> (
advance 1 >>= fun () ->
any_char >>= function
| q2 when q = q2 -> again (String.make 1 q :: acc)
| c -> again (String.make 1 c :: "\\" :: acc))
| Some q2 when q = q2 -> ret acc
| Some c -> advance 1 >>= fun () -> again (String.make 1 c :: acc)
| None ->
fail
("end of input while looking for matching " ^ String.make 1 q
^ " quote")
in
choice
[
char '\'' *> Angstrom.commit *> escaped '\'' [] <* char '\'';
char '"' *> Angstrom.commit *> escaped '"' [] <* char '"';
( take_while (function
| ' ' | '\t' | '\'' | '"' | '\n' -> false
| _ -> true)
>>= fun v ->
if
String.(contains_from v (min 0 @@ length v) '\'')
|| String.(contains_from v (min 0 @@ length v) '"')
then fail "unquoted parameter contains quote"
else return v );
]
let a_whitespace_or_comment = a_comment <|> a_whitespace_unit
let a_ign_whitespace = skip_many a_whitespace_or_comment
let a_ign_whitespace_no_comment = skip_many a_whitespace_unit
let a_whitespace = skip_many1 a_whitespace_or_comment
let not_control_char = function '\x00' .. '\x1f' | '\x7f' -> false | _ -> true
let a_line predicate = take_while predicate <* (end_of_line <|> end_of_input)
let a_number =
take_while1 (function '0' .. '9' -> true | _ -> false) >>= fun str ->
match int_of_string str with
| i when string_of_int i = str -> return i
| _ -> fail (Fmt.str "Invalid number: %S" str)
| exception _ -> fail (Fmt.str "Invalid number: %S" str)
let a_number_range min' max' =
a_number >>= function
| n when n <= max' && min' <= n -> return n
| n -> fail (Fmt.str "Number out of range: %d" n)
let a_client =
(* alias for --tls-client --pull *)
string "client" *> a_ign_whitespace
*> return (`Entries [ B (Tls_mode, `Client); B (Pull, ()) ])
let a_dev =
let a_device_name =
choice
[
( string "tun" *> a_line not_control_char >>| fun x ->
let name =
let x = String.trim x in
if x = "" then None else Some ("tun" ^ x)
in
(Some `Tun, name) );
( string "tap" *> a_line not_control_char >>| fun x ->
let name =
let x = String.trim x in
if x = "" then None else Some ("tap" ^ x)
in
(Some `Tap, name) );
(a_single_param >>| fun name -> (None, Some name));
]
in
string "dev" *> a_whitespace *> a_device_name >>| function
| Some typ, name -> `Entry (B (Dev, (typ, name)))
| None, Some name -> `Dev name
| None, None ->
failwith
"config dev stanza with no name or type; this is a bug in the config \
parser"
let a_dev_type =
(* this is used to specify the type of a [dev] stanza
giving a custom name from which the type cannot be inferred *)
string "dev-type" *> a_whitespace
*> choice [ string "tun" *> return `Tun; string "tap" *> return `Tap ]
>>| fun typ -> `Dev_type typ
let a_proto =
string "proto" *> a_whitespace
*> choice
[
string "tcp6-client" *> return (Some `Ipv6, `Tcp (Some `Client));
string "tcp6-server" *> return (Some `Ipv6, `Tcp (Some `Server));
string "tcp6" *> return (Some `Ipv6, `Tcp None);
string "tcp4-client" *> return (Some `Ipv4, `Tcp (Some `Client));
string "tcp4-server" *> return (Some `Ipv4, `Tcp (Some `Server));
string "tcp-client" *> return (None, `Tcp (Some `Client));
string "tcp-server" *> return (None, `Tcp (Some `Server));
string "tcp4" *> return (Some `Ipv4, `Tcp None);
string "tcp" *> return (None, `Tcp None);
string "udp6" *> return (Some `Ipv6, `Udp);
string "udp4" *> return (Some `Ipv4, `Udp);
string "udp" *> return (None, `Udp);
]
>>| fun prot -> `Entry (B (Proto, prot))
let a_proto_force =
string "proto-force" *> a_whitespace
*> choice [ string "tcp" *> return `Tcp; string "udp" *> return `Udp ]
>>| fun prot -> `Proto_force prot
let a_resolv_retry =
string "resolv-retry" *> a_whitespace
*> choice
[
string "infinite" *> return `Infinite;
(a_number >>= fun i -> return (`Seconds i));
]
>>| fun i -> `Entry (B (Resolv_retry, i))
let a_filepath kind =
choice
[
string "[inline]" *> return (`Need_inline kind);
(a_single_param >>| fun p -> `Path (p, kind));
]
let a_option_with_single_path name kind =
string name *> a_whitespace *> a_filepath kind
let a_ca = a_option_with_single_path "ca" `Ca
let a_ca_payload str =
match X509.Certificate.decode_pem_multiple (Cstruct.of_string str) with
| Ok certs -> Ok (B (Ca, certs))
| Error (`Msg msg) -> Error (Fmt.str "ca: invalid certificate(s): %s" msg)
let a_cert = a_option_with_single_path "cert" `Tls_cert
let a_cert_payload str =
match X509.Certificate.decode_pem (Cstruct.of_string str) with
| Ok cert -> Ok (B (Tls_cert, cert))
| Error (`Msg msg) -> Error (Fmt.str "cert: invalid certificate: %s" msg)
let a_key = a_option_with_single_path "key" `Tls_key
let a_key_payload str =
match X509.Private_key.decode_pem (Cstruct.of_string str) with
| Ok key -> Ok (B (Tls_key, key))
| Error (`Msg msg) -> Error ("no key found in x509 tls-key: " ^ msg)
let a_pkcs12_payload str =
match X509.PKCS12.decode_der (Cstruct.of_string str) with
| Error (`Msg msg) -> Error ("failed to decode PKCS12: " ^ msg)
| Ok p12 -> Ok (B (Pkcs12, p12))
let a_pkcs12 = a_option_with_single_path "pkcs12" `Pkcs12
let a_fingerprint =
(* We assume SHA256: 32 bytes, so 64 hex digits *)
let hex_digit =
any_char >>= function
| ('0' .. '9' | 'a' .. 'f' | 'A' .. 'F') as d -> return d
| d -> Fmt.kstr fail "invalid hex character %C" d
in
let hex_val = function
| '0' .. '9' as c -> Char.code c - Char.code '0'
| 'a' .. 'f' as c -> 10 + Char.code c - Char.code 'a'
| 'A' .. 'F' as c -> 10 + Char.code c - Char.code 'A'
| _ -> assert false
in
let byte =
hex_digit >>= fun a ->
hex_digit >>| fun b -> (hex_val a lsl 4) + hex_val b
in
count 31 (byte <* char ':') >>= fun hd ->
byte >>| fun tl ->
let buf = Cstruct.create 32 in
List.iteri (Cstruct.set_uint8 buf) hd;
Cstruct.set_uint8 buf 31 tl;
buf
let a_multi_fingerprint =
skip_many (a_whitespace_or_comment *> end_of_line)
*> many_till
(a_fingerprint <* end_of_line
<* skip_many (a_whitespace_or_comment *> end_of_line))
end_of_input
>>| fun fps -> B (Peer_fingerprint, fps)
let a_peer_fingerprint =
string "peer-fingerprint" *> a_whitespace
*> choice
[
string "[inline]" *> return (`Need_inline `Peer_fingerprint);
( choice [ a_fingerprint; fail "Bad fingerprint" ] >>| fun fp ->
`Entry (B (Peer_fingerprint, [ fp ])) );
]
let a_key_direction_option =
choice
[
a_whitespace *> string "0" *> return (Some `Outgoing);
a_whitespace *> string "1" *> return (Some `Incoming);
return None;
]
let a_key_direction =
string "key-direction" *> a_whitespace
*> choice
[
string "0" *> return (Some `Outgoing);
string "1" *> return (Some `Incoming);
]
let a_tls_auth =
let with_key_direction =
a_key_direction >>| fun direction -> (`Need_inline (), direction)
in
let with_tls_auth =
a_option_with_single_path "tls-auth" () >>= fun source ->
a_key_direction_option >>| fun direction -> (source, direction)
in
with_tls_auth <|> with_key_direction >>| fun (source, direction) ->
match source with
| `Need_inline () -> `Need_inline (`Tls_auth direction)
| `Path (path, ()) -> `Path (path, `Tls_auth direction)
let a_tls_crypt_v2 =
a_option_with_single_path "tls-crypt-v2" () >>= fun source ->
choice
[
a_whitespace *> string "force-cookie" *> return true;
a_whitespace *> string "allow-noncookie" *> return false;
return false;
(* default is allow-noncookie *)
]
>>| fun force_cookie ->
match source with
| `Need_inline () -> `Need_inline (`Tls_crypt_v2 force_cookie)
| `Path (path, ()) -> `Path (path, `Tls_crypt_v2 force_cookie)
let tls_crypt_v2_client_pem_name = "OpenVPN tls-crypt-v2 client key"
let tls_crypt_v2_server_pem_name = "OpenVPN tls-crypt-v2 server key"
let a_base64_line =
take_while (function
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '+' | '/' | '=' -> true
| _ -> false)
<* (end_of_line <|> fail "Invalid base64 character")
let inline_payload element =