libtpmtss: Load X.509 certificates from TPM 2.0 NV RAM
[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 <library.h>
22
23 typedef struct private_tpm_plugin_t private_tpm_plugin_t;
24
25 /**
26 * private data of tpm_plugin
27 */
28 struct private_tpm_plugin_t {
29
30 /**
31 * public functions
32 */
33 tpm_plugin_t public;
34 };
35
36 METHOD(plugin_t, get_name, char*,
37 private_tpm_plugin_t *this)
38 {
39 return "tpm";
40 }
41
42 METHOD(plugin_t, get_features, int,
43 private_tpm_plugin_t *this, plugin_feature_t *features[])
44 {
45 static plugin_feature_t f_rng[] = {
46 PLUGIN_REGISTER(RNG, tpm_rng_create),
47 PLUGIN_PROVIDE(RNG, RNG_STRONG),
48 PLUGIN_PROVIDE(RNG, RNG_TRUE),
49 };
50 static plugin_feature_t f_privkey[] = {
51 PLUGIN_REGISTER(PRIVKEY, tpm_private_key_connect, FALSE),
52 PLUGIN_PROVIDE(PRIVKEY, KEY_ANY),
53 };
54 static plugin_feature_t f_cert[] = {
55 PLUGIN_REGISTER(CERT_DECODE, tpm_cert_load, FALSE),
56 PLUGIN_PROVIDE(CERT_DECODE, CERT_X509),
57 PLUGIN_DEPENDS(CERT_DECODE, CERT_X509),
58 };
59 static plugin_feature_t f[countof(f_rng) + countof(f_privkey) +
60 countof(f_cert)] = {};
61 static int count = 0;
62
63 if (!count)
64 {
65 plugin_features_add(f, f_privkey, countof(f_privkey), &count);
66 plugin_features_add(f, f_cert, countof(f_cert), &count);
67
68 if (lib->settings->get_bool(lib->settings,
69 "%s.plugins.tpm.use_rng", FALSE, lib->ns))
70 {
71 plugin_features_add(f, f_rng, countof(f_rng), &count);
72 }
73 }
74 *features = f;
75
76 return count;
77 }
78
79 METHOD(plugin_t, destroy, void,
80 private_tpm_plugin_t *this)
81 {
82 free(this);
83 }
84
85 /*
86 * see header file
87 */
88 plugin_t *tpm_plugin_create()
89 {
90 private_tpm_plugin_t *this;
91
92 INIT(this,
93 .public = {
94 .plugin = {
95 .get_name = _get_name,
96 .get_features = _get_features,
97 .destroy = _destroy,
98 },
99 },
100 );
101
102 return &this->public.plugin;
103 }