001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.server.core.integ;
021
022
023import java.io.FileInputStream;
024import java.io.FileOutputStream;
025import java.io.IOException;
026import java.io.InputStream;
027import java.security.GeneralSecurityException;
028import java.security.KeyPair;
029import java.security.KeyPairGenerator;
030import java.security.KeyStore;
031import java.security.cert.X509Certificate;
032
033import org.apache.directory.server.core.api.DirectoryService;
034import org.apache.directory.server.core.security.CertificateUtil;
035import org.apache.directory.server.kerberos.kdc.KdcServer;
036import org.apache.directory.server.ldap.LdapServer;
037
038import sun.security.x509.X500Name;
039
040
041/**
042 * An abstract class created to hold common elements.
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046@SuppressWarnings("restriction")
047public abstract class AbstractLdapTestUnit
048{
049    /** The used DirectoryService instance */
050    public static DirectoryService service;
051
052    /** The used LdapServer instance */
053    public static LdapServer ldapServer;
054
055    /** The used KdcServer instance */
056    public static KdcServer kdcServer;
057
058    public static DirectoryService getService()
059    {
060        return service;
061    }
062
063
064    public static void setService( DirectoryService service )
065    {
066        AbstractLdapTestUnit.service = service;
067    }
068
069
070    public static LdapServer getLdapServer()
071    {
072        return ldapServer;
073    }
074
075
076    public static void setLdapServer( LdapServer ldapServer )
077    {
078        AbstractLdapTestUnit.ldapServer = ldapServer;
079    }
080
081
082    public static KdcServer getKdcServer()
083    {
084        return kdcServer;
085    }
086
087
088    public static void setKdcServer( KdcServer kdcServer )
089    {
090        AbstractLdapTestUnit.kdcServer = kdcServer;
091    }
092    
093    
094    public void changeCertificate( String keyStoreFile, String password, String issuerDn, String subjectDn, int days, String algorithm ) 
095        throws IOException, GeneralSecurityException
096    {
097        KeyStore keyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
098        char[] keyStorePassword = password.toCharArray();
099        
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}