0

I want to retrieve a certificate from a server in linux but i don't want to use the openssl commands in terminal. I want to implement this in my java project where i want to connect to a POP3S server TLS secured. I managed to connect by using: openssl s_client -connect [host]:[port|443] < /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > [host].crt and then i added this certificate to my trusted ones with keytool -import -noprompt -trustcacerts -alias <AliasName> -file <certificate> -keystore <KeystoreFile> -storepass <Password> and it works. Can i achieve this from code(if yes,HOW?) ? It's not the same with this question How to get server certificate chain then verify it's valid and trusted in Java because i need to get the certificate from a localhost POP3S (secured with TLS) server

Community
  • 1
  • 1
  • For non-HTTPS, you'd likely use an [`SSLSocketFactory`](https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/SSLSocketFactory.html) to create an [`SSLSocket`](https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/SSLSocket.html), then you can call `getSession()` to get the [`SSLSession`](https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/SSLSession.html), where you can call `getPeerCertificates()` to get the [`Certificate`](https://docs.oracle.com/javase/8/docs/api/java/security/cert/Certificate.html) object you want. – Andreas Apr 05 '17 at 15:21
  • Duplicate of [JAVA: Extract Server Certificates](http://stackoverflow.com/q/19297446/5221149) – Andreas Apr 05 '17 at 15:23

0 Answers0