os/security/cryptoservices/certificateandkeymgmt/asn1/utctimedec.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) 1998-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
* This file contains the implementation of the UTC time ASN1 object.
sl@0
    16
* At this moment in time this class will only deal with Zulu times and 
sl@0
    17
* doesn't have any kind of handling for time offsets. This is not going to be 
sl@0
    18
* a problem for certificate implementations as the PKIX (RFC2459) profile
sl@0
    19
* defines that certificate validity periods are specified as Zulu times.
sl@0
    20
* Points that need updating if handling for offsets are marked with __OFFSET__
sl@0
    21
*
sl@0
    22
*/
sl@0
    23
sl@0
    24
sl@0
    25
#include <asn1dec.h>
sl@0
    26
sl@0
    27
EXPORT_C TASN1DecUTCTime::TASN1DecUTCTime()
sl@0
    28
	{
sl@0
    29
	}
sl@0
    30
sl@0
    31
sl@0
    32
TTime TASN1DecUTCTime::GetTimeL(const TDesC8& aSource)
sl@0
    33
sl@0
    34
	{
sl@0
    35
	// __OFFSET__ Extract corrected time to include offset too.
sl@0
    36
sl@0
    37
	// Did this checking ought to be done on creation rather than when we attempt to get the result?
sl@0
    38
	// YES!
sl@0
    39
sl@0
    40
	// I guess we ought to check that the contents we've got are long enough to contain a time!
sl@0
    41
	if (aSource.Length()<11)
sl@0
    42
		{
sl@0
    43
		User::Leave(KErrArgument);
sl@0
    44
		}
sl@0
    45
sl@0
    46
	// Check all digits the main bit of time are valid - this doesn't check the seconds or offset
sl@0
    47
	TInt i;
sl@0
    48
	for (i=0;i<10;i++)
sl@0
    49
		{
sl@0
    50
		TUint8 j;
sl@0
    51
		j=(TUint8)(aSource[i]-'0');
sl@0
    52
		if (j>=10)
sl@0
    53
			{
sl@0
    54
			User::Leave(KErrArgument);
sl@0
    55
			}
sl@0
    56
		}
sl@0
    57
	// Uh-oh, looks like we're going to have to pull each bit manually and pop it in a TDateTime thing
sl@0
    58
	TInt Year,Month,Day,Hour,Minute,Second=0;		// Only set seconds to zero 'cos they are optional, everything else is going to be set
sl@0
    59
	Year=(aSource[0]-'0')*10+aSource[1]-'0';
sl@0
    60
	Year+=(Year<50)?2000:1900;						//  collection to fit with PKIX UTC/Generalised time rollover at 2049/2050
sl@0
    61
	Month=(aSource[2]-'0')*10+aSource[3]-'0';
sl@0
    62
	Month--;										// Because the enum for months starts at 0 not 1
sl@0
    63
	Day=((aSource[4]-'0')*10+aSource[5]-'0') -1;//added -1 for offset from zero(!)
sl@0
    64
	Hour=(aSource[6]-'0')*10+aSource[7]-'0';
sl@0
    65
	Minute=(aSource[8]-'0')*10+aSource[9]-'0';
sl@0
    66
	TInt Pos=10;
sl@0
    67
sl@0
    68
	if (aSource.Length()>11)
sl@0
    69
		{
sl@0
    70
		if ((aSource[Pos]>='0')&&(aSource[Pos]<='9'))
sl@0
    71
			{
sl@0
    72
			// seconds
sl@0
    73
			Second=(aSource[Pos++]-'0')*10;
sl@0
    74
			Second += aSource[Pos++]-'0';
sl@0
    75
			}
sl@0
    76
		}
sl@0
    77
	if (aSource.Length()>Pos)
sl@0
    78
		{
sl@0
    79
		switch (aSource[Pos])
sl@0
    80
			{
sl@0
    81
		case 'Z':
sl@0
    82
			// Zulu - nothing more to do
sl@0
    83
			break;
sl@0
    84
		case '+':
sl@0
    85
		case '-':
sl@0
    86
			// __OFFSET__ Extract corrected time to include offset too.
sl@0
    87
		    User::Leave(KErrNotSupported);
sl@0
    88
		    break;
sl@0
    89
		default:
sl@0
    90
			// Error!
sl@0
    91
			User::Leave(KErrArgument);
sl@0
    92
			break;
sl@0
    93
			}
sl@0
    94
		}
sl@0
    95
	else
sl@0
    96
		{
sl@0
    97
		User::Leave(KErrArgument);
sl@0
    98
		}
sl@0
    99
sl@0
   100
	if (Month<EJanuary || Month>EDecember)
sl@0
   101
		{
sl@0
   102
		User::Leave(KErrArgument);
sl@0
   103
		}
sl@0
   104
	TMonth month = (TMonth)Month;
sl@0
   105
sl@0
   106
	if (	(Day<0 || Day>=Time::DaysInMonth(Year,month))	||
sl@0
   107
			(Hour<0 || Hour>=24)	||
sl@0
   108
			(Minute<0 || Minute>=60)	||
sl@0
   109
			(Second<0 || Second>=60)	)
sl@0
   110
		{
sl@0
   111
		User::Leave(KErrArgument);
sl@0
   112
		}
sl@0
   113
sl@0
   114
	TDateTime D(Year,month,Day,Hour,Minute,Second,0);
sl@0
   115
	
sl@0
   116
	TTime T(D);
sl@0
   117
	return T;
sl@0
   118
	}
sl@0
   119
sl@0
   120
// __OFFSET__ Add GetOffset() method
sl@0
   121
sl@0
   122
EXPORT_C TTime TASN1DecUTCTime::DecodeDERL(const TDesC8& aSource,TInt& aPos)
sl@0
   123
sl@0
   124
	{
sl@0
   125
	TPtrC8 Source=aSource.Mid(aPos);
sl@0
   126
	TASN1DecGeneric gen(Source);
sl@0
   127
	gen.InitL();
sl@0
   128
	TTime t = GetTimeL(gen.GetContentDER());
sl@0
   129
	aPos+=gen.LengthDER();
sl@0
   130
	return t;
sl@0
   131
	}
sl@0
   132
sl@0
   133
EXPORT_C TTime TASN1DecUTCTime::DecodeDERL(const TASN1DecGeneric& aGen)
sl@0
   134
sl@0
   135
	{
sl@0
   136
	return GetTimeL(aGen.GetContentDER());
sl@0
   137
	}