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.shared.ldap.codec.extended.operations.certGeneration;
021    
022    
023    import java.nio.ByteBuffer;
024    
025    import org.apache.directory.shared.asn1.AbstractAsn1Object;
026    import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
027    import org.apache.directory.shared.asn1.ber.tlv.Value;
028    import org.apache.directory.shared.asn1.codec.EncoderException;
029    import org.apache.directory.shared.ldap.util.StringTools;
030    
031    
032    /**
033     * 
034     * An extended operation for generating a public key Certificate.
035     * <pre>
036     *   CertGenerateObject ::= SEQUENCE 
037     *   {
038     *      targetDN        IA5String,
039     *      issuerDN        IA5String,
040     *      subjectDN       IA5String,
041     *      keyAlgorithm    IA5String
042     *   }
043     * </pre>
044     * 
045     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046     * @version $Rev$, $Date$
047     */
048    public class CertGenerationObject extends AbstractAsn1Object
049    {
050    
051        /** the DN of the server entry which will be updated*/
052        private String targetDN;
053    
054        /** the issuer DN that will be set in the certificate*/
055        private String issuerDN;// = "CN=ApacheDS, OU=Directory, O=ASF, C=US";
056    
057        /** the DN of the subject that is present in the certificate*/
058        private String subjectDN;// = "CN=ApacheDS, OU=Directory, O=ASF, C=US";
059    
060        /** name of the algorithm used for generating the keys*/
061        private String keyAlgorithm;// = "RSA";
062    
063        /** stores the length of the request*/
064        private int requestLength = 0;
065    
066    
067        @Override
068        public int computeLength()
069        {
070            int len = StringTools.getBytesUtf8( targetDN ).length;
071            requestLength = 1 + Value.getNbBytes( len ) + len;
072    
073            len = StringTools.getBytesUtf8( issuerDN ).length;
074            requestLength += 1 + Value.getNbBytes( len ) + len;
075    
076            len = StringTools.getBytesUtf8( subjectDN ).length;
077            requestLength += 1 + Value.getNbBytes( len ) + len;
078    
079            len = StringTools.getBytesUtf8( keyAlgorithm ).length;
080            requestLength += 1 + Value.getNbBytes( len ) + len;
081    
082            return 1 + Value.getNbBytes( requestLength ) + requestLength;
083        }
084    
085    
086        public ByteBuffer encode() throws EncoderException
087        {
088            ByteBuffer bb = ByteBuffer.allocate( computeLength() );
089    
090            bb.put( UniversalTag.SEQUENCE_TAG );
091            bb.put( Value.getBytes( requestLength ) );
092    
093            Value.encode( bb, targetDN );
094            Value.encode( bb, issuerDN );
095            Value.encode( bb, subjectDN );
096            Value.encode( bb, keyAlgorithm );
097    
098            return bb;
099        }
100    
101    
102        public String getTargetDN()
103        {
104            return targetDN;
105        }
106    
107    
108        public void setTargetDN( String targetDN )
109        {
110            this.targetDN = targetDN;
111        }
112    
113    
114        public String getIssuerDN()
115        {
116            return issuerDN;
117        }
118    
119    
120        public void setIssuerDN( String issuerDN )
121        {
122            this.issuerDN = issuerDN;
123        }
124    
125    
126        public String getSubjectDN()
127        {
128            return subjectDN;
129        }
130    
131    
132        public void setSubjectDN( String subjectDN )
133        {
134            this.subjectDN = subjectDN;
135        }
136    
137    
138        public String getKeyAlgorithm()
139        {
140            return keyAlgorithm;
141        }
142    
143    
144        public void setKeyAlgorithm( String keyAlgorithm )
145        {
146            this.keyAlgorithm = keyAlgorithm;
147        }
148    
149    
150        @Override
151        public String toString()
152        {
153            StringBuilder sb = new StringBuilder();
154            sb.append( "Certficate Generation Object { " ).append( " Target DN: " ).append( targetDN ).append( ',' );
155            sb.append( " Issuer DN: " ).append( issuerDN ).append( ',' );
156            sb.append( " Subject DN: " ).append( subjectDN ).append( ',' );
157            sb.append( " Key Algorithm: " ).append( keyAlgorithm ).append( " }" );
158    
159            return sb.toString();
160        }
161    }