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