os/security/cryptoservices/certificateandkeymgmt/asn1/gentimeenc.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/gentimeenc.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,74 @@
     1.4 +/*
     1.5 +* Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Implementation for DER encoding of GeneralizedTime.
    1.19 +* Following from the decoding class
    1.20 +* 1) fractions of seconds are not supported
    1.21 +* 2) regional time zone offsets are not supported
    1.22 +*
    1.23 +*/
    1.24 +
    1.25 +
    1.26 +#include "panic.h"
    1.27 +
    1.28 +#include <asn1enc.h>
    1.29 +
    1.30 +const TUint KEncodingLengthWithoutSeconds = 13;
    1.31 +const TUint KEncodingLengthWithSeconds = 15;
    1.32 +
    1.33 +
    1.34 +EXPORT_C CASN1EncGeneralizedTime* CASN1EncGeneralizedTime::NewLC(const TTime& aTime)
    1.35 +	{
    1.36 +	CASN1EncGeneralizedTime* self = new (ELeave) CASN1EncGeneralizedTime(aTime);
    1.37 +	CleanupStack::PushL(self);
    1.38 +	self->ConstructL();
    1.39 +	return self;
    1.40 +	}
    1.41 +
    1.42 +EXPORT_C CASN1EncGeneralizedTime* CASN1EncGeneralizedTime::NewL(const TTime& aTime)
    1.43 +	{
    1.44 +	CASN1EncGeneralizedTime* self = NewLC(aTime);
    1.45 +	CleanupStack::Pop(self);
    1.46 +	return self;
    1.47 +	}
    1.48 +
    1.49 +CASN1EncGeneralizedTime::CASN1EncGeneralizedTime(const TTime& aTime)
    1.50 +: CASN1EncPrimitive(EASN1GeneralizedTime), iDateTime(aTime.DateTime())
    1.51 +	{
    1.52 +	__ASSERT_ALWAYS(iDateTime.Year() < 10000, Panic(KErrYearTooBigForGeneralizedTime));
    1.53 +	}
    1.54 +
    1.55 +
    1.56 +void CASN1EncGeneralizedTime::CalculateContentsLengthDER()
    1.57 +	{
    1.58 +	iContentsLengthDER = 
    1.59 +		iDateTime.Second() ? KEncodingLengthWithSeconds : KEncodingLengthWithoutSeconds;
    1.60 +	}
    1.61 +
    1.62 +
    1.63 +void CASN1EncGeneralizedTime::WriteContentsDERL(TDes8& aBuf) const
    1.64 +	{
    1.65 +	aBuf.SetLength(0);
    1.66 +	aBuf.AppendNumFixedWidth(iDateTime.Year(), EDecimal, 4);
    1.67 +	aBuf.AppendNumFixedWidth(iDateTime.Month() + 1, EDecimal, 2);  // Our months are zero-based
    1.68 +	aBuf.AppendNumFixedWidth(iDateTime.Day() + 1, EDecimal, 2);    // Our days are zero-based
    1.69 +	aBuf.AppendNumFixedWidth(iDateTime.Hour(), EDecimal, 2);
    1.70 +	aBuf.AppendNumFixedWidth(iDateTime.Minute(), EDecimal, 2);
    1.71 +	if (iDateTime.Second())
    1.72 +		{
    1.73 +		aBuf.AppendNumFixedWidth(iDateTime.Second(), EDecimal, 2);
    1.74 +		}
    1.75 +
    1.76 +	aBuf.Append('Z');
    1.77 +	}