Various stylistic fixes
[strongswan.git] / src / charon-tkm / src / tkm / tkm_private_key.c
1 /*
2 * Copyright (C) 2012-2013 Reto Buerki
3 * Copyright (C) 2012-2013 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <utils/debug.h>
18 #include <tkm/constants.h>
19 #include <tkm/client.h>
20
21 #include "tkm_utils.h"
22 #include "tkm_types.h"
23 #include "tkm_private_key.h"
24
25 typedef struct private_tkm_private_key_t private_tkm_private_key_t;
26
27 /**
28 * Private data of a tkm_private_key_t object.
29 */
30 struct private_tkm_private_key_t {
31
32 /**
33 * Public interface for this signer.
34 */
35 tkm_private_key_t public;
36
37 /**
38 * Key ID.
39 */
40 identification_t *id;
41
42 /**
43 * Key type.
44 */
45 key_type_t key_type;
46
47 /**
48 * Reference count.
49 */
50 refcount_t ref;
51
52 };
53
54 METHOD(private_key_t, get_type, key_type_t,
55 private_tkm_private_key_t *this)
56 {
57 return this->key_type;
58 }
59
60 METHOD(private_key_t, sign, bool,
61 private_tkm_private_key_t *this, signature_scheme_t scheme,
62 chunk_t data, chunk_t *signature)
63 {
64 signature_type sig;
65 init_message_type msg;
66 sign_info_t sign;
67 isa_id_type isa_id;
68
69 if (data.ptr == NULL)
70 {
71 DBG1(DBG_LIB, "unable to get signature information");
72 return FALSE;
73 }
74 sign = *(sign_info_t *)(data.ptr);
75
76 chunk_to_sequence(&sign.init_message, &msg, sizeof(init_message_type));
77 isa_id = sign.isa_id;
78 chunk_free(&sign.init_message);
79
80 if (ike_isa_sign(isa_id, 1, msg, &sig) != TKM_OK)
81 {
82 DBG1(DBG_LIB, "signature operation failed");
83 return FALSE;
84 }
85
86 sequence_to_chunk(sig.data, sig.size, signature);
87 return TRUE;
88 }
89
90 METHOD(private_key_t, decrypt, bool,
91 private_tkm_private_key_t *this, encryption_scheme_t scheme,
92 chunk_t crypto, chunk_t *plain)
93 {
94 return FALSE;
95 }
96
97 METHOD(private_key_t, get_keysize, int,
98 private_tkm_private_key_t *this)
99 {
100 return 0;
101 }
102
103 METHOD(private_key_t, get_public_key, public_key_t*,
104 private_tkm_private_key_t *this)
105 {
106 return NULL;
107 }
108
109 METHOD(private_key_t, get_encoding, bool,
110 private_tkm_private_key_t *this, cred_encoding_type_t type,
111 chunk_t *encoding)
112 {
113 return FALSE;
114 }
115
116 METHOD(private_key_t, get_fingerprint, bool,
117 private_tkm_private_key_t *this, cred_encoding_type_t type, chunk_t *fp)
118 {
119 *fp = this->id->get_encoding(this->id);
120 return TRUE;
121 }
122
123 METHOD(private_key_t, get_ref, private_key_t*,
124 private_tkm_private_key_t *this)
125 {
126 ref_get(&this->ref);
127 return &this->public.key;
128 }
129
130 METHOD(private_key_t, destroy, void,
131 private_tkm_private_key_t *this)
132 {
133 if (ref_put(&this->ref))
134 {
135 this->id->destroy(this->id);
136 free(this);
137 }
138 }
139
140 /**
141 * See header.
142 */
143 tkm_private_key_t *tkm_private_key_init(identification_t * const id)
144 {
145 private_tkm_private_key_t *this;
146 certificate_t *cert;
147 public_key_t *pubkey;
148
149 INIT(this,
150 .public = {
151 .key = {
152 .get_type = _get_type,
153 .sign = _sign,
154 .decrypt = _decrypt,
155 .get_keysize = _get_keysize,
156 .get_public_key = _get_public_key,
157 .equals = private_key_equals,
158 .belongs_to = private_key_belongs_to,
159 .get_fingerprint = _get_fingerprint,
160 .has_fingerprint = private_key_has_fingerprint,
161 .get_encoding = _get_encoding,
162 .get_ref = _get_ref,
163 .destroy = _destroy,
164 },
165 },
166 .ref = 1,
167 .id = id->clone(id),
168 );
169
170 /* get key type from associated public key */
171 cert = lib->credmgr->get_cert(lib->credmgr, CERT_ANY, KEY_ANY, id, FALSE);
172 if (!cert)
173 {
174 destroy(this);
175 return NULL;
176 }
177
178 pubkey = cert->get_public_key(cert);
179 if (!pubkey)
180 {
181 cert->destroy(cert);
182 destroy(this);
183 return NULL;
184 }
185 this->key_type = pubkey->get_type(pubkey);
186 pubkey->destroy(pubkey);
187 cert->destroy(cert);
188
189 return &this->public;
190 }