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