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.message.extended;
021
022
023 import javax.naming.NamingException;
024 import javax.naming.ldap.ExtendedResponse;
025
026 import org.apache.directory.shared.asn1.codec.DecoderException;
027 import org.apache.directory.shared.asn1.codec.EncoderException;
028 import org.apache.directory.shared.i18n.I18n;
029 import org.apache.directory.shared.ldap.codec.extended.operations.certGeneration.CertGenerationDecoder;
030 import org.apache.directory.shared.ldap.codec.extended.operations.certGeneration.CertGenerationObject;
031 import org.apache.directory.shared.ldap.message.ExtendedRequestImpl;
032 import org.apache.directory.shared.ldap.message.internal.InternalResultResponse;
033 import org.slf4j.Logger;
034 import org.slf4j.LoggerFactory;
035
036
037 /**
038 *
039 * An extended operation requesting the server to generate a public/private key pair and a certificate
040 * and store them in a specified target entry in the DIT.
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 * @version $Rev$, $Date$
044 */
045 public class CertGenerationRequest extends ExtendedRequestImpl
046 {
047 /** The serial version UUID */
048 private static final long serialVersionUID = 1L;
049
050 private CertGenerationObject certGenObj;
051
052 private static final Logger LOG = LoggerFactory.getLogger( CertGenerationRequest.class );
053
054 public static final String EXTENSION_OID = "1.3.6.1.4.1.18060.0.1.8";
055
056 /**
057 *
058 * Creates a new instance of CertGenerationRequest.
059 *
060 * @param messageId the message id
061 * @param targerDN the DN of target entry whose key and certificate values will be changed
062 * @param issuerDN DN to be used as the issuer's DN in the certificate
063 * @param subjectDN DN to be used as certificate's subject
064 * @param keyAlgorithm crypto algorithm name to be used for generating the keys
065 */
066 public CertGenerationRequest( int messageId, String targerDN, String issuerDN, String subjectDN, String keyAlgorithm )
067 {
068 super( messageId );
069 setOid( EXTENSION_OID );
070
071 this.certGenObj = new CertGenerationObject();
072 certGenObj.setTargetDN( targerDN );
073 certGenObj.setIssuerDN( issuerDN );
074 certGenObj.setSubjectDN( subjectDN );
075 certGenObj.setKeyAlgorithm( keyAlgorithm );
076 }
077
078
079 private void encodePayload() throws EncoderException
080 {
081 payload = certGenObj.encode().array();
082 }
083
084
085 public void setPayload( byte[] payload )
086 {
087 CertGenerationDecoder decoder = new CertGenerationDecoder();
088 try
089 {
090 certGenObj = ( CertGenerationObject ) decoder.decode( payload );
091 if ( payload != null )
092 {
093 this.payload = new byte[payload.length];
094 System.arraycopy( payload, 0, this.payload, 0, payload.length );
095 }
096 else
097 {
098 this.payload = null;
099 }
100 }
101 catch ( DecoderException e )
102 {
103 LOG.error( I18n.err( I18n.ERR_04165 ), e );
104 throw new RuntimeException( e );
105 }
106 }
107
108
109 public ExtendedResponse createExtendedResponse( String id, byte[] berValue, int offset, int length )
110 throws NamingException
111 {
112 return ( ExtendedResponse ) getResultResponse();
113 }
114
115
116 public byte[] getEncodedValue()
117 {
118 return getPayload();
119 }
120
121
122 public byte[] getPayload()
123 {
124 if ( payload == null )
125 {
126 try
127 {
128 encodePayload();
129 }
130 catch ( EncoderException e )
131 {
132 LOG.error( I18n.err( I18n.ERR_04167 ), e );
133 throw new RuntimeException( e );
134 }
135 }
136
137 if ( payload == null )
138 {
139 return null;
140 }
141
142 final byte[] copy = new byte[payload.length];
143 System.arraycopy( payload, 0, copy, 0, payload.length );
144 return copy;
145 }
146
147
148 public InternalResultResponse getResultResponse()
149 {
150 if ( response == null )
151 {
152 response = new CertGenerationResponse( getMessageId() );
153 }
154
155 return response;
156 }
157
158
159 public String getTargetDN()
160 {
161 return certGenObj.getTargetDN();
162 }
163
164
165 public void setTargetDN( String targetDN )
166 {
167 certGenObj.setTargetDN( targetDN );
168 }
169
170
171 public String getIssuerDN()
172 {
173 return certGenObj.getIssuerDN();
174 }
175
176
177 public void setIssuerDN( String issuerDN )
178 {
179 certGenObj.setIssuerDN( issuerDN );
180 }
181
182
183 public String getSubjectDN()
184 {
185 return certGenObj.getSubjectDN();
186 }
187
188
189 public void setSubjectDN( String subjectDN )
190 {
191 certGenObj.setSubjectDN( subjectDN );
192 }
193
194
195 public String getKeyAlgorithm()
196 {
197 return certGenObj.getKeyAlgorithm();
198 }
199
200
201 public void setKeyAlgorithm( String keyAlgorithm )
202 {
203 certGenObj.setKeyAlgorithm( keyAlgorithm );
204 }
205
206 }