[186cb75] | 1 | #include <assert.h> |
---|
| 2 | |
---|
[5450aeb] | 3 | #include <library.h> |
---|
| 4 | #include <credentials/certificates/certificate.h> |
---|
| 5 | #include <credentials/certificates/x509.h> |
---|
| 6 | #include <credentials/certificates/ac.h> |
---|
| 7 | |
---|
[43e3b71] | 8 | #include "abac_verifier.h" |
---|
[5450aeb] | 9 | |
---|
[3c251d0] | 10 | #include "abac_util.h" |
---|
[5450aeb] | 11 | |
---|
[43e3b71] | 12 | typedef struct _abac_id_cert_t { |
---|
[5450aeb] | 13 | char *keyid; |
---|
| 14 | certificate_t *cert; |
---|
| 15 | |
---|
| 16 | UT_hash_handle hh; |
---|
[43e3b71] | 17 | } abac_id_cert_t; |
---|
[5450aeb] | 18 | |
---|
[401a054] | 19 | struct _abac_credential_t { |
---|
[1743825] | 20 | abac_role_t *head; |
---|
| 21 | abac_role_t *tail; |
---|
[5f99fbc] | 22 | certificate_t *cert; |
---|
| 23 | certificate_t *issuer; |
---|
[fbb591e] | 24 | |
---|
| 25 | int refcount; |
---|
[5f99fbc] | 26 | }; |
---|
| 27 | |
---|
[43e3b71] | 28 | abac_id_cert_t *id_certs = NULL; |
---|
[5450aeb] | 29 | |
---|
| 30 | // convert a chunk to a lowercase binary string |
---|
| 31 | // malloc's the string |
---|
| 32 | static char *_chunk_to_string(chunk_t chunk) { |
---|
| 33 | int i; |
---|
| 34 | |
---|
[3c251d0] | 35 | char *ret = abac_xmalloc(chunk.len * 2 + 1); |
---|
[5450aeb] | 36 | |
---|
| 37 | for (i = 0; i < chunk.len; ++i) |
---|
| 38 | sprintf(ret + 2 * i, "%02x", chunk.ptr[i]); |
---|
| 39 | |
---|
| 40 | return ret; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | // verify that cert was issued by issuer |
---|
| 44 | // cert and issuer can be the same, in which case the self-sig is validated |
---|
[0779c99] | 45 | static int _verify_signature(certificate_t *issuer, certificate_t *cert) { |
---|
[5450aeb] | 46 | if (cert->issued_by(cert, issuer)) |
---|
| 47 | if (cert->get_validity(cert, NULL, NULL, NULL)) |
---|
| 48 | return 1; |
---|
| 49 | return 0; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Init the verifier subsystem. |
---|
| 54 | */ |
---|
[43e3b71] | 55 | void abac_verifier_init(void) { |
---|
[5450aeb] | 56 | atexit(library_deinit); |
---|
| 57 | |
---|
[1324a63] | 58 | // silence all debugging |
---|
| 59 | dbg_default_set_level(-1); |
---|
| 60 | |
---|
[5450aeb] | 61 | if (!library_init(NULL)) |
---|
| 62 | exit(SS_RC_LIBSTRONGSWAN_INTEGRITY); |
---|
| 63 | |
---|
| 64 | if (!lib->plugins->load(lib->plugins, NULL, |
---|
| 65 | lib->settings->get_str(lib->settings, "pki.load", PLUGINS))) |
---|
| 66 | exit(SS_RC_INITIALIZATION_FAILED); |
---|
| 67 | } |
---|
| 68 | |
---|
[186cb75] | 69 | /** |
---|
| 70 | * Uninitialize the system, free up any allocated memory. |
---|
| 71 | */ |
---|
[43e3b71] | 72 | void abac_verifier_deinit(void) { |
---|
| 73 | abac_id_cert_t *id; |
---|
[186cb75] | 74 | |
---|
| 75 | while ((id = id_certs) != NULL) { |
---|
| 76 | HASH_DEL(id_certs, id); |
---|
| 77 | |
---|
| 78 | free(id->keyid); |
---|
| 79 | id->cert->destroy(id->cert); |
---|
| 80 | free(id); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
[5450aeb] | 84 | /** |
---|
| 85 | * Load an ID certificate. |
---|
| 86 | */ |
---|
[e898049] | 87 | static int _load_id(certificate_t *cert) { |
---|
[327e808] | 88 | abac_id_cert_t *id_cert = NULL; |
---|
[5450aeb] | 89 | char *keyid = NULL; |
---|
| 90 | chunk_t id; |
---|
| 91 | int ret; |
---|
[2ef48fa] | 92 | x509_t *x509 = (x509_t *)cert; |
---|
[5450aeb] | 93 | |
---|
[e898049] | 94 | assert(cert != NULL); |
---|
[5450aeb] | 95 | |
---|
[2ef48fa] | 96 | // get the key ID |
---|
| 97 | id = x509->get_subjectKeyIdentifier(x509); |
---|
[327e808] | 98 | keyid = _chunk_to_string(id); |
---|
| 99 | |
---|
| 100 | // if we already have this cert 'error' with success |
---|
| 101 | HASH_FIND_STR(id_certs, keyid, id_cert); |
---|
| 102 | if (id_cert != NULL) { |
---|
| 103 | ret = ABAC_CERT_SUCCESS; |
---|
| 104 | goto error; |
---|
| 105 | } |
---|
| 106 | |
---|
[5450aeb] | 107 | // validate sig |
---|
[0779c99] | 108 | ret = _verify_signature(cert, cert); |
---|
| 109 | if (!ret) { |
---|
| 110 | ret = ABAC_CERT_BAD_SIG; |
---|
| 111 | goto error; |
---|
| 112 | } |
---|
[5450aeb] | 113 | |
---|
| 114 | // success, add the key to the map of certificates |
---|
[327e808] | 115 | id_cert = abac_xmalloc(sizeof(abac_id_cert_t)); |
---|
[5450aeb] | 116 | id_cert->keyid = keyid; |
---|
| 117 | id_cert->cert = cert; |
---|
| 118 | HASH_ADD_KEYPTR(hh, id_certs, id_cert->keyid, strlen(id_cert->keyid), id_cert); |
---|
| 119 | |
---|
[0779c99] | 120 | return ABAC_CERT_SUCCESS; |
---|
[5450aeb] | 121 | |
---|
| 122 | error: |
---|
[327e808] | 123 | if (keyid != NULL) free(keyid); |
---|
[5450aeb] | 124 | |
---|
[0779c99] | 125 | return ret; |
---|
[5450aeb] | 126 | } |
---|
| 127 | |
---|
[e898049] | 128 | /** |
---|
| 129 | * Load an ID cert from a file. |
---|
| 130 | */ |
---|
[43e3b71] | 131 | int abac_verifier_load_id_file(char *filename) { |
---|
[61278ec] | 132 | if (lib == NULL) |
---|
| 133 | errx(1, "looks like you didn't call libabac_init() (lib is NULL)"); |
---|
| 134 | |
---|
[e898049] | 135 | // load the cert |
---|
| 136 | certificate_t *cert = lib->creds->create( |
---|
| 137 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 138 | BUILD_FROM_FILE, filename, |
---|
| 139 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 140 | BUILD_END |
---|
| 141 | ); |
---|
| 142 | if (cert == NULL) |
---|
[0779c99] | 143 | return ABAC_CERT_INVALID; |
---|
[e898049] | 144 | return _load_id(cert); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Load an ID cert from a chunk. |
---|
| 149 | */ |
---|
[43e3b71] | 150 | int abac_verifier_load_id_chunk(chunk_t chunk) { |
---|
[e898049] | 151 | // load the cert |
---|
| 152 | certificate_t *cert = lib->creds->create( |
---|
| 153 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 154 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 155 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 156 | BUILD_END |
---|
| 157 | ); |
---|
| 158 | if (cert == NULL) |
---|
[0779c99] | 159 | return ABAC_CERT_INVALID; |
---|
[e898049] | 160 | return _load_id(cert); |
---|
| 161 | } |
---|
| 162 | |
---|
[5450aeb] | 163 | /** |
---|
| 164 | * Load an attribute cert. |
---|
| 165 | * Returns true only if the certificate is valid and is issued by the proper |
---|
| 166 | * authority. |
---|
| 167 | */ |
---|
[0779c99] | 168 | static int _load_attribute_cert(certificate_t *cert, abac_credential_t **cred_ret) { |
---|
[804a66a] | 169 | ietf_attributes_t *attr_cert = NULL; |
---|
[1743825] | 170 | abac_role_t *head_role = NULL; |
---|
| 171 | abac_role_t *tail_role = NULL; |
---|
[43e3b71] | 172 | abac_id_cert_t *issuer; |
---|
[5450aeb] | 173 | int ret; |
---|
| 174 | |
---|
| 175 | // get the attr |
---|
| 176 | ac_t *ac = (ac_t *)cert; |
---|
[804a66a] | 177 | attr_cert = ac->get_groups(ac); |
---|
[0779c99] | 178 | if (attr_cert == NULL) { |
---|
| 179 | ret = ABAC_CERT_INVALID; |
---|
| 180 | goto error; |
---|
| 181 | } |
---|
[5450aeb] | 182 | |
---|
[804a66a] | 183 | char *attr_string = attr_cert->get_string(attr_cert); |
---|
[0779c99] | 184 | if (attr_string == NULL) { |
---|
| 185 | ret = ABAC_CERT_INVALID; |
---|
| 186 | goto error; |
---|
| 187 | } |
---|
[5450aeb] | 188 | |
---|
| 189 | // split into head/tail parts |
---|
| 190 | char head[256], tail[256]; |
---|
| 191 | ret = sscanf(attr_string, "%255s <- %255s", head, tail); |
---|
[0779c99] | 192 | if (ret != 2) { |
---|
| 193 | ret = ABAC_CERT_INVALID; |
---|
| 194 | goto error; |
---|
| 195 | } |
---|
[5450aeb] | 196 | |
---|
| 197 | // must be a role |
---|
[dcc1a8e] | 198 | head_role = abac_role_from_string(head); |
---|
[5450aeb] | 199 | if (head_role == NULL) goto error; |
---|
[0779c99] | 200 | if (!abac_role_is_role(head_role)) { |
---|
| 201 | ret = ABAC_CERT_INVALID; |
---|
| 202 | goto error; |
---|
| 203 | } |
---|
[5450aeb] | 204 | |
---|
| 205 | // make sure the tail role is valid |
---|
[dcc1a8e] | 206 | tail_role = abac_role_from_string(tail); |
---|
[0779c99] | 207 | if (tail_role == NULL) { |
---|
| 208 | ret = ABAC_CERT_INVALID; |
---|
| 209 | goto error; |
---|
| 210 | } |
---|
[5450aeb] | 211 | |
---|
| 212 | // get the issuer based on keyid |
---|
[dcc1a8e] | 213 | char *principal = abac_role_principal(head_role); |
---|
[5450aeb] | 214 | HASH_FIND_STR(id_certs, principal, issuer); |
---|
[0779c99] | 215 | if (issuer == NULL) { |
---|
| 216 | ret = ABAC_CERT_MISSING_ISSUER; |
---|
| 217 | goto error; |
---|
| 218 | } |
---|
[5450aeb] | 219 | |
---|
| 220 | // make sure the issuer's signed it |
---|
[0779c99] | 221 | ret = _verify_signature(issuer->cert, cert); |
---|
| 222 | if (!ret) { |
---|
| 223 | ret = ABAC_CERT_BAD_SIG; |
---|
| 224 | goto error; |
---|
| 225 | } |
---|
[5450aeb] | 226 | |
---|
| 227 | // at this point we know we have a good attribute cert |
---|
[401a054] | 228 | abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); |
---|
| 229 | cred->head = head_role; |
---|
| 230 | cred->tail = tail_role; |
---|
| 231 | cred->cert = cert; |
---|
| 232 | cred->issuer = issuer->cert->get_ref(issuer->cert); |
---|
[fbb591e] | 233 | cred->refcount = 1; |
---|
[0779c99] | 234 | *cred_ret = cred; |
---|
[5450aeb] | 235 | |
---|
| 236 | // free up some crap |
---|
[804a66a] | 237 | attr_cert->destroy(attr_cert); |
---|
[5450aeb] | 238 | |
---|
[0779c99] | 239 | return ABAC_CERT_SUCCESS; |
---|
[5450aeb] | 240 | |
---|
| 241 | error: |
---|
| 242 | if (cert != NULL) cert->destroy(cert); |
---|
[804a66a] | 243 | if (attr_cert != NULL) attr_cert->destroy(attr_cert); |
---|
[dcc1a8e] | 244 | if (head_role != NULL) abac_role_free(head_role); |
---|
| 245 | if (tail_role != NULL) abac_role_free(tail_role); |
---|
[5450aeb] | 246 | |
---|
[0779c99] | 247 | return ret; |
---|
[5450aeb] | 248 | } |
---|
[5f99fbc] | 249 | |
---|
[e898049] | 250 | /** |
---|
| 251 | * Load an attribute cert from a file. |
---|
| 252 | */ |
---|
[0779c99] | 253 | int abac_verifier_load_attribute_cert_file(char *filename, abac_credential_t **cred) { |
---|
[e898049] | 254 | // load the cert |
---|
| 255 | certificate_t *cert = lib->creds->create( |
---|
| 256 | lib->creds, CRED_CERTIFICATE, CERT_X509_AC, |
---|
| 257 | BUILD_FROM_FILE, filename, |
---|
| 258 | BUILD_END |
---|
| 259 | ); |
---|
| 260 | if (cert == NULL) |
---|
[0779c99] | 261 | return ABAC_CERT_INVALID; |
---|
| 262 | return _load_attribute_cert(cert, cred); |
---|
[e898049] | 263 | } |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Load an attribute cert from a chunk. |
---|
| 267 | */ |
---|
[0779c99] | 268 | int abac_verifier_load_attribute_cert_chunk(chunk_t chunk, abac_credential_t **cred) { |
---|
[e898049] | 269 | // load the cert |
---|
| 270 | certificate_t *cert = lib->creds->create( |
---|
| 271 | lib->creds, CRED_CERTIFICATE, CERT_X509_AC, |
---|
| 272 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 273 | BUILD_END |
---|
| 274 | ); |
---|
| 275 | if (cert == NULL) |
---|
[0779c99] | 276 | return ABAC_CERT_INVALID; |
---|
| 277 | return _load_attribute_cert(cert, cred); |
---|
[e898049] | 278 | } |
---|
| 279 | |
---|
[5f99fbc] | 280 | /** |
---|
| 281 | * Return the head role. |
---|
| 282 | */ |
---|
[401a054] | 283 | abac_role_t *abac_credential_head(abac_credential_t *cred) { |
---|
| 284 | return cred->head; |
---|
[5f99fbc] | 285 | } |
---|
| 286 | |
---|
| 287 | /** |
---|
| 288 | * Return the tail role. |
---|
| 289 | */ |
---|
[401a054] | 290 | abac_role_t *abac_credential_tail(abac_credential_t *cred) { |
---|
| 291 | return cred->tail; |
---|
[5f99fbc] | 292 | } |
---|
[85cdf53] | 293 | |
---|
| 294 | /** |
---|
| 295 | * Return the encoding of the attribute cert. |
---|
| 296 | */ |
---|
[401a054] | 297 | abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) { |
---|
| 298 | chunk_t encoding = cred->cert->get_encoding(cred->cert); |
---|
[9efbfbf] | 299 | abac_chunk_t ret = { encoding.ptr, encoding.len }; |
---|
| 300 | return ret; |
---|
[85cdf53] | 301 | } |
---|
| 302 | |
---|
| 303 | /** |
---|
| 304 | * Return the encoding of the issuer cert. |
---|
| 305 | */ |
---|
[401a054] | 306 | abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) { |
---|
| 307 | chunk_t encoding = cred->issuer->get_encoding(cred->issuer); |
---|
[9efbfbf] | 308 | abac_chunk_t ret = { encoding.ptr, encoding.len }; |
---|
| 309 | return ret; |
---|
[85cdf53] | 310 | } |
---|
[186cb75] | 311 | |
---|
| 312 | /** |
---|
[fbb591e] | 313 | * Increase the ref count of a credential. |
---|
[186cb75] | 314 | */ |
---|
[401a054] | 315 | abac_credential_t *abac_credential_dup(abac_credential_t *cred) { |
---|
| 316 | assert(cred != NULL); |
---|
[186cb75] | 317 | |
---|
[fbb591e] | 318 | ++cred->refcount; |
---|
| 319 | return cred; |
---|
[186cb75] | 320 | } |
---|
| 321 | |
---|
| 322 | /** |
---|
[fbb591e] | 323 | * Decrease the reference count of a credential, freeing it when it reaches 0. |
---|
[186cb75] | 324 | */ |
---|
[401a054] | 325 | void abac_credential_free(abac_credential_t *cred) { |
---|
| 326 | if (cred == NULL) |
---|
[186cb75] | 327 | return; |
---|
| 328 | |
---|
[fbb591e] | 329 | --cred->refcount; |
---|
| 330 | if (cred->refcount > 0) |
---|
| 331 | return; |
---|
| 332 | |
---|
[401a054] | 333 | abac_role_free(cred->head); |
---|
| 334 | abac_role_free(cred->tail); |
---|
| 335 | cred->cert->destroy(cred->cert); |
---|
| 336 | cred->issuer->destroy(cred->issuer); // reference counted |
---|
[186cb75] | 337 | |
---|
[401a054] | 338 | free(cred); |
---|
[186cb75] | 339 | } |
---|