]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libtpmtss/tpm_tss.c
Merge branch 'debug-level-build'
[thirdparty/strongswan.git] / src / libtpmtss / tpm_tss.c
1 /*
2 * Copyright (C) 2016 Andreas Steffen
3 *
4 * Copyright (C) secunet Security Networks AG
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 "tpm_tss.h"
18 #include "tpm_tss_tss2.h"
19 #include "tpm_tss_trousers.h"
20
21 /**
22 * Register plugins if built statically
23 */
24 #ifdef STATIC_PLUGIN_CONSTRUCTORS
25 #include "plugin_constructors.c"
26 #endif
27
28 /**
29 * Reference counter for library initialization
30 */
31 static refcount_t libtpmtss_ref = 0;
32
33 /**
34 * Described in header.
35 */
36 bool libtpmtss_init(void)
37 {
38 if (ref_cur(&libtpmtss_ref) || tpm_tss_tss2_init())
39 {
40 ref_get(&libtpmtss_ref);
41 return TRUE;
42 }
43 return FALSE;
44 }
45
46 /**
47 * Described in header.
48 */
49 void libtpmtss_deinit(void)
50 {
51 if (ref_cur(&libtpmtss_ref) && ref_put(&libtpmtss_ref))
52 {
53 tpm_tss_tss2_deinit();
54 }
55 }
56
57 typedef tpm_tss_t*(*tpm_tss_create)(void);
58
59 /**
60 * See header.
61 */
62 tpm_tss_t *tpm_tss_probe(tpm_version_t version)
63 {
64 tpm_tss_create stacks[] = {
65 tpm_tss_tss2_create,
66 tpm_tss_trousers_create,
67 };
68 tpm_tss_t *tpm;
69 int i;
70
71 for (i = 0; i < countof(stacks); i++)
72 {
73 tpm = stacks[i]();
74 if (tpm)
75 {
76 if (version == TPM_VERSION_ANY || version == tpm->get_version(tpm))
77 {
78 return tpm;
79 }
80 }
81 }
82 return NULL;
83 }