swtpm_cert: simplify two's completement implementation

Simplify the two's complement implementation by converting the number
into a big endian and writing it out into a byte array that is prefixed
with a 0-byte. This covers all unsigned ints while the previous imple-
mentation would have been wrong once the number exceeded 255.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
This commit is contained in:
Stefan Berger 2018-05-04 16:53:19 -04:00
parent 029e1bd05a
commit ff641f7cdc

View File

@ -186,22 +186,6 @@ hex_str_to_bin(const char *hexstr, int *modulus_len)
return result;
}
static size_t
calc_twos_complement(unsigned int number,
unsigned char *twoscomp, size_t twoscomp_len)
{
size_t i = 0;
twoscomp[i++] = 0;
do {
twoscomp[i++] = (number & 0xff);
number >>= 8;
} while (number && i < twoscomp_len);
return i;
}
static gnutls_pubkey_t
create_rsa_from_modulus(unsigned char *modulus, unsigned int modulus_len,
uint32_t exponent)
@ -493,8 +477,8 @@ create_tpm_specification_info(const char *spec_family,
{
ASN1_TYPE at = ASN1_TYPE_EMPTY;
int err;
unsigned char twoscomp[5];
size_t twoscomp_len;
unsigned int bigendian;
unsigned char twoscomp[1 + sizeof(bigendian)] = { 0, };
err = asn_init();
if (err != ASN1_SUCCESS) {
@ -521,21 +505,23 @@ create_tpm_specification_info(const char *spec_family,
goto cleanup;
}
twoscomp_len = calc_twos_complement(spec_level, twoscomp, sizeof(twoscomp));
bigendian = htobe32(spec_level);
memcpy(&twoscomp[1], &bigendian, sizeof(bigendian));
err = asn1_write_value(at,
"tpmSpecificationSeq.tpmSpecificationSet.tpmSpecification.level",
twoscomp, twoscomp_len);
twoscomp, sizeof(twoscomp));
if (err != ASN1_SUCCESS) {
fprintf(stderr, "c1d. asn1_write_value error: %d\n", err);
goto cleanup;
}
twoscomp_len = calc_twos_complement(spec_revision, twoscomp, sizeof(twoscomp));
bigendian = htobe32(spec_revision);
memcpy(&twoscomp[1], &bigendian, sizeof(bigendian));
err = asn1_write_value(at,
"tpmSpecificationSeq.tpmSpecificationSet.tpmSpecification.revision",
twoscomp, twoscomp_len);
twoscomp, sizeof(twoscomp));
if (err != ASN1_SUCCESS) {
fprintf(stderr, "c1e. asn1_write_value error: %d\n", err);
goto cleanup;