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