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     */
020    package org.apache.directory.server.ldap.handlers.extended;
021    
022    
023    import java.nio.ByteBuffer;
024    import java.util.Collections;
025    import java.util.HashSet;
026    import java.util.Set;
027    
028    import org.apache.directory.server.core.entry.ClonedServerEntry;
029    import org.apache.directory.server.core.security.TlsKeyGenerator;
030    import org.apache.directory.server.ldap.ExtendedOperationHandler;
031    import org.apache.directory.server.ldap.LdapServer;
032    import org.apache.directory.server.ldap.LdapSession;
033    import org.apache.directory.shared.asn1.ber.Asn1Decoder;
034    import org.apache.directory.shared.asn1.codec.DecoderException;
035    import org.apache.directory.shared.ldap.codec.extended.operations.certGeneration.CertGenerationContainer;
036    import org.apache.directory.shared.ldap.codec.extended.operations.certGeneration.CertGenerationDecoder;
037    import org.apache.directory.shared.ldap.codec.extended.operations.certGeneration.CertGenerationObject;
038    import org.apache.directory.shared.ldap.message.extended.CertGenerationRequest;
039    import org.apache.directory.shared.ldap.message.extended.CertGenerationResponse;
040    import org.apache.directory.shared.ldap.message.internal.InternalExtendedRequest;
041    import org.apache.directory.shared.ldap.name.DN;
042    import org.slf4j.Logger;
043    import org.slf4j.LoggerFactory;
044    
045    /**
046     * An extended handler for digital certificate generation
047     * 
048     * @org.apache.xbean.XBean
049     *
050     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
051     * @version $Rev$, $Date$
052     */
053    public class CertGenerationRequestHandler implements ExtendedOperationHandler
054    {
055    
056        private static final Set<String> EXTENSION_OIDS;
057    
058        private static final Logger LOG = LoggerFactory.getLogger( CertGenerationRequestHandler.class );
059    
060        static
061        {
062            Set<String> set = new HashSet<String>( 2 );
063            set.add( CertGenerationRequest.EXTENSION_OID );
064            set.add( CertGenerationResponse.EXTENSION_OID );
065            EXTENSION_OIDS = Collections.unmodifiableSet( set );
066        }
067    
068    
069        public String getOid()
070        {
071            return CertGenerationRequest.EXTENSION_OID;
072        }
073    
074    
075        public Set<String> getExtensionOids()
076        {
077            return EXTENSION_OIDS;
078        }
079    
080    
081        public void handleExtendedOperation( LdapSession session, InternalExtendedRequest req ) throws Exception
082        {
083            ByteBuffer bb = ByteBuffer.wrap( req.getPayload() );
084            Asn1Decoder decoder = new CertGenerationDecoder();
085            CertGenerationContainer container = new CertGenerationContainer();
086            
087            try
088            {
089                decoder.decode( bb, container );
090            }
091            catch( DecoderException e )
092            {
093                throw e;
094            }
095            
096            CertGenerationObject certGenObj = container.getCertGenerationObject();
097            
098            ClonedServerEntry entry = session.getCoreSession().lookup( new DN( certGenObj.getTargetDN() ) );
099            if( entry != null )
100            {
101                TlsKeyGenerator.addKeyPair( entry.getOriginalEntry(), certGenObj.getIssuerDN(), certGenObj.getSubjectDN(), certGenObj.getKeyAlgorithm() );
102            }
103        }
104    
105    
106        public void setLdapServer( LdapServer ldapServer )
107        {
108        }
109    
110    }