SSL 通信及 java keystore 工具介绍

http://www.javacodegeeks.com/2014/07/java-keystore-tutorial.html

Table Of Contents

1. Introduction
2. SSL and how it works
3. Private Keys
4. Public Certificates
5. Root Certificates
6. Certificate Authorities
7. Certificate Chain
8. Keystore using Java keytool
9. Keystore Commands
10. Configure SSL using Keystores and Self Signed Certificates on Apache Tomcat

1. Introduction

Who of us didn’t visit ebay, amazon to buy anything or his personal bank account to check it. Do you think that those sites are secure enough to put your personal data like (credit card number or bank account number, etc.,)?

Most of those sites use the Socket Layer (SSL) protocol to secure their Internet applications. SSL allows the data from a client, such as a Web browser, to be encrypted prior to transmission so that someone trying to sniff the data is unable to decipher it.

Many Java application servers and Web servers support the use of keystores for SSL configuration. If you’re building secure Java programs, learning to build a keystore is the first step.

2. SSL and how it works

A HTTP-based SSL connection is always initiated by the client using a URL starting with https:// instead of with http://. At the beginning of an SSL session, an SSL handshake is performed. This handshake produces the cryptographic parameters of the session. A simplified overview of how the SSL handshake is processed is shown in the diagram below.

