Skip to content

Commit 04c8b56

Browse files
hyperfinitismJuergenReppSIT
authored andcommitted
fix(tpm2_alg_util): avoid mutating input scheme in handle_scheme_sign
Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
1 parent 4a87504 commit 04c8b56

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

Makefile.am

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ LDADD = \
2727

2828
AM_DISTCHECK_CONFIGURE_FLAGS = --with-bashcompdir='$$(datarootdir)/bash-completion/completions'
2929

30-
AM_CFLAGS += -DCMOCKA_ENABLE_DEPRECATED=1
31-
AM_CFLAGS += -Wno-error=deprecated-declarations
32-
AM_CFLAGS += -Wno-error=discarded-qualifiers
33-
3430
# keep me sorted
3531
bin_PROGRAMS =
3632
FAPI_CFLAGS = $(EXTRA_CFLAGS) $(TSS2_FAPI_CFLAGS) $(CODE_COVERAGE_CFLAGS) $(CRYPTO_CFLAGS)

lib/tpm2_alg_util.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ static alg_parser_rc handle_sym_common(const char *ext, TPMT_SYM_DEF_OBJECT *s,
152152
* You cannot change all the variables in this, as they are dependent
153153
* on names in that routine; this is for simplicity.
154154
*/
155-
#define do_scheme_halg(scheme, advance, alg) \
155+
#define DO_SCHEME_HALG(scheme_, advance, alg) \
156156
do { \
157-
scheme += advance; \
157+
scheme_ += advance; \
158158
s->scheme.scheme = alg; \
159159
do_scheme_hash_alg = true; \
160160
found = true; \
@@ -174,16 +174,18 @@ static alg_parser_rc handle_scheme_sign(const char *scheme,
174174
return alg_parser_rc_error;
175175
}
176176

177+
char *buf_ptr = buf;
178+
177179
// Get the scheme and symetric details
178180
TPMS_ASYM_PARMS *s = &public->publicArea.parameters.asymDetail;
179181

180-
if (!strcmp(scheme, "null")) {
182+
if (!strcmp(buf_ptr, "null")) {
181183
public->publicArea.parameters.asymDetail.scheme.scheme = TPM2_ALG_NULL;
182184
return alg_parser_rc_continue;
183185
}
184186

185187
char *halg = NULL;
186-
char *split = strchr(scheme, '-');
188+
char *split = strchr(buf_ptr, '-');
187189
if (split) {
188190
*split = '\0';
189191
halg = split + 1;
@@ -193,51 +195,53 @@ static alg_parser_rc handle_scheme_sign(const char *scheme,
193195
bool do_scheme_hash_alg = false;
194196

195197
if (public->publicArea.type == TPM2_ALG_ECC) {
196-
if (!strncmp(scheme, "ecdsa", 5)) {
197-
do_scheme_halg(scheme, 5, TPM2_ALG_ECDSA);
198-
} else if (!strncmp(scheme, "ecdh", 4)) {
199-
do_scheme_halg(scheme, 4, TPM2_ALG_ECDH);
200-
} else if (!strncmp(scheme, "ecschnorr", 9)) {
201-
do_scheme_halg(scheme, 9, TPM2_ALG_ECSCHNORR);
202-
} else if (!strncmp(scheme, "sm2", 3)) {
203-
do_scheme_halg(scheme, 3, TPM2_ALG_SM2);
204-
} else if (!strncmp(scheme, "ecdaa", 5)) {
205-
do_scheme_halg(scheme, 5, TPM2_ALG_ECDAA);
198+
if (!strncmp(buf_ptr, "ecdsa", 5)) {
199+
DO_SCHEME_HALG(buf_ptr, 5, TPM2_ALG_ECDSA);
200+
} else if (!strncmp(buf_ptr, "ecdh", 4)) {
201+
DO_SCHEME_HALG(buf_ptr, 4, TPM2_ALG_ECDH);
202+
} else if (!strncmp(buf_ptr, "ecschnorr", 9)) {
203+
DO_SCHEME_HALG(buf_ptr, 9, TPM2_ALG_ECSCHNORR);
204+
} else if (!strncmp(buf_ptr, "sm2", 3)) {
205+
DO_SCHEME_HALG(buf_ptr, 3, TPM2_ALG_SM2);
206+
} else if (!strncmp(buf_ptr, "ecdaa", 5)) {
207+
DO_SCHEME_HALG(buf_ptr, 5, TPM2_ALG_ECDAA);
206208
/*
207209
* ECDAA has both a commit-counter value and hashing algorithm.
208210
* The default commit-counter value is set to zero to use the first
209211
* commit-id.
210212
*/
211-
if (scheme[0] == '\0') {
212-
scheme = "0";
213+
const char *count_str = buf_ptr;
214+
if (buf_ptr[0] == '\0') {
215+
count_str = "0";
213216
}
214217

215218
TPMS_SIG_SCHEME_ECDAA *e = &s->scheme.details.ecdaa;
216219

217-
bool res = tpm2_util_string_to_uint16(scheme, &e->count);
220+
bool res = tpm2_util_string_to_uint16(count_str, &e->count);
218221
if (!res) {
219222
return alg_parser_rc_error;
220223
}
221-
} else if (!strcmp("null", scheme)) {
224+
} else if (!strcmp(buf_ptr, "null")) {
222225
s->scheme.scheme = TPM2_ALG_NULL;
226+
found = true;
223227
}
224228
} else {
225-
if (!strcmp(scheme, "rsaes")) {
229+
if (!strcmp(buf_ptr, "rsaes")) {
226230
/*
227231
* rsaes has no hash alg or details, so it MUST
228232
* match exactly, notice strcmp and NOT strNcmp!
229233
*/
230234
s->scheme.scheme = TPM2_ALG_RSAES;
231235
found = true;
232-
} else if (!strcmp("null", scheme)) {
236+
} else if (!strcmp(buf_ptr, "null")) {
233237
s->scheme.scheme = TPM2_ALG_NULL;
234238
found = true;
235-
} else if (!strncmp("rsapss", scheme, 6)) {
236-
do_scheme_halg(scheme, 6, TPM2_ALG_RSAPSS);
237-
} else if (!strncmp("rsassa", scheme, 6)) {
238-
do_scheme_halg(scheme, 6, TPM2_ALG_RSASSA);
239-
} else if (!strncmp(scheme, "oaep", 4)) {
240-
do_scheme_halg(scheme, 4, TPM2_ALG_OAEP);
239+
} else if (!strncmp(buf_ptr, "rsapss", 6)) {
240+
DO_SCHEME_HALG(buf_ptr, 6, TPM2_ALG_RSAPSS);
241+
} else if (!strncmp(buf_ptr, "rsassa", 6)) {
242+
DO_SCHEME_HALG(buf_ptr, 6, TPM2_ALG_RSASSA);
243+
} else if (!strncmp(buf_ptr, "oaep", 4)) {
244+
DO_SCHEME_HALG(buf_ptr, 4, TPM2_ALG_OAEP);
241245
}
242246
}
243247

0 commit comments

Comments
 (0)