[3ed053d] | 1 | #ifndef __ABAC_HH__ |
---|
| 2 | #define __ABAC_HH__ |
---|
| 3 | |
---|
| 4 | #include <cstdio> |
---|
| 5 | #include <stdexcept> |
---|
| 6 | #include <string> |
---|
| 7 | #include <vector> |
---|
| 8 | |
---|
| 9 | namespace ABAC { |
---|
| 10 | extern "C" { |
---|
| 11 | #include "abac.h" |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | class Attribute; |
---|
| 15 | class ID; |
---|
[94605f2] | 16 | class Role; |
---|
| 17 | class Credential; |
---|
| 18 | |
---|
| 19 | class Context { |
---|
| 20 | public: |
---|
| 21 | Context() { m_ctx = abac_context_new(); } |
---|
| 22 | Context(const Context &context) { m_ctx = abac_context_dup(context.m_ctx); } |
---|
| 23 | ~Context() { abac_context_free(m_ctx); } |
---|
| 24 | |
---|
| 25 | int load_id_file(char *filename) { return abac_context_load_id_file(m_ctx, filename); } |
---|
| 26 | int load_id_chunk(abac_chunk_t cert) { return abac_context_load_id_chunk(m_ctx, cert); } |
---|
| 27 | int load_attribute_file(char *filename) { return abac_context_load_attribute_file(m_ctx, filename); } |
---|
| 28 | int load_attribute_chunk(abac_chunk_t cert) { return abac_context_load_attribute_chunk(m_ctx, cert); } |
---|
| 29 | |
---|
| 30 | void load_directory(char *path) { abac_context_load_directory(m_ctx, path); } |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | /* abac query, returns a vector of credentials on success, NULL on fail */ |
---|
| 34 | std::vector<Credential> query(char *role, char *principal, bool &success) { |
---|
| 35 | abac_credential_t **creds, **end; |
---|
| 36 | int i, success_int; |
---|
| 37 | creds = abac_context_query(m_ctx, role, principal, &success_int); |
---|
| 38 | success = success_int; |
---|
| 39 | |
---|
| 40 | for (i = 0; creds[i] != NULL; ++i) |
---|
| 41 | ; |
---|
| 42 | |
---|
| 43 | end = &creds[i]; |
---|
| 44 | std::vector<Credential> credentials = std::vector<Credential>(creds, end); |
---|
| 45 | abac_context_credentials_free(creds); |
---|
| 46 | return credentials; |
---|
| 47 | } |
---|
| 48 | std::vector<Credential> credentials() { |
---|
| 49 | abac_credential_t **creds, **end; |
---|
| 50 | int i; |
---|
| 51 | |
---|
| 52 | creds = abac_context_credentials(m_ctx); |
---|
| 53 | for (i = 0; creds[i] != NULL; ++i) |
---|
| 54 | ; |
---|
| 55 | |
---|
| 56 | end = &creds[i]; |
---|
| 57 | std::vector<Credential> credentials = std::vector<Credential>(creds, end); |
---|
| 58 | |
---|
| 59 | abac_context_credentials_free(creds); |
---|
| 60 | return credentials; |
---|
| 61 | } |
---|
[34565bf] | 62 | void set_nickname(char *key, char *nick) { |
---|
| 63 | abac_context_set_nickname(m_ctx, key, nick); |
---|
| 64 | } |
---|
[94605f2] | 65 | private: |
---|
| 66 | abac_context_t *m_ctx; |
---|
| 67 | friend class Role; |
---|
[afcafea] | 68 | friend class Attribute; |
---|
[94605f2] | 69 | }; |
---|
[3ed053d] | 70 | |
---|
| 71 | class Role { |
---|
| 72 | public: |
---|
| 73 | Role() : m_role(NULL) { } // do not use: here for swig |
---|
| 74 | Role(abac_role_t *role) { m_role = abac_role_dup(role); } |
---|
| 75 | Role(char *role_name) { m_role = abac_role_from_string(role_name); } |
---|
| 76 | Role(const Role &role) { m_role = abac_role_dup(role.m_role); } |
---|
| 77 | ~Role() { abac_role_free(m_role); } |
---|
| 78 | bool is_principal() const { return abac_role_is_principal(m_role); } |
---|
| 79 | bool is_role() const { return abac_role_is_role(m_role); } |
---|
| 80 | bool is_linking() const { return abac_role_is_linking(m_role); } |
---|
| 81 | |
---|
| 82 | char *string() const { return abac_role_string(m_role); } |
---|
[94605f2] | 83 | char *short_string(Context& c) const { |
---|
| 84 | return abac_role_short_string(m_role, c.m_ctx); |
---|
| 85 | } |
---|
[3ed053d] | 86 | char *linked_role() const { return abac_role_linked_role(m_role); } |
---|
| 87 | char *role_name() const { return abac_role_role_name(m_role); } |
---|
| 88 | char *principal() const { return abac_role_principal(m_role); } |
---|
| 89 | |
---|
| 90 | private: |
---|
| 91 | abac_role_t *m_role; |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | class Credential { |
---|
| 95 | public: |
---|
| 96 | Credential() : m_cred(NULL) { } // do not use: here for swig |
---|
| 97 | Credential(abac_credential_t *cred) : |
---|
| 98 | m_head(abac_credential_head(cred)), |
---|
| 99 | m_tail(abac_credential_tail(cred)), |
---|
| 100 | m_cred(abac_credential_dup(cred)) |
---|
| 101 | { } |
---|
| 102 | Credential(const Credential &cred) : |
---|
| 103 | m_head(cred.m_head), |
---|
| 104 | m_tail(cred.m_tail), |
---|
| 105 | m_cred(abac_credential_dup(cred.m_cred)) |
---|
| 106 | { } |
---|
| 107 | ~Credential() { abac_credential_free(m_cred); } |
---|
| 108 | const Role &head() { return m_head; } |
---|
| 109 | const Role &tail() { return m_tail; } |
---|
| 110 | abac_chunk_t attribute_cert() { return abac_credential_attribute_cert(m_cred); } |
---|
| 111 | abac_chunk_t issuer_cert() { return abac_credential_issuer_cert(m_cred); } |
---|
| 112 | |
---|
| 113 | private: |
---|
| 114 | abac_credential_t *m_cred; |
---|
| 115 | Role m_head, m_tail; |
---|
| 116 | }; |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | class ID { |
---|
| 120 | public: |
---|
| 121 | ID() : m_id(NULL) { } // do not use: required by swig |
---|
| 122 | ID(const ID &id) { m_id = abac_id_dup(id.m_id); } |
---|
| 123 | ~ID() { abac_id_free(m_id); } |
---|
| 124 | |
---|
| 125 | ID(char *filename) : m_id(NULL) { |
---|
| 126 | m_id = abac_id_from_file(filename); |
---|
| 127 | if (m_id == NULL) |
---|
| 128 | throw std::invalid_argument("Could not load ID cert"); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | ID(abac_chunk_t chunk) : m_id(NULL) { |
---|
| 132 | m_id = abac_id_from_chunk(chunk); |
---|
| 133 | if (m_id == NULL) |
---|
| 134 | throw std::invalid_argument("Could not load ID certificate with a chunk"); |
---|
| 135 | } |
---|
| 136 | ID(char *cn, int validity) : m_id(NULL) { |
---|
| 137 | int ret = abac_id_generate(&m_id, cn, validity); |
---|
| 138 | if (ret == ABAC_GENERATE_INVALID_CN) |
---|
| 139 | throw std::invalid_argument("CN must be alphanumeric and start with a letter"); |
---|
| 140 | if (ret == ABAC_GENERATE_INVALID_VALIDITY) |
---|
| 141 | throw std::invalid_argument("Validity must be > 0 days"); |
---|
| 142 | } |
---|
| 143 | void load_privkey(char *filename) { |
---|
| 144 | int ret = abac_id_privkey_from_file(m_id, filename); |
---|
| 145 | if (ret != ABAC_SUCCESS) |
---|
| 146 | throw std::invalid_argument("Could not load private key"); |
---|
| 147 | } |
---|
| 148 | void load_privkey_chunk(abac_chunk_t chunk) { |
---|
| 149 | int ret = abac_id_privkey_from_chunk(m_id, chunk); |
---|
| 150 | if (ret != ABAC_SUCCESS) |
---|
| 151 | throw std::invalid_argument("Could not load private key with a chunk"); |
---|
| 152 | } |
---|
| 153 | int has_privkey() { |
---|
| 154 | int ret= abac_id_has_privkey(m_id); |
---|
| 155 | return ret; |
---|
| 156 | } |
---|
| 157 | char *keyid() { return abac_id_keyid(m_id); } |
---|
[4f79997] | 158 | char *cn() { return abac_id_cn(m_id); } |
---|
[3ed053d] | 159 | char *cert_filename() { return abac_id_cert_filename(m_id); } |
---|
| 160 | void write_cert(FILE *out) { abac_id_write_cert(m_id, out); } |
---|
| 161 | void write_cert(const std::string &name) { |
---|
| 162 | FILE *out = fopen(name.c_str(), "a+"); |
---|
| 163 | if (out == NULL) |
---|
| 164 | throw std::invalid_argument("Could not open certificate file for writing"); |
---|
| 165 | write_cert(out); |
---|
| 166 | fclose(out); |
---|
| 167 | } |
---|
| 168 | // Simplifies access from swig |
---|
| 169 | void write_cert_file(const char *n) { |
---|
| 170 | write_cert(std::string(n)); |
---|
| 171 | } |
---|
| 172 | void write_cert_name(const char *n) { |
---|
| 173 | write_cert(std::string(n)); |
---|
| 174 | fprintf(stderr,"ABAC::ID::write_cert_name is deprecated, please use ABAC::ID::write_cert_name\n"); |
---|
| 175 | } |
---|
| 176 | char *privkey_filename() { return abac_id_privkey_filename(m_id); } |
---|
| 177 | void write_privkey(FILE *out) { |
---|
| 178 | int ret = abac_id_write_privkey(m_id, out); |
---|
| 179 | if (ret!=ABAC_SUCCESS) throw std::logic_error("No private key loaded"); |
---|
| 180 | } |
---|
| 181 | void write_privkey(const std::string &name) { |
---|
| 182 | FILE *out = fopen(name.c_str(), "a+"); |
---|
| 183 | if (out == NULL) |
---|
| 184 | throw std::invalid_argument("Could not open privkey file for writing"); |
---|
| 185 | write_privkey(out); |
---|
| 186 | fclose(out); |
---|
| 187 | } |
---|
| 188 | // Simplifies access from swig |
---|
| 189 | void write_privkey_file(const char *name) { |
---|
| 190 | write_privkey(std::string(name)); |
---|
| 191 | } |
---|
| 192 | void write_privkey_name(const char *name) { |
---|
| 193 | write_privkey(std::string(name)); |
---|
| 194 | fprintf(stderr,"ABAC::ID::write_privkey_name is deprecated, please use ABAC::ID::write_privkey_file\n"); |
---|
| 195 | } |
---|
| 196 | abac_chunk_t cert_chunk() { return abac_id_cert_chunk(m_id); } |
---|
| 197 | abac_chunk_t privkey_chunk() { return abac_id_privkey_chunk(m_id); } |
---|
| 198 | |
---|
| 199 | friend class Attribute; |
---|
| 200 | |
---|
| 201 | private: |
---|
| 202 | abac_id_t *m_id; |
---|
| 203 | }; |
---|
| 204 | |
---|
| 205 | class Attribute { |
---|
| 206 | public: |
---|
| 207 | Attribute() : m_attr(NULL) { } // do not use: required by swig |
---|
| 208 | ~Attribute() { abac_attribute_free(m_attr); } |
---|
| 209 | |
---|
| 210 | Attribute(ID &issuer, char *role, int validity) : m_attr(NULL) { |
---|
| 211 | int ret = abac_attribute_create(&m_attr, issuer.m_id, role, validity); |
---|
| 212 | if (ret == ABAC_ATTRIBUTE_ISSUER_NOKEY) |
---|
| 213 | throw std::invalid_argument("Issuer has no private key"); |
---|
| 214 | if (ret == ABAC_ATTRIBUTE_INVALID_ROLE) |
---|
| 215 | throw std::invalid_argument("Role name must be alphanumeric"); |
---|
| 216 | if (ret == ABAC_ATTRIBUTE_INVALID_VALIDITY) |
---|
| 217 | throw std::invalid_argument("Validity must be > 0 days"); |
---|
| 218 | if (ret == ABAC_ATTRIBUTE_INVALID_ISSUER) |
---|
| 219 | throw std::invalid_argument("Issuer's validity expired"); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | bool principal(char *keyid) { |
---|
| 224 | if (baked()) throw std::logic_error("Cert is already baked"); |
---|
| 225 | return abac_attribute_principal(m_attr, keyid); |
---|
| 226 | } |
---|
| 227 | bool role(char *keyid, char *role) { |
---|
| 228 | if (baked()) throw std::logic_error("Cert is already baked"); |
---|
| 229 | return abac_attribute_role(m_attr, keyid, role); |
---|
| 230 | } |
---|
| 231 | bool linking_role(char *keyid, char *role, char *linked) { |
---|
| 232 | if (baked()) throw std::logic_error("Cert is already baked"); |
---|
| 233 | return abac_attribute_linking_role(m_attr, keyid, role, linked); |
---|
| 234 | } |
---|
| 235 | bool bake() { |
---|
| 236 | if (baked()) throw std::logic_error("Cert is already baked"); |
---|
| 237 | return abac_attribute_bake(m_attr); |
---|
| 238 | } |
---|
| 239 | |
---|
[afcafea] | 240 | bool bake(Context& c) { |
---|
| 241 | if (baked()) throw std::logic_error("Cert is already baked"); |
---|
| 242 | return abac_attribute_bake_context(m_attr, c.m_ctx); |
---|
| 243 | } |
---|
| 244 | |
---|
[3ed053d] | 245 | bool baked() { return abac_attribute_baked(m_attr); } |
---|
| 246 | |
---|
[bc12f3d] | 247 | void set_output_format(char *fmt) { |
---|
| 248 | abac_attribute_set_output_format(m_attr, fmt); |
---|
| 249 | } |
---|
[34565bf] | 250 | |
---|
| 251 | char *get_output_format() { |
---|
| 252 | return abac_attribute_get_output_format(m_attr); |
---|
| 253 | } |
---|
[bc12f3d] | 254 | |
---|
[3ed053d] | 255 | void write(FILE *out) { |
---|
| 256 | int ret = abac_attribute_write(m_attr, out); |
---|
| 257 | if (ret!=ABAC_SUCCESS) throw std::logic_error("Cert is not baked"); |
---|
| 258 | } |
---|
| 259 | void write(const std::string &name) { |
---|
| 260 | FILE *out = fopen(name.c_str(), "w"); |
---|
| 261 | if (out == NULL) |
---|
| 262 | throw std::invalid_argument("Could not open certificate file for writing"); |
---|
| 263 | write(out); |
---|
| 264 | fclose(out); |
---|
| 265 | } |
---|
| 266 | void write_file(const char *name) { |
---|
| 267 | int ret = abac_attribute_write_file(m_attr, name); |
---|
| 268 | if (ret!=ABAC_SUCCESS) throw std::logic_error("Cert is not baked"); |
---|
| 269 | } |
---|
| 270 | void write_name(const char *name) { |
---|
| 271 | write_file(name); |
---|
| 272 | fprintf(stderr,"ABAC::Attribute::write_name is deprecated, please use ABAC::Attribute::write_name\n"); |
---|
| 273 | } |
---|
| 274 | abac_chunk_t cert_chunk() { |
---|
| 275 | abac_chunk_t ret=abac_attribute_cert_chunk(m_attr); |
---|
| 276 | if(ret.len == 0) |
---|
| 277 | throw std::logic_error("Cert is not baked"); |
---|
| 278 | return ret; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | private: |
---|
| 282 | abac_attribute_t *m_attr; |
---|
| 283 | }; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | #endif /* __ABAC_HH__ */ |
---|