Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
244 views

Generate Certificate Chains For Testing Java Applications PDF

The document describes how to use the OpenSSL toolkit to generate certificate chains for testing Java applications. It provides instructions for creating a root certificate and intermediate certificates signed by the root certificate, allowing the creation of certificate chains of any length. Sample code is also presented for loading the generated certificates into a Java keystore.

Uploaded by

420sree5705
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views

Generate Certificate Chains For Testing Java Applications PDF

The document describes how to use the OpenSSL toolkit to generate certificate chains for testing Java applications. It provides instructions for creating a root certificate and intermediate certificates signed by the root certificate, allowing the creation of certificate chains of any length. Sample code is also presented for loading the generated certificates into a Java keystore.

Uploaded by

420sree5705
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

01/09/2013

IBM

Generate certificate chains for testing Java applications


English Sign in (or register)

Technical topics

Evaluation software

Community

Events

Search developerWork s

Generate certificate chains for testing Java applications


Put the OpenSSL toolkit to work Paul Abbott (pabbott@hursley.ibm.com), Software Engineer, IBM Summary: Learn how to create digital-certificate chains to test your software. IBM Software Engineer, Paul H. Abbott, clarifies this seldom-documented process by showing you how to use the freely available OpenSSL toolkit to create a certificate chain of any length. He also describes common certificate attributes and shows you some sample Java code for reading the certificates you create into a Java keystore. Date: 18 Aug 2004 Level: Introductory Also available in: Japanese Activity: 76481 views Comments: 4 (View | Add comment - Sign in) Average rating (91 votes) Rate this article If you're developing Java software that relies on public key infrastructure (PKI) for security, you often need to create digital-certificate chains (also known as certification paths) for testing purposes. This is a relatively simple task but one for which clear documentation is scarce. This article describes how to create arbitrary-length certificate chains using the open source OpenSSL toolkit (see Resources). You'll also learn about some common certificate attributes and examine a sample program that reads certificates into a Java keystore. Digital certificates: A quick overview This article assumes that you're familiar with PKI basics, so I'll give you just a quick overview of the purpose and structure of digital certificates to help clarify the concept of a certificate chain. The primary use for digital certificates is to verify the origin of signed data, such as e-mail and JAR files. Verifying the signed data with a certificate lets the recipient know the origin of the data and if it has been altered in transit. A digital certificate contains, at a high level, a distinguished name (DN) and a public key. The DN identifies an entity -- a person, for example -- that holds the private key that matches the public key of the certificate. You tie the two together by signing the certificate with a private key and placing the signature in the certificate. A certificate signed by the private key that matches the public key of the certificate is known as a self-signed certificate. Root certification authority (CA) certificates fall into this category. User certificates are often signed by a different private key, such as a private key of the CA . This constitutes a two-certificate chain. Verifying that a user certificate is genuine involves verifying its signature, which requires the public key of the CA, from its certificate. But before the public key of the CA can be used, the enclosing CA certificate needs to be verified. Because the CA certificate is self signed, the CA public key is used to verify the certificate. A user certificate need not be signed by the private key of the root CA. It could be signed by the private key of an intermediary whose certificate is signed by the private key of the CA. This is an instance of a three-certificate chain: user certificate, intermediary certificate, and CA certificate. But more than one intermediary can be part of the chain, so certificate chains can be of any length. Another point worth noting is that a certificate can contain additional information, known as extensions. Extensions can specify the use of the certificate, among other things. Certain extensions can be very important, depending on the use of the certificate. Create certificates with OpenSSL A number of tools are available for creating certificates. You can use keytool, a command-line tool shipped with the Java SDK, to create self-signed certificates. But certificate chains require more-complex software, such as OpenSSL. Obtaining OpenSSL OpenSSL is available as a free download (see Resources). If you use UNIX, you're likely to find that OpenSSL has either been installed with the OS or is an optional install supplied with the OS. Linux users should look in: /usr/share/ssl. The Microsoft MKS toolkit also ships with a version of OpenSSL. (It's in C:\Program Files\MKS Toolkit\etc\openssl on my machine.) If you use Windows but don't have MKS, you can get the necessary binaries from SourceForge (see Resources). Most installations consist of three main files: the OpenSSL binary, a CA.sh shell script, and an openssl.cnf configuration file. (The package available from SourceForge is missing the CA.sh file. You can obtain the missing file by downloading the source bundle.)
www.ibm.com/developerworks/library/j-certgen/ 1/11

01/09/2013

Generate certificate chains for testing Java applications

After installation, make sure that CA.sh and the OpenSSL executable are on your path. Then you're ready to begin by creating a root certificate. Create a root certificate The CA shell script makes creating a root certificate a relatively simple job. First, change to the directory where you want to put the CA data store. (I use the temp\OpenSSL directory.) Then type:
C An e w c a

