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;
032import java.util.Hashtable;
033
034import javax.naming.Context;
035import javax.security.auth.x500.X500Principal;
036
037import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
038import org.apache.directory.server.core.api.DirectoryService;
039import org.apache.directory.server.core.security.CertificateUtil;
040import org.apache.directory.server.ldap.LdapServer;
041
042
043/**
044 * An abstract class created to hold common elements.
045 *
046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047 */
048public abstract class AbstractLdapTestUnit
049{
050    /** The class DirectoryService instance */
051    public static DirectoryService classDirectoryService;
052
053    /** The test DirectoryService instance */
054    public static DirectoryService methodDirectoryService;
055
056    /** The current DirectoryService instance */
057    public static DirectoryService directoryService;
058
059    /** The class LdapServer instance */
060    public static LdapServer classLdapServer;
061
062    /** The test LdapServer instance */
063    public static LdapServer methodLdapServer;
064
065    /** The current LdapServer instance */
066    public static LdapServer ldapServer;
067
068    /** The Ldap connection template */
069    public static LdapConnectionTemplate ldapConnectionTemplate;
070    
071    /** The current revision */
072    public static long revision = 0L;
073
074    public DirectoryService getService()
075    {
076        return directoryService;
077    }
078
079
080    public void setService( DirectoryService directoryService )
081    {
082        AbstractLdapTestUnit.directoryService = directoryService;
083    }
084
085    public LdapServer getLdapServer()
086    {
087        return ldapServer;
088    }
089
090
091    public void setLdapServer( LdapServer ldapServer )
092    {
093        AbstractLdapTestUnit.ldapServer = ldapServer;
094    }
095    
096    
097    public void changeCertificate( String keyStoreFile, String password, String issuerDn, String subjectDn, int days, String algorithm ) 
098        throws IOException, GeneralSecurityException
099    {
100        KeyStore keyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
101        char[] keyStorePassword = password.toCharArray();
102        
103        try ( InputStream keyStoreData = new FileInputStream( keyStoreFile ) )
104        {
105            keyStore.load( null, keyStorePassword );
106        }
107        
108        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance( "EC" );
109        KeyPair keyPair = keyPairGenerator.generateKeyPair();
110        
111        // Generate the subject's name
112        X500Principal subject = new X500Principal( "CN=" + subjectDn + ",OU=directory,O=apache,C=US" );
113        
114        // Generate the issuer's name
115        X500Principal issuer = new X500Principal( "CN=" + issuerDn + ",OU=directory,O=apache,C=US" );
116
117        // Create the self-signed certificate
118        X509Certificate certificate = CertificateUtil.generateCertificate( subject, issuer, keyPair, days, algorithm );
119        
120        keyStore.setKeyEntry( "apachedsKey", keyPair.getPrivate(), keyStorePassword, new X509Certificate[] { certificate } );
121        
122        try ( FileOutputStream out = new FileOutputStream( keyStoreFile ) )
123        {
124            keyStore.store( out, keyStorePassword );
125        }
126    }
127    
128    
129    protected Hashtable<String, Object> setDefaultJNDIEnv()
130    {
131        return setDefaultJNDIEnv( "com.sun.jndi.ldap.LdapCtxFactory" );
132    }
133    
134    
135    protected Hashtable<String, Object> setDefaultJNDIEnv( String factoryName )
136    {
137        Hashtable<String, Object> env = new Hashtable<String, Object>();
138        
139        env.put( DirectoryService.JNDI_KEY, getService() );
140        env.put( Context.PROVIDER_URL, "" );
141        env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
142        env.put( Context.SECURITY_CREDENTIALS, "secret" );
143        env.put( Context.SECURITY_AUTHENTICATION, "simple" );
144        env.put( Context.INITIAL_CONTEXT_FACTORY, factoryName );
145
146        return env;
147    }
148}