• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading expiration dates on pem or pfx certificates

 
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
Hello.if you have a minute, I was thinking of coding an "early wanring"/detection to send an email when a tls/ssl cert is within, say, 60 days of expiration, then 30, etc. However,java.security.KeyStore doesn't seem to like a pem format converted from pfx with openssl. Here is the sequence of events..

1.  issued a self-signed my_cert.pfx for server by CA

2.  command line....  openssl pkcs12 -in my_cert.pfx -out my_cert.pem -nodes

3. Deployed/configured my_cert.pem

4. Been monitoring the expiration dates via command line openssl but thought I'd try to create that early warning utility

5. These are the key lines in code (standard fare I think out of what I see)...



6. Hard coding my_cert.pfx for file_path, the pfx seems to load and the expiration date is read correctly (later in code).

7. However, my_cert.pem for file+path throws the java.io.IOException Invalid keystore format exception on the load line. ugh. I'm not sure what's up, the pem has been validated and as I said, been doing it's TLS job. Thank you so much for reading.
 
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
If you are starting with a PEM (Base64 encoded) representation of the certificate, remove the armoring, use  the CertificateFactory to create a certificate, and then read whatever attributes you need.

Here's a quick example:
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
If starting with a DER:
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
ok...let me look at that... thanks.

I see the problem with my

line where the default is apparently PKSCS12. If I use this line...



it still works for PFX. But I wouldn't know the instance type for a PEM file (out of the list of allowable KeyStore instance types I see)...

jceks
jks
dks
pkcs11
pkcs12

so I'll look at the example you provided as maybe it has to do with the armoring.
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
With a PKCS12 archive, it should be something like this (untested; assuming only a single entry):
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
thanks so much. i was able to write a java program for the pkcs12 but i have a question regarding the pem code, actually all three types you posted. Isn't the var declaration javascript?
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:

Thomas Griffith wrote:Isn't the var declaration javascript?


The reserved type name var was added in Java 10.  Instead of explicitly stating the type, it can inferred based on the right-hand-side of the declaration statement (the initializer).

The details are here: JEP 286: Local-Variable Type Inference
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
oh, ok. I'm kinda stuck with 8 for now but I'll work with it. I was wondering about security of a pem file, or a ley file in pem format, sitting on a server like Tomcat. What would keep anybody from coding an inputstream, bytearray reader to  read a private key? Password protection on the file? Is the password applied in openssl when converting from pfx to pem?
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
When the key is stored in PEM or DER format, the only protection is going to be whatever access control is provided by the operating system and file system.

I believe that Tomcat does support using storing the private key (and certificate) in a KeyStore, which would normally be password protected.
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
yeah, I was going to look at adding a password to the pem file when I converted it from the CA issued pfx. I had to do that because the certificates are out of order in the pfx, had to reorder them into server, intermediate, root.  The original pfx has a passcode which i had to enter in openssl when converting to pem. I presumed this would carry over to the resultant pem but I guess it doesn't...so I was going to look into adding a password to the pem during the conversion step in openssl. But i'm not entirily sure if that password would protect form somebody streaming the pem for the private key, like I'm attempting here for the expiration date.

I'm working in 8 and I'm having some trouble with the following lines (haivng to explicitly declare the objects)...



I am getting compile error "incompatible types: String cannot be converted to URI"

so figuring this is looking for a url and not a path, changed to...



Same error "incompatible types: String cannot be converted to URI"

Should I do this with a FileInputStream? ty.
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
If you are going to use the Windows path notation, you will need to escape the bash slashes:
I prefer to use Unix paths even when running in a Windows environment:
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
yeah, i escape them already, I was typing what the path format looks like from System.out.println....
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:

Thomas Griffith wrote:... the certificates are out of order in the pfx, had to reorder them into server, intermediate, root.


If there is more than one entry in the archive, then you may need to iterate through then to find the host certificate.  I didn't do that in the example that I shared (I mentioned: assuming only a single entry),
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
If you can share a more complete example of the non-working code, I (or someone else) can take a look.
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
Hi. I wound up converting the pem to a password protected key store, as you suggested, after realizing pem basically exposes the private key and certs. I converted the pem back to pkcs12 with openssl and set up the config in server.xml. Then coded a scheduled process using the core PKCS12 code above to read the expiration date stuff and starting the reminders on a weekly basis once it gets to 90 days before expiration. thanks so much.

