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
|
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 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 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 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"
open Option.Let_syntax
(* let (tag_name, attributes, children) = elt in *)
let compile_attribute_value (elt : xml_element) : lean_expression option =
let (tag_name, attrs, children) = elt in
assert (String.equal tag_name "AttributeValue");
let%bind kind = get_attr "DataType" attrs in
match List.nth_exn children 0 with
| PCData s -> Some (Tuple [(String kind); (String s)])
| _ -> failwith "invalid value expression"
let compile_attribute_designator (elt : xml_element) : lean_expression option =
let (tag_name, attrs, children) = elt in
assert (String.equal tag_name "AttributeDesignator");
let%bind category = get_attr "Category" attrs in
let%bind identifier = get_attr "AttributeId" attrs in
let%bind kind = get_attr "DataType" attrs in
let%bind must_be_present = get_attr "MustBePresent" attrs in
let issuer = (match get_attr "Issuer" attrs with
| Some s -> (Application ((Identifier "some"), [(String s)]))
| _ -> (Application ((Identifier "none"), [])))
in
Some (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 option =
(* TODO: Can the match contain multiple value/designator pairs? *)
let (tag_name, attrs, children) = elt in
assert (String.equal tag_name "Match");
let%bind alg_id = get_attr "MatchId" attrs in
let%bind value = get_child "AttributeValue" children in
let%bind value = compile_attribute_value value in
let%bind designator = get_child "AttributeDesignator" children in
let%bind designator = compile_attribute_designator designator in
Some (Application
((Identifier "Match.mk"),
[(String alg_id);
designator;
value]))
let compile_all_of (elt : xml_element) : lean_expression option =
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 option =
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 rec option_of_maybe_list : 'a option list -> 'a list option = function
| Some x :: xs -> (match option_of_maybe_list xs with
| Some xs' -> Some (x::xs')
| None -> None)
| [] -> Some []
| _ -> None
let compile_target (elt : xml_element) : lean_expression option =
let (tag_name, _, children) = elt in
assert (String.equal tag_name "Target");
let%bind xs = List.map ~f:compile_any_of (xml_element_of_children children)
|> option_of_maybe_list in
Some (List xs)
(* failwith "unimplemented" *)
let compile_policy (attrs : (string * string) list) (children : Xml.xml list) : lean_expression option =
let%bind alg_id = get_attr "RuleCombiningAlgId" attrs in
let%bind target = get_child "Target" children in
let%bind target = compile_target target in
Some (Application
((Identifier "Policy.mk"),
[(compile_rule_combinator_algorithm alg_id);
target;
(List []); (* TODO: Compile obligation expressions *)
(List []) (* TODO: Compile advice expressions *)
]))
let compile : Xml.xml -> lean_expression option = 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 () =
match compile @@ Xml.parse_string policy with
| Some ast ->
ast
|> fmt_lean_expression
|> Caml.print_endline
| _ -> failwith "compilation failed!"
|