This results in a dialog like the one in Listing 1, which includes sample information that I entered at the prompts: Listing 1. Creating a root certificate
$C A . s hn e w c a C Ac e r t i f i c a t ef i l e n a m e( o re n t e rt oc r e a t e ) M a k i n gC Ac e r t i f i c a t e. . . U s i n gc o n f i g u r a t i o nf r o mC : / P R O G R A ~ 1 / M K S T O O ~ 1 / e t c / o p e n s s l / o p e n s s l . c n f L o a d i n g' s c r e e n 'i n t or a n d o ms t a t e-d o n e G e n e r a t i n ga1 0 2 4b i tR S Ap r i v a t ek e y . . . . . . + + + + + + . . . + + + + + + w r i t i n gn e wp r i v a t ek e yt o' . / d e m o C A / p r i v a t e / . / c a k e y . p e m ' E n t e rP E Mp a s sp h r a s e : V e r i f y i n gp a s s w o r d-E n t e rP E Mp a s sp h r a s e : Y o ua r ea b o u tt ob ea s k e dt oe n t e ri n f o r m a t i o nt h a tw i l lb ei n c o r p o r a t e d i n t oy o u rc e r t i f i c a t er e q u e s t . W h a ty o ua r ea b o u tt oe n t e ri sw h a ti sc a l l e daD i s t i n g u i s h e dN a m eo raD N . Y o uw i l ls e ean u m b e ro ff i e l d s ,b u ty o uc a nl e a v es o m eb l a n k . F o rs o m ef i e l d st h e r ew i l lb ead e f a u l tv a l u e , I fy o ue n t e r' . ' ,t h ef i e l dw i l lb el e f tb l a n k . C o u n t r yN a m e( 2l e t t e rc o d e )[ A U ] : U K S t a t eo rP r o v i n c eN a m e( f u l ln a m e )[ S o m e S t a t e ] : H a m p s h i r e L o c a l i t yN a m e( e . g . ,c i t y )[ ] : W i n c h e s t e r O r g a n i z a t i o nN a m e( e . g . ,c o m p a n y )[ I n t e r n e tW i d g i t sP t yL t d ] : I B MU KL t d O r g a n i z a t i o n a lU n i tN a m e( e . g . ,s e c t i o n )[ ] : J T C C o m m o nN a m e( e . g . ,Y O U Rn a m e )[ ] : P a u l sR o o tC e r t i f i c a t e E m a i lA d d r e s s[ ] : P a u l _ A b b o t t @ u k . i b m . c o m $

On completion of the dialog, OpenSSL creates the following directory structure:


d e m o C A / c a c e r t . p e m i n d e x . t x t s e r i a l c e r t s / c r l / n e w c e r t s / p r i v a t e / c a k e y . p e m

-r o o tc e r t i f i c a t e -e m p t y -t e x tf i l ec o n t a i n i n g" 0 1 " -e m p t y -e m p t y -e m p t y -p r i v a t ek e y

The PEM file format


OpenSSL uses the PEM file format to store certificates and keys. PEM is essentially Base64 encoded binary surrounded by begin and end lines, such as
B E G I NC E R T I F I C A T E -

and
E N DC E R T I F I C A T E -

for certificate files. Outside of these markers might be additional information, such as a textual representation of the encoded contents. The files are ASCII, and you can open them with any text editor. Check out this example PEM file. Here's a quick rundown of what's in the main directory: cacert.pem is the PEM encoded (see the sidebar, The PEM file format) root certificate of this CA. The root certificate verifies the certificates signed by the root private key. index.txt is a file containing a list of all issued certificates.
www.ibm.com/developerworks/library/j-certgen/ 2/11

01/09/2013

Generate certificate chains for testing Java applications

serial holds the next available serial number that will be assigned to a certificate issued by this CA. To put it another way, it's the unique serial number assigned to a certificate when a signing request is signed by this root certificate. cakey.pem is the root private key. This key is used to sign a certificate request. It is also PEM encoded. You define the directory name (demoCA in this example) and the root certificate's validity period, which defaults to 365 days, in CA.sh. You must edit this file if you want to alter these values. Generate a user certificate Generating a user certificate takes two steps: generating a request and signing the request. The CA shell script can perform both steps using the n e w r e q(generate a new request) and s i g n(sign a new request) operators. Generating a new request Executing the C An e w r e qcommand initiates a dialog similar to the one you went through to generate the new root certificate. One of the key differences is that the dialog prompts you for a PEM pass phrase, which OpenSSL uses to encode the private key before it writes it to the output file. The output file, named newreq.pem, contains the private key and the signing request, which you can think of as an unsigned certificate. Listing 2 shows an example of a new-request dialog. Listing 2. Example -newreq dialog
U s i n gc o n f i g u r a t i o nf r o m C : / P R O G R A ~ 1 / M K S T O O ~ 1 / e t c / o p e n s s l / o p e n s s l . c n f L o a d i n g' s c r e e n 'i n t or a n d o ms t a t e-d o n e G e n e r a t i n ga1 0 2 4b i tR S Ap r i v a t ek e y . . . . . . . . . . . . . . . . . . . . . . + + + + + + . . . + + + + + + w r i t i n gn e wp r i v a t ek e yt o' n e w r e q . p e m ' E n t e rP E Mp a s sp h r a s e : V e r i f y i n gp a s s w o r d-E n t e rP E Mp a s sp h r a s e : Y o ua r ea b o u tt ob ea s k e dt oe n t e ri n f o r m a t i o nt h a tw i l lb ei n c o r p o r a t e d i n t oy o u rc e r t i f i c a t er e q u e s t . W h a ty o ua r ea b o u tt oe n t e ri sw h a ti sc a l l e daD i s t i n g u i s h e dN a m eo raD N . Y o uw i l ls e ean u m b e ro ff i e l d s ,b u ty o uc a nl e a v es o m eb l a n k . F o rs o m ef i e l d st h e r ew i l lb ead e f a u l tv a l u e . I fy o ue n t e r' . ' ,t h ef i e l dw i l lb el e f tb l a n k . C o u n t r yN a m e( 2l e t t e rc o d e )[ A U ] : U K S t a t eo rP r o v i n c eN a m e( f u l ln a m e )[ S o m e S t a t e ] : H a m p s h i r e L o c a l i t yN a m e( e . g . ,c i t y )[ ] : W i n c h e s t e r O r g a n i z a t i o nN a m e( e . g . ,c o m p a n y )[ I n t e r n e tW i d g i t sP t yL t d ] : I B MU kL t d O r g a n i z a t i o n a lU n i tN a m e( e . g . ,s e c t i o n )[ ] : J E T C o m m o nN a m e( e . g . ,Y O U Rn a m e )[ ] : P a u lA b b o t t E m a i lA d d r e s s[ ] : P a u l _ H _ A b b o t t @ u k . i b m . c o m P l e a s ee n t e rt h ef o l l o w i n g' e x t r a 'a t t r i b u t e s t ob es e n tw i t hy o u rc e r t i f i c a t er e q u e s t Ac h a l l e n g ep a s s w o r d[ ] : q w e r t y A no p t i o n a lc o m p a n yn a m e[ ] : R e q u e s t( a n dp r i v a t ek e y )i si nn e w r e q . p e m

