UpdateSignKeyDialog.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2018 Intel Corporation. //
  3. // //
  4. // All rights reserved. This program and the accompanying materials //
  5. // are made available under the terms of the Eclipse Public License v1.0 //
  6. // which accompanies this distribution, and is available at //
  7. // http://www.eclipse.org/legal/epl-v10.html //
  8. // //
  9. // Contributors: //
  10. // Intel Corporation - initial implementation and documentation //
  11. ///////////////////////////////////////////////////////////////////////////
  12. package com.intel.sgx.dialogs;
  13. import java.io.File;
  14. import javax.swing.JOptionPane;
  15. import org.eclipse.jface.dialogs.InputDialog;
  16. import org.eclipse.swt.SWT;
  17. import org.eclipse.swt.events.SelectionEvent;
  18. import org.eclipse.swt.events.SelectionListener;
  19. import org.eclipse.swt.layout.GridData;
  20. import org.eclipse.swt.layout.GridLayout;
  21. import org.eclipse.swt.widgets.Button;
  22. import org.eclipse.swt.widgets.Composite;
  23. import org.eclipse.swt.widgets.Control;
  24. import org.eclipse.swt.widgets.FileDialog;
  25. import org.eclipse.swt.widgets.Group;
  26. import org.eclipse.swt.widgets.Label;
  27. import org.eclipse.swt.widgets.Shell;
  28. import org.eclipse.swt.widgets.Text;
  29. import com.intel.sgx.handlers.UpdateSigningKey;
  30. public class UpdateSignKeyDialog extends SGXDialogBase {
  31. private Shell shell;
  32. public Text sourceKeyFileField;
  33. public Text destinationKeyFileField;
  34. public static boolean regenerate = false;
  35. private final SelectionListener destinationKeyFileSelectionListener = new SelectionListener() {
  36. @Override
  37. public void widgetSelected(SelectionEvent event) {
  38. String result = destinationKeyFileField.getText();
  39. FileDialog dialog = new FileDialog(shell, SWT.OPEN);
  40. dialog.setFilterPath(getCurrentProjectPath().toOSString());
  41. dialog.setFilterExtensions(new String [] {"*.pem", "*"});
  42. result = dialog.open();
  43. destinationKeyFileField.setText(result);
  44. }
  45. @Override
  46. public void widgetDefaultSelected(SelectionEvent e) {
  47. }
  48. };
  49. private final SelectionListener sourceKeyFileSelectionListener = new SelectionListener() {
  50. @Override
  51. public void widgetSelected(SelectionEvent event) {
  52. String result = sourceKeyFileField.getText();
  53. FileDialog dialog = new FileDialog(shell, SWT.OPEN);
  54. dialog.setFilterExtensions(new String [] {"*.pem", "*"});
  55. dialog.setFilterPath(getCurrentProjectPath().toOSString());
  56. result = dialog.open();
  57. sourceKeyFileField.setText(result);
  58. }
  59. @Override
  60. public void widgetDefaultSelected(SelectionEvent e) {
  61. }
  62. };
  63. final private UpdateSigningKey handler;
  64. public UpdateSignKeyDialog(Shell parentShell, UpdateSigningKey handler) {
  65. super(parentShell);
  66. this.shell = parentShell;
  67. this.handler = handler;
  68. setShellStyle(SWT.RESIZE | SWT.TITLE);
  69. }
  70. @Override
  71. protected Control createDialogArea(Composite parent) {
  72. Composite composite = (Composite) super.createDialogArea(parent);
  73. final GridLayout gridLayout = new GridLayout(1,false);
  74. composite.setLayout(gridLayout);
  75. destinationKeyFileField = addGroup(composite, "Enclave Signing Key:",
  76. "Select the Signing Key to be Updated or Generated.",
  77. "Enclave Signing Key:", "Select", destinationKeyFileSelectionListener);
  78. sourceKeyFileField = addGroup(composite, "Import:",
  79. "To import your own Signing Key use the Import Signing Key option.",
  80. "Import Signing Key:", "Import Key", sourceKeyFileSelectionListener);
  81. addGroup2(composite);
  82. return composite;
  83. }
  84. protected void addGroup2(Composite composite) {
  85. final Group container2 = new Group(composite, SWT.None);
  86. container2.setLayout(new GridLayout(3,false));
  87. GridData innergrid2 = new GridData(GridData.FILL_HORIZONTAL);
  88. innergrid2.horizontalSpan = 3;
  89. container2.setLayoutData(innergrid2);
  90. container2.setText("Generate:");
  91. final Label messageLabel3 = new Label(container2, SWT.NONE);
  92. messageLabel3.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false, 3, 1));
  93. messageLabel3.setText("To Generate a new Signing Key use the Generate Signing Key option.");
  94. Label warningLabel2 = new Label(container2,SWT.FILL | SWT.WRAP);
  95. warningLabel2.setText("Generate a new Signing Key:");
  96. warningLabel2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
  97. Label dummy2 = new Label(container2,0);
  98. dummy2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
  99. final Button updateSigningKey = new Button(container2, SWT.PUSH);
  100. updateSigningKey.setText("Generate Key");
  101. GridData buttonGridData2 = new GridData(GridData.END);
  102. buttonGridData2.horizontalAlignment = SWT.RIGHT;
  103. buttonGridData2.horizontalSpan = 1;
  104. buttonGridData2.minimumWidth = 120;
  105. updateSigningKey.setLayoutData(buttonGridData2);
  106. updateSigningKey.addSelectionListener(new SelectionListener() {
  107. @Override
  108. public void widgetSelected(SelectionEvent event) {
  109. if( !destinationKeyFileField.getText().isEmpty())
  110. {
  111. regenerate = true;
  112. UpdateSignKeyDialog.this.setReturnCode(InputDialog.OK);
  113. okPressed();
  114. UpdateSignKeyDialog.this.close();
  115. }
  116. else
  117. JOptionPane.showMessageDialog(null, "Enclave Signing Key field is not provided.", "Error",
  118. JOptionPane.ERROR_MESSAGE);
  119. }
  120. @Override
  121. public void widgetDefaultSelected(SelectionEvent e) {
  122. }
  123. });
  124. }
  125. @Override
  126. protected void configureShell(Shell newShell) {
  127. super.configureShell(newShell);
  128. newShell.setText("Import or (Re)Generate Enclave Signing Key");
  129. }
  130. @Override
  131. protected void okPressed() {
  132. handler.sourceKeyFile = sourceKeyFileField.getText();
  133. handler.destinationKeyFile = destinationKeyFileField.getText();
  134. if((!sourceKeyFileField.getText().isEmpty() && !destinationKeyFileField.getText().isEmpty() &&
  135. (new File(sourceKeyFileField.getText())).isFile())
  136. || regenerate == true )
  137. {
  138. System.out.println("regenerate = " + regenerate);
  139. super.okPressed();
  140. }
  141. else
  142. {
  143. if(sourceKeyFileField.getText().isEmpty() && destinationKeyFileField.getText().isEmpty())
  144. JOptionPane.showMessageDialog(null, "Enclave Signing Key and Import Singing Key are not provided.", "Error",
  145. JOptionPane.ERROR_MESSAGE);
  146. else
  147. {
  148. if(sourceKeyFileField.getText().isEmpty())
  149. JOptionPane.showMessageDialog(null, "Import Singing Key is not provided.", "Error",
  150. JOptionPane.ERROR_MESSAGE);
  151. else
  152. if(!(new File(sourceKeyFileField.getText())).isFile())
  153. JOptionPane.showMessageDialog(null, "Invalid Import Singing Key.", "Error",
  154. JOptionPane.ERROR_MESSAGE);
  155. if(destinationKeyFileField.getText().isEmpty())
  156. JOptionPane.showMessageDialog(null, "Enclave Signing Key is not provided.", "Error",
  157. JOptionPane.ERROR_MESSAGE);
  158. }
  159. }
  160. }
  161. }