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 @Override
087 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
088 {
089 ByteBuffer bb = ByteBuffer.allocate( computeLength() );
090
091 bb.put( UniversalTag.SEQUENCE_TAG );
092 bb.put( Value.getBytes( requestLength ) );
093
094 Value.encode( bb, targetDN );
095 Value.encode( bb, issuerDN );
096 Value.encode( bb, subjectDN );
097 Value.encode( bb, keyAlgorithm );
098
099 return bb;
100 }
101
102
103 public String getTargetDN()
104 {
105 return targetDN;
106 }
107
108
109 public void setTargetDN( String targetDN )
110 {
111 this.targetDN = targetDN;
112 }
113
114
115 public String getIssuerDN()
116 {
117 return issuerDN;
118 }
119
120
121 public void setIssuerDN( String issuerDN )
122 {
123 this.issuerDN = issuerDN;
124 }
125
126
127 public String getSubjectDN()
128 {
129 return subjectDN;
130 }
131
132
133 public void setSubjectDN( String subjectDN )
134 {
135 this.subjectDN = subjectDN;
136 }
137
138
139 public String getKeyAlgorithm()
140 {
141 return keyAlgorithm;
142 }
143
144
145 public void setKeyAlgorithm( String keyAlgorithm )
146 {
147 this.keyAlgorithm = keyAlgorithm;
148 }
149
150
151 @Override
152 public String toString()
153 {
154 StringBuilder sb = new StringBuilder();
155 sb.append( "Certficate Generation Object { " ).append( " Target DN: " ).append( targetDN ).append( ',' );
156 sb.append( " Issuer DN: " ).append( issuerDN ).append( ',' );
157 sb.append( " Subject DN: " ).append( subjectDN ).append( ',' );
158 sb.append( " Key Algorithm: " ).append( keyAlgorithm ).append( " }" );
159
160 return sb.toString();
161 }
162 }