Signing a request Executing the C As i g ncommand signs a request using the private key of the root CA, held in private/cakey.pem. The request needs to be in a file called newreq.pem, and the generated certificate is written to a file called newcert.pem; both files are in the current directory. Listing 3 shows a sample request-signing dialog. Listing 3. Example -sign dialog
$C A . s hs i g n U s i n gc o n f i g u r a t i o nf r o mC : / P R O G R A ~ 1 / M K S T O O ~ 1 / e t c / o p e n s s l / o p e n s s l . c n f L o a d i n g' s c r e e n 'i n t or a n d o ms t a t e-d o n e E n t e rP E Mp a s sp h r a s e : C h e c kt h a tt h er e q u e s tm a t c h e st h es i g n a t u r e S i g n a t u r eo k T h eS u b j e c t sD i s t i n g u i s h e dN a m ei sa sf o l l o w s c o u n t r y N a m e : P R I N T A B L E : ' U K ' s t a t e O r P r o v i n c e N a m e : P R I N T A B L E : ' H a m p s h i r e ' l o c a l i t y N a m e : P R I N T A B L E : ' W i n c h e s t e r '

www.ibm.com/developerworks/library/j-certgen/

3/11

01/09/2013

Generate certificate chains for testing Java applications

o r g a n i z a t i o n N a m e : P R I N T A B L E : ' I B MU KL t d ' o r g a n i z a t i o n a l U n i t N a m e : P R I N T A B L E : ' J E T ' c o m m o n N a m e : P R I N T A B L E : ' P a u lA b b o t t ' e m a i l A d d r e s s : I A 5 S T R I N G : ' P a u l _ H _ A b b o t t @ u k . i b m . c o m ' C e r t i f i c a t ei st ob ec e r t i f i e du n t i lJ u n2 22 0 : 5 0 : 5 52 0 0 5G M T( 3 6 5d a y s ) S i g nt h ec e r t i f i c a t e ?[ y / n ] : y

