Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:15:45+00:00 2026-06-03T03:15:45+00:00

There is some code in our system for automatically generating self-signed certificates into a

  • 0

There is some code in our system for automatically generating self-signed certificates into a key store which is then used by Jetty. If a key for a given host already exists then nothing happens but if it doesn’t exist, we generate a new key, like this:

public void generateKey(String commonName) {
    X500Name x500Name = new X500Name("CN=" + commonName);
    CertAndKeyGen keyPair = new CertAndKeyGen("DSA", "SHA1withDSA");
    keyPair.generate(1024);
    PrivateKey privateKey = keyPair.getPrivateKey();
    X509Certificate certificate = keyPair.getSelfCertificate(x500Name, 20*365*24*60*60);
    Certificate[] chain = { certificate };
    keyStore.setEntry(commonName, privateKey, "secret".toCharArray(), chain);
}

This all works fine as long as there is only one key and certificate in the key store. Once you have multiple keys, weird things happen when you try to connect:

java.io.IOException: HTTPS hostname wrong:  should be <127.0.0.1>

This was quite a mystifying error but I finally managed to track it down by writing a unit test which connects to the server and asserts that the CN on the certificate matches the hostname. What I found was quite interesting – Jetty seems to arbitrarily choose which certificate to present to the client, but in a consistent fashion.

For instance:

  • If “CN=localhost” and “CN=cheese.mydomain” are in the key store, it always chose “CN=cheese.mydomain”.
  • If “CN=127.0.0.1” and “CN=cheese.mydomain” are in the key store, it always chose “CN=cheese.mydomain”.
  • If “CN=192.168.222.100” (cheese.mydomain) and “CN=cheese.mydomain” are in the key store, it always chose “CN=192.168.222.100”.

I wrote some code which loops through the certificates in the store to print them out and found that it isn’t consistently choosing the first certificate or anything trivial like that.

So exactly what criteria does it use? Initially I thought that localhost was special but then the third example baffled me completely.

I take it that this is somehow decided by the KeyManagerFactory, which is SunX509 in my case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:15:45+00:00Added an answer on June 3, 2026 at 3:15 am

    This is indeed ultimately decided by the KeyManager (generally obtained from a KeyManagerFactory).

    A keystore can have a number of certificates stored under different aliases. If no alias is explicitly configured via certAlias in the Jetty configuration, the SunX509 implementation will pick the first aliases it finds for which there is a private key and a key of the right type for the chosen cipher suite (typically RSA, but probably DSA in your case here). There’s a bit more to it to the choice logic, if you look at the Sun provider implementation, but you shouldn’t really rely on the order in general, just the alias name.

    You can of course give Jetty your own SSLContext with your own X509KeyManager to choose the alias. You would have to implement:

     chooseServerAlias(String keyType, Principal[] issuers, Socket socket)
    

    Unfortunately, apart from keyType and issuers, all you get to make the decision is the socket itself. At best, the useful information you get there are the local IP address and the remote one.

    Unless your server is listening to multiple IP addresses on the same port, you will always get the same local IP address. (Here, obviously, you have at least two: 127.0.0.1 and 192.168.222.100, but I suspect you’re not really interested in localhost except for your own tests.) You would need Server Name Indication (SNI) support on the server side to be able to make a decision based on the requested host names (by clients that support it). Unfortunately, SNI was only introduced in Java 7, but only on the client side.

    Another problem you will face here is that Java clients will complain about IP addresses in the Subject DN’s CN. Some browsers would tolerate this, but this is not compliant with the HTTPS specification (RFC 2818). IP addresses must be Subject Alternative Name entries of IP-address type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using DbSimple, but there is some code which i could write into another
We have just purchased some software that provides an API into our phone system
in our system we store orders. We always have customer specific code in our
I'm using .net. Is there some code somewhere that can change $11,456.50 -> eleven
There is some code that I'm trying to convert from IList to IEnumerable :
there are some code as this: <div class=test> <div class=upsell-tags> <?php echo $this->getChildHtml('product_additional_data') ?>
There is some code that I am using written by someone else that I
is there some ruby code I can use to install a gem from a
In $var there is some code. I'm trying to operate a nl2br() on text
I am reading WIX script written by others. There are some code really confuses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.