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.shared.ldap.extras.extended.ads_impl.certGeneration;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.shared.asn1.AbstractAsn1Object;
026import org.apache.directory.shared.asn1.EncoderException;
027import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
028import org.apache.directory.shared.asn1.ber.tlv.Value;
029import org.apache.directory.shared.ldap.extras.extended.CertGenerationRequest;
030import org.apache.directory.shared.util.Strings;
031
032
033/**
034 * 
035 * An extended operation for generating a public key Certificate.
036 * <pre>
037 *   CertGenerateObject ::= SEQUENCE 
038 *   {
039 *      targetDN        IA5String,
040 *      issuerDN        IA5String,
041 *      subjectDN       IA5String,
042 *      keyAlgorithm    IA5String
043 *   }
044 * </pre>
045 * 
046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047 */
048public class CertGenerationObject extends AbstractAsn1Object
049{
050    private CertGenerationRequest request;
051    
052    
053    public CertGenerationObject( CertGenerationRequest request )
054    {
055        this.request = request;
056    }
057    
058    
059    /** stores the length of the request*/
060    private int requestLength = 0;
061
062
063    @Override
064    public int computeLength()
065    {
066        int len = Strings.getBytesUtf8( request.getTargetDN() ).length;
067        requestLength = 1 + Value.getNbBytes( len ) + len;
068
069        len = Strings.getBytesUtf8( request.getIssuerDN() ).length;
070        requestLength += 1 + Value.getNbBytes( len ) + len;
071
072        len = Strings.getBytesUtf8( request.getSubjectDN() ).length;
073        requestLength += 1 + Value.getNbBytes( len ) + len;
074
075        len = Strings.getBytesUtf8( request.getKeyAlgorithm() ).length;
076        requestLength += 1 + Value.getNbBytes( len ) + len;
077
078        return 1 + Value.getNbBytes( requestLength ) + requestLength;
079    }
080
081
082    public ByteBuffer encode() throws EncoderException
083    {
084        ByteBuffer bb = ByteBuffer.allocate( computeLength() );
085
086        bb.put( UniversalTag.SEQUENCE.getValue() );
087        bb.put( Value.getBytes( requestLength ) );
088
089        Value.encode( bb, request.getTargetDN() );
090        Value.encode( bb, request.getIssuerDN() );
091        Value.encode( bb, request.getSubjectDN() );
092        Value.encode( bb, request.getKeyAlgorithm() );
093
094        return bb;
095    }
096}