1o u to f1c e r t i f i c a t er e q u e s t sc e r t i f i e d ,c o m m i t ?[ y / n ] y W r i t eo u td a t a b a s ew i t h1n e we n t r i e s D a t aB a s eU p d a t e d C e r t i f i c a t e : D a t a : V e r s i o n :3( 0 x 2 ) S e r i a lN u m b e r :2( 0 x 2 ) S i g n a t u r eA l g o r i t h m :m d 5 W i t h R S A E n c r y p t i o n I s s u e r :C = U K ,S T = H a m p s h i r e ,L = W i n c h e s t e r ,O = I B MU KL t d , O U = J T C ,C N = P a u lHA b b o t t / E m a i l = P a u l _ H _ A b b o t t @ u k . i b m . c o m V a l i d i t y N o tB e f o r e :J u n2 22 0 : 5 0 : 5 52 0 0 4G M T N o tA f t e r:J u n2 22 0 : 5 0 : 5 52 0 0 5G M T S u b j e c t :C = U K ,S T = H a m p s h i r e ,L = W i n c h e s t e r ,O = I B MU kL t d , O U = J E T ,C N = P a u lA b b o t t / E m a i l = P a u l _ H _ A b b o t t @ u k . i b m . c o m S u b j e c tP u b l i cK e yI n f o : P u b l i cK e yA l g o r i t h m :r s a E n c r y p t i o n R S AP u b l i cK e y :( 1 0 2 4b i t ) M o d u l u s( 1 0 2 4b i t ) : 0 0 : e 2 : b 5 : 9 0 : 5 0 : e 5 : d d : 7 c : 7 9 : c 3 : 4 9 : a 5 : c 9 : e e : 2 9 : 3 a : d a : 1 d : 8 b : 6 a : b 0 : 0 b : a 0 : a 1 : c f : 7 9 : f c : b e : 5 0 : d b : c b : 3 7 : b 7 : 5 4 : 0 0 : b b : 6 e : 7 4 : e 6 : a 4 : 1 1 : d 4 : c 6 : 5 a : 0 2 : 4 6 : 3 b : b 4 : 3 3 : 7 2 : 9 7 : 5 b : c f : 9 d : 9 a : 3 2 : 9 b : e 6 : 3 4 : e 9 : 4 b : 3 0 : 4 e : b 6 : 6 8 : 5 5 : 8 a : 3 f : 8 0 : f 3 : 5 e : c 9 : 6 3 : c c : 4 e : c 2 : c 0 : c 3 : 3 4 : 2 f : 9 3 : 9 f : f a : c a : 1 b : 4 4 : f 5 : c 8 : 8 7 : e c : 1 d : 1 2 : a 9 : 8 c : 3 a : b 9 : 2 8 : 8 3 : 4 d : b 5 : 1 8 : f f : 3 4 : 3 a : a 9 : e 7 : 7 e : 4 e : c 4 : 2 1 : 8 e : 5 6 : e 7 : d c : f 5 : 0 7 : 4 6 : 3 9 : c 8 : d 8 : f f : 0 0 : d 3 : 8 7 : 2 0 : 2 e : 0 6 : 1 8 : 1 9 E x p o n e n t :6 5 5 3 7( 0 x 1 0 0 0 1 ) X 5 0 9 v 3e x t e n s i o n s : X 5 0 9 v 3B a s i cC o n s t r a i n t s : C A : F A L S E N e t s c a p eC o m m e n t : O p e n S S LG e n e r a t e dC e r t i f i c a t e X 5 0 9 v 3S u b j e c tK e yI d e n t i f i e r : F A : 6 5 : 4 4 : F B : 3 A : E 6 : 1 5 : 1 F : C 5 : 4 0 : 6 C : E B : F 4 : D A : A B : B 9 : C D : F 2 : 2 D : 5 4 X 5 0 9 v 3A u t h o r i t yK e yI d e n t i f i e r : k e y i d : 9 3 : 8 C : B 4 : F 0 : 3 0 : 9 5 : 7 7 : 5 9 : 2 E : A 1 : 3 B : 0 C : A 5 : 3 A : C 6 : 9 2 : F A : 1 6 : 3 1 : 6 D D i r N a m e : / C = U K / S T = H a m p s h i r e / L = W i n c h e s t e r / O = I B MU KL t d / O U = J T C / C N = P a u lHA b b o t t / E m a i l = P a u l _ H _ A b b o t t @ u k . i b m . c o m s e r i a l : 0 0 S i g n a t u r eA l g o r i t h m :m d 5 W i t h R S A E n c r y p t i o n 2 7 : 4 3 : 6 d : 8 9 : c 3 : 6 1 : d 4 : a f : 3 e : d c : 5 5 : a 3 : 9 a : a 7 : 7 d : 6 6 : 4 e : 2 9 : 2 e : 4 3 : f 0 : 9 0 : c 6 : 9 c : 0 f : 6 2 : 2 4 : b 2 : 4 c : 9 e : 2 c : f 7 : d 7 : 8 4 : c e : 7 f : b 6 : c 8 : 0 9 : 3 d : b 4 : 8 0 : c 8 : 2 6 : 9 a : a 8 : 6 b : 2 f : d f : 8 f : e 3 : 8 b : 8 0 : f 5 : 1 0 : 2 8 : 8 0 : 2 8 : 5 e : 9 4 : 5 5 : b e : 6 1 : e 5 : 1 8 : 4 e : d 4 : a 8 : c 2 : 9 e : 6 d : 9 b : 5 2 : 6 4 : 9 4 : 3 3 : b 3 : a 5 : 6 8 : 7 9 : e 2 : 8 5 : 8 6 : 0 1 : e 6 : a a : 0 f : 1 e : 5 4 : 2 d : 8 0 : b 1 : 3 7 : 3 8 : 6 6 : c c : 0 9 : 9 a : 0 e : 3 0 : 0 a : e 8 : b 9 : 0 0 : 7 d : d a : a 2 : a 1 : b b : 3 c : 8 3 : 3 7 : 2 f : 1 6 : 6 a : 5 d : 8 4 : 2 5 : 6 6 : 2 3 : d 2 : 6 7 : a 9 : 0 2 : a 4 : 3 3 : 9 6 : 5 6 B E G I NC E R T I F I C A T E M I I D 0 j C C A z u g A w I B A g I B A j A N B g k q h k i G 9 w 0 B A Q Q F A D C B m j E L M A k G A 1 U E B h M C V U s x E j A Q B g N V B A g T C U h h b X B z a G l y Z T E T M B E G A 1 U E B x M K V 2 l u Y 2 h l c 3 R l c j E T M B E G A 1 U E C h M K S U J N I F V L I E x 0 Z D E M M A o G A 1 U E C x M D S l R D M R Y w F A Y D V Q Q D E w 1 Q Y X V s I E g g Q W J i b 3 R 0 M S c w J Q Y J K o Z I h v c N A Q k B F h h Q Y X V s X 0 h f Q W J i b 3 R 0 Q H V r L m l i b S 5 j b 2 0 w H h c N M D Q w N j I y M j A 1 M D U 1 W h c N M D U w N j I y M j A 1 M D U 1 W j C B m D E L M A k G A 1 U E B h M C V U s x E j A Q B g N V B A g T C U h h b X B z a G l y Z T E T M B E G A 1 U E B x M K V 2 l u Y 2 h l c 3 R l c j E T M B E G A 1 U E C h M K S U J N I F V r I E x 0 Z D E M M A o G A 1 U E C x M D S k V U M R Q w E g Y D V Q Q D E w t Q Y X V s I E F i Y m 9 0 d D E n M C U G C S q G S I b 3 D Q E J A R Y Y U G F 1 b F 9 I X 0 F i Y m 9 0 d E B 1 a y 5 p Y m 0 u Y 2 9 t M I G f M A 0 G C S q G S I b 3 D Q E B A Q U A A 4 G N A D C B i Q K B g Q D i t Z B Q 5 d 1 8 e c N J p c n u K T r a H Y t q s A u g o c 9 5 / L 5 Q 2 8 s 3 t 1 Q A u 2 5 0 5 q Q R 1 M Z a A k Y 7 t D N y l 1 v P n Z o y m + Y 0 6 U s w T r Z o V Y o / g P N e y W P M T s L A w z Q v k 5 / 6 y h t E 9 c i H 7 B 0 S q Y w 6 u S i D T b U Y / z Q 6 q e d + T s Q h j l b n 3 P U H R j n I 2 P 8 A 0 4 c g L g Y Y G Q I D A Q A B o 4 I B J j C C A S I w C Q Y D V R 0 T B A I w A D A s B g l g h k g B h v h C A Q 0 E H x Y d T 3 B l b l N T T C B H Z W 5 l c m F 0 Z W Q g Q 2 V y d G l m a W N h d G U w H Q Y D V R 0 O B B Y E F P p l R P s 6 5 h U f x U B s 6 / T a q 7 n N 8 i 1 U M I H H B g N V H S M E g b 8 w g b y A F J O M t P A w l X d Z L q E 7 D K U 6 x p L 6 F j F t o Y G g p I G d M I G a M Q s w C Q Y D V Q Q G E w J V S z E S M B A G A 1 U E C B M J S G F t c H N o a X J l M R M w E Q Y D V Q Q H E w p X a W 5 j a G V z d G V y M R M w E Q Y D V Q Q K E w p J Q k 0 g V U s g T H R k M Q w w C g Y D V Q Q L E w N K V E M x F j A U B g N V B A M T D V B h d W w g S C B B Y m J v d H Q x J z A l B g k q h k i G 9 w 0 B C Q E W G F B h d W x f S F 9 B Y m J v d H R A d W s u a W J t L m N v b Y I B A D A N B g k q h k i G 9 w 0 B A Q Q F A A O B g Q A n Q 2 2 J w 2 H U r z 7 c V a O a p 3 1 m T i k u Q / C Q x p w P Y i S y T J 4 s 9 9 e E z n + 2 y A k 9 t I D I J p q o a y / f j + O L g P U Q K I A o X p R V v m H l G E 7 U q M K e b Z t S Z J Q z s 6 V o e e K F h g H m q g 8 e V C 2 A s T c 4 Z s w J m g 4 w C u i 5 A H 3 a o q G 7 P I M 3 L x Z q X Y Q l Z i P S Z 6 k C p D O W V g = = E N DC E R T I F I C A T E -

