Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#873] Fixed Checkstyle Issues In HIRS_STRUCTS #876

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions HIRS_Structs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ configurations.checkstyle {
}
}
checkstyleMain {
source ='src/main/java'
source = 'src/main/java'
}
checkstyleTest {
source ='src/test/java'
source = 'src/test/java'
}
tasks.withType(Checkstyle) {
reports {
Expand Down
4 changes: 2 additions & 2 deletions HIRS_Structs/config/spotbugs/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<!-- Docs at http://findbugs.sourceforge.net/manual/filter.html -->
<FindBugsFilter>
<Match>
<Package name="~hirs\.structs.*" />
<Package name="~hirs\.structs.*"/>
</Match>
<Match>
<!-- https://github.com/spotbugs/spotbugs/pull/2748 -->
<Bug pattern="CT_CONSTRUCTOR_THROW" />
<Bug pattern="CT_CONSTRUCTOR_THROW"/>
</Match>

<!-- <Match>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

import hirs.structs.elements.Struct;
import hirs.structs.elements.StructElementLength;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
* StructBuilder implementation.
*
* @param <T> the type of Struct to build
*/
public class SimpleStructBuilder<T extends Struct> implements StructBuilder {
private T struct;
private final Class<T> clazz;
private T struct;

/**
* Instantiates the builder.
*
* @param clazz The type of struct to build
*/
public SimpleStructBuilder(final Class<T> clazz) {
Expand All @@ -29,7 +31,7 @@ private void resetStruct() {
try {
struct = ConstructorUtils.invokeConstructor(clazz);
} catch (InstantiationException | IllegalAccessException
| NoSuchMethodException | InvocationTargetException e) {
| NoSuchMethodException | InvocationTargetException e) {
throw new StructBuilderException(
String.format("Unexpected error constructing new instance: %s",
clazz.getSimpleName(), e.getMessage()), e);
Expand Down Expand Up @@ -59,17 +61,13 @@ public SimpleStructBuilder<T> set(final String field, final String value) {
public SimpleStructBuilder<T> set(final String field, final Number value) {
try {
String type = clazz.getDeclaredField(field).getType().getSimpleName();
switch (clazz.getDeclaredField(field).getType().getSimpleName()) {
case "short":
return setField(field, value.shortValue());
case "int":
return setField(field, value.intValue());
case "byte":
return setField(field, value.byteValue());
default:
throw new StructBuilderException(
String.format("Unhandled numeric field type: %s", type));
}
return switch (clazz.getDeclaredField(field).getType().getSimpleName()) {
case "short" -> setField(field, value.shortValue());
case "int" -> setField(field, value.intValue());
case "byte" -> setField(field, value.byteValue());
default -> throw new StructBuilderException(
String.format("Unhandled numeric field type: %s", type));
};
} catch (NoSuchFieldException | SecurityException e) {
throw new StructBuilderException(
String.format("Unexpected error setting field: %s",
Expand Down Expand Up @@ -122,7 +120,7 @@ private SimpleStructBuilder<T> setField(final String fieldName, final Object val
FieldUtils.writeField(field, struct, value);
field.setAccessible(false);
} catch (NoSuchFieldException | SecurityException
| IllegalArgumentException | IllegalAccessException e) {
| IllegalArgumentException | IllegalAccessException e) {
throw new StructBuilderException(
String.format("Unexpected error setting field: %s",
fieldName, e.getMessage()), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final byte[] convert(final Struct struct) {

// using output stream resources, serialize the specified struct
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream)) {
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream)) {

// obtain the struct elements definition
StructElements structElements = struct.getClass().getAnnotation(StructElements.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public interface StructConverter {
*
* @param data to be parsed
* @param type type of data being parsed
* @param <T> the {@link Struct} type
* @param <T> the {@link Struct} type
* @return de-serialized struct
*/
<T extends Struct> T convert(final byte[] data, final Class<T> type);
<T extends Struct> T convert(byte[] data, Class<T> type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public @interface StructElementLength {

/**
* the field that this length represents.
* @return the field that this length represents.
*/
String fieldName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public @interface StructElements {

/**
* elements in order to be processed by a converter.
* @return elements in order to be processed by a converter.
*/
String[] elements();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* A container for an encoded {@link hirs.structs.elements.tpm.IdentityRequest},
* its associated endorsement credential, and its device information.
*/
@StructElements(elements = { "requestLength", "request",
"endorsementCredentialModulusLength", "endorsementCredentialModulus",
"endorsementCredentialLength", "endorsementCredential",
"deviceInfoReportLength", "deviceInfoReport" })
@StructElements(elements = {"requestLength", "request",
"endorsementCredentialModulusLength", "endorsementCredentialModulus",
"endorsementCredentialLength", "endorsementCredential",
"deviceInfoReportLength", "deviceInfoReport"})
public class IdentityRequestEnvelope implements Struct {

@StructElementLength(fieldName = "request")
Expand Down Expand Up @@ -72,23 +72,20 @@ public int getEndorsementCredentialLength() {
}

/**
*
* @return the endorsementCredential
*/
public byte[] getEndorsementCredential() {
return Arrays.copyOf(endorsementCredential, endorsementCredential.length);
}

/**
*
* @return the length of the device info report
*/
public int getDeviceInfoReportLength() {
return deviceInfoReportLength;
}

/**
*
* @return the device info report
*/
public byte[] getDeviceInfoReport() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/**
* Data structure used by the ACA to respond back to a client's {@link IdentityRequestEnvelope}.
*/
@StructElements(elements = { "asymmetricContentsSize", "asymmetricContents",
"symmetricAttestation" })
@StructElements(elements = {"asymmetricContentsSize", "asymmetricContents",
"symmetricAttestation"})
public class IdentityResponseEnvelope implements Struct {

@StructElementLength(fieldName = "asymmetricContents")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* the envelope contains the Identity Credential that is signed by the ACA. This along with the key
* parameters are typically sent to the TPM to activate an Identity.
*/
@StructElements(elements = { "credentialSize", "algorithm", "credential" })
@StructElements(elements = {"credentialSize", "algorithm", "credential"})
public class SymmetricAttestation implements Struct {

@StructElementLength(fieldName = "credential")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* As defined in TCPA 4.20, the key parameters data structure describes the parameters used to
* generate a key pair and to store the parts of a key.
*/
@StructElements(elements = { "algorithmId", "encryptionScheme", "signatureScheme", "paramsSize",
"params" })
@StructElements(elements = {"algorithmId", "encryptionScheme", "signatureScheme", "paramsSize",
"params"})
public class AsymmetricKeyParams implements Struct {

private int algorithmId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* portion of an asymmetric key pair. It contains all the information necessary for it's unambiguous
* usage.
*/
@StructElements(elements = { "asymmetricKeyParams", "storePubKey" })
@StructElements(elements = {"asymmetricKeyParams", "storePubKey"})
public class AsymmetricPublicKey implements Struct {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public enum EncryptionScheme {
this.encryptionScheme = encryptionScheme;
}

@Override
public String toString() {
return this.encryptionScheme;
}

/**
* Maps an {@link EncryptionScheme} based upon an integer. If the scheme is unmapped, the
* default, {@link #PKCS1} is returned.
Expand All @@ -48,11 +43,14 @@ public String toString() {
* @return the encryption scheme, or if unknown, the default.
*/
public static EncryptionScheme fromInt(final int scheme) {
switch (scheme) {
case OAEP_VALUE:
return OAEP;
default:
return PKCS1;
if (scheme == OAEP_VALUE) {
return OAEP;
}
return PKCS1;
}

@Override
public String toString() {
return this.encryptionScheme;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* identity process. This structure contains information that is required by the Attestation
* Certificate Authority to attest an identity request.
*/
@StructElements(elements = { "version", "labelSize", "identityBindingSize", "endorsementSize",
@StructElements(elements = {"version", "labelSize", "identityBindingSize", "endorsementSize",
"platformSize", "conformanceSize", "identityKey", "label", "identityBinding",
"endorsementCredential", "platformCredential", "conformanceCredential" })
"endorsementCredential", "platformCredential", "conformanceCredential"})
public class IdentityProof implements Struct {

private Version version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* As specified in TCPA 4.30.2 specification. This structure is sent to the Attestation Certificate
* Authority to create an Identity Credential.
*/
@StructElements(elements = { "asymmetricBlobSize", "symmetricBlobSize", "asymmetricAlgorithm",
"symmetricAlgorithm", "asymmetricBlob", "symmetricBlob" })
@StructElements(elements = {"asymmetricBlobSize", "symmetricBlobSize", "asymmetricAlgorithm",
"symmetricAlgorithm", "asymmetricBlob", "symmetricBlob"})
public class IdentityRequest implements Struct {

@StructElementLength(fieldName = "asymmetricBlob")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* portion of an asymmetric key pair. It contains all the information necessary for it's unambiguous
* usage.
*/
@StructElements(elements = { "asymmetricKeyParams", "storePubKey" })
@StructElements(elements = {"asymmetricKeyParams", "storePubKey"})
public class PublicKey implements Struct {

private AsymmetricKeyParams asymmetricKeyParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Parameters that are used to describe a particular {@link AsymmetricKeyParams} as specified by the
* TCPA 4.20.
*/
@StructElements(elements = { "keyLength", "totalPrimes", "exponentSize", "exponent" })
@StructElements(elements = {"keyLength", "totalPrimes", "exponentSize", "exponent"})
public class RsaSubParams implements Struct {

private int keyLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* As specified in TCPA Main Specification section 4.27.2. This structure represents a public key of
* an asymmetric key pair.
*/
@StructElements(elements = { "keyLength", "key" })
@StructElements(elements = {"keyLength", "key"})
public class StorePubKey implements Struct {

@StructElementLength(fieldName = "key")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Part of the TPM Identity Request. This Structure is encrypted inside the request and is typically
* unencrypted by an Attestation Certificate Authority.
*/
@StructElements(elements = { "algorithmId", "encryptionScheme", "keySize", "key" })
@StructElements(elements = {"algorithmId", "encryptionScheme", "keySize", "key"})
public class SymmetricKey implements Struct {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
/**
* Represents a symmetric key as specified in section 4.20 of the TCPA.
*/
@StructElements(elements = { "algorithmId", "encryptionScheme", "signatureScheme", "paramsSize",
"params" })
@StructElements(elements = {"algorithmId", "encryptionScheme", "signatureScheme", "paramsSize",
"params"})
public class SymmetricKeyParams implements Struct {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Represents a dynamic key parameters data structure that is enclosed inside a {@link
* SymmetricKeyParams}.
*/
@StructElements(elements = { "keyLength", "blockSize", "ivSize", "iv" })
@StructElements(elements = {"keyLength", "blockSize", "ivSize", "iv"})
public class SymmetricSubParams implements Struct {

private int keyLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* As specified in the TCPA Main Specification section 4.5. This structure represents the version of
* the TPM.
*/
@StructElements(elements = { "major", "minor", "revisionMajor", "revisionMinor" })
@StructElements(elements = {"major", "minor", "revisionMajor", "revisionMinor"})
public class Version implements Struct {

private byte major;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package hirs.structs.converters;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

/**
* Tests suite for {@link SimpleStructConverter}.
Expand All @@ -13,24 +14,25 @@ public class SimpleStructBuilderTest {

/**
* Tests {@link SimpleStructBuilder#build()}.
* @throws NoSuchFieldException sometimes
* @throws IllegalAccessException sometimes
*
* @throws NoSuchFieldException sometimes
* @throws IllegalAccessException sometimes
* @throws IllegalArgumentException sometimes
*/
@Test
public final void testBuild() throws NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
TestStruct struct = new SimpleStructBuilder<>(TestStruct.class)
.set("testShort", NUMBER)
.set("testByte", NUMBER)
.set("testEmbeddedStruct", new SimpleStructBuilder<>(TestEmbeddedStruct.class)
.set("embeddedShort", NUMBER)
.set("embedded", ARRAY)
.build())
.set("testVariableStruct", new SimpleStructBuilder<>(TestVariableStruct.class)
.set("testArray", ARRAY)
.build())
.build();
.set("testShort", NUMBER)
.set("testByte", NUMBER)
.set("testEmbeddedStruct", new SimpleStructBuilder<>(TestEmbeddedStruct.class)
.set("embeddedShort", NUMBER)
.set("embedded", ARRAY)
.build())
.set("testVariableStruct", new SimpleStructBuilder<>(TestVariableStruct.class)
.set("testArray", ARRAY)
.build())
.build();

assertEquals(NUMBER, struct.getTestShort());
assertEquals(NUMBER, struct.getTestByte());
Expand Down
Loading
Loading