With the password protected key store, however, brings the Tomcat issue of storing passwords in plain text within server.xml  (for the key store) and any context.xml files (for JDBC and other db system connects). Do you know if that Digester.bat can be used to SHA-256 passwords, then the hash can be thrown into the above xml files in lieu of clear text passwords, and if Tomcat stores these SHA-256 mappings with clear text passwords somewhere as needed for the external db connects  (as well as the connect to the PKCS12 keystore for the expiration date)?
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:

Thomas Griffith wrote:... Then coded a scheduled process using the core PKCS12 code above to read the expiration date stuff and starting the reminders on a weekly basis once it gets to 90 days before expiration. thanks so much.

You're welcome - glad to hear that you got it working.

Thomas Griffith wrote:... after realizing pem basically exposes the private key and certs

There is nothing sensitive in the X.509 certificates.  In-fact they are offered to the (anonymous) client in plain-text every time a TLS connection is being established.

Thomas Griffith wrote:With the password protected key store, however, brings the Tomcat issue of storing passwords in plain text within server.xml  (for the key store)

I'm pretty sure that Tomcat supports PKCS12 keystores directly, so you should not need to keep the private key in the clear on the file system (however, the encryption key for the keystore will be in the file system).

Thomas Griffith wrote:Do you know if that Digester.bat can be used to SHA-256 passwords

I don't know what Digester.bat is, but I can't imagine it working.  The private key is used to create and verify digital signatures - it is not a password.

I'm not sure what your security concerns/requirements are, but I worked on some PCI (payment card industry) like projects where we were absolutely paranoid about the private key get exfiltrated, and would not have passed security audits having the private key or encrypting key in the open.  Our solution was to use HSMs to generate and store the private keys (and symmetric encryption keys), and have the HSM sign and verify content using these internally-stored keys.  Keys stored in an HSM cannot be extracted.  The cost of an HSM can range from $100 for a low-end USB device to $50,000 for a high-end network-connected device.
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
oh, thanks for bringing HSM up. I was curious, so the HSM is connected at all times to the server/vm where Tomcat or any HTTP server using TLS/SSL would be running, is that right? Becasue every HTTPS connection requires the private key for decryption, right? the HSM is essentially a server partition?

Regarding Tomcat passwords, in server.xml,  it requires the

certificateKeyStoreFile (path within Tomcat Home)
certificateKeystorePassword
certificateKesystoreType (JKS, PKCS12, etc)

But I was hoping to hash the key store password part here in server.xml as well as in context xml files with JDBC connections (containing passwords to dbs). Was kind of reading that Tomcat relies on OS security for this but hoping to hash them wiht digester.bat, put the hash into these xml configs, and Tomcat able to take hash and feed the real plain text passwords to the keystore or dbs. That's why i am looking at the digesster.bat thing.
 
Saloon Keeper
Posts: 15621
366
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
How would Tomcat be able to retrieve the original password from a password hash? Hash functions are intended to be one-directional. That's why they're also called "digest functions". You're not getting your meal back after you've digested it.

Anyway, why are you scared of storing the keystore password in plain text? You can just run Tomcat under some service account and give only this service account read permissions to the configuration file.

If somebody with admin rights has bad intentions, or simply doesn't know what they're doing, you've already lost the battle, even if you DID encrypt the keystore password.
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
you know how those sec guys can get with their scans. I also get a little leery of asking network guys for specific access setting because I find myself upgrading Tomcat point releases a lot and deploy in parallel to old instance (ultimately removing the old) . I know what you're saying though, it's the only way. I've been reading about this for two days and encrypting the passwords in config files and key store(s) seems to come back to every security apparatus has to have an entry point vulnerability. I once heard the director of "The Blues Brothers" say that for the mall scenes, they hired security guards to watch over the merchandise then had to hire guards to watch the guards.

I also get that Tomcat running as a service on startup also sets up the TLS and connector stuff so it needs to access the clear passwords sans human intervention.

But at the same time, password protecting the key store, then storing the password out there seems redundant.  Why not just use open pem files instead of creating a key store, for example?

So i looked at the Tomcat disgest.bat thing to see if maybe it could use a SHA 256 algorithm or whatever, to work like a complicated authentication system..

1) salt/hash any passwords you want.
2) stores the salt/hash/clear pw mapping in some secret protected, self-encoded/decoded "place" or whatever...in some lib ...some "table"...
3) you toss the salted hash password into corresponding xml file
4) then on any request, basically during service startup but on user requests as well, maps the salted hash in the whatever xml to the clear password and shoves that into the access request
5) But in any case, the digester looks like it only works on tomcat-users.xml and users entering pw's.
6) I also think for @2 above, as you said, the digester just spits out the hash, you'd need an external db in a realm to store the salt/hash/real mappings.