www.ibm.com/developerworks/library/j-certgen/

4/11

01/09/2013
S i g n e dc e r t i f i c a t ei si nn e w c e r t . p e m

Generate certificate chains for testing Java applications

Note that the PEM password requested during the dialog is the password used to encrypt the private key of the root CA -- not the user's private key. An examination of the demoCA directory, after signing, shows that the index.txt and serial files have been updated. The generated public key has also been placed in demoCA/newcert/ directory under a filename that reflects its serial number -- for example, 01.pem. At this point, you have a user certificate, a user private key, and a root certificate. If this is all you need, you're done. Read on if you want to find out how to control the contents of the certificate or create a three- (or more) certificate chain. Under the covers of CA.sh So far, I've shown how to use the CA.sh shell script to simplify the process of certificate generation. But under the covers, CA.sh uses the OpenSSL command to perform all necessary key generation and signing operations. You control OpenSSL through the openssl.cnf configuration file. I'll discuss some of the sections in this file and the CA.sh operations that they affect. CA -newca The C An e w c aoperation, as I mentioned earlier, creates the necessary directory structure for the CA:
d i r c e r t s c r l _ d i r d a t a b a s e n e w _ c e r t s _ d i r c e r t i f i c a t e s e r i a l c r l p r i v a t e _ k e y =. / d e m o C A =$ d i r / c e r t s =$ d i r / c r l =$ d i r / i n d e x . t x t =$ d i r / n e w c e r t s #W h e r ee v e r y t h i n gi sk e p t #W h e r et h ei s s u e dc e r t sa r ek e p t #W h e r et h ei s s u e dc r la r ek e p t #d a t a b a s ei n d e xf i l e . #d e f a u l tp l a c ef o rn e wc e r t s .

=$ d i r / c a c e r t . p e m #T h eC Ac e r t i f i c a t e =$ d i r / s e r i a l #T h ec u r r e n ts e r i a ln u m b e r =$ d i r / c r l . p e m #T h ec u r r e n tC R L =$ d i r / p r i v a t e / c a k e y . p e m #T h ep r i v a t ek e y

This operation creates the directories defined in the CA_default section of openssl.cnf, although CA.sh does not make direct reference to the configuration file. If you want to use a different structure, you need to modify openssl.cnf and manually create the required directories. Alternatively, you can modify CA.sh to create the directories. The following call to OpenSSL generates a root certificate:
o p e n s s lr e qn e wx 5 0 9k e y o u t. / d e m o C A / p r i v a t e / c a k e y . p e m o u t. / d e m o C A / c a c e r t . p e md a y s3 6 5

