Monday, June 10, 2013

Core java security interview questions and answers for java developer

Here are some of the core java interview questions related to java security that you could expect in an interview for a Java developer.

Q What is the difference between a keystore and a truststore?
      The truststore contains the trusted entries and root certificates. The keystore contains the private keys. They both have the same file name extension (.jks).

What does JKS stand for?
     Java key store

What are the other standards for managing keystores that you know of?
     Java understands only JKS. Other systems include PKCS12 and others typically well known in non-java world.

Do I always need a keystore?
     No; you need it only if you intend to authenticate yourself against the server.

What are the contents of a jks file?
    A jks file typically contains private/public key pairs, public keys/certificates, root certificate authority entries.

Is there a default truststore?
    Each java installation will have a /lib/security/cacerts which contains certificates of all the popular root certificate authorities.

Is this same as the certificate repository used by browsers?
   No; each browser has their own certificate repository which contains the popular root certificate authorities whom the browser trust.

Can I use same jks file as both truststore and keystore?
Yes; you can. In this case, the jks file contains private keys and trusted certificates/ca entries.

How do you configure truststore in your web application?
   The default truststore/keystore could set by setting VM parameter (-D option) or invoke System.setProperty() for the following properties:-

What is 2 way SSL ?
    In one way SSL  (aka typical https connection), the server authenticates itself to the client using server certificate. In 2 way SSL , in addition to the above step, the client authenticates itself to the server.

How do you configure it?
   This involves additional configuration of setting the keystore in addition to setting the truststore.

What is a SSLContext?
    SSLContext is java class that acts as factory for secure socket factories. To configure a custom keystore/truststore, you get an instance of it and intialize it with keystore and truststore.

How do you configure a specific truststore for a https connection?
    If you need to configure a specific truststore for a particular https connection (be it a web service or https get/post url), you could define a SSLContext with a keystore and truststore. The new SSLContext object will be set in the https connection. 

What are the errors that you have encountered during your development?
    The common errors are
       1. certificate_unknown
       2. the trustAnchors parameter must be non-empty
       3. unable to find valid certification path to requested target

When do you get a certificate unknown error?
    If the server side certificate is not present in your truststore, this error will occur. One of the most common reasons for this would be a custom truststore that does not contain the certificates or the server presents itself with self-signed certificate or a certificate signed by non-public certificate issuing authority. Most organizations will have their own root ca's to issue certificates for testing environments and internal use.

How do you solve the certificate unknown error?
    Get the certificate presented by the server; import it to your truststore. There are 2 ways to do this. The simplest approach is to give the URL in the browser, view the certificate, export it and import the same to your truststore. The second approach would be to write a sample piece of code that connects to https url and write the certificate contents into file; This could be imported to your truststore.

How do you debug a SSL handshake issue?
    To debug the issues during a ssl handshake, we need to set the VM parameter This will spit out the debug information in your log file.


  1. These Java interview questions are really informative and new to me. I hope these will definetely helps me to get shortlisted on the interview.
    Big Data Training in Chennai | Hadoop Training in Chennai

  2. Hi Bru,

    Gasping at your brilliance! Thanks a tonne for sharing all that content. Can’t stop reading. Honestly!

    . I'm trying to manually decrypt SSL traffic using openjdk libraries and server's private key.

    I succeed partially as the whole message is decrypted ok, except the first 16 bytes of the decrypted communication always end up as garbage.

    openjdk ServerSocket implementation works fine, as it decodes all the data without problems, so it must be an error in my manual implementation.

    Any insight is much appreciated!

    Manual implementation.

    I look forward to see your next updates.

    Thanks & Regards,