[64fa9ba] | 1 | #ifndef __ABAC_HH__ |
---|
| 2 | #define __ABAC_HH__ |
---|
| 3 | |
---|
[2af3b82] | 4 | #include <string> |
---|
[9dffbd6] | 5 | #include <vector> |
---|
| 6 | |
---|
[64fa9ba] | 7 | namespace ABAC { |
---|
| 8 | extern "C" { |
---|
| 9 | #include "abac.h" |
---|
| 10 | } |
---|
[8bd77b5] | 11 | static int debug=0; |
---|
| 12 | class Role; |
---|
| 13 | class Oset; |
---|
[d9c3886] | 14 | /*** |
---|
| 15 | ABAC::Constraint |
---|
[6e8997e] | 16 | Constraint on a data term. |
---|
| 17 | There are 3 types: |
---|
| 18 | - Role constraint on a principal |
---|
| 19 | - Oset constraint on a principal, or a data object |
---|
| 20 | - Range/List constraint on a data object |
---|
| 21 | It holds a ptr to a abac_condition_t structure |
---|
[d9c3886] | 22 | ***/ |
---|
[8bd77b5] | 23 | class Constraint { |
---|
| 24 | public: |
---|
[d9c3886] | 25 | /*** |
---|
[6e8997e] | 26 | f Constraint() |
---|
[ed3dc05] | 27 | default constructor, do not use, for swig only |
---|
[6e8997e] | 28 | f Constraint(const Constraint &) |
---|
[ed3dc05] | 29 | copy constructor, used for cloning a constraint |
---|
[6e8997e] | 30 | f ~Constraint() |
---|
[ed3dc05] | 31 | default destructor |
---|
[d9c3886] | 32 | ***/ |
---|
| 33 | Constraint() : m_constraint(NULL) { } |
---|
[8bd77b5] | 34 | Constraint(const Constraint &constraint) { |
---|
[0b3ac65] | 35 | m_constraint =abac_condition_dup(constraint.m_constraint); |
---|
| 36 | } |
---|
[8bd77b5] | 37 | ~Constraint() { |
---|
[0b3ac65] | 38 | if(m_constraint) abac_condition_free(m_constraint); |
---|
| 39 | } |
---|
[d9c3886] | 40 | /*** |
---|
[6e8997e] | 41 | f Constraint(Role &) |
---|
[ed3dc05] | 42 | constructor that takes a constraining role |
---|
| 43 | [role:?R[{role-constraint}] |
---|
[6e8997e] | 44 | f Constraint(Oset &) |
---|
[ed3dc05] | 45 | constructor that takes a constraining oset |
---|
| 46 | [oset:?O[{oset-constraint}] |
---|
| 47 | [urn:?F[keyid:$alpha_keyid].oset:documents([string:?P])] |
---|
[6e8997e] | 48 | f Constraint(abac_condition_t *) |
---|
[ed3dc05] | 49 | constructor that takes an abac_condition_t structure |
---|
[6e8997e] | 50 | f Constraint(char *) |
---|
[ed3dc05] | 51 | constructor that takes one of following string |
---|
| 52 | as its vartype to set up a range constraint: |
---|
| 53 | "integer" |
---|
| 54 | "urn" |
---|
| 55 | "float" |
---|
| 56 | "boolean" |
---|
| 57 | "time" |
---|
| 58 | "string" |
---|
| 59 | it should be followed with one or many of following utility |
---|
| 60 | calls. |
---|
[d9c3886] | 61 | ***/ |
---|
[0b3ac65] | 62 | Constraint(Role& role); |
---|
| 63 | Constraint(Oset& oset); |
---|
| 64 | Constraint(abac_condition_t *constraint): |
---|
[d9c3886] | 65 | m_constraint(abac_condition_dup(constraint)) |
---|
[0b3ac65] | 66 | { } |
---|
| 67 | Constraint(char *vartype) { |
---|
| 68 | m_constraint=abac_condition_create(vartype); |
---|
| 69 | } |
---|
[d9c3886] | 70 | /*** |
---|
[6e8997e] | 71 | f void add_constraint_integer_max(int) |
---|
[ed3dc05] | 72 | void add_constraint_integer_min(int) |
---|
| 73 | utility routines to setup a integer range constraint |
---|
| 74 | [integer:?I[10 .. 20]] |
---|
[6e8997e] | 75 | f void add_constraint_integer_target(int) |
---|
[ed3dc05] | 76 | utility routine to setup a integer list constraint |
---|
| 77 | [integer:?I[10,20]] |
---|
[d9c3886] | 78 | ***/ |
---|
[ca72963] | 79 | void add_constraint_integer_max(int val) { |
---|
[0b3ac65] | 80 | abac_condition_add_range_integer_item(m_constraint,abac_max_item_type(),val); |
---|
[ca72963] | 81 | } |
---|
| 82 | void add_constraint_integer_min(int val) { |
---|
[0b3ac65] | 83 | abac_condition_add_range_integer_item(m_constraint,abac_min_item_type(),val); |
---|
[ca72963] | 84 | } |
---|
| 85 | void add_constraint_integer_target(int val) { |
---|
[0b3ac65] | 86 | abac_condition_add_range_integer_item(m_constraint,abac_target_item_type(),val); |
---|
[ca72963] | 87 | } |
---|
[d9c3886] | 88 | /*** |
---|
[6e8997e] | 89 | f void add_constraint_float_max(float) |
---|
[ed3dc05] | 90 | void add_constraint_float_min(float) |
---|
| 91 | utility routines to setup a float range constraint |
---|
| 92 | [float:?F[1.0 .. 2.5]] |
---|
[6e8997e] | 93 | f void add_constraint_float_target(float) |
---|
[ed3dc05] | 94 | utility routine to setup a float list constraint |
---|
| 95 | [float:?F[0.5, 2.5]] |
---|
[d9c3886] | 96 | ***/ |
---|
[ca72963] | 97 | void add_constraint_float_max(float val) { |
---|
[0b3ac65] | 98 | abac_condition_add_range_float_item(m_constraint,abac_max_item_type(),val); |
---|
[ca72963] | 99 | } |
---|
| 100 | void add_constraint_float_min(float val) { |
---|
[0b3ac65] | 101 | abac_condition_add_range_float_item(m_constraint,abac_min_item_type(),val); |
---|
[ca72963] | 102 | } |
---|
| 103 | void add_constraint_float_target(float val) { |
---|
[0b3ac65] | 104 | abac_condition_add_range_float_item(m_constraint,abac_target_item_type(),val); |
---|
[ca72963] | 105 | } |
---|
[0b3ac65] | 106 | /*** |
---|
[6e8997e] | 107 | f void add_constraint_time_max(char*) |
---|
[ed3dc05] | 108 | void add_constraint_time_min(char*) |
---|
| 109 | utility routines to setup a time range constraint, |
---|
| 110 | takes quoted string values, beyond T is optional |
---|
[6e8997e] | 111 | [time:?F["20120228T" .. "20120228T090000"]] |
---|
| 112 | f void add_constraint_time_target(char*) |
---|
[ed3dc05] | 113 | utility routine to setup a time list constraint |
---|
[6e8997e] | 114 | [time:?M["20201101T182930","20201101T"]] |
---|
[0b3ac65] | 115 | ***/ |
---|
[ca72963] | 116 | void add_constraint_time_max(char* val) { |
---|
[0b3ac65] | 117 | abac_condition_add_range_time_item(m_constraint,abac_max_item_type(),val); |
---|
[ca72963] | 118 | } |
---|
| 119 | void add_constraint_time_min(char* val) { |
---|
| 120 | abac_condition_add_range_time_item(m_constraint,abac_min_item_type(),val); |
---|
| 121 | } |
---|
| 122 | void add_constraint_time_target(char* val) { |
---|
| 123 | abac_condition_add_range_time_item(m_constraint,abac_target_item_type(),val); |
---|
| 124 | } |
---|
[0b3ac65] | 125 | /*** |
---|
[6e8997e] | 126 | f void add_constraint_urn_target(char*) |
---|
[ed3dc05] | 127 | utility routine to setup a an urn list constraint |
---|
| 128 | [urn:?U["fileA","http://fileB"]] |
---|
[6e8997e] | 129 | f void add_constraint_string_target(char*) |
---|
[ed3dc05] | 130 | utility routine to setup a a string list constraint |
---|
| 131 | [string:?S["abc",'efg',"hij"]] |
---|
[6e8997e] | 132 | f void add_constraint_boolean_target(char*) |
---|
[ed3dc05] | 133 | utility routine to setup a a boolean list constraint |
---|
| 134 | [boolean:?B['true']] |
---|
[0b3ac65] | 135 | ***/ |
---|
| 136 | void add_constraint_urn_target(char* val) |
---|
| 137 | { abac_condition_add_range_urn_item(m_constraint,val); } |
---|
| 138 | void add_constraint_string_target(char* val) |
---|
| 139 | { abac_condition_add_range_string_item(m_constraint,val); } |
---|
| 140 | void add_constraint_boolean_target(char* val) |
---|
| 141 | { abac_condition_add_range_boolean_item(m_constraint,val); } |
---|
| 142 | /*** |
---|
[6e8997e] | 143 | f char *string() const |
---|
[ed3dc05] | 144 | returns literal string of the constraint |
---|
[6e8997e] | 145 | f char *typed_string() const |
---|
[ed3dc05] | 146 | returns typed literal string of the constraint |
---|
[0b3ac65] | 147 | ***/ |
---|
| 148 | char *string() const |
---|
| 149 | { return abac_condition_string(m_constraint); } |
---|
| 150 | char *typed_string() const |
---|
| 151 | { return abac_condition_typed_string(m_constraint); } |
---|
| 152 | /*** |
---|
[6e8997e] | 153 | f abac_condition_t *constraint() |
---|
[ed3dc05] | 154 | returns internal constraint structure |
---|
[0b3ac65] | 155 | ***/ |
---|
[8bd77b5] | 156 | abac_condition_t *constraint() |
---|
[0b3ac65] | 157 | { return m_constraint; } |
---|
[8bd77b5] | 158 | private: |
---|
| 159 | abac_condition_t *m_constraint; |
---|
| 160 | }; |
---|
| 161 | |
---|
[0b3ac65] | 162 | /*** |
---|
| 163 | ABAC::DataTerm |
---|
[ed3dc05] | 164 | A data term is associated with Role or Oset as a parameter that |
---|
| 165 | maybe be instantiated, or uninstantiated but being constrained, |
---|
[0b3ac65] | 166 | or as a principal oset term (standalone right handside of an oset |
---|
| 167 | policy rule). It holds a pointer to a abac_term_t structure and |
---|
| 168 | an optional constraint that may be imposed on this data term if it |
---|
| 169 | is indeed an unistantiated variable. |
---|
| 170 | ***/ |
---|
[8bd77b5] | 171 | class DataTerm { |
---|
| 172 | public: |
---|
[0b3ac65] | 173 | /*** |
---|
[6e8997e] | 174 | f DataTerm() |
---|
[ed3dc05] | 175 | default constructor, do not use, for swig only |
---|
[6e8997e] | 176 | f DataTerm(const DataTerm &) |
---|
[ed3dc05] | 177 | copy constructor, used for cloning a data term |
---|
[6e8997e] | 178 | f ~DataTerm() |
---|
[ed3dc05] | 179 | default destructor |
---|
[0b3ac65] | 180 | ***/ |
---|
[8bd77b5] | 181 | DataTerm() : m_term(NULL) { } // do not use: here for swig |
---|
[0b3ac65] | 182 | DataTerm(const DataTerm &dterm) { |
---|
| 183 | m_term =abac_term_dup(dterm.m_term); |
---|
| 184 | abac_condition_t *constraint=abac_term_constraint(m_term); |
---|
| 185 | if(constraint) m_cond=new Constraint(constraint); |
---|
| 186 | else m_cond=NULL; |
---|
| 187 | } |
---|
[8bd77b5] | 188 | ~DataTerm() { |
---|
[0b3ac65] | 189 | if(m_term) abac_term_free(m_term); |
---|
| 190 | if(m_cond) free(m_cond); |
---|
| 191 | } |
---|
| 192 | /*** ??? |
---|
[6e8997e] | 193 | f DataTerm(abac_term_t *) |
---|
[ed3dc05] | 194 | constructor to make data term from abac_term_t structure |
---|
[0b3ac65] | 195 | ***/ |
---|
| 196 | DataTerm(abac_term_t *term) { |
---|
| 197 | m_term=abac_term_dup(term); |
---|
| 198 | abac_condition_t *constraint=abac_term_constraint(m_term); |
---|
| 199 | if(constraint) m_cond=new Constraint(constraint); |
---|
| 200 | else m_cond=NULL; |
---|
| 201 | } |
---|
| 202 | /*** |
---|
[6e8997e] | 203 | f DataTerm(char*) |
---|
[ed3dc05] | 204 | constructor to make named principal data term for the oset RHS |
---|
[6e8997e] | 205 | f DataTerm(char*, char*, Constraint*) |
---|
[ed3dc05] | 206 | constructor for making a variable data term or an instantiated |
---|
| 207 | data term |
---|
[0b3ac65] | 208 | ***/ |
---|
[d037f54] | 209 | DataTerm(char *sha) { |
---|
[0b3ac65] | 210 | if(debug) printf("adding a Dataterm named principal(%s)\n",sha); |
---|
| 211 | int isnamed=1; |
---|
| 212 | int type=e_TERM_PRINCIPAL; |
---|
| 213 | m_term=abac_term_named_create(type,sha); |
---|
[d037f54] | 214 | } |
---|
[440ba20] | 215 | DataTerm(char* typenm, char *name, Constraint *cond=NULL) { |
---|
[0b3ac65] | 216 | int type=abac_verify_term_type(typenm); |
---|
| 217 | if(debug) printf("adding a Dataterm (%s)\n",name); |
---|
| 218 | if(type==ABAC_TERM_FAIL) |
---|
| 219 | abac_errx(1, "DataTerm, fail to create the term"); |
---|
| 220 | if(cond) { |
---|
| 221 | m_cond=cond; |
---|
| 222 | m_term=abac_term_create(type,name,cond->constraint()); |
---|
| 223 | } else { |
---|
| 224 | m_cond=NULL; |
---|
| 225 | m_term=abac_term_create(type,name,NULL); |
---|
[8bd77b5] | 226 | } |
---|
[0b3ac65] | 227 | } |
---|
| 228 | /*** |
---|
[6e8997e] | 229 | f char *string() const |
---|
[ed3dc05] | 230 | returns literal string of the data term |
---|
[6e8997e] | 231 | f char *typed_string() const |
---|
[ed3dc05] | 232 | returns typed literal string of the data term |
---|
[0b3ac65] | 233 | ***/ |
---|
| 234 | char *string() const |
---|
| 235 | { return abac_term_string(m_term); } |
---|
| 236 | char *typed_string() const |
---|
| 237 | { return abac_term_typed_string(m_term); } |
---|
| 238 | /*** |
---|
[6e8997e] | 239 | f bool is_time() const |
---|
[ed3dc05] | 240 | bool is_string() const |
---|
| 241 | bool is_urn() const |
---|
| 242 | bool is_integer() const |
---|
| 243 | returns true if data term is of certain type |
---|
[0b3ac65] | 244 | ***/ |
---|
| 245 | bool is_time() const |
---|
| 246 | { return abac_term_is_time_type(m_term); } |
---|
| 247 | bool is_string() const |
---|
| 248 | { return abac_term_is_string_type(m_term); } |
---|
| 249 | bool is_urn() const |
---|
| 250 | { return abac_term_is_urn_type(m_term); } |
---|
| 251 | bool is_integer() const |
---|
| 252 | { return abac_term_is_integer_type(m_term); } |
---|
| 253 | /*** |
---|
[6e8997e] | 254 | f int add_constraint(const Contraint&) |
---|
[ed3dc05] | 255 | utiltiy routine to add a constraint to this data term |
---|
[0b3ac65] | 256 | ***/ |
---|
| 257 | int add_constraint(const Constraint& cond) { |
---|
| 258 | m_cond=new Constraint(cond); |
---|
| 259 | abac_term_add_constraint(m_term, m_cond->constraint()); |
---|
| 260 | } |
---|
| 261 | /*** |
---|
[6e8997e] | 262 | f int type() const |
---|
[ed3dc05] | 263 | returns subtype of the data term |
---|
[6e8997e] | 264 | f char *name() const |
---|
[ed3dc05] | 265 | returns the name of the data term |
---|
[0b3ac65] | 266 | ***/ |
---|
| 267 | int type() const |
---|
| 268 | { abac_term_type(m_term); } |
---|
| 269 | char *name() const |
---|
| 270 | { return abac_term_name(m_term); } |
---|
| 271 | /*** ??? value |
---|
[6e8997e] | 272 | f char *value() const |
---|
[ed3dc05] | 273 | Not implemented |
---|
[0b3ac65] | 274 | ***/ |
---|
| 275 | char *value() const { } |
---|
| 276 | /*** |
---|
[6e8997e] | 277 | f abac_term_t *term() |
---|
[ed3dc05] | 278 | returns internal data term structure |
---|
[6e8997e] | 279 | f Constraint *constraint() |
---|
[ed3dc05] | 280 | returns internal constraint structure to the data term |
---|
[0b3ac65] | 281 | ***/ |
---|
| 282 | abac_term_t *term() |
---|
| 283 | { return m_term; } |
---|
| 284 | Constraint *constraint() |
---|
| 285 | { return m_cond; } |
---|
[8bd77b5] | 286 | private: |
---|
| 287 | abac_term_t *m_term; |
---|
| 288 | Constraint *m_cond; |
---|
| 289 | }; |
---|
| 290 | |
---|
| 291 | |
---|
[ed3dc05] | 292 | /*** |
---|
| 293 | ABAC::Role |
---|
[6e8997e] | 294 | A Role is role specification of a set of entitities for a principal |
---|
[ed3dc05] | 295 | ***/ |
---|
[2af3b82] | 296 | class Role { |
---|
| 297 | public: |
---|
[ed3dc05] | 298 | /*** |
---|
[6e8997e] | 299 | f Role() |
---|
[ed3dc05] | 300 | default constructor, do not use, for swig only |
---|
[6e8997e] | 301 | f Role(const Role &) |
---|
[ed3dc05] | 302 | copy constructor, used for cloning a role |
---|
[6e8997e] | 303 | f ~Role() |
---|
[ed3dc05] | 304 | default destructor |
---|
| 305 | ***/ |
---|
[8bd77b5] | 306 | Role() : m_role(NULL) { } // do not use: here for swig |
---|
[ed3dc05] | 307 | Role(const Role &role) { |
---|
| 308 | m_role = abac_aspect_dup(role.m_role); |
---|
| 309 | } |
---|
| 310 | ~Role() { |
---|
| 311 | if (m_role) abac_aspect_free(m_role); |
---|
| 312 | } |
---|
| 313 | /*** |
---|
[6e8997e] | 314 | f Role(abac_aspect_t*) |
---|
[ed3dc05] | 315 | constructor that takes an abac_aspect_t structure |
---|
[6e8997e] | 316 | f Role(char*) |
---|
[ed3dc05] | 317 | constructor that builds a bare bone role with just principal's name |
---|
[6e8997e] | 318 | f Role(char*, char*) |
---|
[ed3dc05] | 319 | constructor that builds a bare bone role with just principal's name |
---|
| 320 | and a role name |
---|
| 321 | ***/ |
---|
[8bd77b5] | 322 | Role(abac_aspect_t *role): m_role(abac_aspect_dup(role)) |
---|
[ed3dc05] | 323 | { } |
---|
[8bd77b5] | 324 | Role(char *principal_name) : |
---|
| 325 | m_role(abac_aspect_role_principal_create(principal_name)) |
---|
[ed3dc05] | 326 | { } |
---|
[8bd77b5] | 327 | Role(char *principal_name, char *role_name) : |
---|
| 328 | m_role(abac_aspect_role_create(principal_name, role_name)) |
---|
[ed3dc05] | 329 | { } |
---|
| 330 | /*** |
---|
[6e8997e] | 331 | f bool is_principal() const |
---|
[ed3dc05] | 332 | return true if the role is a principal object(made from |
---|
| 333 | a data term), the right hand side of, |
---|
| 334 | [keyid:A].role:r <- [keyid:B] |
---|
| 335 | ***/ |
---|
| 336 | bool is_principal() const |
---|
| 337 | { return abac_aspect_is_principal(m_role); } |
---|
| 338 | /*** |
---|
[6e8997e] | 339 | f bool is_linking() const |
---|
[ed3dc05] | 340 | returns true if the role is a linking role like |
---|
| 341 | the right hand side of, |
---|
| 342 | [keyid:A].role:r1 <- [keyid:B].role:r2.role:r3 |
---|
| 343 | ***/ |
---|
| 344 | bool is_linking() const |
---|
| 345 | { return abac_aspect_is_linking(m_role); } |
---|
| 346 | /*** |
---|
[6e8997e] | 347 | f char *string() const |
---|
[ed3dc05] | 348 | returns literal string of the role |
---|
[6e8997e] | 349 | f char *typed_string() const |
---|
[ed3dc05] | 350 | returns typed literal string of the role |
---|
| 351 | ***/ |
---|
| 352 | char *string() const |
---|
| 353 | { return abac_aspect_string(m_role); } |
---|
| 354 | char *typed_string() |
---|
| 355 | { return abac_aspect_typed_string(m_role); } |
---|
| 356 | /*** |
---|
[6e8997e] | 357 | f char *linked_role() const |
---|
[ed3dc05] | 358 | returns linked part of a linking role, for |
---|
[6e8997e] | 359 | [keyid:A].role:r1.role:r2, it returns r1 |
---|
[ed3dc05] | 360 | ***/ |
---|
| 361 | char *linked_role() const |
---|
| 362 | { return abac_aspect_linked_role_name(m_role); } |
---|
| 363 | /*** |
---|
[6e8997e] | 364 | f char *role_name() const |
---|
[ed3dc05] | 365 | returns the role name of any role (the part after the last dot) |
---|
[6e8997e] | 366 | [keyid:A].role.r1.role:r2, it returns r2 |
---|
| 367 | [keyid:A].role.r1, it returns r1 |
---|
[ed3dc05] | 368 | ***/ |
---|
| 369 | char *role_name() const |
---|
| 370 | { return abac_aspect_aspect_name(m_role); } |
---|
| 371 | char *principal() const |
---|
| 372 | { return abac_aspect_principal_name(m_role); } |
---|
| 373 | |
---|
| 374 | /*** |
---|
[6e8997e] | 375 | f int add_data_term(DataTerm&) |
---|
[ed3dc05] | 376 | add a data term to the role |
---|
| 377 | ***/ |
---|
[8bd77b5] | 378 | int add_data_term(DataTerm& d) { |
---|
[ed3dc05] | 379 | abac_aspect_add_param(m_role, d.term()); |
---|
| 380 | return 1; |
---|
| 381 | } |
---|
| 382 | /*** |
---|
[6e8997e] | 383 | f std::vector<DataTerm> get_data_terms(bool &) |
---|
[ed3dc05] | 384 | return the data terms bound to this role. |
---|
| 385 | ??? If the role is returned in a proof, these will all have values. |
---|
| 386 | ***/ |
---|
[d037f54] | 387 | std::vector<DataTerm> get_data_terms(bool &success) { |
---|
[ed3dc05] | 388 | abac_term_t **terms, **end; |
---|
| 389 | int i; |
---|
| 390 | terms = abac_param_list_vectorize(abac_aspect_aspect_params(m_role)); |
---|
| 391 | for (i = 0; terms[i] != NULL; ++i) |
---|
| 392 | ; |
---|
| 393 | end = &terms[i]; |
---|
| 394 | std::vector<DataTerm> dataterms = std::vector<DataTerm>(terms, end); |
---|
| 395 | abac_terms_free(terms); |
---|
| 396 | success=1; |
---|
| 397 | return dataterms; |
---|
| 398 | } |
---|
[8bd77b5] | 399 | int add_linking_data_term(DataTerm& d) { |
---|
[ed3dc05] | 400 | abac_aspect_add_linked_param(m_role, d.term()); |
---|
| 401 | return 1; |
---|
| 402 | } |
---|
| 403 | /*** |
---|
[6e8997e] | 404 | f std::vector<DataTerm> get_linked_data_terms(bool &) |
---|
[ed3dc05] | 405 | return the data terms bound to this role's linking role. |
---|
| 406 | ??? If the role is returned in a proof, these will all have values. |
---|
| 407 | ***/ |
---|
[d037f54] | 408 | std::vector<DataTerm> get_linked_data_terms(bool &success) { |
---|
[ed3dc05] | 409 | abac_term_t **terms, **end; |
---|
| 410 | int i; |
---|
| 411 | terms = abac_param_list_vectorize(abac_aspect_linked_role_params(m_role)); |
---|
| 412 | for (i = 0; terms[i] != NULL; ++i) |
---|
| 413 | ; |
---|
| 414 | end = &terms[i]; |
---|
| 415 | std::vector<DataTerm> dataterms = std::vector<DataTerm>(terms, end); |
---|
| 416 | abac_terms_free(terms); |
---|
| 417 | success=1; |
---|
| 418 | return dataterms; |
---|
| 419 | } |
---|
| 420 | /*** |
---|
[6e8997e] | 421 | f abac_aspect_t *role() |
---|
[ed3dc05] | 422 | returns the interal libabac representation of this role |
---|
| 423 | ***/ |
---|
[8bd77b5] | 424 | abac_aspect_t *role() {return m_role;} |
---|
[2af3b82] | 425 | private: |
---|
[8bd77b5] | 426 | abac_aspect_t *m_role; |
---|
[2af3b82] | 427 | }; |
---|
| 428 | |
---|
[ed3dc05] | 429 | /*** |
---|
| 430 | ABAC::Oset |
---|
[6e8997e] | 431 | An Oset is oset specification of a set of entitities for a principal |
---|
[ed3dc05] | 432 | ***/ |
---|
[8bd77b5] | 433 | class Oset { |
---|
[9dffbd6] | 434 | public: |
---|
[ed3dc05] | 435 | /*** |
---|
[6e8997e] | 436 | f Oset() |
---|
[ed3dc05] | 437 | default constructor, do not use, for swig only |
---|
[6e8997e] | 438 | f Oset(const Oset &) |
---|
[ed3dc05] | 439 | copy constructor, used for cloning an oset |
---|
[6e8997e] | 440 | f ~Oset() |
---|
[ed3dc05] | 441 | default destructor |
---|
| 442 | ***/ |
---|
[8bd77b5] | 443 | Oset() : m_oset(NULL) { } // do not use: here for swig |
---|
[ed3dc05] | 444 | Oset(const Oset &oset) |
---|
| 445 | { m_oset =abac_aspect_dup(oset.m_oset); } |
---|
| 446 | ~Oset() |
---|
| 447 | { if(m_oset) abac_aspect_free(m_oset); } |
---|
| 448 | /*** |
---|
[6e8997e] | 449 | f Oset(abac_aspect_t *) |
---|
[ed3dc05] | 450 | constructor that takes abac_aspect_t structure |
---|
[6e8997e] | 451 | f Oset(char *) |
---|
[ed3dc05] | 452 | constructor that makes a principal oset, ie [keyid:B] |
---|
[6e8997e] | 453 | f Oset(char *, char *) |
---|
[ed3dc05] | 454 | constructor that makes a regular oset, ie. [keyid:B].oset:o |
---|
[6e8997e] | 455 | f Oset(DataTerm&) |
---|
[ed3dc05] | 456 | constructor that makes an object oset, ie. [urn:'file/fileA'] |
---|
| 457 | ***/ |
---|
[8bd77b5] | 458 | Oset(abac_aspect_t *oset): m_oset(abac_aspect_dup(oset)) |
---|
[ed3dc05] | 459 | { } |
---|
[5110d42] | 460 | Oset(char *oset_name) : m_oset(abac_aspect_oset_principal_create(oset_name)) |
---|
[ed3dc05] | 461 | { } |
---|
[8bd77b5] | 462 | Oset(char *principal_name, char *oset_name) : |
---|
| 463 | m_oset(abac_aspect_oset_create(principal_name, oset_name)) |
---|
[ed3dc05] | 464 | { } |
---|
[5110d42] | 465 | Oset(DataTerm& d) : |
---|
| 466 | m_oset(abac_aspect_oset_object_create(d.term())) |
---|
[ed3dc05] | 467 | { } |
---|
| 468 | |
---|
| 469 | /*** |
---|
[6e8997e] | 470 | f bool is_object(), ie <- [integer:10] |
---|
[ed3dc05] | 471 | return ture if this oset is an object oset |
---|
| 472 | ***/ |
---|
| 473 | bool is_object() const |
---|
| 474 | { return abac_aspect_is_object(m_oset); } |
---|
| 475 | /*** |
---|
[6e8997e] | 476 | f bool is_principal() const |
---|
[ed3dc05] | 477 | return true if the oset is a principal object(made from |
---|
| 478 | a data term), the right hand side of, |
---|
| 479 | [keyid:A].oset:o <- [keyid:B] |
---|
| 480 | ***/ |
---|
| 481 | bool is_principal() const |
---|
| 482 | { return abac_aspect_is_principal(m_oset); } |
---|
| 483 | /*** |
---|
[6e8997e] | 484 | f bool is_linking() const |
---|
[ed3dc05] | 485 | returns true if the oset is a linking oset like |
---|
| 486 | the right hand side of, |
---|
| 487 | [keyid:A].oset:o1 <- [keyid:B].role:r1.oset:o2 |
---|
| 488 | ***/ |
---|
| 489 | bool is_linking() const |
---|
| 490 | { return abac_aspect_is_linking(m_oset); } |
---|
| 491 | /*** |
---|
[6e8997e] | 492 | f char *string() const |
---|
[ed3dc05] | 493 | returns literal string of the oset |
---|
[6e8997e] | 494 | f char *typed_string() const |
---|
[ed3dc05] | 495 | returns typed literal string of the oset |
---|
| 496 | ***/ |
---|
| 497 | char *string() const |
---|
| 498 | { return abac_aspect_string(m_oset); } |
---|
| 499 | char *typed_string() |
---|
| 500 | { return abac_aspect_typed_string(m_oset); } |
---|
| 501 | /*** |
---|
[6e8997e] | 502 | f char *linked_role() const |
---|
[ed3dc05] | 503 | returns linked part of a linking oset, for |
---|
[6e8997e] | 504 | [keyid:A].role:r1.oset:o1, it returns r1 |
---|
[ed3dc05] | 505 | ***/ |
---|
| 506 | char *linked_role() const |
---|
| 507 | { return abac_aspect_linked_role_name(m_oset); } |
---|
| 508 | /*** |
---|
[6e8997e] | 509 | f char *oset_name() const |
---|
[ed3dc05] | 510 | returns oset name, |
---|
[6e8997e] | 511 | [keyid:A].role:r1.oset:o1, it returns o1 |
---|
| 512 | [keyid:A].oset:o1, it returns o1 |
---|
[ed3dc05] | 513 | ***/ |
---|
| 514 | char *oset_name() const |
---|
| 515 | { return abac_aspect_aspect_name(m_oset); } |
---|
| 516 | /*** |
---|
[6e8997e] | 517 | f char *principal() const |
---|
[ed3dc05] | 518 | returns principal name, |
---|
[6e8997e] | 519 | [keyid:A].role:r1.oset:o1, it returns A |
---|
[ed3dc05] | 520 | ***/ |
---|
| 521 | char *principal() const |
---|
| 522 | { return abac_aspect_principal_name(m_oset); } |
---|
| 523 | /*** |
---|
[6e8997e] | 524 | f char *object() const |
---|
[ed3dc05] | 525 | returns object's name when the oset is a principal object |
---|
[6e8997e] | 526 | [keyid:A].oset:values <- [integer:10], it returns 10 |
---|
[ed3dc05] | 527 | ***/ |
---|
| 528 | char *object() const |
---|
| 529 | { return abac_aspect_object_name(m_oset); } |
---|
| 530 | /*** |
---|
[6e8997e] | 531 | f int add_data_term(DataTerm&) |
---|
[ed3dc05] | 532 | add a data term to this oset's parameter set |
---|
| 533 | always returns 1 |
---|
| 534 | ***/ |
---|
[8bd77b5] | 535 | int add_data_term(DataTerm& d) { |
---|
[ed3dc05] | 536 | abac_aspect_add_param(m_oset, d.term()); |
---|
| 537 | return 1; |
---|
| 538 | } |
---|
| 539 | /*** |
---|
[6e8997e] | 540 | f std::vector<DataTerm> get_data_terms(bool &) |
---|
[ed3dc05] | 541 | returns the data terms bound to this oset. |
---|
| 542 | ??? If the oset is returned in a proof, these will all have values. |
---|
| 543 | ***/ |
---|
[d037f54] | 544 | std::vector<DataTerm> get_data_terms(bool &success) { |
---|
[ed3dc05] | 545 | abac_term_t **terms, **end; |
---|
| 546 | int i; |
---|
| 547 | terms = abac_param_list_vectorize(abac_aspect_aspect_params(m_oset)); |
---|
| 548 | for (i = 0; terms[i] != NULL; ++i) |
---|
| 549 | ; |
---|
| 550 | end = &terms[i]; |
---|
| 551 | std::vector<DataTerm> dataterms = std::vector<DataTerm>(terms, end); |
---|
| 552 | abac_terms_free(terms); |
---|
| 553 | success=1; |
---|
| 554 | return dataterms; |
---|
| 555 | } |
---|
| 556 | /*** |
---|
[6e8997e] | 557 | f int add_linking_data_term(DataTerm&) |
---|
[ed3dc05] | 558 | add a data term to this oset's linking role's parameter set. |
---|
| 559 | always returns 1 |
---|
| 560 | ***/ |
---|
[8bd77b5] | 561 | int add_linking_data_term(DataTerm& d) { |
---|
[ed3dc05] | 562 | abac_aspect_add_linked_param(m_oset, d.term()); |
---|
| 563 | return 1; |
---|
| 564 | } |
---|
| 565 | /*** |
---|
[6e8997e] | 566 | f std::vector<DataTerm> get_linked_data_terms(bool &) |
---|
[ed3dc05] | 567 | returns the data terms bound to this oset's linking role. |
---|
| 568 | ??? If the oset is returned in a proof, these will all have values. |
---|
| 569 | ***/ |
---|
[d037f54] | 570 | std::vector<DataTerm> get_linked_data_terms(bool &success) { |
---|
[ed3dc05] | 571 | abac_term_t **terms, **end; |
---|
| 572 | int i; |
---|
| 573 | terms = abac_param_list_vectorize(abac_aspect_linked_role_params(m_oset)); |
---|
| 574 | for (i = 0; terms[i] != NULL; ++i) |
---|
| 575 | ; |
---|
| 576 | end = &terms[i]; |
---|
| 577 | std::vector<DataTerm> dataterms = std::vector<DataTerm>(terms, end); |
---|
| 578 | abac_terms_free(terms); |
---|
| 579 | success=1; |
---|
| 580 | return dataterms; |
---|
| 581 | } |
---|
| 582 | /*** |
---|
[6e8997e] | 583 | f abac_aspect_t *oset() |
---|
[ed3dc05] | 584 | returns the internal libabac representation of the oset |
---|
| 585 | ***/ |
---|
[8bd77b5] | 586 | abac_aspect_t *oset() {return m_oset;} |
---|
[9dffbd6] | 587 | private: |
---|
[8bd77b5] | 588 | abac_aspect_t *m_oset; |
---|
[9dffbd6] | 589 | }; |
---|
| 590 | |
---|
[8bd77b5] | 591 | |
---|
[1621520] | 592 | /*** |
---|
| 593 | ABAC::ID |
---|
| 594 | An ID holds a principal credential. It maybe imported from an existing |
---|
| 595 | ID credential via external files, constructed from a streaming chunk, |
---|
| 596 | or instantiated on the fly |
---|
| 597 | ***/ |
---|
[8bd77b5] | 598 | class ID { |
---|
[64fa9ba] | 599 | public: |
---|
[1621520] | 600 | /*** |
---|
[6e8997e] | 601 | f ID() |
---|
[1621520] | 602 | default constructor, do not use, for swig only |
---|
[6e8997e] | 603 | f ID(const ID &) |
---|
[1621520] | 604 | copy constructor, used for cloning an ID |
---|
[6e8997e] | 605 | f ~ID() |
---|
[1621520] | 606 | default destructor |
---|
| 607 | ***/ |
---|
[8bd77b5] | 608 | ID() : m_id(NULL) { } // do not use: here for swig |
---|
| 609 | ID(const ID &id) { m_id =abac_id_dup(id.m_id); } |
---|
| 610 | ~ID() { if(m_id) abac_id_free(m_id); } |
---|
[1621520] | 611 | /*** ??? |
---|
[6e8997e] | 612 | f ID(abac_id_t *) |
---|
[1621520] | 613 | constructor from abac_id_t |
---|
[6e8997e] | 614 | f ID(abac_id_credential_t *) |
---|
[1621520] | 615 | constructor from abac_id_t |
---|
| 616 | ***/ |
---|
| 617 | ID(abac_id_t *id): m_id(abac_id_dup(id)) |
---|
| 618 | { } |
---|
| 619 | ID(abac_id_credential_t *idcred) { |
---|
| 620 | if(idcred) |
---|
| 621 | m_id=abac_id_dup(abac_id_credential_id(idcred)); |
---|
| 622 | else m_id=NULL; |
---|
| 623 | } |
---|
[64fa9ba] | 624 | |
---|
[1621520] | 625 | /*** |
---|
[6e8997e] | 626 | f ID(char *) |
---|
[1621520] | 627 | load an ID cert from a file, will throw an exception |
---|
| 628 | if the cert cannot be loaded |
---|
| 629 | ***/ |
---|
[8bd77b5] | 630 | ID(char *filename) { |
---|
[1621520] | 631 | m_id=abac_id_from_file(filename); |
---|
| 632 | if(m_id==NULL) |
---|
| 633 | abac_errx(1, "Id creation from filename failed"); |
---|
| 634 | } |
---|
| 635 | /*** |
---|
[6e8997e] | 636 | f ID(char *,int) |
---|
[1621520] | 637 | generates a new ID with the supplied CN and validity period |
---|
| 638 | - CN must be alphanumeric and begin with a letter |
---|
| 639 | - validity must be at least one second |
---|
| 640 | will throw an exception if either of the above is violated |
---|
| 641 | ***/ |
---|
[8bd77b5] | 642 | ID(char *cn, int validity) { |
---|
[1621520] | 643 | int rt=abac_id_generate(&m_id, cn, validity); |
---|
| 644 | if(rt != ABAC_ID_SUCCESS) |
---|
| 645 | abac_errx(1, "Id creation failed"); |
---|
| 646 | } |
---|
| 647 | /*** |
---|
[6e8997e] | 648 | f void load_privkey(char *) |
---|
[1621520] | 649 | loads the private key associated with the ID credential |
---|
| 650 | will throw an exception if the key cannot be loaded |
---|
| 651 | ***/ |
---|
[8bd77b5] | 652 | void load_privkey(char *filename) { |
---|
[adc0815] | 653 | int rt=abac_id_load_privkey_file(m_id, filename); |
---|
| 654 | if(rt != 1) |
---|
| 655 | abac_errx(1, "Failed to load private key"); |
---|
| 656 | } |
---|
[1621520] | 657 | /*** |
---|
[6e8997e] | 658 | f abac_id_t *id() |
---|
[1621520] | 659 | returns the abac_id_t |
---|
| 660 | returns the interal libabac representation of this id |
---|
| 661 | ***/ |
---|
[8bd77b5] | 662 | abac_id_t *id() { return m_id; } |
---|
[64fa9ba] | 663 | |
---|
[1621520] | 664 | /*** |
---|
[6e8997e] | 665 | f char *keyid() |
---|
[1621520] | 666 | returns the SHA1 keyid of the id cert |
---|
[6e8997e] | 667 | f char *name() |
---|
[adc0815] | 668 | returns the CN (the parameter passed to the constructor or the |
---|
| 669 | CN of the cert). |
---|
[1621520] | 670 | ***/ |
---|
[8bd77b5] | 671 | char *keyid() { return abac_id_keyid(m_id); } |
---|
| 672 | char *name() { return abac_id_cn(m_id); } |
---|
[1621520] | 673 | /*** |
---|
[6e8997e] | 674 | f bool has_privkey() |
---|
[1621520] | 675 | returns true if the ID has an associated private key |
---|
| 676 | ***/ |
---|
| 677 | bool has_privkey() |
---|
| 678 | { return abac_id_has_privkey(m_id); } |
---|
| 679 | |
---|
| 680 | /*** |
---|
[6e8997e] | 681 | f void write_cert(FILE *) |
---|
[1621520] | 682 | writes a PEM-encoded cert to the file handle |
---|
[6e8997e] | 683 | f void write_cert(char *) |
---|
[1621520] | 684 | writes a PEM-encoded cert to a file named out |
---|
| 685 | ***/ |
---|
| 686 | void write_cert(FILE *out) |
---|
| 687 | { abac_id_write_cert(m_id, out); } |
---|
[8bd77b5] | 688 | void write_cert(char *filename) { |
---|
[1621520] | 689 | FILE *out = fopen(filename, "a"); |
---|
| 690 | write_cert(out); |
---|
| 691 | fclose(out); |
---|
| 692 | } |
---|
| 693 | /*** |
---|
[6e8997e] | 694 | f void write_privkey(FILE *) |
---|
[1621520] | 695 | writes a PEM-encoded private key to the file handle |
---|
| 696 | throws an exception if no private key is loaded |
---|
[6e8997e] | 697 | f void write_privkey(char *) |
---|
[1621520] | 698 | writes a PEM-encoded private key a file named out |
---|
| 699 | throws an exception if no private key is loaded |
---|
| 700 | ***/ |
---|
[8bd77b5] | 701 | void write_privkey(FILE *out) { |
---|
[1621520] | 702 | if(!has_privkey()) |
---|
| 703 | abac_errx(1, "No privkey to write"); |
---|
| 704 | abac_id_write_privkey(m_id, out); |
---|
| 705 | } |
---|
[8bd77b5] | 706 | void write_privkey(char *filename) { |
---|
[1621520] | 707 | FILE *out = fopen(filename, "a"); |
---|
| 708 | write_privkey(out); |
---|
| 709 | fclose(out); |
---|
| 710 | } |
---|
| 711 | /*** |
---|
[6e8997e] | 712 | f abac_chunk_t cert_chunk() |
---|
[1621520] | 713 | returns a DER-encoded binary representation of the X.509 ID cert |
---|
| 714 | associated with this ID. |
---|
| 715 | can be passed to libabac's Context::load_id_chunk() |
---|
| 716 | ***/ |
---|
| 717 | abac_chunk_t cert_chunk() |
---|
| 718 | { return abac_id_cert_chunk(m_id); } |
---|
| 719 | /*** |
---|
[6e8997e] | 720 | f char *string() |
---|
[1621520] | 721 | returns literal string of the id credential |
---|
| 722 | ***/ |
---|
[5110d42] | 723 | char *string() { |
---|
[1621520] | 724 | char *tmp=NULL; |
---|
| 725 | if(has_privkey()) |
---|
| 726 | asprintf(&tmp,"(%s,%s,y)",abac_id_name(m_id),abac_id_idtype_string(m_id)); |
---|
| 727 | else asprintf(&tmp,"(%s,%s,n)",abac_id_name(m_id),abac_id_idtype_string(m_id)); |
---|
| 728 | return tmp; |
---|
| 729 | } |
---|
[8bd77b5] | 730 | public: |
---|
| 731 | abac_id_t *m_id; |
---|
| 732 | }; |
---|
[64fa9ba] | 733 | |
---|
[8bd77b5] | 734 | |
---|
[1621520] | 735 | /*** |
---|
| 736 | ABAC::Attribute |
---|
| 737 | This is the attribute representation for the access policy rule |
---|
[6e8997e] | 738 | LHS <- RHS |
---|
[1621520] | 739 | The sequence of generation is to |
---|
[6e8997e] | 740 | first, instantiate the object, ie, LHS (head) |
---|
| 741 | second, adding subject(s) to it, ie, RHS (tail) |
---|
| 742 | and then baking it. |
---|
[1621520] | 743 | Only once it's baked can you access the X.509 cert. |
---|
| 744 | Once it's been baked you can no longer add subjects to it |
---|
| 745 | ***/ |
---|
[8bd77b5] | 746 | class Attribute { |
---|
| 747 | public: |
---|
[1621520] | 748 | /*** |
---|
[6e8997e] | 749 | f Attribute() |
---|
[1621520] | 750 | default constructor, do not use, for swig only |
---|
[6e8997e] | 751 | f Attribute(const Attribute &) |
---|
[1621520] | 752 | copy constructor, used for cloning an attribute |
---|
[6e8997e] | 753 | f ~Attribute() |
---|
[1621520] | 754 | default destructor |
---|
| 755 | ***/ |
---|
| 756 | Attribute() : m_attr(NULL) { } |
---|
| 757 | Attribute(const Attribute &id) |
---|
| 758 | { m_attr =abac_attribute_dup(id.m_attr); } |
---|
| 759 | ~Attribute() |
---|
| 760 | { if(m_attr) abac_attribute_free(m_attr); } |
---|
| 761 | /*** |
---|
[6e8997e] | 762 | f Attribute(abac_attribute_t *) |
---|
[adc0815] | 763 | constructor that takes abac_attribute_t, locally used |
---|
[6e8997e] | 764 | f Attribute(abac_credential_t *) |
---|
[adc0815] | 765 | constructor that takes abac_credential_t, locally used |
---|
[1621520] | 766 | ***/ |
---|
[8bd77b5] | 767 | Attribute(abac_attribute_t *attr): m_attr(abac_attribute_dup(attr)) |
---|
[adc0815] | 768 | { } |
---|
| 769 | Attribute(abac_credential_t *cred) |
---|
| 770 | { m_attr=abac_attribute_dup(abac_credential_attribute(cred)); } |
---|
| 771 | /*** |
---|
[6e8997e] | 772 | f Attribute(Role&, int) |
---|
[adc0815] | 773 | constructor that creates an attribute policy to be signed by the issuer |
---|
| 774 | with the given role with a specified validity period |
---|
| 775 | An exception will be thrown if: |
---|
| 776 | - the issuer has no private key |
---|
| 777 | - the Head role is invalid |
---|
| 778 | - the validity period is invalid (must be >= 1 second) |
---|
| 779 | ***/ |
---|
[8bd77b5] | 780 | Attribute(Role& head, int validity) { |
---|
[adc0815] | 781 | int rt=abac_attribute_create(&m_attr, head.role(), NULL, validity); |
---|
| 782 | if(rt!=ABAC_ATTRIBUTE_SUCCESS) |
---|
| 783 | abac_errx(1, "attribute(role), unable to make an attribute"); |
---|
| 784 | } |
---|
| 785 | /*** |
---|
[6e8997e] | 786 | f Attribute(Oset&, int) |
---|
[adc0815] | 787 | constructor that creates an attribute policy to be signed by the issuer |
---|
| 788 | with the given oset with a specified validity period |
---|
| 789 | An exception will be thrown if: |
---|
| 790 | - the issuer has no private key |
---|
| 791 | - the Head oset is invalid |
---|
| 792 | - the validity period is invalid (must be >= 1 second) |
---|
| 793 | ***/ |
---|
[8bd77b5] | 794 | Attribute(Oset& head, int validity) { |
---|
[adc0815] | 795 | int rt=abac_attribute_create(&m_attr, head.oset(), NULL, validity); |
---|
| 796 | if(rt!=ABAC_ATTRIBUTE_SUCCESS) |
---|
| 797 | abac_errx(1, "attribute(oset), unable to make an attribute"); |
---|
| 798 | } |
---|
| 799 | /*** |
---|
[6e8997e] | 800 | f bool add_tail(Role&) |
---|
[adc0815] | 801 | Add a role tail. Call multiple times for intersections |
---|
[6e8997e] | 802 | f bool add_tail(Oset&) |
---|
[adc0815] | 803 | Add an oset tail. Call multiple times for intersections |
---|
| 804 | ***/ |
---|
[8bd77b5] | 805 | bool add_tail(Role& tail) { |
---|
[adc0815] | 806 | if(abac_attribute_add_tail(m_attr, tail.role())) |
---|
| 807 | return 1; |
---|
| 808 | else return 0; |
---|
| 809 | } |
---|
[8bd77b5] | 810 | bool add_tail(Oset& tail) { |
---|
[adc0815] | 811 | if(abac_attribute_add_tail(m_attr, tail.oset())) |
---|
| 812 | return 1; |
---|
| 813 | else return 0; |
---|
| 814 | } |
---|
| 815 | /*** |
---|
[6e8997e] | 816 | f char *head_string() |
---|
[adc0815] | 817 | returns literal string of head of the attribute |
---|
[6e8997e] | 818 | f char *tail_string() |
---|
[adc0815] | 819 | returns literal string of tail of the attribute |
---|
| 820 | ***/ |
---|
[8bd77b5] | 821 | char *head_string() { |
---|
[adc0815] | 822 | abac_aspect_t *head=abac_attribute_head(m_attr); |
---|
| 823 | char *string=abac_aspect_string(head); |
---|
| 824 | return string; |
---|
| 825 | } |
---|
[8bd77b5] | 826 | char *tail_string() { |
---|
[adc0815] | 827 | abac_aspect_t *tail=abac_attribute_tail(m_attr); |
---|
| 828 | char *string=abac_aspect_string(tail); |
---|
| 829 | return string; |
---|
| 830 | } |
---|
| 831 | /*** |
---|
[6e8997e] | 832 | f char *head_typed_string() |
---|
[adc0815] | 833 | returns typed literal string of head of the attribute |
---|
[6e8997e] | 834 | f char *tail_typed_string() |
---|
[adc0815] | 835 | returns typed literal string of tail of the attribute |
---|
| 836 | ***/ |
---|
[8bd77b5] | 837 | char *head_typed_string() { |
---|
[adc0815] | 838 | abac_aspect_t *head=abac_attribute_head(m_attr); |
---|
| 839 | char *string=abac_aspect_typed_string(head); |
---|
| 840 | return string; |
---|
| 841 | } |
---|
[8bd77b5] | 842 | char *tail_typed_string() { |
---|
[adc0815] | 843 | abac_aspect_t *tail=abac_attribute_tail(m_attr); |
---|
| 844 | char *string=abac_aspect_typed_string(tail); |
---|
| 845 | return string; |
---|
| 846 | } |
---|
| 847 | /*** |
---|
[6e8997e] | 848 | f char *string() |
---|
[adc0815] | 849 | returns literal string of the attribute |
---|
[6e8997e] | 850 | f char *typed_string() |
---|
[adc0815] | 851 | returns typed literal string of the attribute |
---|
| 852 | ***/ |
---|
[8bd77b5] | 853 | char *string() { |
---|
[adc0815] | 854 | char *head=head_string(); |
---|
| 855 | char *tail=tail_string(); |
---|
| 856 | if(head==NULL || tail==NULL) |
---|
| 857 | abac_errx(1, "attribute string, head and tail can not be NULL"); |
---|
| 858 | char *tmp=NULL; |
---|
| 859 | asprintf(&tmp,"%s<-%s",head,tail); |
---|
| 860 | return tmp; |
---|
| 861 | } |
---|
[8bd77b5] | 862 | char *typed_string() { |
---|
[adc0815] | 863 | char *head=head_typed_string(); |
---|
| 864 | char *tail=tail_typed_string(); |
---|
| 865 | if(head==NULL || tail==NULL) |
---|
| 866 | abac_errx(1, "attribute string, head and tail can not be NULL"); |
---|
| 867 | char *tmp=NULL; |
---|
| 868 | asprintf(&tmp,"%s<-%s",head,tail); |
---|
| 869 | return tmp; |
---|
| 870 | } |
---|
| 871 | /*** ??? not sure about implmentation |
---|
[6e8997e] | 872 | f const Role &role_head() |
---|
[adc0815] | 873 | returns the head role |
---|
[6e8997e] | 874 | f const Oset &oset_head() |
---|
[adc0815] | 875 | returns the oset head |
---|
| 876 | ***/ |
---|
[8bd77b5] | 877 | const Role &role_head() { |
---|
[adc0815] | 878 | abac_aspect_t *head=abac_attribute_head(m_attr); |
---|
| 879 | static Role role=Role(head); |
---|
| 880 | return role; |
---|
| 881 | } |
---|
[8bd77b5] | 882 | const Oset &oset_head() { |
---|
[adc0815] | 883 | abac_aspect_t *head=abac_attribute_tail(m_attr); |
---|
| 884 | static Oset oset=Oset(head); |
---|
| 885 | return oset; |
---|
| 886 | } |
---|
| 887 | /*** ??? |
---|
[6e8997e] | 888 | f std::vector<Role> role_tails(bool &) |
---|
[adc0815] | 889 | retrieve tail role which maybe more than 1 if intersecting |
---|
[6e8997e] | 890 | f std::vector<Oset> oset_tails(bool &) |
---|
[adc0815] | 891 | retrieve tail oset which maybe more than 1 if intersecting |
---|
| 892 | ***/ |
---|
[d037f54] | 893 | std::vector<Role> role_tails(bool &success) { |
---|
[adc0815] | 894 | abac_aspect_t **tails, **end; |
---|
| 895 | int i; |
---|
| 896 | /* make sure it is role */ |
---|
| 897 | if(!abac_attribute_is_role(m_attr)) { |
---|
| 898 | success=0; |
---|
| 899 | abac_errx(1, "role_tails, attribute is not a role"); |
---|
[8bd77b5] | 900 | } |
---|
[adc0815] | 901 | tails = abac_attribute_tail_vectorized(m_attr); |
---|
| 902 | for (i = 0; tails[i] != NULL; ++i) |
---|
| 903 | ; |
---|
| 904 | end = &tails[i]; |
---|
| 905 | std::vector<Role> roles = std::vector<Role>(tails, end); |
---|
| 906 | abac_aspects_free(tails); |
---|
| 907 | success=1; |
---|
| 908 | return roles; |
---|
| 909 | } |
---|
[d037f54] | 910 | std::vector<Oset> oset_tails(bool &success) { |
---|
[adc0815] | 911 | abac_aspect_t **tails, **end; |
---|
| 912 | int i; |
---|
| 913 | /* make sure that tail is not role */ |
---|
| 914 | if(abac_attribute_is_role(m_attr)) { |
---|
| 915 | success=0; |
---|
| 916 | abac_errx(1, "oset_tails, attribute is not an oset"); |
---|
[8bd77b5] | 917 | } |
---|
[adc0815] | 918 | tails = abac_attribute_tail_vectorized(m_attr); |
---|
| 919 | for (i = 0; tails[i] != NULL; ++i) |
---|
| 920 | ; |
---|
| 921 | end = &tails[i]; |
---|
| 922 | std::vector<Oset> osets = std::vector<Oset>(tails, end); |
---|
| 923 | success=1; |
---|
| 924 | abac_aspects_free(tails); |
---|
| 925 | return osets; |
---|
| 926 | } |
---|
| 927 | /*** |
---|
[6e8997e] | 928 | f abac_attribute_t *attribute() |
---|
[adc0815] | 929 | return libabac structure for attribute |
---|
| 930 | ***/ |
---|
[8bd77b5] | 931 | abac_attribute_t *attribute() { return m_attr; } |
---|
[4e426c9] | 932 | |
---|
[adc0815] | 933 | /*** |
---|
[6e8997e] | 934 | f bool bake() |
---|
[adc0815] | 935 | Generate the cert. Call this after you've added subjects to your cert. |
---|
| 936 | This returns false if there are no subjects |
---|
| 937 | This will throw an exception if the cert's already been baked. |
---|
| 938 | ***/ |
---|
[8bd77b5] | 939 | bool bake() { |
---|
[adc0815] | 940 | /* can not bake in ABAC_CN mode */ |
---|
| 941 | if(USE("ABAC_CN")) |
---|
| 942 | abac_errx(1, "bake, can not bake the cert with env(ABAC_CN) set"); |
---|
| 943 | int rt=abac_attribute_bake(m_attr); |
---|
| 944 | if(rt!=1) |
---|
| 945 | abac_errx(1, "bake, can not bake the cert"); |
---|
| 946 | } |
---|
| 947 | /*** |
---|
[6e8997e] | 948 | f bool baked() |
---|
[adc0815] | 949 | returns true iff the cert has been baked. |
---|
| 950 | ***/ |
---|
| 951 | bool baked() |
---|
| 952 | { return abac_attribute_baked(m_attr); } |
---|
| 953 | |
---|
| 954 | /*** |
---|
[6e8997e] | 955 | f void write_cert(FILE *) |
---|
[adc0815] | 956 | write the DER-encoded X.509 attribute cert to the open file handle |
---|
| 957 | Throws an exception if the cert isn't baked |
---|
| 958 | ***/ |
---|
[8bd77b5] | 959 | void write_cert(FILE *out) { |
---|
[adc0815] | 960 | int rt= abac_attribute_write(m_attr,out); |
---|
| 961 | if(!rt) |
---|
| 962 | abac_errx(1, "write, cert is not baked"); |
---|
| 963 | } |
---|
| 964 | /*** |
---|
[6e8997e] | 965 | f void write_cert(char *) |
---|
[adc0815] | 966 | write the DER-encoded X.509 attribute cert to a file named out |
---|
| 967 | Throws an exception if the cert isn't baked |
---|
| 968 | ***/ |
---|
[8bd77b5] | 969 | void write_cert(char *filename) { |
---|
[adc0815] | 970 | FILE *out = fopen(filename, "w"); |
---|
| 971 | write_cert(out); |
---|
| 972 | fclose(out); |
---|
| 973 | } |
---|
| 974 | /*** |
---|
[6e8997e] | 975 | f abac_chunk_t cert_chunk() |
---|
[adc0815] | 976 | returns a DER-encoded binary representation of the X.509 attribute |
---|
| 977 | cert associated with this cert |
---|
| 978 | Throws an exception if the cert isn't baked |
---|
| 979 | the chunk can be passed to libabac's Context::load_attribute_chunk() |
---|
| 980 | ***/ |
---|
| 981 | abac_chunk_t cert_chunk() |
---|
| 982 | { return abac_attribute_cert_chunk(m_attr); } |
---|
| 983 | |
---|
| 984 | /*** |
---|
[6e8997e] | 985 | f int consume() |
---|
[adc0815] | 986 | generate yap clauses and injected into db |
---|
| 987 | ***/ |
---|
[440ba20] | 988 | int consume() { |
---|
| 989 | /* attribute needs to be baked */ |
---|
[adc0815] | 990 | if(!baked()) { |
---|
| 991 | return ABAC_ATTRIBUTE_FAIL; |
---|
[440ba20] | 992 | } |
---|
[adc0815] | 993 | } |
---|
[8bd77b5] | 994 | private: |
---|
| 995 | abac_attribute_t *m_attr; |
---|
| 996 | }; |
---|
[9dffbd6] | 997 | |
---|
[8bd77b5] | 998 | |
---|
[adc0815] | 999 | /*** |
---|
| 1000 | ABAC::Context |
---|
[6e8997e] | 1001 | An ABAC Context |
---|
[adc0815] | 1002 | ***/ |
---|
[8bd77b5] | 1003 | class Context { |
---|
| 1004 | public: |
---|
[adc0815] | 1005 | /*** |
---|
[6e8997e] | 1006 | f Context() |
---|
[adc0815] | 1007 | default constructor |
---|
[6e8997e] | 1008 | f Context(const Context &) |
---|
[adc0815] | 1009 | copy constructor, used for cloning the context |
---|
[6e8997e] | 1010 | f ~Context() |
---|
[adc0815] | 1011 | default destructor |
---|
| 1012 | ***/ |
---|
[8bd77b5] | 1013 | Context() { m_ctx = abac_context_new(); m_abac_version=strdup("1.0"); } |
---|
| 1014 | Context(const Context &context) { |
---|
[adc0815] | 1015 | m_ctx = abac_context_dup(context.m_ctx); |
---|
| 1016 | m_abac_version=strdup(context.m_abac_version); |
---|
| 1017 | } |
---|
[8bd77b5] | 1018 | ~Context() { |
---|
[adc0815] | 1019 | abac_context_free(m_ctx); |
---|
| 1020 | if(m_abac_version) free(m_abac_version); |
---|
| 1021 | } |
---|
| 1022 | /*** |
---|
[6e8997e] | 1023 | f void dump_yap() |
---|
[adc0815] | 1024 | dump the complete yap prolog database |
---|
| 1025 | ***/ |
---|
| 1026 | void dump_yap() |
---|
| 1027 | { show_yap_db("dump_yap"); } |
---|
[8bd77b5] | 1028 | |
---|
[adc0815] | 1029 | /*** |
---|
[6e8997e] | 1030 | f int load_id(ABAC::ID&) |
---|
[adc0815] | 1031 | load id cert from ID |
---|
[6e8997e] | 1032 | f int load_id_file(char *) |
---|
[adc0815] | 1033 | load id cert from an idkey combo file. key retrieval will be attempt |
---|
| 1034 | but won't fail if not found |
---|
[6e8997e] | 1035 | f int load_id_file(char *, char *) |
---|
[adc0815] | 1036 | load id cert from an id file and a key file |
---|
[6e8997e] | 1037 | f int load_id_chunk(abac_chunk_t) |
---|
[adc0815] | 1038 | load id cert from a chunk structure |
---|
[6e8997e] | 1039 | returns: |
---|
[adc0815] | 1040 | ABAC_CERT_SUCCESS successfully loaded |
---|
| 1041 | ABAC_CERT_INVALID invalid certificate (or file not found) |
---|
| 1042 | ABAC_CERT_BAD_SIG invalid signature |
---|
| 1043 | ***/ |
---|
| 1044 | int load_id(ABAC::ID& id) |
---|
| 1045 | { return abac_context_load_id_id(m_ctx, id.id()); } |
---|
| 1046 | int load_id_file(char *filename) |
---|
| 1047 | { return abac_context_load_id_idkey_file(m_ctx, filename); } |
---|
| 1048 | int load_id_file(char *filename, char *keyfilename) |
---|
| 1049 | { return abac_context_load_id_id_file_key_file(m_ctx, filename, keyfilename); } |
---|
| 1050 | int load_id_chunk(abac_chunk_t cert) |
---|
| 1051 | { return abac_context_load_id_chunk(m_ctx, cert); } |
---|
| 1052 | |
---|
| 1053 | /*** |
---|
[6e8997e] | 1054 | f int load_attribute(ABAC::Attribute&) |
---|
[adc0815] | 1055 | load attribute credential from attribute structure |
---|
[6e8997e] | 1056 | f int load_attribute_file(char *) |
---|
[adc0815] | 1057 | load attribute credential from a file |
---|
[6e8997e] | 1058 | f int load_attribute_chunk(abac_chunk_t) |
---|
[adc0815] | 1059 | load attribute credential from a chunk |
---|
[6e8997e] | 1060 | f returns the same values as above, additionally |
---|
| 1061 | returns ABAC_CERT_MISSING_ISSUER if the issuer |
---|
| 1062 | certificate has not been loaded |
---|
[adc0815] | 1063 | ***/ |
---|
| 1064 | int load_attribute(ABAC::Attribute& a) |
---|
| 1065 | { return abac_context_load_attribute_attribute(m_ctx, a.attribute()); } |
---|
| 1066 | int load_attribute_file(char *filename) |
---|
| 1067 | { return abac_context_load_attribute_file(m_ctx, filename); } |
---|
| 1068 | int load_attribute_chunk(abac_chunk_t cert) |
---|
| 1069 | { return abac_context_load_attribute_chunk(m_ctx, cert); } |
---|
| 1070 | |
---|
| 1071 | /*** |
---|
[6e8997e] | 1072 | f void load_directory(char *) |
---|
[adc0815] | 1073 | load a directory full of certificates: |
---|
[6e8997e] | 1074 | f first: ${path}/*_ID.{der,pem} as identity certificates |
---|
[adc0815] | 1075 | implicitly looking for ${path}/*_private.{der,pem} as |
---|
| 1076 | the private key file |
---|
| 1077 | then: ${path}/*_IDKEY.{der,pem} as id/key combo certificate |
---|
| 1078 | last: ${path}/*_attr.der as attribute certificates |
---|
| 1079 | ***/ |
---|
| 1080 | void load_directory(char *path) |
---|
| 1081 | { abac_context_load_directory(m_ctx, path); } |
---|
| 1082 | /*** |
---|
[6e8997e] | 1083 | f std::vector<Attribute> query(char *, char *, bool &) |
---|
[adc0815] | 1084 | the string version is for query that is composed by hand with SHA or |
---|
| 1085 | in non ABAC_CN mode |
---|
[6e8997e] | 1086 | f std::vector<Attribute> query(Role &, Role &, bool &) |
---|
[adc0815] | 1087 | std::vector<Attribute> query(Oset &, Oset &, bool &) |
---|
| 1088 | runs the query: |
---|
[8bd77b5] | 1089 | role <-?- principal |
---|
[6e8997e] | 1090 | oset <-?- principal/obj |
---|
[adc0815] | 1091 | returns true/false in success |
---|
[6e8997e] | 1092 | returns a proof upon success, |
---|
| 1093 | partial proof on failure (not implemented yet) |
---|
[adc0815] | 1094 | ***/ |
---|
[8bd77b5] | 1095 | std::vector<Attribute> query(char *role, char *principal, bool &success) { |
---|
[adc0815] | 1096 | abac_credential_t **creds, **end; |
---|
| 1097 | int i, success_int; |
---|
[8bd77b5] | 1098 | |
---|
[adc0815] | 1099 | creds = abac_context_query(m_ctx, role, principal, &success_int); |
---|
| 1100 | success = success_int; |
---|
[8bd77b5] | 1101 | |
---|
[adc0815] | 1102 | for (i = 0; creds[i] != NULL; ++i) |
---|
| 1103 | ; |
---|
[9dffbd6] | 1104 | |
---|
[adc0815] | 1105 | end = &creds[i]; |
---|
| 1106 | std::vector<Attribute> attributes = std::vector<Attribute>(creds, end); |
---|
| 1107 | if(debug) printf("query, got rules(%d)\n", i); |
---|
[9dffbd6] | 1108 | |
---|
[adc0815] | 1109 | abac_context_credentials_free(creds); |
---|
[9dffbd6] | 1110 | |
---|
[adc0815] | 1111 | return attributes; |
---|
| 1112 | } |
---|
[64fa9ba] | 1113 | |
---|
[5110d42] | 1114 | std::vector<Attribute> query(Role &role, Role &p_role, bool &success) { |
---|
[adc0815] | 1115 | abac_credential_t **creds, **end; |
---|
| 1116 | int i, success_int; |
---|
[5110d42] | 1117 | |
---|
[adc0815] | 1118 | creds = abac_context_query_with_structure(m_ctx, role.role(), p_role.role(), &success_int); |
---|
| 1119 | success = success_int; |
---|
[5110d42] | 1120 | |
---|
[adc0815] | 1121 | for (i = 0; creds[i] != NULL; ++i) |
---|
| 1122 | ; |
---|
[5110d42] | 1123 | |
---|
[adc0815] | 1124 | end = &creds[i]; |
---|
| 1125 | std::vector<Attribute> attributes = std::vector<Attribute>(creds, end); |
---|
[5110d42] | 1126 | |
---|
[adc0815] | 1127 | abac_context_credentials_free(creds); |
---|
[5110d42] | 1128 | |
---|
[adc0815] | 1129 | return attributes; |
---|
| 1130 | } |
---|
[5110d42] | 1131 | |
---|
| 1132 | std::vector<Attribute> query(Oset &oset, Oset &p_oset, bool &success) { |
---|
[adc0815] | 1133 | abac_credential_t **creds, **end; |
---|
| 1134 | int i, success_int; |
---|
| 1135 | |
---|
| 1136 | creds = abac_context_query_with_structure(m_ctx, oset.oset(), p_oset.oset(), &success_int); |
---|
| 1137 | success = success_int; |
---|
[5110d42] | 1138 | |
---|
[adc0815] | 1139 | for (i = 0; creds[i] != NULL; ++i) |
---|
| 1140 | ; |
---|
[5110d42] | 1141 | |
---|
[adc0815] | 1142 | end = &creds[i]; |
---|
| 1143 | std::vector<Attribute> attributes = std::vector<Attribute>(creds, end); |
---|
| 1144 | if(debug) printf("query, returning rules(%d)\n", i); |
---|
[5110d42] | 1145 | |
---|
[adc0815] | 1146 | abac_context_credentials_free(creds); |
---|
[5110d42] | 1147 | |
---|
[adc0815] | 1148 | return attributes; |
---|
| 1149 | } |
---|
[5110d42] | 1150 | |
---|
[adc0815] | 1151 | /*** |
---|
[6e8997e] | 1152 | f std::vector<Attribute> context_credentials(bool &) |
---|
[adc0815] | 1153 | returns a vector of all the credentials loaded in the context |
---|
| 1154 | extracted from the internal data structure |
---|
| 1155 | ***/ |
---|
[8bd77b5] | 1156 | std::vector<Attribute> context_credentials(bool &success) { |
---|
[adc0815] | 1157 | abac_credential_t **creds, **end; |
---|
| 1158 | int i; |
---|
| 1159 | success = 1; |
---|
[3c4fd68] | 1160 | |
---|
[adc0815] | 1161 | creds = abac_context_credentials(m_ctx); |
---|
| 1162 | for (i = 0; creds[i] != NULL; ++i) |
---|
| 1163 | ; |
---|
[3c4fd68] | 1164 | |
---|
[adc0815] | 1165 | end = &creds[i]; |
---|
| 1166 | std::vector<Attribute> attributes = std::vector<Attribute>(creds, end); |
---|
| 1167 | if(debug) printf("credentials, got (%d)\n", i); |
---|
[3c4fd68] | 1168 | |
---|
[adc0815] | 1169 | abac_context_credentials_free(creds); |
---|
| 1170 | if(debug) show_yap_db("calling from context_credentials"); |
---|
| 1171 | return attributes; |
---|
| 1172 | } |
---|
[5110d42] | 1173 | |
---|
[adc0815] | 1174 | /*** |
---|
[6e8997e] | 1175 | f std::vector<Attribute> context_principals(bool &) |
---|
[adc0815] | 1176 | returns a vector of all the principals loaded in the context |
---|
| 1177 | extracted from the internal data structure |
---|
| 1178 | ***/ |
---|
[5110d42] | 1179 | std::vector<ID> context_principals(bool &success) { |
---|
[adc0815] | 1180 | abac_id_credential_t **ids, **end; |
---|
| 1181 | int i; |
---|
| 1182 | success = 1; |
---|
[5110d42] | 1183 | |
---|
[adc0815] | 1184 | ids = abac_context_principals(m_ctx); |
---|
| 1185 | for (i = 0; ids[i] != NULL; ++i) |
---|
| 1186 | ; |
---|
[5110d42] | 1187 | |
---|
[adc0815] | 1188 | end = &ids[i]; |
---|
| 1189 | std::vector<ID> principals = std::vector<ID>(ids, end); |
---|
| 1190 | if(debug) printf("principals, got (%d)\n", i); |
---|
[5110d42] | 1191 | |
---|
[adc0815] | 1192 | abac_context_principals_free(ids); |
---|
| 1193 | return principals; |
---|
| 1194 | } |
---|
| 1195 | /*** |
---|
[6e8997e] | 1196 | f char *version() |
---|
[adc0815] | 1197 | return the version of this interface |
---|
| 1198 | ***/ |
---|
[8bd77b5] | 1199 | char *version() const { return m_abac_version; } |
---|
[3c4fd68] | 1200 | |
---|
[64fa9ba] | 1201 | private: |
---|
| 1202 | abac_context_t *m_ctx; |
---|
[8bd77b5] | 1203 | char *m_abac_version; |
---|
[64fa9ba] | 1204 | }; |
---|
[8bd77b5] | 1205 | |
---|
[0b3ac65] | 1206 | Constraint::Constraint(Role& role) |
---|
| 1207 | { m_constraint=abac_condition_create_from_aspect(role.role()); } |
---|
| 1208 | Constraint::Constraint(Oset &oset) |
---|
| 1209 | { m_constraint=abac_condition_create_from_aspect(oset.oset()); } |
---|
[64fa9ba] | 1210 | } |
---|
| 1211 | |
---|
| 1212 | #endif /* __ABAC_HH__ */ |
---|