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 public CertGenerationRequestDecorator( LdapApiService codec, CertGenerationRequest decoratedMessage ) 056 { 057 super( codec, decoratedMessage ); 058 certGenerationRequest = decoratedMessage; 059 } 060 061 062 public CertGenerationRequest getCertGenerationRequest() 063 { 064 return certGenerationRequest; 065 } 066 067 068 /** 069 * {@inheritDoc} 070 */ 071 @Override 072 public void setRequestValue( byte[] requestValue ) 073 { 074 CertGenerationDecoder decoder = new CertGenerationDecoder(); 075 076 try 077 { 078 certGenerationRequest = decoder.decode( requestValue ); 079 080 if ( requestValue != null ) 081 { 082 this.requestValue = new byte[requestValue.length]; 083 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length ); 084 } 085 else 086 { 087 this.requestValue = null; 088 } 089 } 090 catch ( DecoderException e ) 091 { 092 LOG.error( I18n.err( I18n.ERR_04165 ), e ); 093 throw new RuntimeException( e ); 094 } 095 } 096 097 098 /** 099 * {@inheritDoc} 100 */ 101 @Override 102 public byte[] getRequestValue() 103 { 104 if ( requestValue == null ) 105 { 106 try 107 { 108 requestValue = encodeInternal().array(); 109 } 110 catch ( EncoderException e ) 111 { 112 LOG.error( I18n.err( I18n.ERR_04167 ), e ); 113 throw new RuntimeException( e ); 114 } 115 } 116 117 if ( requestValue == null ) 118 { 119 return null; 120 } 121 122 final byte[] copy = new byte[requestValue.length]; 123 System.arraycopy( requestValue, 0, copy, 0, requestValue.length ); 124 return copy; 125 } 126 127 128 /** 129 * {@inheritDoc} 130 */ 131 @Override 132 public CertGenerationResponse getResultResponse() 133 { 134 return ( CertGenerationResponse ) getDecorated().getResultResponse(); 135 } 136 137 138 /** 139 * {@inheritDoc} 140 */ 141 public String getTargetDN() 142 { 143 return getDecorated().getTargetDN(); 144 } 145 146 147 /** 148 * {@inheritDoc} 149 */ 150 public void setTargetDN( String targetDN ) 151 { 152 getDecorated().setTargetDN( targetDN ); 153 } 154 155 156 /** 157 * {@inheritDoc} 158 */ 159 public String getIssuerDN() 160 { 161 return getDecorated().getIssuerDN(); 162 } 163 164 165 /** 166 * {@inheritDoc} 167 */ 168 public void setIssuerDN( String issuerDN ) 169 { 170 getDecorated().setIssuerDN( issuerDN ); 171 } 172 173 174 /** 175 * {@inheritDoc} 176 */ 177 public String getSubjectDN() 178 { 179 return getDecorated().getSubjectDN(); 180 } 181 182 183 /** 184 * {@inheritDoc} 185 */ 186 public void setSubjectDN( String subjectDN ) 187 { 188 getDecorated().setSubjectDN( subjectDN ); 189 } 190 191 192 /** 193 * {@inheritDoc} 194 */ 195 public String getKeyAlgorithm() 196 { 197 return getDecorated().getKeyAlgorithm(); 198 } 199 200 201 /** 202 * {@inheritDoc} 203 */ 204 public void setKeyAlgorithm( String keyAlgorithm ) 205 { 206 getDecorated().setKeyAlgorithm( keyAlgorithm ); 207 } 208 209 210 /** 211 * Compute the CertGenerationRequest length 212 * 213 * <pre> 214 * 0x30 L1 215 * | 216 * +--> 0x04 LL target DN 217 * +--> 0x04 LL issuer DN 218 * +--> 0x04 LL subject DN 219 * +--> 0x04 LL key algorithm 220 * </pre> 221 */ 222 /* no qualifier */ int computeLengthInternal() 223 { 224 int len = Strings.getBytesUtf8( certGenerationRequest.getTargetDN() ).length; 225 requestLength = 1 + TLV.getNbBytes( len ) + len; 226 227 len = Strings.getBytesUtf8( certGenerationRequest.getIssuerDN() ).length; 228 requestLength += 1 + TLV.getNbBytes( len ) + len; 229 230 len = Strings.getBytesUtf8( certGenerationRequest.getSubjectDN() ).length; 231 requestLength += 1 + TLV.getNbBytes( len ) + len; 232 233 len = Strings.getBytesUtf8( certGenerationRequest.getKeyAlgorithm() ).length; 234 requestLength += 1 + TLV.getNbBytes( len ) + len; 235 236 return 1 + TLV.getNbBytes( requestLength ) + requestLength; 237 } 238 239 240 /** 241 * Encodes the CertGenerationRequest extended operation. 242 * 243 * @return A ByteBuffer that contains the encoded PDU 244 * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong. 245 */ 246 /* no qualifier */ ByteBuffer encodeInternal() throws EncoderException 247 { 248 // Allocate the bytes buffer. 249 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() ); 250 251 bb.put( UniversalTag.SEQUENCE.getValue() ); 252 bb.put( TLV.getBytes( requestLength ) ); 253 254 BerValue.encode( bb, certGenerationRequest.getTargetDN() ); 255 BerValue.encode( bb, certGenerationRequest.getIssuerDN() ); 256 BerValue.encode( bb, certGenerationRequest.getSubjectDN() ); 257 BerValue.encode( bb, certGenerationRequest.getKeyAlgorithm() ); 258 259 return bb; 260 } 261}