title: Java TLS socket : No trusted certificate found
body: <p>Let me explain quickly what I'm trying to do. I'm trying to build my own Apple's Push Notification service in java (for testing purposes). This service works thanks to TLS socket.</p>
  
  <p>I have a java client to create a TLS socket to send push notifications to the APNs. I changed the host url to redirect the socket to localhost:2195. Now I'm trying to write a java socket server to get the notification request.</p>
  
  <p>However, I get an exception during the handshake and can't find how to fix it.</p>
  
  <p>Note : I'm using the same certificate on both sides, it's a standard .p12 file that works to send push notifications to the APNs.</p>
  
  <p>Here is the client (simplified) :</p>
  
  <pre><code>KeyStore ks = KeyStore.getInstance("PKCS12");
  ks.load(new FileInputStream(certificatePath), password.toCharArray());
  
  KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
  kmf.init(ks, password.toCharArray());
  
  TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509"); 
  tmf.init((KeyStore)null);
  
  SSLContext sc = SSLContext.getInstance("TLS"); 
  sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 
  
  SSLSocketFactory ssf = sc.getSocketFactory(); 
  SSLSocket socket = (SSLSocket) ssf.createSocket(InetAddress.getLocalHost(), 2195);
  socket.startHandshake();
  </code></pre>
  
  <p>Here is the server :</p>
  
  <pre><code>KeyStore ks = KeyStore.getInstance("PKCS12");
  ks.load(new FileInputStream(certificatePath), password.toCharArray());
  
  KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
  kmf.init(ks, password.toCharArray());
  
  SSLContext context = SSLContext.getInstance("TLS");
  context.init(kmf.getKeyManagers(), null, null);
  
  SSLServerSocketFactory ssf = context.getServerSocketFactory();
  serverSocket = (SSLServerSocket) ssf.createServerSocket(2195);
  </code></pre>
  
  <p>And here is the exception :</p>
  
  <pre><code>javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
  </code></pre>
  
  <p>I guess the client isn't trusting the server's certificate. I tryed to set the client's TrustManager to accept the server's p12 and it worked, however I need this to work without editing the client (since it's working that way with the real APNs).</p>
  
  <p>What kind of certificate needs the server to be trusted by the client ?</p>
  
  <p>Thanks in advance.</p>