*/
public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
{
if ( buffer == null )
{
throw new EncoderException( I18n.err( I18n.ERR_148 ) );
}
try
{
// The KRB-ERROR APPLICATION tag
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_TAG );
buffer.put( TLV.getBytes( krbErrorLength ) );
// The KRB_ERROR sequence
buffer.put( UniversalTag.SEQUENCE.getValue() );
buffer.put( TLV.getBytes( krbErrorSeqLength ) );
// pvno tag and value
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_PVNO_TAG );
buffer.put( TLV.getBytes( pvnoLength ) );
BerValue.encode( buffer, getProtocolVersionNumber() );
// msg-type tag and value
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_MSGTYPE_TAG );
buffer.put( TLV.getBytes( msgTypeLength ) );
BerValue.encode( buffer, getMessageType().getValue() );
// ctime tag and value if any
if ( cTimeLength > 0 )
{
// The tag
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_CTIME_TAG );
buffer.put( TLV.getBytes( cTimeLength ) );
// The value
buffer.put( UniversalTag.GENERALIZED_TIME.getValue() );
buffer.put( ( byte ) 0x0F );
buffer.put( cTime.getBytes() );
}
// cusec tag and value if any
if ( cusec != null )
{
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_CUSEC_TAG );
buffer.put( TLV.getBytes( cusecLength ) );
BerValue.encode( buffer, cusec );
}
// stime tag and value
// The tag
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_STIME_TAG );
buffer.put( TLV.getBytes( sTimeLength ) );
// The value
buffer.put( UniversalTag.GENERALIZED_TIME.getValue() );
buffer.put( ( byte ) 0x0F );
buffer.put( sTime.getBytes() );
// susec tag and value
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_SUSEC_TAG );
buffer.put( TLV.getBytes( susecLength ) );
BerValue.encode( buffer, susec );
// error-code tag and value
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_ERROR_CODE_TAG );
buffer.put( TLV.getBytes( errorCodeLength ) );
BerValue.encode( buffer, errorCode.getValue() );
// crealm tage and value, if any
if ( cRealm != null )
{
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_CREALM_TAG );
buffer.put( TLV.getBytes( cRealmLength ) );
buffer.put( UniversalTag.GENERAL_STRING.getValue() );
buffer.put( TLV.getBytes( crealmBytes.length ) );
buffer.put( crealmBytes );
}
// cname tag and value, if any
if ( cName != null )
{
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_CNAME_TAG );
buffer.put( TLV.getBytes( cNameLength ) );
cName.encode( buffer );
}
// realm tag and value
// the tag
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_REALM_TAG );
buffer.put( TLV.getBytes( realmLength ) );
// The value
buffer.put( UniversalTag.GENERAL_STRING.getValue() );
buffer.put( TLV.getBytes( realmBytes.length ) );
buffer.put( realmBytes );
// sname tag and value
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_SNAME_TAG );
buffer.put( TLV.getBytes( sNameLength ) );
sName.encode( buffer );
// etext tag and value, if any
if ( eText != null )
{
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_ETEXT_TAG );
buffer.put( TLV.getBytes( eTextLength ) );
buffer.put( UniversalTag.GENERAL_STRING.getValue() );
buffer.put( TLV.getBytes( eTextBytes.length ) );
buffer.put( eTextBytes );
}
// edata tag and value, if any
if ( eData != null )
{
buffer.put( ( byte ) KerberosConstants.KRB_ERROR_EDATA_TAG );
buffer.put( TLV.getBytes( eDataLength ) );
BerValue.encode( buffer, eData );
}
}
catch ( BufferOverflowException boe )
{
log.error( I18n.err( I18n.ERR_734_CANNOT_ENCODE_KRBERROR, 1 + TLV.getNbBytes( krbErrorLength )
+ krbErrorLength, buffer.capacity() ) );
throw new EncoderException( I18n.err( I18n.ERR_138 ) );
}
if ( IS_DEBUG )
{
log.debug( "KrbError encoding : {}", Strings.dumpBytes( buffer.array() ) );