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.ldap.handlers.extended;
021
022
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.Set;
026
027import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequest;
028import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationResponse;
029import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationResponseImpl;
030import org.apache.directory.api.ldap.model.entry.Entry;
031import org.apache.directory.api.ldap.model.name.Dn;
032import org.apache.directory.server.core.api.entry.ClonedServerEntry;
033import org.apache.directory.server.core.security.TlsKeyGenerator;
034import org.apache.directory.server.ldap.ExtendedOperationHandler;
035import org.apache.directory.server.ldap.LdapServer;
036import org.apache.directory.server.ldap.LdapSession;
037
038
039/**
040 * An extended handler for digital certificate generation
041 * 
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class CertGenerationRequestHandler
045    implements ExtendedOperationHandler<CertGenerationRequest, CertGenerationResponse>
046{
047    private static final Set<String> EXTENSION_OIDS;
048
049    static
050    {
051        Set<String> set = new HashSet<>( 2 );
052        set.add( CertGenerationRequest.EXTENSION_OID );
053        set.add( CertGenerationResponse.EXTENSION_OID );
054        EXTENSION_OIDS = Collections.unmodifiableSet( set );
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    public String getOid()
062    {
063        return CertGenerationRequest.EXTENSION_OID;
064    }
065
066
067    /**
068     * {@inheritDoc}
069     */
070    public Set<String> getExtensionOids()
071    {
072        return EXTENSION_OIDS;
073    }
074
075
076    /**
077     * {@inheritDoc}
078     */
079    public void handleExtendedOperation( LdapSession session, CertGenerationRequest req ) throws Exception
080    {
081        Entry entry = session.getCoreSession().lookup( new Dn( req.getTargetDN() ) );
082
083        if ( entry != null )
084        {
085            TlsKeyGenerator.addKeyPair(
086                ( ( ClonedServerEntry ) entry ).getOriginalEntry(),
087                req.getIssuerDN(),
088                req.getSubjectDN(),
089                req.getKeyAlgorithm() );
090        }
091
092        CertGenerationResponse certGenerationResponse = new CertGenerationResponseImpl( req.getMessageId() );
093
094        // write the response
095        session.getIoSession().write( certGenerationResponse );
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    public void setLdapServer( LdapServer ldapServer )
103    {
104    }
105}