os/security/cryptoservices/certificateandkeymgmt/asn1/gentimeenc.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Implementation for DER encoding of GeneralizedTime.
sl@0
    16
* Following from the decoding class
sl@0
    17
* 1) fractions of seconds are not supported
sl@0
    18
* 2) regional time zone offsets are not supported
sl@0
    19
*
sl@0
    20
*/
sl@0
    21
sl@0
    22
sl@0
    23
#include "panic.h"
sl@0
    24
sl@0
    25
#include <asn1enc.h>
sl@0
    26
sl@0
    27
const TUint KEncodingLengthWithoutSeconds = 13;
sl@0
    28
const TUint KEncodingLengthWithSeconds = 15;
sl@0
    29
sl@0
    30
sl@0
    31
EXPORT_C CASN1EncGeneralizedTime* CASN1EncGeneralizedTime::NewLC(const TTime& aTime)
sl@0
    32
	{
sl@0
    33
	CASN1EncGeneralizedTime* self = new (ELeave) CASN1EncGeneralizedTime(aTime);
sl@0
    34
	CleanupStack::PushL(self);
sl@0
    35
	self->ConstructL();
sl@0
    36
	return self;
sl@0
    37
	}
sl@0
    38
sl@0
    39
EXPORT_C CASN1EncGeneralizedTime* CASN1EncGeneralizedTime::NewL(const TTime& aTime)
sl@0
    40
	{
sl@0
    41
	CASN1EncGeneralizedTime* self = NewLC(aTime);
sl@0
    42
	CleanupStack::Pop(self);
sl@0
    43
	return self;
sl@0
    44
	}
sl@0
    45
sl@0
    46
CASN1EncGeneralizedTime::CASN1EncGeneralizedTime(const TTime& aTime)
sl@0
    47
: CASN1EncPrimitive(EASN1GeneralizedTime), iDateTime(aTime.DateTime())
sl@0
    48
	{
sl@0
    49
	__ASSERT_ALWAYS(iDateTime.Year() < 10000, Panic(KErrYearTooBigForGeneralizedTime));
sl@0
    50
	}
sl@0
    51
sl@0
    52
sl@0
    53
void CASN1EncGeneralizedTime::CalculateContentsLengthDER()
sl@0
    54
	{
sl@0
    55
	iContentsLengthDER = 
sl@0
    56
		iDateTime.Second() ? KEncodingLengthWithSeconds : KEncodingLengthWithoutSeconds;
sl@0
    57
	}
sl@0
    58
sl@0
    59
sl@0
    60
void CASN1EncGeneralizedTime::WriteContentsDERL(TDes8& aBuf) const
sl@0
    61
	{
sl@0
    62
	aBuf.SetLength(0);
sl@0
    63
	aBuf.AppendNumFixedWidth(iDateTime.Year(), EDecimal, 4);
sl@0
    64
	aBuf.AppendNumFixedWidth(iDateTime.Month() + 1, EDecimal, 2);  // Our months are zero-based
sl@0
    65
	aBuf.AppendNumFixedWidth(iDateTime.Day() + 1, EDecimal, 2);    // Our days are zero-based
sl@0
    66
	aBuf.AppendNumFixedWidth(iDateTime.Hour(), EDecimal, 2);
sl@0
    67
	aBuf.AppendNumFixedWidth(iDateTime.Minute(), EDecimal, 2);
sl@0
    68
	if (iDateTime.Second())
sl@0
    69
		{
sl@0
    70
		aBuf.AppendNumFixedWidth(iDateTime.Second(), EDecimal, 2);
sl@0
    71
		}
sl@0
    72
sl@0
    73
	aBuf.Append('Z');
sl@0
    74
	}