ssl/libcrypto/src/crypto/certretriever/createx509.cpp
changeset 71 28ccaba883f4
parent 0 e4d67989cc36
child 57 2efc27d87e1c
child 72 403e7f6ed6c5
equal deleted inserted replaced
67:a1e347446159 71:28ccaba883f4
     1 /*
     1 /*
     2 Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     2 Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     3 
     3 
     4 Redistribution and use in source and binary forms, with or without 
     4 Redistribution and use in source and binary forms, with or without 
     5 modification, are permitted provided that the following conditions are met:
     5 modification, are permitted provided that the following conditions are met:
     6 
     6 
     7 * Redistributions of source code must retain the above copyright notice, this 
     7 * Redistributions of source code must retain the above copyright notice, this 
    29 
    29 
    30 
    30 
    31 #include "createx509.h"
    31 #include "createx509.h"
    32 
    32 
    33 
    33 
    34 X509* CX509_Initializer::CreateX509(CX509Certificate* X509Cert)
    34 X509* CX509_Initializer::CreateX509L(CX509Certificate* X509Cert)
    35 {
    35 {
    36 	X509* ret = X509_new();
    36 	X509* ret = X509_new();
    37 	TBool serail = ETrue;
    37 	TBool serail = ETrue;
    38 	
    38 	
    39 	if(ret != NULL)
    39 	if(ret != NULL)
    44 		ret->cert_info->validity = CreateX509_VAL(X509Cert); 
    44 		ret->cert_info->validity = CreateX509_VAL(X509Cert); 
    45 
    45 
    46 //issuer
    46 //issuer
    47 		const CX500DistinguishedName& IssName = X509Cert->IssuerName();	
    47 		const CX500DistinguishedName& IssName = X509Cert->IssuerName();	
    48 		X509_NAME_free(ret->cert_info->issuer);
    48 		X509_NAME_free(ret->cert_info->issuer);
    49 		ret->cert_info->issuer = CreateX509_NAME(IssName);
    49 		ret->cert_info->issuer = CreateX509_NAMEL(IssName);
    50 
    50 
    51 //subject
    51 //subject
    52 		const CX500DistinguishedName& SubName = X509Cert->SubjectName();	
    52 		const CX500DistinguishedName& SubName = X509Cert->SubjectName();	
    53 		X509_NAME_free(ret->cert_info->subject);
    53 		X509_NAME_free(ret->cert_info->subject);
    54 		ret->cert_info->subject = CreateX509_NAME(SubName);
    54 		ret->cert_info->subject = CreateX509_NAMEL(SubName);
    55 	//	const HBufC * name = SubName.DisplayNameL();
    55 	//	const HBufC * name = SubName.DisplayNameL();
    56 
    56 
    57 //signature
    57 //signature
    58 		const TPtrC8* sig_alg_ptr = X509Cert->DataElementEncoding(CX509Certificate::EAlgorithmId);
    58 		const TPtrC8* sig_alg_ptr = X509Cert->DataElementEncoding(CX509Certificate::EAlgorithmId);
    59 		X509_ALGOR_free(ret->cert_info->signature);
    59 		X509_ALGOR_free(ret->cert_info->signature);
    60 		ret->cert_info->signature = CreateX509_ALGOR(sig_alg_ptr);
    60 		ret->cert_info->signature = CreateX509_ALGORL(sig_alg_ptr);
    61 
    61 
    62 //serialnumber
    62 //serialnumber
    63 		const TPtrC8 sernum = X509Cert->SerialNumber();
    63 		const TPtrC8 sernum = X509Cert->SerialNumber();
    64 		ASN1_INTEGER_free(ret->cert_info->serialNumber);
    64 		ASN1_INTEGER_free(ret->cert_info->serialNumber);
    65 		ret->cert_info->serialNumber = CreateASN1_STRING(sernum.Length(),V_ASN1_INTEGER,(unsigned char *)sernum.Ptr(),0);		
    65 		ret->cert_info->serialNumber = CreateASN1_STRING(sernum.Length(),V_ASN1_INTEGER,(unsigned char *)sernum.Ptr(),0);		
    86 		if(subUID_enc)
    86 		if(subUID_enc)
    87 			ret->cert_info->subjectUID = CreateASN1_STRING(subUID_enc->Length(),V_ASN1_BIT_STRING,(unsigned char *)subUID_enc->Ptr(),0);
    87 			ret->cert_info->subjectUID = CreateASN1_STRING(subUID_enc->Length(),V_ASN1_BIT_STRING,(unsigned char *)subUID_enc->Ptr(),0);
    88 
    88 
    89 //key
    89 //key
    90 		X509_PUBKEY_free(ret->cert_info->key);
    90 		X509_PUBKEY_free(ret->cert_info->key);
    91 		ret->cert_info->key = CreateX509_PUBKEY(X509Cert);
    91 		ret->cert_info->key = CreateX509_PUBKEYL(X509Cert);
    92 
    92 
    93 	
    93 	
    94 //extension
    94 //extension
    95 
    95 
    96 
    96 
    97 		ret->cert_info->extensions = CreateSTACKOF_X509_EXTENSION(X509Cert);
    97 		ret->cert_info->extensions = CreateSTACKOF_X509_EXTENSIONL(X509Cert);
    98 
    98 
    99 		
    99 		
   100 //name
   100 //name
   101 		ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
   101 		ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
   102 
   102 
   103 //sig_alg
   103 //sig_alg
   104 		X509_ALGOR_free(ret->sig_alg);
   104 		X509_ALGOR_free(ret->sig_alg);
   105 		ret->sig_alg = CreateX509_ALGOR(sig_alg_ptr);
   105 		ret->sig_alg = CreateX509_ALGORL(sig_alg_ptr);
   106 		
   106 		
   107 //signature
   107 //signature
   108 		const TPtrC8 sig = X509Cert->Signature();
   108 		const TPtrC8 sig = X509Cert->Signature();
   109 		ASN1_STRING_free(ret->signature);
   109 		ASN1_STRING_free(ret->signature);
   110 		ret->signature = CreateASN1_STRING(sig.Length(), V_ASN1_BIT_STRING, (unsigned char *)sig.Ptr(), ASN1_STRING_FLAG_BITS_LEFT);
   110 		ret->signature = CreateASN1_STRING(sig.Length(), V_ASN1_BIT_STRING, (unsigned char *)sig.Ptr(), ASN1_STRING_FLAG_BITS_LEFT);
   112 		
   112 		
   113 	return ret;
   113 	return ret;
   114 }
   114 }
   115 
   115 
   116 
   116 
   117 X509_ALGOR* CX509_Initializer::CreateX509_ALGOR(const TPtrC8* ptr)
   117 X509_ALGOR* CX509_Initializer::CreateX509_ALGORL(const TPtrC8* ptr)
   118 {
   118 {
   119 	X509_ALGOR* ret = X509_ALGOR_new();
   119 	X509_ALGOR* ret = X509_ALGOR_new();
   120 	
   120 	
   121 	TASN1DecGeneric dec((TDesC8 &)*ptr);	
   121 	TASN1DecGeneric dec((TDesC8 &)*ptr);	
   122 	dec.InitL();
   122 	dec.InitL();
   166 	return ret;
   166 	return ret;
   167 }
   167 }
   168 
   168 
   169 
   169 
   170 
   170 
   171 X509_NAME* CX509_Initializer::CreateX509_NAME(const CX500DistinguishedName& DistName)
   171 X509_NAME* CX509_Initializer::CreateX509_NAMEL(const CX500DistinguishedName& DistName)
   172 {
   172 {
   173 	X509_NAME* ret = X509_NAME_new();
   173 	X509_NAME* ret = X509_NAME_new();
   174 	
   174 	
   175 	CASN1EncSequence * Asn1Seq = DistName.EncodeASN1LC();			
   175 	CASN1EncSequence * Asn1Seq = DistName.EncodeASN1LC();			
   176 
   176 
   179 	oct.FillZ();
   179 	oct.FillZ();
   180 	oct.SetLength(KMaxNameLength);
   180 	oct.SetLength(KMaxNameLength);
   181 	TUint writePos = 0;
   181 	TUint writePos = 0;
   182 	Asn1Seq->WriteDERL(oct, writePos);			
   182 	Asn1Seq->WriteDERL(oct, writePos);			
   183 
   183 
   184   	TInt len = Fill_X509_NAME_ENTRY(ret, octetData->Des());
   184   	TInt len = Fill_X509_NAME_ENTRYL(ret, octetData->Des());
   185 	
   185 	
   186 	char *p = (char *)oct.PtrZ();
   186 	char *p = (char *)oct.PtrZ();
   187 
   187 
   188     ret->bytes->data = (char *)OPENSSL_malloc(len);        // no need to free this. BUF_MEM_free will free if not NULL
   188     ret->bytes->data = (char *)OPENSSL_malloc(len);        // no need to free this. BUF_MEM_free will free if not NULL
   189     if(ret->bytes->data)
   189     if(ret->bytes->data)
   200 	return ret;
   200 	return ret;
   201 } 
   201 } 
   202 
   202 
   203 
   203 
   204 // return the length of the encoded sequence
   204 // return the length of the encoded sequence
   205 TInt CX509_Initializer::Fill_X509_NAME_ENTRY(X509_NAME * name, const TDesC8& aBinaryData)
   205 TInt CX509_Initializer::Fill_X509_NAME_ENTRYL(X509_NAME * name, const TDesC8& aBinaryData)
   206 {
   206 {
   207 	TInt aPos = 0;
   207 	TInt aPos = 0;
   208 	TASN1DecGeneric dec(aBinaryData.Right(aBinaryData.Length() - aPos));
   208 	TASN1DecGeneric dec(aBinaryData.Right(aBinaryData.Length() - aPos));
   209 	dec.InitL();
   209 	dec.InitL();
   210 	
   210 	
   362  return ret;
   362  return ret;
   363 }
   363 }
   364 
   364 
   365 
   365 
   366 
   366 
   367 X509_PUBKEY* CX509_Initializer::CreateX509_PUBKEY(CX509Certificate* X509Cert)
   367 X509_PUBKEY* CX509_Initializer::CreateX509_PUBKEYL(CX509Certificate* X509Cert)
   368 {
   368 {
   369 	X509_PUBKEY* ret = X509_PUBKEY_new();
   369 	X509_PUBKEY* ret = X509_PUBKEY_new();
   370 
   370 
   371 //algor
   371 //algor
   372 	const TPtrC8* ptr = X509Cert->DataElementEncoding(CX509Certificate::ESubjectPublicKeyInfo);
   372 	const TPtrC8* ptr = X509Cert->DataElementEncoding(CX509Certificate::ESubjectPublicKeyInfo);
   386 			TASN1DecGeneric rdn(ptr->Right(ptr->Length() - aPos));
   386 			TASN1DecGeneric rdn(ptr->Right(ptr->Length() - aPos));
   387 			rdn.InitL();
   387 			rdn.InitL();
   388 			TPtrC8 newPtr = rdn.Encoding();
   388 			TPtrC8 newPtr = rdn.Encoding();
   389 			
   389 			
   390 			X509_ALGOR_free(ret->algor);	// free the one allocated by X509_PUBKEY_new
   390 			X509_ALGOR_free(ret->algor);	// free the one allocated by X509_PUBKEY_new
   391 			ret->algor = CreateX509_ALGOR(&newPtr);
   391 			ret->algor = CreateX509_ALGORL(&newPtr);
   392 		}
   392 		}
   393 	
   393 	
   394 //public_key
   394 //public_key
   395 	const CSubjectPublicKeyInfo& pubkey = X509Cert->PublicKey();
   395 	const CSubjectPublicKeyInfo& pubkey = X509Cert->PublicKey();
   396 	const TPtrC8 keyDat =	pubkey.KeyData();
   396 	const TPtrC8 keyDat =	pubkey.KeyData();
   405 }
   405 }
   406 
   406 
   407 
   407 
   408 
   408 
   409 
   409 
   410 STACK_OF(X509_EXTENSION)* CX509_Initializer::CreateSTACKOF_X509_EXTENSION(CX509Certificate* X509Cert)
   410 STACK_OF(X509_EXTENSION)* CX509_Initializer::CreateSTACKOF_X509_EXTENSIONL(CX509Certificate* X509Cert)
   411 {
   411 {
   412 	//STACK_OF(X509_EXTENSION) * ret = sk_X509_EXTENSION_new_null();
   412 	//STACK_OF(X509_EXTENSION) * ret = sk_X509_EXTENSION_new_null();
   413 	STACK_OF(X509_EXTENSION) * ret = NULL;
   413 	STACK_OF(X509_EXTENSION) * ret = NULL;
   414 	
   414 	
   415 	const TPtrC8* ptr = X509Cert->DataElementEncoding(CX509Certificate::EExtensionList);
   415 	const TPtrC8* ptr = X509Cert->DataElementEncoding(CX509Certificate::EExtensionList);