]>
git.proxmox.com Git - swtpm.git/blob - src/swtpm/capabilities.c
2 * capabilities.c -- capabilities
4 * (c) Copyright IBM Corporation 2019.
6 * Author: Stefan Berger <stefanb@us.ibm.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
14 * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * Neither the names of the IBM Corporation nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 #include <libtpms/tpm_library.h>
46 #include <libtpms/tpm_error.h>
48 #include "capabilities.h"
51 /* Convert the RSA key size indicators supported by libtpms into capability
53 * libtpms may return us something like this here:
54 * "TPMAttributes":{"manufacturer":"id:00001014",\
55 * "version":"id:20191023","model":"swtpm","RSAKeySizes":[1024,2048,3072]}}
57 * or an older version may not report RSA keysizes:
58 * "TPMAttributes":{"manufacturer":"id:00001014",\
59 * "version":"id:20191023","model":"swtpm"}}
61 static int get_rsa_keysize_caps(char **keysizecaps
)
65 const char *needle
= "\"RSAKeySizes\":[";
66 char *info_data
= TPMLIB_GetInfo(4 /*TPMLIB_INFO_TPMFEATURES*/);
74 start
= strstr(info_data
, needle
);
76 start
+= strlen(needle
);
78 unsigned long int keysize
= strtoul(start
, &endptr
, 10);
80 if (*endptr
!= ',' && *endptr
!= ']') {
81 logprintf(STDERR_FILENO
, "Malformed TPMLIB_GetInfo() string\n");
86 n
= snprintf(buffer
+ offset
, sizeof(buffer
) - offset
,
87 ", \"rsa-keysize-%lu\"",
89 if (n
< 0 || (unsigned)n
>= sizeof(buffer
) - offset
) {
90 logprintf(STDERR_FILENO
, "%s: buffer is too small\n", __func__
);
101 *keysizecaps
= strndup(buffer
, sizeof(buffer
) - 1);
102 if (*keysizecaps
== NULL
)
111 logprintf(STDERR_FILENO
, "Out of memory\n");
116 int capabilities_print_json(bool cusetpm
)
122 const char *cmdarg_seccomp
= "\"cmdarg-seccomp\", ";
124 const char *cmdarg_seccomp
= "";
126 const char *with_tpm1
= "";
127 const char *with_tpm2
= "";
128 char *keysizecaps
= NULL
;
130 ret
= get_rsa_keysize_caps(&keysizecaps
);
134 if (TPMLIB_ChooseTPMVersion(TPMLIB_TPM_VERSION_1_2
) == TPM_SUCCESS
)
135 with_tpm1
= "\"tpm-1.2\", ";
136 if (TPMLIB_ChooseTPMVersion(TPMLIB_TPM_VERSION_2
) == TPM_SUCCESS
)
137 with_tpm2
= "\"tpm-2.0\", ";
139 n
= asprintf(&string
,
141 "\"type\": \"swtpm\", "
145 "\"version\": \"" VERSION
"\" "
149 !cusetpm
? "\"tpm-send-command-header\", ": "",
150 !cusetpm
? "\"flags-opt-startup\", " : "",
152 true ? "\"cmdarg-key-fd\", " : "",
153 true ? "\"cmdarg-pwd-fd\"" : "",
154 keysizecaps
? keysizecaps
: ""
158 logprintf(STDERR_FILENO
, "Out of memory\n");
164 fprintf(stdout
, "%s\n", string
);