f695c078d9a59bbe9ab92c3a7b60ddf1f5a578a4
[strongswan.git] / src / libstrongswan / credentials / keys / shared_key.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "shared_key.h"
17
18 ENUM(shared_key_type_names, SHARED_ANY, SHARED_PIN,
19 "ANY",
20 "IKE",
21 "EAP",
22 "PRIVATE_KEY_PASS",
23 "PIN",
24 );
25
26 typedef struct private_shared_key_t private_shared_key_t;
27
28 /**
29 * private data of shared_key
30 */
31 struct private_shared_key_t {
32
33 /**
34 * public functions
35 */
36 shared_key_t public;
37
38 /**
39 * type of this shared key
40 */
41 shared_key_type_t type;
42
43 /**
44 * associated shared key data
45 */
46 chunk_t key;
47
48 /**
49 * reference counter
50 */
51 refcount_t ref;
52 };
53
54 /**
55 * Implements shared_key_t.get_type
56 */
57 static shared_key_type_t get_type(private_shared_key_t *this)
58 {
59 return this->type;
60 }
61
62 /**
63 * Implements shared_key_t.get_key
64 */
65 static chunk_t get_key(private_shared_key_t *this)
66 {
67 return this->key;
68 }
69
70 /**
71 * Implements shared_key_t.get_ref
72 */
73 static shared_key_t* get_ref(private_shared_key_t *this)
74 {
75 ref_get(&this->ref);
76 return &this->public;
77 }
78
79 /**
80 * Implementation of shared_key_t.destroy
81 */
82 static void destroy(private_shared_key_t *this)
83 {
84 if (ref_put(&this->ref))
85 {
86 free(this->key.ptr);
87 free(this);
88 }
89 }
90
91 /*
92 * see header file
93 */
94 shared_key_t *shared_key_create(shared_key_type_t type, chunk_t key)
95 {
96 private_shared_key_t *this = malloc_thing(private_shared_key_t);
97
98 this->public.get_type = (shared_key_type_t (*)(shared_key_t *this))get_type;
99 this->public.get_key = (chunk_t (*)(shared_key_t *this))get_key;
100 this->public.get_ref = (shared_key_t* (*)(shared_key_t *this))get_ref;
101 this->public.destroy = (void(*)(shared_key_t*))destroy;
102
103 this->type = type;
104 this->key = key;
105 this->ref = 1;
106
107 return &this->public;
108 }
109