Add TKM public key implementation
[strongswan.git] / src / charon-tkm / src / tkm / tkm_public_key.c
1 /*
2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 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
19 #include "tkm_public_key.h"
20
21 typedef struct private_tkm_public_key_t private_tkm_public_key_t;
22
23 /**
24 * Private data of tkm_public_key_t object.
25 */
26 struct private_tkm_public_key_t {
27
28 /**
29 * Public interface for this signer.
30 */
31 tkm_public_key_t public;
32
33 /**
34 * Public modulus.
35 */
36 chunk_t n;
37
38 /**
39 * Public exponent.
40 */
41 chunk_t e;
42
43 /**
44 * Reference count.
45 */
46 refcount_t ref;
47 };
48
49 METHOD(public_key_t, get_type, key_type_t,
50 private_tkm_public_key_t *this)
51 {
52 return KEY_RSA;
53 }
54
55 METHOD(public_key_t, verify, bool,
56 private_tkm_public_key_t *this, signature_scheme_t scheme,
57 chunk_t data, chunk_t signature)
58 {
59 return TRUE;
60 }
61
62 METHOD(public_key_t, encrypt_, bool,
63 private_tkm_public_key_t *this, encryption_scheme_t scheme,
64 chunk_t plain, chunk_t *crypto)
65 {
66 return FALSE;
67 }
68
69 METHOD(public_key_t, get_keysize, int,
70 private_tkm_public_key_t *this)
71 {
72 return 0;
73 }
74
75 METHOD(public_key_t, get_encoding, bool,
76 private_tkm_public_key_t *this, cred_encoding_type_t type,
77 chunk_t *encoding)
78 {
79 return NULL;
80 }
81
82 METHOD(public_key_t, get_fingerprint, bool,
83 private_tkm_public_key_t *this, cred_encoding_type_t type, chunk_t *fp)
84 {
85 if (lib->encoding->get_cache(lib->encoding, type, this, fp))
86 {
87 return TRUE;
88 }
89 return lib->encoding->encode(lib->encoding, type, this, fp,
90 CRED_PART_RSA_MODULUS, this->n,
91 CRED_PART_RSA_PUB_EXP, this->e,
92 CRED_PART_END);
93 }
94
95 METHOD(public_key_t, get_ref, public_key_t*,
96 private_tkm_public_key_t *this)
97 {
98 ref_get(&this->ref);
99 return &this->public.key;
100 }
101
102 METHOD(public_key_t, destroy, void,
103 private_tkm_public_key_t *this)
104 {
105 if (ref_put(&this->ref))
106 {
107 lib->encoding->clear_cache(lib->encoding, this);
108 chunk_free(&this->n);
109 chunk_free(&this->e);
110 free(this);
111 }
112 }
113
114 /**
115 * See header.
116 */
117 tkm_public_key_t *tkm_public_key_load(key_type_t type, va_list args)
118 {
119 private_tkm_public_key_t *this;
120 chunk_t n, e;
121
122 n = e = chunk_empty;
123 while (TRUE)
124 {
125 switch (va_arg(args, builder_part_t))
126 {
127 case BUILD_RSA_MODULUS:
128 n = va_arg(args, chunk_t);
129 continue;
130 case BUILD_RSA_PUB_EXP:
131 e = va_arg(args, chunk_t);
132 continue;
133 case BUILD_END:
134 break;
135 default:
136 return NULL;
137 }
138 break;
139 }
140
141 if (!e.ptr || !n.ptr)
142 {
143 return NULL;
144 }
145
146 INIT(this,
147 .public = {
148 .key = {
149 .get_type = _get_type,
150 .verify = _verify,
151 .encrypt = _encrypt_,
152 .equals = public_key_equals,
153 .get_keysize = _get_keysize,
154 .get_fingerprint = _get_fingerprint,
155 .has_fingerprint = public_key_has_fingerprint,
156 .get_encoding = _get_encoding,
157 .get_ref = _get_ref,
158 .destroy = _destroy,
159 },
160 },
161 .ref = 1,
162 .n = chunk_clone(n),
163 .e = chunk_clone(e),
164 );
165
166 return &this->public;
167 }