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
|
open Base
let policy = {|<?xml version="1.0" encoding="UTF-8"?>
<Policy
xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17
http://docs.oasis-open.org/xacml/3.0/xacml-core-v3-schema-wd-17.xsd"
PolicyId="urn:oasis:names:tc:xacml:3.0:example:SimplePolicy1"
Version="1.0"
RuleCombiningAlgId="urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:deny-overrides">
<Description>
Medi Corp access control policy
</Description>
<Target/>
<Rule
RuleId= "urn:oasis:names:tc:xacml:3.0:example:SimpleRule1"
Effect="Permit">
<Description>
Any subject with an e-mail name in the med.example.com domain
can perform any action on any resource.
</Description>
<Target>
<AnyOf>
<AllOf>
<Match
MatchId="urn:oasis:names:tc:xacml:1.0:function:rfc822Name-match">
<AttributeValue
DataType="http://www.w3.org/2001/XMLSchema#string"
>med.example.com</AttributeValue>
<AttributeDesignator
MustBePresent="false"
Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject"
AttributeId="urn:oasis:names:tc:xacml:1.0:subject:subject-id"
DataType="urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name"/>
</Match>
</AllOf>
</AnyOf>
</Target>
</Rule>
</Policy>
|}
type xml_element = (string * (string * string) list * Xml.xml list)
type lean_expression = Identifier of string
| Bool of bool
| String of string
| Number of int
| List of lean_expression list
| Tuple of lean_expression list
| Application of lean_expression * lean_expression list
let rec fmt_lean_expression : lean_expression -> string = function
| Identifier s -> s
| Bool b -> if b then "True" else "False"
| String s -> Printf.sprintf "\"%s\"" s
| Number n -> Int.to_string n
| List xs -> Printf.sprintf "[%s]" (String.concat ~sep:", " (List.map ~f:fmt_lean_expression xs))
| Tuple xs -> Printf.sprintf "(%s)" (String.concat ~sep:", " (List.map ~f:fmt_lean_expression xs))
| Application (head, args) -> Printf.sprintf "(%s %s)" (fmt_lean_expression head) (String.concat ~sep:" " (List.map ~f:fmt_lean_expression args))
let get_exn opt message =
match opt with
| Some x -> x
| _ -> failwith message
let rec get_attr (needle : string) : (string * string) list -> string option = function
| (k, v) :: xs -> if String.equal k needle then Some v else get_attr needle xs
| _ -> None
let get_attr_exn needle attrs : string =
get_exn (get_attr needle attrs) "missing attribute"
let rec get_children (kind : string) : Xml.xml list -> xml_element list = function
| (Element ((tag_name, _, _) as x)) :: xs ->
if String.equal tag_name kind
then x :: get_children kind xs
else get_children kind xs
| PCData s :: xs -> get_children kind xs
| _ -> []
let get_child kind xs = List.nth (get_children kind xs) 0
let get_child_exn kind xs =
get_exn (get_child kind xs) "no such child"
let xml_element_of_children (children : Xml.xml list) : xml_element list =
List.map children ~f:(function
| Element x -> x
| _ -> failwith "called on element with non-element children")
let compile_rule_combinator_algorithm : string -> lean_expression = function
| "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:deny-overrides" ->
(Identifier "RuleCombinatorAlgorithm.deny_overrides")
| "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:ordered-deny-overrides" ->
(Identifier "RuleCombinatorAlgorithm.ordered_deny_overrides")
| "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides" ->
(Identifier "RuleCombinatorAlgorithm.ordered_deny_overrides")
| "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:ordered-permit-overrides" ->
(Identifier "RuleCombinatorAlgorithm.ordered_permit_overrides")
| "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:deny-unless-permit" ->
(Identifier "RuleCombinatorAlgorithm.deny_unless_permit")
| "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-unless-deny" ->
(Identifier "RuleCombinatorAlgorithm.permit_unless_deny")
| "urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:first-applicable" ->
(Identifier "RuleCombinatorAlgorithm.first_applicable")
| "urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:deny-overrides" ->
(Identifier "RuleCombinatorAlgorithm.legacy_deny_overrides")
| "urn:oasis:names:tc:xacml:1.1:rule-combining-algorithm:ordered-deny-overrides" ->
(Identifier "RuleCombinatorAlgorithm.legacy_ordered_deny_overrides")
| "urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:permit-overrides" ->
(Identifier "RuleCombinatorAlgorithm.legacy_permit_overrides")
| "urn:oasis:names:tc:xacml:1.1:rule-combining-algorithm:ordered-permit-overrides" ->
(Identifier "RuleCombinatorAlgorithm.legacy_ordered_permit_overrides")
| _ -> failwith "unknown rule combinator"
(* let (tag_name, attributes, children) = elt in *)
let compile_attribute_value (elt : xml_element) : lean_expression =
let (tag_name, attrs, children) = elt in
assert (String.equal tag_name "AttributeValue");
let kind = get_attr_exn "DataType" attrs in
match List.nth_exn children 0 with
| PCData s -> (Tuple [(String kind); (String s)])
| _ -> failwith "invalid value expression"
let compile_attribute_designator (elt : xml_element) : lean_expression =
let (tag_name, attrs, children) = elt in
assert (String.equal tag_name "AttributeDesignator");
let category = get_attr_exn "Category" attrs in
let identifier = get_attr_exn "AttributeId" attrs in
let kind = get_attr_exn "DataType" attrs in
let must_be_present = get_attr_exn "MustBePresent" attrs in
let issuer = (match get_attr "Issuer" attrs with
| Some s -> (Application ((Identifier "some"), [(String s)]))
| _ -> (Application ((Identifier "none"), [])))
in
Application
((Identifier "AttributeDesignator.mk"),
[(String category);
(String identifier);
(Identifier kind);
issuer;
(Bool (Bool.of_string must_be_present))])
let compile_match (elt : xml_element) : lean_expression =
(* TODO: Can the match contain multiple value/designator pairs? *)
let (tag_name, attrs, children) = elt in
assert (String.equal tag_name "Match");
let alg_id = get_attr_exn "MatchId" attrs in
let value = get_child_exn "AttributeValue" children in
let designator = get_child_exn "AttributeDesignator" children in
Application
((Identifier "Match.mk"),
[(String alg_id);
compile_attribute_designator designator;
compile_attribute_value value])
let compile_all_of (elt : xml_element) : lean_expression =
let (tag_name, _, children) = elt in
assert (String.equal tag_name "AllOf");
if phys_equal (List.length children) 0
then failwith "empty <AllOf>"
else failwith "unimplemented"
let compile_any_of (elt : xml_element) : lean_expression =
let (tag_name, _, children) = elt in
assert (String.equal tag_name "AnyOf");
if phys_equal (List.length children) 0
then failwith "empty <AnyOf>"
else failwith "unimplemented"
let compile_target (elt : xml_element) : lean_expression =
let (tag_name, _, children) = elt in
assert (String.equal tag_name "Target");
List (List.map ~f:compile_any_of (xml_element_of_children children))
let compile_policy (attrs : (string * string) list) (children : Xml.xml list) : lean_expression =
let alg_id = get_attr_exn "RuleCombiningAlgId" attrs in
let target = get_child_exn "Target" children in
let target = compile_target target in
let metadata =
Application
((Identifier "Policy.mk"),
[(compile_rule_combinator_algorithm alg_id);
target;
(List []); (* TODO: Compile obligation expressions *)
(List [])]) (* TODO: Compile advice expressions *) in
let wrapped =
Application
((Identifier "PolicyElement.Policy"), [metadata]) in
Application
((Identifier "ElementTree.Node"), [wrapped; List []])
let compile : Xml.xml -> lean_expression = function
| Element (tag_name, attributes, children) ->
(match tag_name with
| "Policy" -> compile_policy attributes children
| _ -> failwith (Printf.sprintf "(%s)" tag_name))
| PCData s -> failwith s
let () = policy
|> Xml.parse_string
|> compile
|> fmt_lean_expression
|> Caml.print_endline
|