The d a y sflag sets the lifespan of the root certificate to 365 days (one year), which overrides any default value specified in openssl.cnf. CA -newreq The r e qsection of openssl.cnf controls generation of a new certificate request, as shown in the example below:
[r e q] d e f a u l t _ b i t s d e f a u l t _ k e y f i l e d i s t i n g u i s h e d _ n a m e a t t r i b u t e s

=1 0 2 4 =p r i v k e y . p e m =r e q _ d i s t i n g u i s h e d _ n a m e =r e q _ a t t r i b u t e s

The first line determines the length of the generated key pair, and the second determines the default destination for the generated private key. The next two lines refer to other sections in the same file. The r e q _ d i s t i n g u i s h e d _ n a m esection defines the components of the DN to be placed in the certificate request, and these components ultimately go into the signed certificate:
[r e q _ d i s t i n g u i s h e d _ n a m e] c o u n t r y N a m e c o u n t r y N a m e _ d e f a u l t c o u n t r y N a m e _ m i n c o u n t r y N a m e _ m a x

=C o u n t r yN a m e( 2l e t t e rc o d e ) =A U =2 =2

www.ibm.com/developerworks/library/j-certgen/

5/11

01/09/2013
s t a t e O r P r o v i n c e N a m e l o c a l i t y N a m e o r g a n i z a t i o n N a m e o r g a n i z a t i o n a l U n i t N a m e c o m m o n N a m e e m a i l A d d r e s s

Generate certificate chains for testing Java applications


=S t a t eo rP r o v i n c eN a m e( f u l ln a m e ) =L o c a l i t yN a m e( e . g . ,c i t y ) =O r g a n i z a t i o nN a m e( e . g . ,c o m p a n y ) =O r g a n i z a t i o n a lU n i tN a m e( e . g . ,s e c t i o n ) =C o m m o nN a m e( e . g . ,Y O U Rn a m e ) =E m a i lA d d r e s s

Each part of this section consists of up to four values, as shown in lines two through five of the example: The message prompt printed to the screen A default value (suffix of _ d e f a u l t ) The minimum number of characters the user is allowed to type (suffix of _ m i n ) The maximum number of characters the user is allowed to enter (suffix of _ m a x ) A number of other options are available. They include defining your own object identifier (OID) types and having the distinguished name values defined in the configuration file rather than supplied by the user, which could be useful for batch operation. The r e q _ a t t r i b u t e ssection lets you define the minimum and maximum lengths of the encryption password, among other things. The OpenSSL call for the new-request operation is:
o p e n s s lr e qn e wk e y o u tn e w r e q . p e mo u tn e w r e q . p e md a y s3 6 5

Note that the k e y o u toption overrides the d e f a u l t _ k e y f i l eline in the configuration file. Again, the lifespan of the generatedcertificate is set to 365 days. If you compare this line to the one that generates the root certificate, you'll see only one difference: x 5 0 9 . This tells OpenSSL that you want a self-signed certificate. You can find more information on how to use OpenSSL to generate signing requests in the r e qsection (see Resources) of the OpenSSL documentation. CA -sign The sign operation, like the request-generation operation, uses a call to OpenSSL to sign the certificate:
o p e n s s lc ap o l i c yp o l i c y _ a n y t h i n go u tn e w c e r t . p e m i n f i l e sn e w r e q . p e m

It's interesting to note that the OpenSSL command is c arather than s i g n , and that the private key used for signing isn't specified. The openssl.cnf configuration file defines this information. The relevant section is C A _ d e f a u l t , although you can use the n a m eoption to override this. The p o l i c yparameter specifies the policy section in the configuration file. You use this section to specify which fields must be present in the DN request and which are optional. I typically use this configuration:
[p o l i c y _ a n y t h i n g] c o u n t r y N a m e s t a t e O r P r o v i n c e N a m e l o c a l i t y N a m e o r g a n i z a t i o n N a m e o r g a n i z a t i o n a l U n i t N a m e c o m m o n N a m e e m a i l A d d r e s s

=o p t i o n a l =o p t i o n a l =o p t i o n a l =o p t i o n a l =o p t i o n a l =s u p p l i e d =o p t i o n a l

As you can see, this configuration requires very little by default. It's worth noting that the documentation states that a DN field not listed here will be silently deleted from a signed request. Sign a third certificate The Create a root certificate and Generate a user certificate sections of this article showed how to use the CA.sh shell script to create root certificates and then sign a user certificate using the root certificate. This process is sufficient if you want to have a two-certificate chain, but what if you need three or more certificates in a chain? The answer is that you use the OpenSSL command directly. Now that you know how the CA.sh script works, you can use this knowledge to create a three-certificate chain. The steps are similar to those for creating a two-certificate chain. For simplicity, you'll reuse the CA certificate/private key and the user request from the earlier example.
www.ibm.com/developerworks/library/j-certgen/ 6/11

01/09/2013

Generate certificate chains for testing Java applications

Begin by renaming the existing user-request file to userRequest1.pem. Then create a new user certificate, which you'll call userCert1. The existing user certificate is not flagged as a CA certificate and can't be used for signing other certificates. (This becomes apparent only when you try to verify a certificate chain containing such a certificate.) You need to add the following section, which allows the certificate to be used for certificate signing, to your configuration file:
[m y _ v 3 _ e x t] b a s i c C o n s t r a i n t s=C A : t r u e

