PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/pki-core-9.0.21/base/silent/src/common/Con2Agent.java

#
Java | 329 lines | 193 code | 79 blank | 57 comment | 12 complexity | e99a76ec5ca7f941e86b6116115c828a MD5 | raw file
Possible License(s): GPL-2.0
  1. // --- BEGIN COPYRIGHT BLOCK ---
  2. // This program is free software; you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation; version 2 of the License.
  5. //
  6. // This program is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. // GNU General Public License for more details.
  10. //
  11. // You should have received a copy of the GNU General Public License along
  12. // with this program; if not, write to the Free Software Foundation, Inc.,
  13. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. //
  15. // (C) 2007 Red Hat, Inc.
  16. // All rights reserved.
  17. // --- END COPYRIGHT BLOCK ---
  18. import java.net.*;
  19. import java.io.*;
  20. import java.util.*;
  21. import java.math.*;
  22. import org.mozilla.jss.*;
  23. import org.mozilla.jss.CryptoManager;
  24. import org.mozilla.jss.util.*;
  25. import org.mozilla.jss.ssl.*;
  26. import org.mozilla.jss.crypto.*;
  27. import org.mozilla.jss.CertDatabaseException;
  28. import org.mozilla.jss.pkcs11.*;
  29. import org.mozilla.jss.pkcs11.PK11Token;
  30. import sun.misc.*;
  31. import java.lang.Exception;
  32. import java.security.*;
  33. import java.net.URLEncoder;
  34. /**
  35. * CMS Test framework .
  36. * Submits a requests to agent port with sslclient authentication.
  37. */
  38. public class Con2Agent implements SSLClientCertificateSelectionCallback,
  39. SSLCertificateApprovalCallback {
  40. private int i, port;
  41. private String host, certdir, certnickname, tokenpwd, certname, query;
  42. private String ACTIONURL;
  43. private BufferedReader stdin = null;
  44. private StringBuffer stdout = new StringBuffer();
  45. public Con2Agent() {}
  46. /**
  47. *Constructor. Takes hostname , portnumber , certificate nickname, token password ,client certdb directory
  48. * @param hostname
  49. * @param portnumber
  50. * @param agent cert nickname
  51. * @param token password
  52. * @param certdb directory
  53. */
  54. public Con2Agent(String hs, int p, String cname, String tpwd, String cdir) {
  55. host = hs;
  56. port = p;
  57. certnickname = cname;
  58. tokenpwd = tpwd;
  59. certdir = cdir;
  60. }
  61. public boolean approve(X509Certificate x509, SSLCertificateApprovalCallback.ValidityStatus status) {
  62. return true;
  63. }
  64. public String select(Vector nicknames) {
  65. Enumeration e = nicknames.elements();
  66. System.out.println("nicknames size = " + nicknames.size());
  67. int i = 0;
  68. while (e.hasMoreElements()) {
  69. String s = (String) e.nextElement();
  70. i++;
  71. }
  72. if (i > 0) {
  73. return (String) nicknames.elementAt(0);
  74. } else {
  75. return null;
  76. }
  77. }
  78. // Get and Set methods
  79. /*
  80. * Get the page returned by the server
  81. */
  82. public StringBuffer getPage() {
  83. return stdout;
  84. }
  85. /*
  86. * Set the query string to be submitted to the server
  87. */
  88. public void setQueryString(String qu) {
  89. query = qu;
  90. }
  91. /*
  92. *Set token password
  93. */
  94. public void setTokenPassword(String pwd) {
  95. tokenpwd = pwd;
  96. }
  97. /*
  98. * Set Client cert database
  99. */
  100. public void setCertDBDir(String cdir) {
  101. certdir = cdir;
  102. }
  103. /*
  104. * Set host name
  105. */
  106. public void setHost(String hs) {
  107. host = hs;
  108. }
  109. /*
  110. * set Agent port number
  111. */
  112. public void setPort(int p) {
  113. port = p;
  114. }
  115. /*
  116. * Set Agent cert nickname
  117. */
  118. public void setCertNickName(String cname) {
  119. certnickname = cname;
  120. }
  121. /*
  122. * Set action URL
  123. */
  124. public void setActionURL(String url) {
  125. ACTIONURL = url;
  126. }
  127. // Submit requests
  128. public boolean Send() {
  129. boolean st = false;
  130. try {
  131. if (!loginCertDB()) {
  132. return false;
  133. }
  134. SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
  135. System.out.println("Con2Agent.java: host = " + host);
  136. System.out.println("Con2Agent.java: port = " + port);
  137. System.out.println("Con2Agent.java: certnickname = " + certnickname);
  138. socket.setClientCertNickname(certnickname);
  139. System.out.println("Connected to the socket");
  140. OutputStream rawos = socket.getOutputStream();
  141. BufferedOutputStream os = new BufferedOutputStream(rawos);
  142. PrintStream ps = new PrintStream(os);
  143. System.out.println(ACTIONURL);
  144. System.out.println("Query :" + query);
  145. ps.println("POST " + ACTIONURL + " HTTP/1.0");
  146. ps.println("Connection: Keep-Alive");
  147. ps.println("Content-type: application/x-www-form-urlencoded");
  148. ps.println("Content-length: " + query.length());
  149. ps.println("");
  150. ps.println(query);
  151. ps.println("\r");
  152. ps.flush();
  153. os.flush();
  154. BufferedReader stdin1 = new BufferedReader(
  155. new InputStreamReader(socket.getInputStream()));
  156. String line;
  157. while ((line = stdin1.readLine()) != null) {
  158. stdout.append(line + "\n");
  159. System.out.println(line);
  160. }
  161. // Send Connection: close to let the server close the connection.
  162. // Else the socket on the server side continues to remain in TIME_WAIT state
  163. ps.println("Connection: close");
  164. ps.flush();
  165. os.flush();
  166. os.close();
  167. rawos.close();
  168. ps.close();
  169. stdin1.close();
  170. socket.close();
  171. if (socket.isClosed()) {
  172. System.out.println("Con2Agent.java : Socket is Closed");
  173. } else {
  174. System.out.println("Con2Agent.java : Socket not Closed");
  175. }
  176. } catch (Exception e) {
  177. System.out.println("some exception: in Send routine" + e);
  178. return false;
  179. }
  180. return true;
  181. }
  182. private boolean loginCertDB() {
  183. CryptoManager manager;
  184. Password pass1 = null, pass2 = null;
  185. try {
  186. System.out.println("Step 1: Initializing CryptoManager");
  187. CryptoManager.initialize(certdir);
  188. System.out.println("Step 2: Login to Cert Database");
  189. manager = CryptoManager.getInstance();
  190. CryptoToken token = (PK11Token) manager.getInternalKeyStorageToken();
  191. if (token.isLoggedIn()) {
  192. System.out.println("Con2Agent: Logged in incorrect");
  193. }
  194. System.out.println("tokenpwd:" + tokenpwd);
  195. char[] passchar1 = new char[tokenpwd.length()];
  196. tokenpwd.getChars(0, tokenpwd.length(), passchar1, 0);
  197. pass1 = new Password((char[]) passchar1.clone());
  198. token.login(pass1);
  199. X509Certificate cert2 = manager.findCertByNickname(certnickname);
  200. certname = cert2.getNickname();
  201. return true;
  202. } catch (AlreadyInitializedException e) {
  203. System.out.println("Crypto manager already initialized");
  204. return true;
  205. } catch (NumberFormatException e) {
  206. System.err.println("Invalid key size: " + e);
  207. return false;
  208. } catch (java.security.InvalidParameterException e) {
  209. System.err.println("Invalid key size: " + e);
  210. return false;
  211. } catch (Exception e) {
  212. System.err.println("some exception:" + e);
  213. e.printStackTrace();
  214. return false;
  215. }
  216. }
  217. public boolean Send_withGET() {
  218. boolean st = false;
  219. try {
  220. if (!loginCertDB()) {
  221. return false;
  222. }
  223. SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
  224. socket.setClientCertNickname(certnickname);
  225. System.out.println("Connected to the socket");
  226. OutputStream rawos = socket.getOutputStream();
  227. BufferedOutputStream os = new BufferedOutputStream(rawos);
  228. PrintStream ps = new PrintStream(os);
  229. System.out.println("Query in con2agent :" + query);
  230. System.out.println("ACTIONURL in con2agent : " + ACTIONURL);
  231. ps.println("GET " + ACTIONURL + query + " HTTP/1.0");
  232. ps.println("");
  233. ps.println("\r");
  234. ps.flush();
  235. os.flush();
  236. BufferedReader stdin2 = new BufferedReader(
  237. new InputStreamReader(socket.getInputStream()));
  238. String line;
  239. while ((line = stdin2.readLine()) != null) {
  240. stdout.append(line + "\n");
  241. }
  242. stdin2.close();
  243. socket.close();
  244. } catch (Exception e) {
  245. System.err.println("some exception: in Send routine" + e);
  246. return false;
  247. }
  248. return true;
  249. }
  250. } // end of class