/// <summary> /// Click handler for the 'ResetPin' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ResetPin_Click(object sender, RoutedEventArgs e) { if (!rootPage.ValidateTPMSmartCard()) { rootPage.NotifyUser("Use Scenario One to create a TPM virtual smart card.", NotifyType.ErrorMessage); return; } Button b = sender as Button; b.IsEnabled = false; try { SmartCard card = await rootPage.GetSmartCard(); SmartCardProvisioning provisioning = await SmartCardProvisioning.FromSmartCardAsync(card); rootPage.NotifyUser("Resetting smart card PIN...", NotifyType.StatusMessage); // When requesting a PIN reset, a SmartCardPinResetHandler must be // provided as an argument. This handler must use the challenge // it receives and the card's admin key to calculate and set the // response. bool result = await provisioning.RequestPinResetAsync( (pinResetSender, request) => { SmartCardPinResetDeferral deferral = request.GetDeferral(); try { IBuffer response = ChallengeResponseAlgorithm.CalculateResponse(request.Challenge, rootPage.AdminKey); request.SetResponse(response); } finally { deferral.Complete(); } }); if (result) { rootPage.NotifyUser("Smart card PIN reset operation completed.", NotifyType.StatusMessage); } else { rootPage.NotifyUser("Smart card PIN reset operation was canceled by the user.", NotifyType.StatusMessage); } } catch (Exception ex) { rootPage.NotifyUser("Resetting smart card PIN failed with exception: " + ex.ToString(), NotifyType.ErrorMessage); } finally { b.IsEnabled = true; } }
/// <summary> /// Click handler for the 'Delete' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void Delete_Click(object sender, RoutedEventArgs e) { if (!rootPage.ValidateTPMSmartCard()) { rootPage.NotifyUser("Use Scenario One to create a TPM virtual smart card.", NotifyType.ErrorMessage); return; } Button b = sender as Button; b.IsEnabled = false; rootPage.NotifyUser("Deleting the TPM virtual smart card...", NotifyType.ErrorMessage); try { SmartCard card = await rootPage.GetSmartCard(); // The following two lines are not directly related to TPM virtual // smart card creation, but are used to demonstrate how to handle // CardRemoved events by registering an event handler with a // SmartCardReader object. Since we are using a TPM virtual smart // card in this case, the card cannot actually be added to or // removed from the reader, but a CardRemoved event will fire // when the reader and card are deleted. // // We must retain a reference to the SmartCardReader object to // which we are adding the event handler. We use += to add the // HandleCardRemoved method as an event handler. The function // will be automatically boxed in a TypedEventHandler, but // the function signature match the template arguments for // the specific event - in this case, // <SmartCardReader, CardRemovedEventArgs> reader = card.Reader; reader.CardRemoved += HandleCardRemoved; bool result = await SmartCardProvisioning.RequestVirtualSmartCardDeletionAsync(card); if (result) { rootPage.NotifyUser("TPM virtual smart card deletion completed.", NotifyType.StatusMessage); rootPage.SmartCardReaderDeviceId = null; } else { rootPage.NotifyUser("TPM virtual smart card deletion was canceled by the user.", NotifyType.StatusMessage); } } catch (Exception ex) { rootPage.NotifyUser("TPM virtual smartcard deletion failed with exception: " + ex.ToString(), NotifyType.ErrorMessage); } finally { b.IsEnabled = true; } }
/// <summary> /// Click handler for the 'ChangeAdminKey' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ChangeAdminKey_Click(object sender, RoutedEventArgs e) { if (!rootPage.ValidateTPMSmartCard()) { rootPage.NotifyUser("Use Scenario One to create a TPM virtual smart card.", NotifyType.ErrorMessage); return; } Button b = sender as Button; b.IsEnabled = false; try { IBuffer newadminkey = CryptographicBuffer.GenerateRandom(MainPage.ADMIN_KEY_LENGTH_IN_BYTES); SmartCard card = await rootPage.GetSmartCard(); SmartCardProvisioning provisioning = await SmartCardProvisioning.FromSmartCardAsync(card); rootPage.NotifyUser("Changing smart card admin key...", NotifyType.StatusMessage); using (SmartCardChallengeContext context = await provisioning.GetChallengeContextAsync()) { IBuffer response = ChallengeResponseAlgorithm.CalculateResponse(context.Challenge, rootPage.AdminKey); await context.ChangeAdministrativeKeyAsync(response, newadminkey); rootPage.AdminKey = newadminkey; } rootPage.NotifyUser("Smart card change admin key operation completed.", NotifyType.StatusMessage); } catch (Exception ex) { rootPage.NotifyUser("Changing smart card admin key operation failed with exception: " + ex.ToString(), NotifyType.ErrorMessage); } finally { b.IsEnabled = true; } }
/// <summary> /// Click handler for the 'TransmitAPDU' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void Transmit_Click(object sender, RoutedEventArgs e) { if (!rootPage.ValidateTPMSmartCard()) { rootPage.NotifyUser("Use Scenario One to create a TPM virtual smart card.", NotifyType.ErrorMessage); return; } Button b = sender as Button; b.IsEnabled = false; try { SmartCard card = await rootPage.GetSmartCard(); IBuffer result = null; using (SmartCardConnection connection = await card.ConnectAsync()) { // Read EF.ATR file // The command is meant specifically for GIDS cards // (such as TPM VSCs), and will fail on other types. byte[] readEfAtrBytes = { 0x00, 0xCB, 0x2F, 0x01, 0x02, 0x5C, 0x00, 0xFF }; IBuffer readEfAtr = CryptographicBuffer.CreateFromByteArray(readEfAtrBytes); result = await connection.TransmitAsync(readEfAtr); rootPage.NotifyUser("Response: " + CryptographicBuffer.EncodeToHexString(result), NotifyType.StatusMessage); } } catch (Exception ex) { rootPage.NotifyUser("Transmitting APDU to card failed with exception: " + ex.ToString(), NotifyType.ErrorMessage); } finally { b.IsEnabled = true; } }
/// <summary> /// Click handler for the 'ChangePin' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ChangePin_Click(object sender, RoutedEventArgs e) { if (!rootPage.ValidateTPMSmartCard()) { rootPage.NotifyUser("Use Scenario One to create a TPM virtual smart card.", NotifyType.ErrorMessage); return; } Button b = sender as Button; b.IsEnabled = false; try { SmartCard card = await rootPage.GetSmartCard(); SmartCardProvisioning provisioning = await SmartCardProvisioning.FromSmartCardAsync(card); rootPage.NotifyUser("Changing smart card PIN...", NotifyType.StatusMessage); bool result = await provisioning.RequestPinChangeAsync(); if (result) { rootPage.NotifyUser("Smart card change PIN operation completed.", NotifyType.StatusMessage); } else { rootPage.NotifyUser("Smart card change PIN operation was canceled by the user.", NotifyType.StatusMessage); } } catch (Exception ex) { rootPage.NotifyUser("Changing smart card PIN failed with exception: " + ex.ToString(), NotifyType.ErrorMessage); } finally { b.IsEnabled = true; } }
/// <summary> /// Click handler for the 'VerifyResponse' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void VerifyResponse_Click(object sender, RoutedEventArgs e) { if (!rootPage.ValidateTPMSmartCard()) { rootPage.NotifyUser("Use Scenario One to create a TPM virtual smart card.", NotifyType.ErrorMessage); return; } Button b = sender as Button; b.IsEnabled = false; try { bool verifyResult = false; SmartCard card = await rootPage.GetSmartCard(); SmartCardProvisioning provisioning = await SmartCardProvisioning.FromSmartCardAsync(card); rootPage.NotifyUser("Verifying smart card response...", NotifyType.StatusMessage); using (SmartCardChallengeContext context = await provisioning.GetChallengeContextAsync()) { IBuffer response = ChallengeResponseAlgorithm.CalculateResponse(context.Challenge, rootPage.AdminKey); verifyResult = await context.VerifyResponseAsync(response); } rootPage.NotifyUser("Smart card response verification completed. Result: " + verifyResult.ToString(), NotifyType.StatusMessage); } catch (Exception ex) { rootPage.NotifyUser("Verifying smart card response operation failed with exception: " + ex.ToString(), NotifyType.ErrorMessage); } finally { b.IsEnabled = true; } }