- key derivation for child_sa works
[strongswan.git] / Source / charon / sa / child_sa.c
1 /**
2 * @file child_sa.c
3 *
4 * @brief Implementation of child_sa_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "child_sa.h"
24
25
26 #include <utils/allocator.h>
27 #include <daemon.h>
28
29
30 typedef struct private_child_sa_t private_child_sa_t;
31
32 /**
33 * Private data of a child_sa_t object.
34 */
35 struct private_child_sa_t {
36 /**
37 * Public interface of child_sa_t.
38 */
39 child_sa_t public;
40
41 /**
42 * CHILD_SAs own logger
43 */
44 logger_t *logger;
45
46 /**
47 * Protocols used in this SA
48 */
49 protocol_id_t protocols[2];
50 };
51
52
53 /**
54 * Implementation of child_sa_t.get_spi.
55 */
56 static u_int32_t get_spi(private_child_sa_t *this)
57 {
58 return 0;
59 }
60
61 /**
62 * Implementation of child_sa_t.destroy.
63 */
64 static void destroy(private_child_sa_t *this)
65 {
66 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
67 allocator_free(this);
68 }
69
70 /*
71 * Described in header.
72 */
73 child_sa_t * child_sa_create(child_proposal_t *proposal, prf_plus_t *prf_plus)
74 {
75 private_child_sa_t *this = allocator_alloc_thing(private_child_sa_t);
76 u_int i;
77
78 /* public functions */
79 this->public.get_spi = (u_int32_t(*)(child_sa_t*))get_spi;
80 this->public.destroy = (void(*)(child_sa_t*))destroy;
81
82 /* private data */
83 this->logger = charon->logger_manager->create_logger(charon->logger_manager, CHILD_SA, NULL);
84 proposal->get_protocols(proposal, this->protocols);
85
86 /* derive keys */
87 for (i = 0; i<2; i++)
88 {
89 if (this->protocols[i] != UNDEFINED_PROTOCOL_ID)
90 {
91 algorithm_t *algo;
92 chunk_t key;
93
94 /* get encryption key */
95 if (proposal->get_algorithm(proposal, this->protocols[i], ENCRYPTION_ALGORITHM, &algo))
96 {
97 this->logger->log(this->logger, CONTROL|LEVEL1, "%s: using %s %s, ",
98 mapping_find(protocol_id_m, this->protocols[i]),
99 mapping_find(transform_type_m, ENCRYPTION_ALGORITHM),
100 mapping_find(encryption_algorithm_m, algo->algorithm));
101
102 prf_plus->allocate_bytes(prf_plus, algo->key_size, &key);
103 this->logger->log_chunk(this->logger, PRIVATE, "key:", &key);
104 allocator_free_chunk(&key);
105 }
106
107 /* get integrity key */
108 if (proposal->get_algorithm(proposal, this->protocols[i], INTEGRITY_ALGORITHM, &algo))
109 {
110 this->logger->log(this->logger, CONTROL|LEVEL1, "%s: using %s %s,",
111 mapping_find(protocol_id_m, this->protocols[i]),
112 mapping_find(transform_type_m, INTEGRITY_ALGORITHM),
113 mapping_find(integrity_algorithm_m, algo->algorithm));
114
115 prf_plus->allocate_bytes(prf_plus, algo->key_size, &key);
116 this->logger->log_chunk(this->logger, PRIVATE, "key:", &key);
117 allocator_free_chunk(&key);
118 }
119 }
120 }
121
122 return (&this->public);
123 }