Have you tried that Tomcat Vault? I was about to go down that rabbit hole but not liking how "obscure" this looks....  thanks so much.
 
Stephan van Hulst
Saloon Keeper
Posts: 15621
366
  • Number of slices to send:
    Optional 'thank-you' note:

Thomas Griffith wrote:But at the same time, password protecting the key store, then storing the password out there seems redundant.  Why not just use open pem files instead of creating a key store, for example?


Because KeyStore is idiomatic in Java. Most Java application servers provide a way of configuring a key store to use for stuff like TLS. Besides, a KeyStore can hold more than just a single certificate. It can hold multiple certificate chains, private keys and secret keys. If you wanted to use different certificates or keys for different purposes, you don't need to have all of them strewn about as different files.

2) stores the salt/hash/clear pw mapping in some secret protected, self-encoded/decoded "place" or whatever...in some lib ...some "table"...


Your logic breaks down here. How are you going to keep this "secret protected place" secret and protected?

Have you tried that Tomcat Vault? I was about to go down that rabbit hole but not liking how "obscure" this looks....  thanks so much.


Just consider this: You're trusting a third party application to keep your password safe, but not the operating system that it's running on?

In short, just put the password in the server.xml file and let your OS protect it.
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
Hello. I decided not to mess with those passwords (hashing, xor, etc) as per the advice given although I messed around some with obfuscation just in case sec comes down with something. It's a huge risk with locking yourself out, I think, the more cryptic ya try to get.

One thing I want to do with the key store is read the expiration date and start serving up warning notices starting about 3 months out. I've seen some examples and I have the pem file open with the secret key and server, intermediate, root certs. I've seen this code and I'm kinda wondering what exactly is enumerated in this file..



The pem....



The code goes through one enumeration, not four (key + each cert i the hierarchy) and works but I don't know why. The System.out prints alias - 1. Is it because there are no aliases in here?

when I use keystore.getCertificateChain(alias) it scrolls through the three certificates. Still not sure what the alias is and why it's the point of reference for these methods.

 
Ron McLeod
Marshal
Posts: 4533
572
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Another approach would be to not deal with the keystore at all, and possibly perform your test on a different platform than where the server is running.  Since the server's X509 certificate is freely offered to the client during the TLS negotiation, you can attempt to make a connection to the server, grab the certificate, and then check the certificate's expiry date/period.

Here's a an example written in bash using openssl, but it could easily be done in Java as well:


 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:
wow, that's pretty interesting because the one sticky part I was hitting (other than trying to figure ot what exactly an alias is in a Key store) is basically having to hard code in the key store path into the code. I was initially surprised the exp date would be accessible but thinking about it, the browser has to negotiate/determine all that during the TLS handshake and working it's way up the cert chain until it can compare root with it's trust store.

Is the alias implicitly assigned, even if it's null or blank, to a private key and associated key chain at creation?
 
Ron McLeod
Marshal
Posts: 4533
572
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Thomas Griffith wrote:Is the alias implicitly assigned, even if it's null or blank, to a private key and associated key chain at creation?


The alias should be defined when when the entry is added to the key store.  Depending on the tool/API you used to add the certificates to the store, specifying an alias may have not been mandatory.

Really though, do you care which certificate in the store in for the host and which are for the intermediates?  If any of the certificates in the chain are about to expire, you will need to take action (plus, it in unlikely have the intermediate certificates will expire before your host certificate).
 
Ron McLeod
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
Here's some proof-of-concept code to check using Java.  Since you are using Java 8, you won't be able to use the new built-in HttpClient, but you should be able to do something similar with other clients (Apache HTTPClient, OkHttpClient, etc.).
 
Stephan van Hulst
Saloon Keeper
Posts: 15621
366
  • Number of slices to send:
    Optional 'thank-you' note:
I like it, Ron.
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Number of slices to send:
    Optional 'thank-you' note:

Ron McLeod wrote:
Really though, do you care which certificate in the store in for the host and which are for the intermediates?  If any of the certificates in the chain are about to expire, you will need to take action (plus, it in unlikely have the intermediate certificates will expire before your host certificate).



yeah, I was doing some messing around and parse all the certs in the hierarchy (leaf, intermediate, root) in the key store and spit out their expiration dates. I have three leaf certs on three different servers, but the intermediates do have different expiration dates (root of course the same), But the leaf exp dates all come before the intermediates.

I'm going to check out the Java 11 Httpclient but might be stuck with 8 for now (where I want to host the scheduled procedure is on 8).
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |