libtpmtss: Support for TSS2 v2 libraries
[strongswan.git] / src / libtpmtss / plugins / tpm / tpm_plugin.c
1 /*
2 * Copyright (C) 2017 Andreas Steffen
3 * HSR 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 "tpm_plugin.h"
17 #include "tpm_private_key.h"
18 #include "tpm_cert.h"
19 #include "tpm_rng.h"
20
21 #include <tpm_tss.h>
22 #include <library.h>
23
24 typedef struct private_tpm_plugin_t private_tpm_plugin_t;
25
26 /**
27 * private data of tpm_plugin
28 */
29 struct private_tpm_plugin_t {
30
31 /**
32 * public functions
33 */
34 tpm_plugin_t public;
35 };
36
37 METHOD(plugin_t, get_name, char*,
38 private_tpm_plugin_t *this)
39 {
40 return "tpm";
41 }
42
43 METHOD(plugin_t, get_features, int,
44 private_tpm_plugin_t *this, plugin_feature_t *features[])
45 {
46 static plugin_feature_t f_rng[] = {
47 PLUGIN_REGISTER(RNG, tpm_rng_create),
48 PLUGIN_PROVIDE(RNG, RNG_STRONG),
49 PLUGIN_PROVIDE(RNG, RNG_TRUE),
50 };
51 static plugin_feature_t f_privkey[] = {
52 PLUGIN_REGISTER(PRIVKEY, tpm_private_key_connect, FALSE),
53 PLUGIN_PROVIDE(PRIVKEY, KEY_ANY),
54 };
55 static plugin_feature_t f_cert[] = {
56 PLUGIN_REGISTER(CERT_DECODE, tpm_cert_load, FALSE),
57 PLUGIN_PROVIDE(CERT_DECODE, CERT_X509),
58 PLUGIN_DEPENDS(CERT_DECODE, CERT_X509),
59 };
60 static plugin_feature_t f[countof(f_rng) + countof(f_privkey) +
61 countof(f_cert)] = {};
62 static int count = 0;
63
64 if (!count)
65 {
66 plugin_features_add(f, f_privkey, countof(f_privkey), &count);
67 plugin_features_add(f, f_cert, countof(f_cert), &count);
68
69 if (lib->settings->get_bool(lib->settings,
70 "%s.plugins.tpm.use_rng", FALSE, lib->ns))
71 {
72 plugin_features_add(f, f_rng, countof(f_rng), &count);
73 }
74 }
75 *features = f;
76
77 return count;
78 }
79
80 METHOD(plugin_t, destroy, void,
81 private_tpm_plugin_t *this)
82 {
83 free(this);
84 libtpmtss_deinit();
85 }
86
87 /*
88 * see header file
89 */
90 plugin_t *tpm_plugin_create()
91 {
92 private_tpm_plugin_t *this;
93
94 if (!libtpmtss_init())
95 {
96 return NULL;
97 }
98
99 INIT(this,
100 .public = {
101 .plugin = {
102 .get_name = _get_name,
103 .get_features = _get_features,
104 .destroy = _destroy,
105 },
106 },
107 );
108
109 return &this->public.plugin;
110 }