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