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

fix/tpm-pub-area-parsing #239

Merged
merged 6 commits into from
Aug 5, 2022
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
36 changes: 36 additions & 0 deletions packages/server/src/registration/verifications/tpm/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/**
* A whole lotta domain knowledge is captured here, with hazy connections to source
* documents. Good places to start searching for more info on these values are the
* following Trusted Computing Group TPM Library docs linked in the WebAuthn API:
*
* - https://www.trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-1-Architecture-01.38.pdf
* - https://www.trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-01.38.pdf
* - https://www.trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-3-Commands-01.38.pdf
*/

/**
* 6.9 TPM_ST (Structure Tags)
*/
export const TPM_ST: { [key: number]: string } = {
0x00c4: 'TPM_ST_RSP_COMMAND',
0x8000: 'TPM_ST_NULL',
Expand All @@ -19,6 +32,9 @@ export const TPM_ST: { [key: number]: string } = {
0x8029: 'TPM_ST_FU_MANIFEST',
};

/**
* 6.3 TPM_ALG_ID
*/
export const TPM_ALG: { [key: number]: string } = {
0x0000: 'TPM_ALG_ERROR',
0x0001: 'TPM_ALG_RSA',
Expand Down Expand Up @@ -59,6 +75,9 @@ export const TPM_ALG: { [key: number]: string } = {
0x0044: 'TPM_ALG_ECB',
};

/**
* 6.4 TPM_ECC_CURVE
*/
export const TPM_ECC_CURVE: { [key: number]: string } = {
0x0000: 'TPM_ECC_NONE',
0x0001: 'TPM_ECC_NIST_P192',
Expand All @@ -76,6 +95,12 @@ type ManufacturerInfo = {
id: string;
};

/**
* Sourced from https://trustedcomputinggroup.org/resource/vendor-id-registry/
*
* Latest version:
* https://trustedcomputinggroup.org/wp-content/uploads/TCG-TPM-Vendor-ID-Registry-Version-1.02-Revision-1.00.pdf
*/
export const TPM_MANUFACTURERS: { [key: string]: ManufacturerInfo } = {
'id:414D4400': {
name: 'AMD',
Expand Down Expand Up @@ -154,3 +179,14 @@ export const TPM_MANUFACTURERS: { [key: string]: ManufacturerInfo } = {
id: 'FIDO',
},
};

/**
* Match TPM public area curve ID's to `crv` numbers used in COSE public keys
*/
export const TPM_ECC_CURVE_COSE_CRV_MAP: { [key: string]: number } = {
TPM_ECC_NIST_P256: 1, // p256
TPM_ECC_NIST_P384: 2, // p384
TPM_ECC_NIST_P521: 3, // p521
TPM_ECC_BN_P256: 1, // p256
TPM_ECC_SM2_P256: 1, // p256
};
30 changes: 26 additions & 4 deletions packages/server/src/registration/verifications/tpm/parsePubArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { TPM_ALG, TPM_ECC_CURVE } from './constants';

/**
* Break apart a TPM attestation's pubArea buffer
*
* See 12.2.4 TPMT_PUBLIC here:
* https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-00.96-130315.pdf
*/
export function parsePubArea(pubArea: Buffer): ParsedPubArea {
let pointer = 0;
Expand Down Expand Up @@ -34,6 +37,8 @@ export function parsePubArea(pubArea: Buffer): ParsedPubArea {

// Extract additional curve params according to type
const parameters: { rsa?: RSAParameters; ecc?: ECCParameters } = {};
let unique = Buffer.from([]);

if (type === 'TPM_ALG_RSA') {
const rsaBuffer = pubArea.slice(pointer, (pointer += 10));

Expand All @@ -43,6 +48,14 @@ export function parsePubArea(pubArea: Buffer): ParsedPubArea {
keyBits: rsaBuffer.slice(4, 6).readUInt16BE(0),
exponent: rsaBuffer.slice(6, 10).readUInt32BE(0),
};

/**
* See 11.2.4.5 TPM2B_PUBLIC_KEY_RSA here:
* https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-00.96-130315.pdf
*/
const uniqueLength = pubArea.slice(pointer, (pointer += 2)).readUInt16BE(0);

unique = pubArea.slice(pointer, (pointer += uniqueLength));
} else if (type === 'TPM_ALG_ECC') {
const eccBuffer = pubArea.slice(pointer, (pointer += 8));

Expand All @@ -52,14 +65,23 @@ export function parsePubArea(pubArea: Buffer): ParsedPubArea {
curveID: TPM_ECC_CURVE[eccBuffer.slice(4, 6).readUInt16BE(0)],
kdf: TPM_ALG[eccBuffer.slice(6, 8).readUInt16BE(0)],
};

/**
* See 11.2.5.1 TPM2B_ECC_PARAMETER here:
* https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-00.96-130315.pdf
*/
// Retrieve X
const uniqueXLength = pubArea.slice(pointer, (pointer += 2)).readUInt16BE(0);
const uniqueX = pubArea.slice(pointer, (pointer += uniqueXLength));
// Retrieve Y
const uniqueYLength = pubArea.slice(pointer, (pointer += 2)).readUInt16BE(0);
const uniqueY = pubArea.slice(pointer, (pointer += uniqueYLength));

unique = Buffer.concat([uniqueX, uniqueY]);
} else {
throw new Error(`Unexpected type "${type}" (TPM)`);
}

// Slice out unique of dynamic length
const uniqueLength = pubArea.slice(pointer, (pointer += 2)).readUInt16BE(0);
const unique = pubArea.slice(pointer, (pointer += uniqueLength));

return {
type,
nameAlg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,25 @@ test('should verify TPM response with non-spec-compliant tcgAtTpm SAN structure'

expect(verification.verified).toEqual(true);
});

test('should verify TPM response with ECC public area type', async () => {
const expectedChallenge = 'uzn9u0Tx-LBdtGgERsbkHRBjiUt5i2rvm2BBTZrWqEo';
jest.spyOn(base64url, 'encode').mockReturnValueOnce(expectedChallenge);
const verification = await verifyRegistrationResponse({
credential: {
'id': 'hsS2ywFz_LWf9-lC35vC9uJTVD3ZCVdweZvESUbjXnQ',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CVdweZvESUbjXnSlAQIDJiABIVggHpO4NgvE3jkUaCsbjex6yeTop1Rrh8xIGDg8lLBfQ9ciWCCBc89ef4-b_ggXRzhdVomOYc7cQHYoHB2D8vrQ3Qgv3A',
'clientDataJSON': 'eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoidXpuOXUwVHgtTEJkdEdnRVJzYmtIUkJqaVV0NWkycnZtMkJCVFpyV3FFbyIsIm9yaWdpbiI6Imh0dHBzOi8vd2ViYXV0aG4uaW8iLCJjcm9zc09yaWdpbiI6ZmFsc2V9'
},
'clientExtensionResults': {},
},
expectedChallenge,
expectedOrigin: 'https://webauthn.io',
expectedRPID: 'webauthn.io',
});

expect(verification.verified).toEqual(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { verifySignature } from '../../../helpers/verifySignature';
import { MetadataService } from '../../../services/metadataService';
import { verifyAttestationWithMetadata } from '../../../metadata/verifyAttestationWithMetadata';

import { TPM_ECC_CURVE, TPM_MANUFACTURERS } from './constants';
import { TPM_MANUFACTURERS, TPM_ECC_CURVE_COSE_CRV_MAP } from './constants';
import { parseCertInfo } from './parseCertInfo';
import { parsePubArea } from './parsePubArea';

Expand Down Expand Up @@ -93,10 +93,6 @@ export async function verifyAttestationTPM(options: AttestationFormatVerifierOpt
throw new Error(`Unexpected public key exp ${eSum}, expected ${pubAreaExponent} (TPM|RSA)`);
}
} else if (pubType === 'TPM_ALG_ECC') {
/**
* TODO: Confirm this all works fine. Conformance tools v1.3.4 don't currently test ECC so I
* had to eyeball it based on the **duo-labs/webauthn** library
*/
const crv = cosePublicKey.get(COSEKEYS.crv);
const x = cosePublicKey.get(COSEKEYS.x);
const y = cosePublicKey.get(COSEKEYS.y);
Expand All @@ -120,10 +116,10 @@ export async function verifyAttestationTPM(options: AttestationFormatVerifierOpt
}

const pubAreaCurveID = parameters.ecc.curveID;
const pubKeyCurveID = TPM_ECC_CURVE[(crv as Buffer).readUInt16BE(0)];
if (pubAreaCurveID !== pubKeyCurveID) {
const pubAreaCurveIDMapToCOSECRV = TPM_ECC_CURVE_COSE_CRV_MAP[pubAreaCurveID]
if (pubAreaCurveIDMapToCOSECRV !== crv) {
throw new Error(
`Unexpected public key curve ID "${pubKeyCurveID}", expected "${pubAreaCurveID}" (TPM|ECC)`,
`Public area key curve ID "${pubAreaCurveID}" mapped to "${pubAreaCurveIDMapToCOSECRV}" which did not match public key crv of "${crv}" (TPM|ECC)`,
);
}
} else {
Expand Down