Use the following command to sign the certificate:


o p e n s s lc ap o l i c yp o l i c y _ a n y t h i n ge x t e n s i o n sm y _ v 3 _ e x t o u tu s e r C e r t 1 . p e mi n f i l e su s e r R e q u e s t 1 . p e m

Next, generate a new certificate request for the third certificate. You can either use the C An e w r e qcommand to do this, or you can use the O p e n S S Lcommand directly. Once you have the request file, you need to sign it. As I mentioned earlier, the private key used to sign the request is specified by default in the configuration file, not the o p e n s s lc acommand line. But the o p e n s s lc acommand can include a k e y f i l eoption that lets you override this default. The following command signs use your second certificate to sign the third certificate:
o p e n s s lc ap o l i c yp o l i c y _ a n y t h i n gk e y f i l eu s e r R e q u e s t 1 . p e m c e r tu s e r C e r t 1 . p e mo u tu s e r C e r t 2 . p e mi n f i l e sn e w r e q . p e m

Note that the c e r tparameter specifies the signer's certificate, overriding the default in the configuration file. Invoking the command without it will result in an error because OpenSSL checks that the private key and certificate of the signer match. Assuming that all has gone well, you should have userCert1.pem and userCert2.pem files in the current directory and the root CA certificate file in the demoCA/private directory. These three files constitute the certificate chain. If you're working on Windows, you should be able to install the certificates by simply renaming the files to give them a .cer extension and double-click on them from a file browser. Additional options You might find the following OpenSSL command-line options useful: -startdate, -enddate & -days allow you to specify the validity period of the generated certificate. By default, a certificate is valid for one year from the current date. -notext lets you switch off the textual representation of the generated certificate that appears before the binary-encoded version. The textual version is often not needed. -key is the password used to encrypt the private key. This is useful if you want to call OpenSSL from a batch file. -batch tells OpenSSL to work in batch mode, with no user prompts. -extfile specifies a file containing certificate extensions. Load certificates into a Java keystore A Java keystore is a repository of private keys and the associated X.509 certificate chains authenticating the corresponding public keys. Getting the certificate chain into a Java keystore requires a simple Java program. The code fragment in Listing 4 reads the three-certificate chain you've created into an existing keystore: Listing 4. Loading a certificate chain into a Java keystore
/ /T h ek e y s t o r et oa d dt h ec e r t i f i c a t e st o F i l e I n p u t S t r e a mk s i s=n e wF i l e I n p u t S t r e a m ( " . k e y s t o r e " ) ; / /T h en e wk e y s t o r ew i t ht h ec e r t i f i c a t e sa d d e d F i l e O u t p u t S t r e a mk s o s=n e wF i l e O u t p u t S t r e a m ( " . n e w k e y s t o r e " ) ; / /T h ec e r t i f i c a t ef i l e s ,t ob ea d d e dt ok e y s t o r e F i l e I n p u t S t r e a mc e r t F i l e 1=n e wF i l e I n p u t S t r e a m ( " c a c e r t . c e r " ) ; F i l e I n p u t S t r e a mc e r t F i l e 2=n e wF i l e I n p u t S t r e a m ( " u s e r C e r t 1 . c e r " ) ; F i l e I n p u t S t r e a mc e r t F i l e 3=n e wF i l e I n p u t S t r e a m ( " u s e r C e r t 2 . c e r " ) ; C e r t i f i c a t e F a c t o r yc f=C e r t i f i c a t e F a c t o r y . g e t I n s t a n c e ( " X . 5 0 9 " ) ;

www.ibm.com/developerworks/library/j-certgen/

7/11

01/09/2013

Generate certificate chains for testing Java applications

/ /R e a dt h e3c e r t i f i c a t e si n t om e m o r y C e r t i f i c a t ec e r t 1=c f . g e n e r a t e C e r t i f i c a t e ( c e r t F i l e 1 ) ; C e r t i f i c a t ec e r t 2=c f . g e n e r a t e C e r t i f i c a t e ( c e r t F i l e 2 ) ; C e r t i f i c a t ec e r t 3=c f . g e n e r a t e C e r t i f i c a t e ( c e r t F i l e 3 ) ; / /R e a dt h ek e y s t o r ef i l e ,t y p e = " j k s " K e y S t o r ek s=K e y S t o r e . g e t I n s t a n c e ( " j k s " ) ; c h a r [ ]p a s s w o r d=" p a s s w o r d " . t o C h a r A r r a y ( ) ; k s . l o a d ( k s i s ,p a s s w o r d ) ; / /A d dc e r t i f i c a t e st ok e y s t o r e k s . s e t C e r t i f i c a t e E n t r y ( " M Y R O O T _ A L I A S " ,c e r t 1 ) ; k s . s e t C e r t i f i c a t e E n t r y ( " M Y U S E R 1 _ A L I A S " ,c e r t 2 ) ; k s . s e t C e r t i f i c a t e E n t r y ( " M Y U S E R 2 _ A L I A S " ,c e r t 3 ) ; / /W r i t em o d i f i e dk e y s t o r et of i l es y s t e m k s . s t o r e ( k s o s ,p a s s w o r d ) ; k s o s . c l o s e ( ) ;

In Listing 4, the password for the keystore is p a s s w o r dand the keystore type is j k s . Once the certificates are in the keystore, any Java application that's configured to access the keystore can use them. Sun's JDK documentation includes information on how to do this. Conclusion As you've probably realized by now, OpenSSL can also be used to implement a very simple CA and to issue certificates. You might decide to use it to obtain your certificate chains once your test application goes live. However, this approach carries with it the responsibility for verifying the origin of any certificate request and managing issued certificates. Alternatively, you might decide to use an external CA, such as thawte or VeriSign, to issue certificates -- an approach that has its own pro and cons. This article has shown how to create a certificate chain for testing Java applications that use PKI-based security. To maintain this tight focus, I haven't touched on related areas that might be of interest, such as using the generated certificate chains in other applications (such as e-mail), how to use a generated certificate chain in other languages, and certificate revocation lists. If you're interested in exploring these and other areas in the complex topic of network security and digital certificates, check out some of the excellent sources of information in the Resources section.

Resources Download the latest version of the OpenSSL source code. The OpenSSL Documentation is the main documentation for the OpenSSL command. It's not complete but is still quite useful. If you use Windows but don't have MKS, you can download the necessary OpenSSL binaries from SourceForge. Introduction to Public Key Infrastructure offers a basic introduction to PKI. Digital Certificates: Applied Internet Security (Addison-Wesley Professional; 1998) by Jalal Feghhi and Peter Williams describes digital certificates and their use, mainly from a Windows point of view. Java 2 Network Security, 2nd Edition (Prentice Hall PTR; 1999} by Marco Pistoia et al., provides useful background on Java 2 security. Security expert Brad Rubin wrote this comprehensive two-part introductory tutorial covering Java security. Part 1, Crypto basics (developerWorks, July 2002) guides you through the basics of cryptography and how it is implemented in the Java programming language, and Part 2, Authentication and authorization (developerWorks, July 2002) introduces the basic concepts of authentication and authorization and provides an architectural overview of JAAS. The Internet Engineering Task Force (IETF) Request for Comment (RFC) 2459 profiles the X.509 v3 certificate and X.509 v2 certificate revocation list (CRL) for use in the Internet. IETF RFC 1617 provides naming and structuring guidelines for X.500 directory pilots. "The evolution of Java security" (developerWorks, June 2002) provides a high-level overview of the development and evolution of Java security. The PKI page contains a long list of certification authorities that issue digital certificates. KeyMan from IBM alphaWorks is a tool for managing keys, certificates, CRLs, and the respective repositories for storing and retrieving these items.
www.ibm.com/developerworks/library/j-certgen/ 8/11

01/09/2013

Generate certificate chains for testing Java applications

Find hundreds more Java technology resources on the developerWorks Java technology zone. Browse for books on these and other technical topics. About the author Paul H. Abbott has worked in the IBM Java Technology Centre for more than seven years, porting Sun's JVMs to a range of IBM platforms. He has been responsible for the integration of IBM security code in these JVMs for the last two years. He recently moved to J2EE/JMS development but maintains a strong interest in Java security and moderates the developerWorks Java zone's Java security forum. Close [x]

developerWorks: Sign in
IBM ID: Need an IBM ID? Forgot your IBM ID? Password: Forgot your password? Change your password Keep me signed in. By clicking Submit, you agree to the developerWorks terms of use.
Submit Cancel

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time. All information submitted is secure. Close [x]

Choose your display name


The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks. Please choose a display name between 3-31 characters . Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons. Display name: (Must be between 3 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.


Submit Cancel

All information submitted is secure. Average rating (91 votes) 1 star 2 stars 3 stars 4 stars 5 stars
Submit

1 star 2 stars 3 stars 4 stars 5 stars

www.ibm.com/developerworks/library/j-certgen/

9/11

01/09/2013

Generate certificate chains for testing Java applications

Add comment: Sign in or register to leave a comment. Note: HTML elements are not supported within comments.

Notify me when a comment is added1000 characters left

Post

Total comments (4) hi my name is Esad Bekic. Posted by mirsoornamenti on 11 October 2012 Report abuse When I tried to create the intermediary certificate (userCert1.pem), I got this error: failed to update database TXT_DB error number 2 This was because the "newcert.pem" was too similar to the one I was trying to create. I revoked "newcert.pem" (openssl ca -revoke <filename>) and deleted it, and then everything worked fine. Posted by haughki on 04 September 2012 Report abuse Figured it out: openssl ca -policy policy_anything -keyfile userRequest1.pem -cert userCert1.pem -out userCert2.pem -infiles newreq.pem Should be openssl ca -policy policy_anything -keyfile newkey.pem -cert userCert1.pem -out userCert2.pem -infiles newreq.pem Posted by NicholasDiPiazza on 30 August 2011 Report abuse Upon creating the third certificate... I get this error: I get this error: Using configuration from /usr/lib/ssl/openssl.cnf unable to load CA private key 3648:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:647:Expecting: ANY PRIVATE KEY <p /> Anyone know why this is? <p /> Why is that? Posted by NicholasDiPiazza on 29 August 2011 Report abuse
Print this page Share this page Follow developerWorks

www.ibm.com/developerworks/library/j-certgen/

10/11

01/09/2013
About Help Contact us Submit content Feeds

Generate certificate chains for testing Java applications


Report abuse Terms of use IBM privacy IBM accessibility Faculty Students Business Partners

www.ibm.com/developerworks/library/j-certgen/

11/11

You might also like