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.api.ldap.extras.extended.ads_impl.certGeneration; 021 022 023import java.nio.ByteBuffer; 024 025import org.apache.directory.api.asn1.DecoderException; 026import org.apache.directory.api.asn1.EncoderException; 027import org.apache.directory.api.asn1.ber.tlv.BerValue; 028import org.apache.directory.api.asn1.ber.tlv.TLV; 029import org.apache.directory.api.asn1.ber.tlv.UniversalTag; 030import org.apache.directory.api.i18n.I18n; 031import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator; 032import org.apache.directory.api.ldap.codec.api.LdapApiService; 033import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequest; 034import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationResponse; 035import org.apache.directory.api.util.Strings; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039 040/** 041 * A Decorator for certificate generation extended request. 042 * 043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 044 */ 045public class CertGenerationRequestDecorator extends ExtendedRequestDecorator<CertGenerationRequest> 046 implements CertGenerationRequest 047{ 048 private static final Logger LOG = LoggerFactory.getLogger( CertGenerationRequestDecorator.class ); 049 050 private CertGenerationRequest certGenerationRequest; 051 052 /** stores the length of the request*/ 053 private int requestLength = 0; 054 055 056 /** 057 * Creates a new instance of CertGenerationRequestDecorator. 058 * 059 * @param codec The LDAP Service to use 060 * @param decoratedMessage The certificate generation request 061 */ 062 public CertGenerationRequestDecorator( LdapApiService codec, CertGenerationRequest decoratedMessage ) 063 { 064 super( codec, decoratedMessage ); 065 certGenerationRequest = decoratedMessage; 066 } 067 068 069 /** 070 * @return The certificate generation request 071 */ 072 public CertGenerationRequest getCertGenerationRequest() 073 { 074 return certGenerationRequest; 075 } 076 077 078 /** 079 * {@inheritDoc} 080 */ 081 @Override 082 public void setRequestValue( byte[] requestValue ) 083 { 084 CertGenerationDecoder decoder = new CertGenerationDecoder(); 085 086 try 087 { 088 certGenerationRequest = decoder.decode( requestValue ); 089 090 if ( requestValue != null ) 091 { 092 this.requestValue = new byte[requestValue.length]; 093 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length ); 094 } 095 else 096 { 097 this.requestValue = null; 098 } 099 } 100 catch ( DecoderException e ) 101 { 102 LOG.error( I18n.err( I18n.ERR_04165 ), e ); 103 throw new RuntimeException( e ); 104 } 105 } 106 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override 112 public byte[] getRequestValue() 113 { 114 if ( requestValue == null ) 115 { 116 try 117 { 118 requestValue = encodeInternal().array(); 119 } 120 catch ( EncoderException e ) 121 { 122 LOG.error( I18n.err( I18n.ERR_04167 ), e ); 123 throw new RuntimeException( e ); 124 } 125 } 126 127 final byte[] copy = new byte[requestValue.length]; 128 System.arraycopy( requestValue, 0, copy, 0, requestValue.length ); 129 130 return copy; 131 } 132 133 134 /** 135 * {@inheritDoc} 136 */ 137 @Override 138 public CertGenerationResponse getResultResponse() 139 { 140 return ( CertGenerationResponse ) getDecorated().getResultResponse(); 141 } 142 143 144 /** 145 * {@inheritDoc} 146 */ 147 @Override 148 public String getTargetDN() 149 { 150 return getDecorated().getTargetDN(); 151 } 152 153 154 /** 155 * {@inheritDoc} 156 */ 157 @Override 158 public void setTargetDN( String targetDN ) 159 { 160 getDecorated().setTargetDN( targetDN ); 161 } 162 163 164 /** 165 * {@inheritDoc} 166 */ 167 @Override 168 public String getIssuerDN() 169 { 170 return getDecorated().getIssuerDN(); 171 } 172 173 174 /** 175 * {@inheritDoc} 176 */ 177 @Override 178 public void setIssuerDN( String issuerDN ) 179 { 180 getDecorated().setIssuerDN( issuerDN ); 181 } 182 183 184 /** 185 * {@inheritDoc} 186 */ 187 @Override 188 public String getSubjectDN() 189 { 190 return getDecorated().getSubjectDN(); 191 } 192 193 194 /** 195 * {@inheritDoc} 196 */ 197 @Override 198 public void setSubjectDN( String subjectDN ) 199 { 200 getDecorated().setSubjectDN( subjectDN ); 201 } 202 203 204 /** 205 * {@inheritDoc} 206 */ 207 @Override 208 public String getKeyAlgorithm() 209 { 210 return getDecorated().getKeyAlgorithm(); 211 } 212 213 214 /** 215 * {@inheritDoc} 216 */ 217 @Override 218 public void setKeyAlgorithm( String keyAlgorithm ) 219 { 220 getDecorated().setKeyAlgorithm( keyAlgorithm ); 221 } 222 223 224 /** 225 * Compute the CertGenerationRequest length 226 * 227 * <pre> 228 * 0x30 L1 229 * | 230 * +--> 0x04 LL target DN 231 * +--> 0x04 LL issuer DN 232 * +--> 0x04 LL subject DN 233 * +--> 0x04 LL key algorithm 234 * </pre> 235 */ 236 /* no qualifier */int computeLengthInternal() 237 { 238 int len = Strings.getBytesUtf8( certGenerationRequest.getTargetDN() ).length; 239 requestLength = 1 + TLV.getNbBytes( len ) + len; 240 241 len = Strings.getBytesUtf8( certGenerationRequest.getIssuerDN() ).length; 242 requestLength += 1 + TLV.getNbBytes( len ) + len; 243 244 len = Strings.getBytesUtf8( certGenerationRequest.getSubjectDN() ).length; 245 requestLength += 1 + TLV.getNbBytes( len ) + len; 246 247 len = Strings.getBytesUtf8( certGenerationRequest.getKeyAlgorithm() ).length; 248 requestLength += 1 + TLV.getNbBytes( len ) + len; 249 250 return 1 + TLV.getNbBytes( requestLength ) + requestLength; 251 } 252 253 254 /** 255 * Encodes the CertGenerationRequest extended operation. 256 * 257 * @return A ByteBuffer that contains the encoded PDU 258 * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong. 259 */ 260 /* no qualifier */ByteBuffer encodeInternal() throws EncoderException 261 { 262 // Allocate the bytes buffer. 263 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() ); 264 265 bb.put( UniversalTag.SEQUENCE.getValue() ); 266 bb.put( TLV.getBytes( requestLength ) ); 267 268 BerValue.encode( bb, certGenerationRequest.getTargetDN() ); 269 BerValue.encode( bb, certGenerationRequest.getIssuerDN() ); 270 BerValue.encode( bb, certGenerationRequest.getSubjectDN() ); 271 BerValue.encode( bb, certGenerationRequest.getKeyAlgorithm() ); 272 273 return bb; 274 } 275}