Migrated shared_key to INIT/METHOD macros
[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 METHOD(shared_key_t, get_type, shared_key_type_t,
55 private_shared_key_t *this)
56 {
57 return this->type;
58 }
59
60 METHOD(shared_key_t, get_key, chunk_t,
61 private_shared_key_t *this)
62 {
63 return this->key;
64 }
65
66 METHOD(shared_key_t, get_ref, shared_key_t*,
67 private_shared_key_t *this)
68 {
69 ref_get(&this->ref);
70 return &this->public;
71 }
72
73 METHOD(shared_key_t, destroy, void,
74 private_shared_key_t *this)
75 {
76 if (ref_put(&this->ref))
77 {
78 free(this->key.ptr);
79 free(this);
80 }
81 }
82
83 /*
84 * see header file
85 */
86 shared_key_t *shared_key_create(shared_key_type_t type, chunk_t key)
87 {
88 private_shared_key_t *this;
89
90 INIT(this,
91 .public = {
92 .get_type = _get_type,
93 .get_key = _get_key,
94 .get_ref = _get_ref,
95 .destroy = _destroy,
96 },
97 .type = type,
98 .key = key,
99 .ref = 1,
100 );
101
102 return &this->public;
103 }
104