os/security/cryptoservices/certificateandkeymgmt/x509/x509keysDH.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include <x509keys.h>
    20 #include <asn1dec.h>
    21 #include <asn1enc.h>
    22 #include <x509cert.h>
    23 
    24 EXPORT_C CX509DHPublicKey* CX509DHPublicKey::NewL(const TDesC8& aParamsData, const TDesC8& aKeyData)
    25 	{
    26 	CX509DHPublicKey* me = CX509DHPublicKey::NewLC(aParamsData, aKeyData);
    27 	CleanupStack::Pop(me);
    28 	return (me);
    29 	}
    30 
    31 EXPORT_C CX509DHPublicKey* CX509DHPublicKey::NewLC(const TDesC8& aParamsData, const TDesC8& aKeyData)
    32 	{
    33 		CX509DHPublicKey* me = new (ELeave) CX509DHPublicKey();
    34 		CleanupStack::PushL(me);
    35 		me->ConstructL(aParamsData, aKeyData);
    36 		return (me);	
    37 	}
    38 
    39 EXPORT_C CX509DHPublicKey::~CX509DHPublicKey()
    40 	{
    41 	}
    42 
    43 CX509DHPublicKey::CX509DHPublicKey()
    44 	{
    45 	}
    46 
    47 void CX509DHPublicKey::ConstructL(const TDesC8& aParamsData, const TDesC8& aKeyData)
    48 	{
    49 	CX509DHDomainParams* params=CX509DHDomainParams::NewLC(aParamsData);
    50 	iN = RInteger::NewL(params->P());	
    51 	iG = RInteger::NewL(params->G());	
    52 	iX = RInteger::NewL(aKeyData);	
    53 	CleanupStack::PopAndDestroy(params);	
    54 	}
    55 
    56 //DH params
    57 //DH validation params
    58 EXPORT_C CX509DHValidationParams* CX509DHValidationParams::NewL(const TDesC8& aBinaryData)
    59 	{
    60 	TInt pos = 0;
    61 	return CX509DHValidationParams::NewL(aBinaryData, pos);
    62 	}
    63 
    64 EXPORT_C CX509DHValidationParams* CX509DHValidationParams::NewLC(const TDesC8& aBinaryData)
    65 	{
    66 	TInt pos = 0;
    67 	return CX509DHValidationParams::NewLC(aBinaryData, pos);
    68 	}
    69 
    70 EXPORT_C CX509DHValidationParams* CX509DHValidationParams::NewL(const TDesC8& aBinaryData, TInt& aPos)
    71 	{
    72 	CX509DHValidationParams* self = CX509DHValidationParams::NewLC(aBinaryData, aPos);
    73 	CleanupStack::Pop();
    74 	return self;
    75 	}
    76 
    77 EXPORT_C CX509DHValidationParams* CX509DHValidationParams::NewLC(const TDesC8& aBinaryData, TInt& aPos)
    78 	{
    79 	CX509DHValidationParams* self = new(ELeave) CX509DHValidationParams;
    80 	CleanupStack::PushL(self);
    81 	self->ConstructL(aBinaryData, aPos);
    82 	return self;
    83 	}
    84 
    85 void CX509DHValidationParams::ConstructL(const TDesC8& aBinaryData, TInt& aPos)
    86 	{
    87 	TASN1DecGeneric dec(aBinaryData.Right(aBinaryData.Length() - aPos));
    88 	dec.InitL();
    89 	if (dec.Tag() != EASN1Sequence)
    90 		{
    91 		User::Leave(KErrArgument);
    92 		}
    93 	TInt end = aPos + dec.LengthDER();
    94 	aPos += dec.LengthDERHeader();//add header length to aPos
    95 
    96 	TASN1DecBitString encBS;
    97 	iSeed = encBS.ExtractOctetStringL(aBinaryData, aPos);
    98 	TASN1DecInteger encInt;
    99 	iPGenCounter = encInt.DecodeDERLongL(aBinaryData, aPos);
   100 
   101 	if (aPos != end)
   102 		{
   103 		User::Leave(KErrArgument);
   104 		}	
   105 	}
   106 
   107 EXPORT_C const TPtrC8 CX509DHValidationParams::Seed() const
   108 	{
   109 	return *iSeed;
   110 	}
   111 
   112 EXPORT_C const TInteger& CX509DHValidationParams::PGenCounter() const
   113 	{
   114 	return iPGenCounter;
   115 	}
   116 
   117 CX509DHValidationParams::~CX509DHValidationParams()
   118 	{
   119 	delete iSeed;
   120 	iPGenCounter.Close();
   121 	}
   122 
   123 CX509DHValidationParams::CX509DHValidationParams()
   124 	{
   125 	}
   126 
   127 //DH domain params
   128 EXPORT_C CX509DHDomainParams* CX509DHDomainParams::NewL(const TDesC8& aBinaryData)
   129 	{
   130 	TInt pos = 0;
   131 	return CX509DHDomainParams::NewL(aBinaryData, pos);
   132 	}
   133 
   134 EXPORT_C CX509DHDomainParams* CX509DHDomainParams::NewLC(const TDesC8& aBinaryData)
   135 	{
   136 	TInt pos = 0;
   137 	return CX509DHDomainParams::NewLC(aBinaryData, pos);
   138 	}
   139 
   140 EXPORT_C CX509DHDomainParams* CX509DHDomainParams::NewL(const TDesC8& aBinaryData, TInt& aPos)
   141 	{
   142 	CX509DHDomainParams* self = CX509DHDomainParams::NewLC(aBinaryData, aPos);
   143 	CleanupStack::Pop();
   144 	return self;
   145 	}
   146 
   147 EXPORT_C CX509DHDomainParams* CX509DHDomainParams::NewLC(const TDesC8& aBinaryData, TInt& aPos)
   148 	{
   149 	CX509DHDomainParams* self = new(ELeave) CX509DHDomainParams;
   150 	CleanupStack::PushL(self);
   151 	self->ConstructL(aBinaryData, aPos);
   152 	return self;
   153 	}
   154 
   155 void CX509DHDomainParams::ConstructL(const TDesC8& aBinaryData, TInt& aPos)
   156 	{
   157 	TASN1DecGeneric dec(aBinaryData.Right(aBinaryData.Length() - aPos));
   158 	dec.InitL();
   159 	if (dec.Tag() != EASN1Sequence)
   160 		{
   161 		User::Leave(KErrArgument);
   162 		}
   163 	TInt end = aPos + dec.LengthDER();
   164 	aPos += dec.LengthDERHeader();//add header length to aPos
   165 
   166 	TASN1DecInteger encInt;
   167 	iP = encInt.DecodeDERLongL(aBinaryData, aPos);
   168 	iG = encInt.DecodeDERLongL(aBinaryData, aPos);
   169 	if (aPos < end)
   170 		{
   171 		iQ = encInt.DecodeDERLongL(aBinaryData, aPos);
   172 		}
   173 
   174 	if (aPos < end)
   175 		{
   176 		TASN1DecGeneric gen1(aBinaryData.Right(aBinaryData.Length() - aPos));
   177 		gen1.InitL();
   178 		TBool doneVal = EFalse;
   179 		if (gen1.Tag() == EASN1Integer)
   180 			{
   181 			iJ = encInt.DecodeDERLongL(aBinaryData, aPos);
   182 			}
   183 		else
   184 			{
   185 			iValidationParams = CX509DHValidationParams::NewL(aBinaryData, aPos);
   186 			doneVal = EFalse;
   187 			}
   188 		if ((aPos < end) && (!doneVal))
   189 			{
   190 			iValidationParams = CX509DHValidationParams::NewL(aBinaryData, aPos);
   191 			}
   192 		}
   193 
   194 	if (aPos != end)
   195 		{
   196 		User::Leave(KErrArgument);
   197 		}	
   198 	}
   199 
   200 EXPORT_C const TInteger& CX509DHDomainParams::P() const
   201 	{
   202 	return iP;
   203 	}
   204 
   205 EXPORT_C const TInteger& CX509DHDomainParams::G() const
   206 	{
   207 	return iG;
   208 	}
   209 
   210 EXPORT_C const TInteger& CX509DHDomainParams::Q() const
   211 	{
   212 	return iQ;
   213 	}
   214 
   215 EXPORT_C const TInteger& CX509DHDomainParams::J() const							
   216 	{
   217 	return iJ;
   218 	}
   219 
   220 EXPORT_C const CX509DHValidationParams* CX509DHDomainParams::ValidationParams() const
   221 	{
   222 	return iValidationParams;
   223 	}
   224 
   225 CX509DHDomainParams::~CX509DHDomainParams()
   226 	{
   227 	iP.Close();
   228 	iG.Close();
   229 	iQ.Close();
   230 	iJ.Close();
   231 	delete iValidationParams;
   232 	}
   233 
   234 CX509DHDomainParams::CX509DHDomainParams()
   235 	{
   236 	}
   237 
   238 //	CX509DHKeyPair
   239 
   240 
   241 EXPORT_C CX509DHKeyPair* CX509DHKeyPair::NewL(const TDesC8& aParamsData)
   242 {
   243 	CX509DHKeyPair* me = CX509DHKeyPair::NewLC(aParamsData);
   244 	CleanupStack::Pop(me);
   245 	return (me);
   246 }
   247 
   248 EXPORT_C CX509DHKeyPair* CX509DHKeyPair::NewLC(const TDesC8& aParamsData)
   249 {
   250 	CX509DHKeyPair* me = new (ELeave) CX509DHKeyPair();
   251 	CleanupStack::PushL(me);
   252 	me->ConstructL(aParamsData);
   253 	return (me);
   254 }
   255 
   256 EXPORT_C CX509DHKeyPair::~CX509DHKeyPair()
   257 {}
   258 
   259 CX509DHKeyPair::CX509DHKeyPair()
   260 {}
   261 
   262 void CX509DHKeyPair::ConstructL(const TDesC8& aParamsData)
   263 {
   264 	CX509DHDomainParams* params=CX509DHDomainParams::NewLC(aParamsData);
   265 	RInteger n = RInteger::NewL(params->P());
   266 	CleanupStack::PushL(n);
   267 	RInteger g = RInteger::NewL(params->G());
   268 	CleanupStack::PushL(g);
   269 	
   270 	CDHKeyPair::ConstructL(n, g);
   271 	CleanupStack::Pop(2, &n);	//	n, g owned by this now
   272 	CleanupStack::PopAndDestroy(params);	
   273 }