This is in short how it works:

  1. A browser requests a secure page (usually https://).
  2. The web server sends its public key with its certificate.
  3. The browser checks that the certificate was issued by a trusted party (usually a trusted root CA), that the certificate is still valid and that the certificate is related to the site contacted.
  4. The browser then uses the public key, to encrypt a random symmetric encryption key and sends it to the server with the encrypted URL required as well as other encrypted http data.
  5. The web server decrypts the symmetric encryption key using its private key and uses the symmetric key to decrypt the URL and http data.
  6. The web server sends back the requested html document and http data encrypted with the symmetric key.
  7. The browser decrypts the http data and html document using the symmetric key and displays the information.

The world of SSL has, essentially, three types of certificates: private keys, public keys (also called public certificates or site certificates), and root certificates.

3. Private Keys

The private key contains the identity information of the server, along with a key value. It should keep this key safe and protected by password because it’s used to negotiate the hash during the handshake. It can be used by someone to decrypt the traffic and get your personal information. It like leaving your house key in the door lock.

4. Public Certificates

The public certificate (public key) is the portion that is presented to a client, it likes your personal passport when you show in the Airport. The public certificate, tightly associated to the private key, is created from the private key using a Certificate Signing Request (CSR). After you create a private key, you create a CSR, which is sent to your Certificate Authority (CA). The CA returns a signed certificate, which has information about the server identity and about the CA.

5. Root Certificates

Root CA Certificate is a CA Certificate which is simply a Self-signed Certificate. This certificate represents a entity which issues certificate and is known as Certificate Authority or the CA such as VeriSign, Thawte, etc.

6. Certificate Authorities

Companies who will sign certificates for you such as VeriSign, Thawte, Commodo, GetTrust. Also, many companies and institutions act as their own CA, either by building a complete implementation from scratch, or by using an open source option, such as OpenSSL.

7. Certificate Chain

When a server and client establish an SSL connection, a certificate is presented to the client; the client should determine whether to trust this certificate, a process called the certificate chain. The client examines the issuer of a certificate, searches its list of trusted root certificates, and compares the issuer on the presented certificate to the subjects of the trusted certificates.

If a match is found, the connection proceeds. If not, the Web browsers may pop up a dialog box, warning you that it cannot trust the certificate and offering the option to trust the certificate.

8. Keystore using Java keytool

Java Keytool is a key and certificate management utility. It allows users to manage their own public/private key pairs and certificates. Java Keytool stores the keys and certificates in what is called a keystore. It protects private keys with a password.

Each certificate in a Java keystore is associated with a unique alias. When creating a Java keystore you will first create the .jks file that will initially only contain the private key, then generate a CSR. Then you will import the certificate to the keystore including any root certificates.

9. Keystore Commands

Create Keystore, Keys and Certificate Requests

  • Generate a Java keystore and key pair

    keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -storepass password
  • Generate a certificate signing request (CSR) for an existing Java keystore
    keytool -certreq -alias mydomain -keystore keystore.jks -storepass password -file mydomain.csr
  • Generate a keystore and self-signed certificate
    keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360

Import Certificates

    • Import a root or intermediate CA certificate to an existing Java keystore
keytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks -storepass password
  • Import a signed primary certificate to an existing Java keystore

    keytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks -storepass password

Export Certificates

  • Export a certificate from a keystore

    keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks -storepass password

Check/List/View Certificates

  • Check a stand-alone certificate

    keytool -printcert -v -file mydomain.crt
  • Check which certificates are in a Java keystore
    keytool -list -v -keystore keystore.jks -storepass password
  • Check a particular keystore entry using an alias
    keytool -list -v -keystore keystore.jks -storepass password -alias mydomain

Delete Certificates

  • Delete a certificate from a Java Keytool keystore

    keytool -delete -alias mydomain -keystore keystore.jks -storepass password

Change Passwords

  • Change a Java keystore password

    keytool -storepasswd -new new_storepass -keystore keystore.jks -storepass password
  • Change a private key password
    keytool -keypasswd -alias client -keypass old_password -new new_password -keystore client.jks -storepass password

10. Configure SSL using Keystores and Self Signed Certificates on Apache Tomcat

  1. Generate new keystore and self-signed certificateusing this command, you will prompt to enter specific information such as user name, organization unit, company and location.

    keytool -genkey -alias tomcat -keyalg RSA -keystore /home/ashraf/Desktop/JavaCodeGeek/keystore.jks -validity 360

  2. You can list the certificate details you just created using this command
    keytool -list -keystore /home/ashraf/Desktop/JavaCodeGeek/keystore.jks

  3. Download Tomcat 7
  4. Configure Tomcat’s server to support for SSL or https connection. Adding a connector element in Tomcat\conf\server.xml
    <Connector port="8443" maxThreads="150" scheme="https" secure="true"
    SSLEnabled="true" keystoreFile="/home/ashraf/Desktop/JavaCodeGeek/.keystore" keystorePass="password" clientAuth="false" keyAlias="tomcat" sslProtocol="TLS" />
  5. Start Tomcat and go tohttps://localhost:8443/, you will find the following security issue where the browser will present untrusted error messages. In the case of e-commerce, such error messages result in immediate lack of confidence in the website and organizations risk losing confidence and business from the majority of consumers, that‘s normal as your certificate isn‘t signed yet by CA such as Thawte or Verisign who will verify the identity of the requester and issue a signed certificate.

  6. You can click Proceed anyway till you receive you signed certificate.
时间: 2024-11-08 11:37:21

SSL 通信及 java keystore 工具介绍的相关文章

java学习之常用Java Profiling工具的分析与比较

在 Java 程序的开发过程中,不可避免地会遇到内存使用.性能瓶颈等问题.Java Profiler 工具能帮助开发人员快速.有效地定位这些问题,因此成为了 Java 开发过程中的一个重要工具.目前市场上的 Java Profiler 工具种类繁多,本文将对目前比较常见的几种工具进行简要介绍,并从功能.性能等角度作比较,从而帮助 Java 程序员选择合适的 Java Profiler 工具. 本文主要分为三个部分:第一部分简要介绍 Java Profiler 工具的原理:第二部分对目前常见的 J

JAVA代码覆盖率工具JaCoCo-原理篇

1.2 JAVA覆盖率工具介绍 1.3.3 Apache Maven方式 1.3.4 Eclipse EclDmma Plugin方式 JAVA代码覆盖率工具JaCoCo-实践篇 一.覆盖率项目中使用介绍 1.5执行测试,收集覆盖率结果文件 1.5.1AndroidManifest文件的修改 1.5.2生成覆盖率的apk工具和jacoco-cov-sdk.jar包 二.覆盖率与BVT测试结合 2.1在BVT用例框架中插入覆盖率方法 2.2执行BVT用例,得到覆盖率 2.3批量生成覆盖率报告,解析

java基础-Eclipse开发工具介绍

java基础-Eclipse开发工具介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 所谓工欲善其事必先利其器,即将身为一名Java开发工程师怎么能没有一款好使的IDE呢?今天就为大家介绍一款功能强大的IDE,即Eclipse.它是Java集成开发工具.它可以极大地提升我们的开发效率.可以自动编译,检查错误.在公司中,很多Java工程师都将Eclise作为首选开发工具,而且它还是免费开源的,尽管eclipse有些功能没有,但是你开源下载一些插件让其支持,这足以说明它的扩展性

SSL 通信原理及Tomcat SSL 双向配置

目录1 参考资料 .................................................................................................................................. 12 SSL(Server Socket Layer)简介 ................................................................................

SSL 通信原理及Tomcat SSL 配置

SSL 通信原理及Tomcat SSL 双向配置 目录1 参考资料 .................................................................................................................................. 12 SSL(Server Socket Layer)简介 .......................................................

Android系统性能调优工具介绍

经作者授权,发表Tieto某青年牛的一篇<程序员>大作. Android系统性能调优工具介绍 在软件开发过程中,想必很多读者都遇到过系统性能问题.而解决系统性能问题的几个主要步骤是: 测评:对系统进行大量有针对性的测试,以得到合适的测试数据. 分析系统瓶颈:分析测试数据,找到其中的hotspot(热点,即bottleneck). 性能优化:对hotspot相关的代码进行优化. 由上述步骤可知,性能优化的目标对象是hotspot.如果找到的hotspot并非真正的热点,则性能优化的结果必然是事倍

Java远程技术介绍学习

Java远程技术介绍学习 RMI [既Remote Method Invoke 远程方法调用] 实现方式为,类extend了java.rmi.Remote接口,即成为存在于服务器端的远程对象,提供客户端访问. PS: extends了Remote接口的类或者其他接口中的方法若是声明抛出了RemoteException异常,则表明该方法可被客户端远程访问调用. 同时,远程对象必须实现java.rmi.server.UniCastRemoteObject类,这样才能保证客户端访问获得远程对象时,该远

杂谈X509证书, Java Keystore与Jetty

很多人对JSSE组成部分的Key Tool 工具不太明白,希望本文能有帮助 科班出身的同学应该学过课程“密码学”, 这门课详细解释了现代对称加密的算法原理, 当时学的我云里雾里. 直到现在使用过SSL加密才知道工程上用法是这样的, 老师讲的时候就不能带一点工程实践吗? 简单来说,对称加密体系就是我有一段需要加密的字符, 我用私钥加密之后变成了无意义的密文, 只有用配对的公钥才能对这个密文进行解密还原回来. 下图是个简单的示意,注意由于公私钥是配对的,一般给信息加密的人持有此密钥对. 这套机制可以

java开发工具——Eclipse的常见使用说明

Java开发常见工具介绍: A:操作系统自带的记事本软件 B:高级记事本软件 C:集成开发环境 IDE (Integrated Development Environment) Eclipse的基本使用Eclipse的基本使用: 选择工作空间 工作空间 其实就是我们写的源代码所在的目录 用Eclipse来完成一个HelloWorld案例 代码以项目为基本单位 创建项目 创建包 创建类 编写代码 eclipse的汉化及检查: 使用dropins安装插件 从Eclipse3.5开始,安装目录下就多了