📄 auth.c
字号:
/* If this TFM has been allocated, we are all set */ if (ep->auth_hmacs[id]) continue; /* Allocate the ID */ tfm = crypto_alloc_hash(sctp_hmac_list[id].hmac_name, 0, CRYPTO_ALG_ASYNC); if (IS_ERR(tfm)) goto out_err; ep->auth_hmacs[id] = tfm; } return 0;out_err: /* Clean up any successfull allocations */ sctp_auth_destroy_hmacs(ep->auth_hmacs); return -ENOMEM;}/* Destroy the hmac tfm array */void sctp_auth_destroy_hmacs(struct crypto_hash *auth_hmacs[]){ int i; if (!auth_hmacs) return; for (i = 0; i < SCTP_AUTH_NUM_HMACS; i++) { if (auth_hmacs[i]) crypto_free_hash(auth_hmacs[i]); } kfree(auth_hmacs);}struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id){ return &sctp_hmac_list[hmac_id];}/* Get an hmac description information that we can use to build * the AUTH chunk */struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc){ struct sctp_hmac_algo_param *hmacs; __u16 n_elt; __u16 id = 0; int i; /* If we have a default entry, use it */ if (asoc->default_hmac_id) return &sctp_hmac_list[asoc->default_hmac_id]; /* Since we do not have a default entry, find the first entry * we support and return that. Do not cache that id. */ hmacs = asoc->peer.peer_hmacs; if (!hmacs) return NULL; n_elt = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 1; for (i = 0; i < n_elt; i++) { id = ntohs(hmacs->hmac_ids[i]); /* Check the id is in the supported range */ if (id > SCTP_AUTH_HMAC_ID_MAX) continue; /* See is we support the id. Supported IDs have name and * length fields set, so that we can allocated and use * them. We can safely just check for name, for without the * name, we can't allocate the TFM. */ if (!sctp_hmac_list[id].hmac_name) continue; break; } if (id == 0) return NULL; return &sctp_hmac_list[id];}static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id){ int found = 0; int i; for (i = 0; i < n_elts; i++) { if (hmac_id == hmacs[i]) { found = 1; break; } } return found;}/* See if the HMAC_ID is one that we claim as supported */int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc, __be16 hmac_id){ struct sctp_hmac_algo_param *hmacs; __u16 n_elt; if (!asoc) return 0; hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs; n_elt = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 1; return __sctp_auth_find_hmacid(hmacs->hmac_ids, n_elt, hmac_id);}/* Cache the default HMAC id. This to follow this text from SCTP-AUTH: * Section 6.1: * The receiver of a HMAC-ALGO parameter SHOULD use the first listed * algorithm it supports. */void sctp_auth_asoc_set_default_hmac(struct sctp_association *asoc, struct sctp_hmac_algo_param *hmacs){ struct sctp_endpoint *ep; __u16 id; int i; int n_params; /* if the default id is already set, use it */ if (asoc->default_hmac_id) return; n_params = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 1; ep = asoc->ep; for (i = 0; i < n_params; i++) { id = ntohs(hmacs->hmac_ids[i]); /* Check the id is in the supported range */ if (id > SCTP_AUTH_HMAC_ID_MAX) continue; /* If this TFM has been allocated, use this id */ if (ep->auth_hmacs[id]) { asoc->default_hmac_id = id; break; } }}/* Check to see if the given chunk is supposed to be authenticated */static int __sctp_auth_cid(sctp_cid_t chunk, struct sctp_chunks_param *param){ unsigned short len; int found = 0; int i; if (!param || param->param_hdr.length == 0) return 0; len = ntohs(param->param_hdr.length) - sizeof(sctp_paramhdr_t); /* SCTP-AUTH, Section 3.2 * The chunk types for INIT, INIT-ACK, SHUTDOWN-COMPLETE and AUTH * chunks MUST NOT be listed in the CHUNKS parameter. However, if * a CHUNKS parameter is received then the types for INIT, INIT-ACK, * SHUTDOWN-COMPLETE and AUTH chunks MUST be ignored. */ for (i = 0; !found && i < len; i++) { switch (param->chunks[i]) { case SCTP_CID_INIT: case SCTP_CID_INIT_ACK: case SCTP_CID_SHUTDOWN_COMPLETE: case SCTP_CID_AUTH: break; default: if (param->chunks[i] == chunk) found = 1; break; } } return found;}/* Check if peer requested that this chunk is authenticated */int sctp_auth_send_cid(sctp_cid_t chunk, const struct sctp_association *asoc){ if (!sctp_auth_enable || !asoc || !asoc->peer.auth_capable) return 0; return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);}/* Check if we requested that peer authenticate this chunk. */int sctp_auth_recv_cid(sctp_cid_t chunk, const struct sctp_association *asoc){ if (!sctp_auth_enable || !asoc) return 0; return __sctp_auth_cid(chunk, (struct sctp_chunks_param *)asoc->c.auth_chunks);}/* SCTP-AUTH: Section 6.2: * The sender MUST calculate the MAC as described in RFC2104 [2] using * the hash function H as described by the MAC Identifier and the shared * association key K based on the endpoint pair shared key described by * the shared key identifier. The 'data' used for the computation of * the AUTH-chunk is given by the AUTH chunk with its HMAC field set to * zero (as shown in Figure 6) followed by all chunks that are placed * after the AUTH chunk in the SCTP packet. */void sctp_auth_calculate_hmac(const struct sctp_association *asoc, struct sk_buff *skb, struct sctp_auth_chunk *auth, gfp_t gfp){ struct scatterlist sg; struct hash_desc desc; struct sctp_auth_bytes *asoc_key; __u16 key_id, hmac_id; __u8 *digest; unsigned char *end; int free_key = 0; /* Extract the info we need: * - hmac id * - key id */ key_id = ntohs(auth->auth_hdr.shkey_id); hmac_id = ntohs(auth->auth_hdr.hmac_id); if (key_id == asoc->active_key_id) asoc_key = asoc->asoc_shared_key; else { struct sctp_shared_key *ep_key; ep_key = sctp_auth_get_shkey(asoc, key_id); if (!ep_key) return; asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp); if (!asoc_key) return; free_key = 1; } /* set up scatter list */ end = skb_tail_pointer(skb); sg_init_one(&sg, auth, end - (unsigned char *)auth); desc.tfm = asoc->ep->auth_hmacs[hmac_id]; desc.flags = 0; digest = auth->auth_hdr.hmac; if (crypto_hash_setkey(desc.tfm, &asoc_key->data[0], asoc_key->len)) goto free; crypto_hash_digest(&desc, &sg, sg.length, digest);free: if (free_key) sctp_auth_key_put(asoc_key);}/* API Helpers *//* Add a chunk to the endpoint authenticated chunk list */int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id){ struct sctp_chunks_param *p = ep->auth_chunk_list; __u16 nchunks; __u16 param_len; /* If this chunk is already specified, we are done */ if (__sctp_auth_cid(chunk_id, p)) return 0; /* Check if we can add this chunk to the array */ param_len = ntohs(p->param_hdr.length); nchunks = param_len - sizeof(sctp_paramhdr_t); if (nchunks == SCTP_NUM_CHUNK_TYPES) return -EINVAL; p->chunks[nchunks] = chunk_id; p->param_hdr.length = htons(param_len + 1); return 0;}/* Add hmac identifires to the endpoint list of supported hmac ids */int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep, struct sctp_hmacalgo *hmacs){ int has_sha1 = 0; __u16 id; int i; /* Scan the list looking for unsupported id. Also make sure that * SHA1 is specified. */ for (i = 0; i < hmacs->shmac_num_idents; i++) { id = hmacs->shmac_idents[i]; if (SCTP_AUTH_HMAC_ID_SHA1 == id) has_sha1 = 1; if (!sctp_hmac_list[id].hmac_name) return -EOPNOTSUPP; } if (!has_sha1) return -EINVAL; memcpy(ep->auth_hmacs_list->hmac_ids, &hmacs->shmac_idents[0], hmacs->shmac_num_idents * sizeof(__u16)); ep->auth_hmacs_list->param_hdr.length = htons(sizeof(sctp_paramhdr_t) + hmacs->shmac_num_idents * sizeof(__u16)); return 0;}/* Set a new shared key on either endpoint or association. If the * the key with a same ID already exists, replace the key (remove the * old key and add a new one). */int sctp_auth_set_key(struct sctp_endpoint *ep, struct sctp_association *asoc, struct sctp_authkey *auth_key){ struct sctp_shared_key *cur_key = NULL; struct sctp_auth_bytes *key; struct list_head *sh_keys; int replace = 0; /* Try to find the given key id to see if * we are doing a replace, or adding a new key */ if (asoc) sh_keys = &asoc->endpoint_shared_keys; else sh_keys = &ep->endpoint_shared_keys; key_for_each(cur_key, sh_keys) { if (cur_key->key_id == auth_key->sca_keynumber) { replace = 1; break; } } /* If we are not replacing a key id, we need to allocate * a shared key. */ if (!replace) { cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber, GFP_KERNEL); if (!cur_key) return -ENOMEM; } /* Create a new key data based on the info passed in */ key = sctp_auth_create_key(auth_key->sca_keylen, GFP_KERNEL); if (!key) goto nomem; memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylen); /* If we are replacing, remove the old keys data from the * key id. If we are adding new key id, add it to the * list. */ if (replace) sctp_auth_key_put(cur_key->key); else list_add(&cur_key->key_list, sh_keys); cur_key->key = key; sctp_auth_key_hold(key); return 0;nomem: if (!replace) sctp_auth_shkey_free(cur_key); return -ENOMEM;}int sctp_auth_set_active_key(struct sctp_endpoint *ep, struct sctp_association *asoc, __u16 key_id){ struct sctp_shared_key *key; struct list_head *sh_keys; int found = 0; /* The key identifier MUST correst to an existing key */ if (asoc) sh_keys = &asoc->endpoint_shared_keys; else sh_keys = &ep->endpoint_shared_keys; key_for_each(key, sh_keys) { if (key->key_id == key_id) { found = 1; break; } } if (!found) return -EINVAL; if (asoc) { asoc->active_key_id = key_id; sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL); } else ep->active_key_id = key_id; return 0;}int sctp_auth_del_key_id(struct sctp_endpoint *ep, struct sctp_association *asoc, __u16 key_id){ struct sctp_shared_key *key; struct list_head *sh_keys; int found = 0; /* The key identifier MUST NOT be the current active key * The key identifier MUST correst to an existing key */ if (asoc) { if (asoc->active_key_id == key_id) return -EINVAL; sh_keys = &asoc->endpoint_shared_keys; } else { if (ep->active_key_id == key_id) return -EINVAL; sh_keys = &ep->endpoint_shared_keys; } key_for_each(key, sh_keys) { if (key->key_id == key_id) { found = 1; break; } } if (!found) return -EINVAL; /* Delete the shared key */ list_del_init(&key->key_list); sctp_auth_shkey_free(key); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -