View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.server.core.integ;
21  
22  
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.security.GeneralSecurityException;
28  import java.security.KeyPair;
29  import java.security.KeyPairGenerator;
30  import java.security.KeyStore;
31  import java.security.cert.X509Certificate;
32  
33  import org.apache.directory.server.core.api.DirectoryService;
34  import org.apache.directory.server.core.security.CertificateUtil;
35  import org.apache.directory.server.kerberos.kdc.KdcServer;
36  import org.apache.directory.server.ldap.LdapServer;
37  
38  import sun.security.x509.X500Name;
39  
40  
41  /**
42   * An abstract class created to hold common elements.
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  @SuppressWarnings("restriction")
47  public abstract class AbstractLdapTestUnit
48  {
49      /** The used DirectoryService instance */
50      public static DirectoryService service;
51  
52      /** The used LdapServer instance */
53      public static LdapServer ldapServer;
54  
55      /** The used KdcServer instance */
56      public static KdcServer kdcServer;
57  
58      public static DirectoryService getService()
59      {
60          return service;
61      }
62  
63  
64      public static void setService( DirectoryService service )
65      {
66          AbstractLdapTestUnit.service = service;
67      }
68  
69  
70      public static LdapServer getLdapServer()
71      {
72          return ldapServer;
73      }
74  
75  
76      public static void setLdapServer( LdapServer ldapServer )
77      {
78          AbstractLdapTestUnit.ldapServer = ldapServer;
79      }
80  
81  
82      public static KdcServer getKdcServer()
83      {
84          return kdcServer;
85      }
86  
87  
88      public static void setKdcServer( KdcServer kdcServer )
89      {
90          AbstractLdapTestUnit.kdcServer = kdcServer;
91      }
92      
93      
94      public void changeCertificate( String keyStoreFile, String password, String issuerDn, String subjectDn, int days, String algorithm ) 
95          throws IOException, GeneralSecurityException
96      {
97          KeyStore keyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
98          char[] keyStorePassword = password.toCharArray();
99          
100         try ( InputStream keyStoreData = new FileInputStream( keyStoreFile ) )
101         {
102             keyStore.load( null, keyStorePassword );
103         }
104         
105         KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance( "EC" );
106         KeyPair keyPair = keyPairGenerator.generateKeyPair();
107         
108         // Generate the subject's name
109         X500Name subject = new X500Name( subjectDn, "directory", "apache", "US" );
110         
111         // Generate the issuer's name
112         X500Name issuer = new X500Name( issuerDn, "directory", "apache", "US" );
113 
114         // Create the self-signed certificate
115         X509Certificate certificate = CertificateUtil.generateCertificate( subject, issuer, keyPair, days, algorithm );
116         
117         keyStore.setKeyEntry( "apachedsKey", keyPair.getPrivate(), keyStorePassword, new X509Certificate[] { certificate } );
118         
119         try ( FileOutputStream out = new FileOutputStream( keyStoreFile ) )
120         {
121             keyStore.store( out, keyStorePassword );
122         }
123     }
124 }