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

Implements TPM vendor check function #113

Closed
wants to merge 3 commits into from
Closed

Implements TPM vendor check function #113

wants to merge 3 commits into from

Conversation

lukehinds
Copy link
Member

This change implements a means to check the vendor of the TPM.

The main use for this function is to establish if we are using
a software based TPM and in turn warn that no hardware root of
trust is available

This change implements a means to check the vendor of the TPM.

The main use for this function is to establish if we are using
a software based TPM and in turn warn that no hardware root of
trust is available
@lukehinds
Copy link
Member Author

Still learning the idioms of rust, if you feel anything can be improved / optimised let me know of follow up with a commit

image

@lukehinds lukehinds mentioned this pull request Oct 6, 2020
src/tpm.rs Outdated
use tss_esapi::tss2_esys::{ESYS_TR, ESYS_TR_NONE, ESYS_TR_RH_OWNER};
use tss_esapi::Context;
use tss_esapi::Tcti;

fn int_to_str(num: u32) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name

This is really moving to a String rather than str

Suggested change
fn int_to_str(num: u32) -> String {
fn int_to_string(num: u32) -> String {

Possibly simpler implementation

fn int_to_string(num: u32) -> String {
    num.to_string()
}

Or directly using to_string() 😄

Copy link
Member

@puiterwijk puiterwijk Oct 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's not really a standard integer-to-string operation. It's basically parsing every byte in a u32 (i.e. up to 4 bytes) as a different ascii character. Because that's how TPM does strings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Renaming to int_to_string is reasonable though)

src/tpm.rs Show resolved Hide resolved
}
}

let mut vend_strs: String = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: My brain turns to mush when I see many map/filter/etc.. statements on something. Consider adding a comment explaining what's going on here for folks like me 😆.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry. Let me explain what's going on, so Luke can add comments.

This basically first uses a set of constants (TPM2_PT_VENDOR_STRING_{1,2,3,4}) as lookup indexes into allprops, to get all the values from there where they're set. After that, it removes the values that don't exist (is_some), removes the Option<_> wrappers (unwrap()), removes the items that are actually a value (i.e. != && 0), then puts the individual parts to a different through int_to_str to get a Vec<String> with the parts, and then makes a single string from them (.join("")).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comments. in time we can move this into a helper function (if they all need the some manipulation)

src/tpm.rs Outdated
* software TPM as opposed to hardware TPM.
*/
pub fn get_tpm_vendor() -> Result<String, tss_esapi::Error> {
let mut ctx = get_tpm2_ctx()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to get the context in main() and pass that one through to here as &mut Context.

Copy link
Contributor

@ashcrow ashcrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than the rust format warning in CI

@lukehinds
Copy link
Member Author

After speaking with @puiterwijk , I will close this and he will follow up with a patch implementing the same, but using his new error code contained within PR #121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants