Setting up keystore programmatically before getting default SSL context









up vote
0
down vote

favorite












I am in the impression that once I set system properties when I get SSLContext.getDefault() should return me SSLContext with those set properties. In the following case should be with specified keyStore. Unfortunately that's not what is happening. It falls back JVM's default keystore. Am I missing something ?



 System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

answer = SSLContext.getDefault();









share|improve this question



















  • 1




    What do you get in the logs when you run with -Djavax.net.debug=ssl'? Does it show that it reads /keyStore.jks?
    – Karol Dowbecki
    Nov 9 at 22:07










  • keyStore is : keyStore type is : jks keyStore provider is : init keystore init keymanager of type SunX509 trigger seeding of SecureRandom done seeding SecureRandom Allow unsafe renegotiation: false Allow legacy hello messages: true This is what I get in logs @KarolDowbecki
    – Sagar
    Nov 9 at 22:13











  • Can you re-run with -D properties instead of System.setProperty()? I'm trying to confirm if your keystore location and password is correct.
    – Karol Dowbecki
    Nov 9 at 22:53






  • 1




    (1) is this code executed before any other reference to any SSL-related classes by any code in your JVM processs? (It must be.) (2) Is /keyStore.jks really in your system's root directory (or on Windows the drive's)?
    – dave_thompson_085
    Nov 10 at 2:06










  • @dave_thompson_085 please see my answer. Although I tried putting those two properties in my constructor, that too was too late. So, in the end, put that in a static block. Thanks for the hint.
    – Sagar
    Nov 10 at 14:34















up vote
0
down vote

favorite












I am in the impression that once I set system properties when I get SSLContext.getDefault() should return me SSLContext with those set properties. In the following case should be with specified keyStore. Unfortunately that's not what is happening. It falls back JVM's default keystore. Am I missing something ?



 System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

answer = SSLContext.getDefault();









share|improve this question



















  • 1




    What do you get in the logs when you run with -Djavax.net.debug=ssl'? Does it show that it reads /keyStore.jks?
    – Karol Dowbecki
    Nov 9 at 22:07










  • keyStore is : keyStore type is : jks keyStore provider is : init keystore init keymanager of type SunX509 trigger seeding of SecureRandom done seeding SecureRandom Allow unsafe renegotiation: false Allow legacy hello messages: true This is what I get in logs @KarolDowbecki
    – Sagar
    Nov 9 at 22:13











  • Can you re-run with -D properties instead of System.setProperty()? I'm trying to confirm if your keystore location and password is correct.
    – Karol Dowbecki
    Nov 9 at 22:53






  • 1




    (1) is this code executed before any other reference to any SSL-related classes by any code in your JVM processs? (It must be.) (2) Is /keyStore.jks really in your system's root directory (or on Windows the drive's)?
    – dave_thompson_085
    Nov 10 at 2:06










  • @dave_thompson_085 please see my answer. Although I tried putting those two properties in my constructor, that too was too late. So, in the end, put that in a static block. Thanks for the hint.
    – Sagar
    Nov 10 at 14:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am in the impression that once I set system properties when I get SSLContext.getDefault() should return me SSLContext with those set properties. In the following case should be with specified keyStore. Unfortunately that's not what is happening. It falls back JVM's default keystore. Am I missing something ?



 System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

answer = SSLContext.getDefault();









share|improve this question















I am in the impression that once I set system properties when I get SSLContext.getDefault() should return me SSLContext with those set properties. In the following case should be with specified keyStore. Unfortunately that's not what is happening. It falls back JVM's default keystore. Am I missing something ?



 System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

answer = SSLContext.getDefault();






java security java-security java-security-manager






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 14:04

























asked Nov 9 at 21:58









Sagar

2,37541735




2,37541735







  • 1




    What do you get in the logs when you run with -Djavax.net.debug=ssl'? Does it show that it reads /keyStore.jks?
    – Karol Dowbecki
    Nov 9 at 22:07










  • keyStore is : keyStore type is : jks keyStore provider is : init keystore init keymanager of type SunX509 trigger seeding of SecureRandom done seeding SecureRandom Allow unsafe renegotiation: false Allow legacy hello messages: true This is what I get in logs @KarolDowbecki
    – Sagar
    Nov 9 at 22:13











  • Can you re-run with -D properties instead of System.setProperty()? I'm trying to confirm if your keystore location and password is correct.
    – Karol Dowbecki
    Nov 9 at 22:53






  • 1




    (1) is this code executed before any other reference to any SSL-related classes by any code in your JVM processs? (It must be.) (2) Is /keyStore.jks really in your system's root directory (or on Windows the drive's)?
    – dave_thompson_085
    Nov 10 at 2:06










  • @dave_thompson_085 please see my answer. Although I tried putting those two properties in my constructor, that too was too late. So, in the end, put that in a static block. Thanks for the hint.
    – Sagar
    Nov 10 at 14:34













  • 1




    What do you get in the logs when you run with -Djavax.net.debug=ssl'? Does it show that it reads /keyStore.jks?
    – Karol Dowbecki
    Nov 9 at 22:07










  • keyStore is : keyStore type is : jks keyStore provider is : init keystore init keymanager of type SunX509 trigger seeding of SecureRandom done seeding SecureRandom Allow unsafe renegotiation: false Allow legacy hello messages: true This is what I get in logs @KarolDowbecki
    – Sagar
    Nov 9 at 22:13











  • Can you re-run with -D properties instead of System.setProperty()? I'm trying to confirm if your keystore location and password is correct.
    – Karol Dowbecki
    Nov 9 at 22:53






  • 1




    (1) is this code executed before any other reference to any SSL-related classes by any code in your JVM processs? (It must be.) (2) Is /keyStore.jks really in your system's root directory (or on Windows the drive's)?
    – dave_thompson_085
    Nov 10 at 2:06










  • @dave_thompson_085 please see my answer. Although I tried putting those two properties in my constructor, that too was too late. So, in the end, put that in a static block. Thanks for the hint.
    – Sagar
    Nov 10 at 14:34








1




1




What do you get in the logs when you run with -Djavax.net.debug=ssl'? Does it show that it reads /keyStore.jks?
– Karol Dowbecki
Nov 9 at 22:07




What do you get in the logs when you run with -Djavax.net.debug=ssl'? Does it show that it reads /keyStore.jks?
– Karol Dowbecki
Nov 9 at 22:07












keyStore is : keyStore type is : jks keyStore provider is : init keystore init keymanager of type SunX509 trigger seeding of SecureRandom done seeding SecureRandom Allow unsafe renegotiation: false Allow legacy hello messages: true This is what I get in logs @KarolDowbecki
– Sagar
Nov 9 at 22:13





keyStore is : keyStore type is : jks keyStore provider is : init keystore init keymanager of type SunX509 trigger seeding of SecureRandom done seeding SecureRandom Allow unsafe renegotiation: false Allow legacy hello messages: true This is what I get in logs @KarolDowbecki
– Sagar
Nov 9 at 22:13













Can you re-run with -D properties instead of System.setProperty()? I'm trying to confirm if your keystore location and password is correct.
– Karol Dowbecki
Nov 9 at 22:53




Can you re-run with -D properties instead of System.setProperty()? I'm trying to confirm if your keystore location and password is correct.
– Karol Dowbecki
Nov 9 at 22:53




1




1




(1) is this code executed before any other reference to any SSL-related classes by any code in your JVM processs? (It must be.) (2) Is /keyStore.jks really in your system's root directory (or on Windows the drive's)?
– dave_thompson_085
Nov 10 at 2:06




(1) is this code executed before any other reference to any SSL-related classes by any code in your JVM processs? (It must be.) (2) Is /keyStore.jks really in your system's root directory (or on Windows the drive's)?
– dave_thompson_085
Nov 10 at 2:06












@dave_thompson_085 please see my answer. Although I tried putting those two properties in my constructor, that too was too late. So, in the end, put that in a static block. Thanks for the hint.
– Sagar
Nov 10 at 14:34





@dave_thompson_085 please see my answer. Although I tried putting those two properties in my constructor, that too was too late. So, in the end, put that in a static block. Thanks for the hint.
– Sagar
Nov 10 at 14:34













1 Answer
1






active

oldest

votes

















up vote
0
down vote













I think by the time answer = SSLContext.getDefault(); was about to execute, SSLContext related classed were already loaded. I solved it by putting



System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");

in static block of my class. That way, there properties were set at the time of class loading. Thanks to @dave_thompson_085 for hint.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233770%2fsetting-up-keystore-programmatically-before-getting-default-ssl-context%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I think by the time answer = SSLContext.getDefault(); was about to execute, SSLContext related classed were already loaded. I solved it by putting



    System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
    System.setProperty("javax.net.ssl.keyStorePassword","changeit");

    in static block of my class. That way, there properties were set at the time of class loading. Thanks to @dave_thompson_085 for hint.






    share|improve this answer
























      up vote
      0
      down vote













      I think by the time answer = SSLContext.getDefault(); was about to execute, SSLContext related classed were already loaded. I solved it by putting



      System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
      System.setProperty("javax.net.ssl.keyStorePassword","changeit");

      in static block of my class. That way, there properties were set at the time of class loading. Thanks to @dave_thompson_085 for hint.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        I think by the time answer = SSLContext.getDefault(); was about to execute, SSLContext related classed were already loaded. I solved it by putting



        System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
        System.setProperty("javax.net.ssl.keyStorePassword","changeit");

        in static block of my class. That way, there properties were set at the time of class loading. Thanks to @dave_thompson_085 for hint.






        share|improve this answer












        I think by the time answer = SSLContext.getDefault(); was about to execute, SSLContext related classed were already loaded. I solved it by putting



        System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
        System.setProperty("javax.net.ssl.keyStorePassword","changeit");

        in static block of my class. That way, there properties were set at the time of class loading. Thanks to @dave_thompson_085 for hint.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 14:33









        Sagar

        2,37541735




        2,37541735



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233770%2fsetting-up-keystore-programmatically-before-getting-default-ssl-context%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20