ssl/libcrypto/src/crypto/certretriever/createx509.cpp
changeset 72 403e7f6ed6c5
parent 71 28ccaba883f4
equal deleted inserted replaced
71:28ccaba883f4 72:403e7f6ed6c5
    32 
    32 
    33 
    33 
    34 X509* CX509_Initializer::CreateX509L(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 	if(ret == NULL)
    38 	
    38 		return NULL;
    39 	if(ret != NULL)
    39 	
    40 	{
    40 	TCleanupItem item((void(*)(void*))X509_free,(void*)ret);
    41 	
    41 	CleanupStack::PushL(item);
    42 //validity
    42 			
    43 		X509_VAL_free(ret->cert_info->validity);
    43 	TBool serial = ETrue;
    44 		ret->cert_info->validity = CreateX509_VAL(X509Cert); 
    44 	
    45 
    45 	//validity
    46 //issuer
    46 	X509_VAL_free(ret->cert_info->validity);
    47 		const CX500DistinguishedName& IssName = X509Cert->IssuerName();	
    47 	ret->cert_info->validity = CreateX509_VAL(X509Cert); 
    48 		X509_NAME_free(ret->cert_info->issuer);
    48 	
    49 		ret->cert_info->issuer = CreateX509_NAMEL(IssName);
    49 	//issuer
    50 
    50 	const CX500DistinguishedName& IssName = X509Cert->IssuerName();	
    51 //subject
    51 	X509_NAME_free(ret->cert_info->issuer);
    52 		const CX500DistinguishedName& SubName = X509Cert->SubjectName();	
    52 	ret->cert_info->issuer = CreateX509_NAMEL(IssName);
    53 		X509_NAME_free(ret->cert_info->subject);
    53 	
    54 		ret->cert_info->subject = CreateX509_NAMEL(SubName);
    54 	//subject
       
    55 	const CX500DistinguishedName& SubName = X509Cert->SubjectName();	
       
    56 	X509_NAME_free(ret->cert_info->subject);
       
    57 	ret->cert_info->subject = CreateX509_NAMEL(SubName);
    55 	//	const HBufC * name = SubName.DisplayNameL();
    58 	//	const HBufC * name = SubName.DisplayNameL();
    56 
    59 	
    57 //signature
    60 	//signature
    58 		const TPtrC8* sig_alg_ptr = X509Cert->DataElementEncoding(CX509Certificate::EAlgorithmId);
    61 	const TPtrC8* sig_alg_ptr = X509Cert->DataElementEncoding(CX509Certificate::EAlgorithmId);
    59 		X509_ALGOR_free(ret->cert_info->signature);
    62 	X509_ALGOR_free(ret->cert_info->signature);
    60 		ret->cert_info->signature = CreateX509_ALGORL(sig_alg_ptr);
    63 	ret->cert_info->signature = CreateX509_ALGORL(sig_alg_ptr);
    61 
    64 	
    62 //serialnumber
    65 	//serialnumber
    63 		const TPtrC8 sernum = X509Cert->SerialNumber();
    66 	const TPtrC8 sernum = X509Cert->SerialNumber();
    64 		ASN1_INTEGER_free(ret->cert_info->serialNumber);
    67 	ASN1_INTEGER_free(ret->cert_info->serialNumber);
    65 		ret->cert_info->serialNumber = CreateASN1_STRING(sernum.Length(),V_ASN1_INTEGER,(unsigned char *)sernum.Ptr(),0);		
    68 	ret->cert_info->serialNumber = CreateASN1_STRING(sernum.Length(),V_ASN1_INTEGER,(unsigned char *)sernum.Ptr(),0);		
    66 		if((sernum.Length()== 1) && sernum[0]==0)
    69 	if((sernum.Length()== 1) && sernum[0]==0)
    67 			serail = EFalse;
    70 		serial = EFalse;
    68 			
    71 		
    69 //version
    72 	//version
    70 
    73 	TInt ver = X509Cert->Version();
    71 		TInt ver = X509Cert->Version();
    74 	unsigned char verVal = (unsigned char)(ver-1);		
    72 		unsigned char verVal = (unsigned char)(ver-1);		
    75 	ASN1_INTEGER_free(ret->cert_info->version);
    73 		ASN1_INTEGER_free(ret->cert_info->version);
    76 	if( (verVal) || (!serial))// for X509 V1 certificates, version is null if any serial number present.
    74 		if( (verVal) || (!serail))// for X509 V1 certificates, version is null if any serial number present.
    77 		ret->cert_info->version = CreateASN1_STRING(1,V_ASN1_INTEGER,&verVal,0);
    75 			ret->cert_info->version = CreateASN1_STRING(1,V_ASN1_INTEGER,&verVal,0);
    78 	
    76 
    79 	
    77 
    80 	//issuerUID
    78 //issuerUID
    81 	const TPtrC8* issUID_enc = X509Cert->DataElementEncoding(CX509Certificate::EIssuerUID);
    79 		const TPtrC8* issUID_enc = X509Cert->DataElementEncoding(CX509Certificate::EIssuerUID);
    82 	if(issUID_enc)
    80 		if(issUID_enc)
    83 		ret->cert_info->issuerUID = CreateASN1_STRING(issUID_enc->Length(),V_ASN1_BIT_STRING,(unsigned char *)issUID_enc->Ptr(),0);
    81 			ret->cert_info->issuerUID = CreateASN1_STRING(issUID_enc->Length(),V_ASN1_BIT_STRING,(unsigned char *)issUID_enc->Ptr(),0);
    84 	
    82 		
    85 	
    83 
    86 	//subjectUID
    84 //subjectUID
    87 	const TPtrC8* subUID_enc = X509Cert->DataElementEncoding(CX509Certificate::ESubjectUID);
    85 		const TPtrC8* subUID_enc = X509Cert->DataElementEncoding(CX509Certificate::ESubjectUID);
    88 	if(subUID_enc)
    86 		if(subUID_enc)
    89 		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);
    90 	
    88 
    91 	//key
    89 //key
    92 	X509_PUBKEY_free(ret->cert_info->key);
    90 		X509_PUBKEY_free(ret->cert_info->key);
    93 	ret->cert_info->key = CreateX509_PUBKEYL(X509Cert);
    91 		ret->cert_info->key = CreateX509_PUBKEYL(X509Cert);
    94 	
    92 
    95 	//extension
    93 	
    96 	ret->cert_info->extensions = CreateSTACKOF_X509_EXTENSIONL(X509Cert);
    94 //extension
    97 	
    95 
    98 	//name
    96 
    99 	ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
    97 		ret->cert_info->extensions = CreateSTACKOF_X509_EXTENSIONL(X509Cert);
   100 	
    98 
   101 	//sig_alg
    99 		
   102 	X509_ALGOR_free(ret->sig_alg);
   100 //name
   103 	ret->sig_alg = CreateX509_ALGORL(sig_alg_ptr);
   101 		ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
   104 	
   102 
   105 	//signature
   103 //sig_alg
   106 	const TPtrC8 sig = X509Cert->Signature();
   104 		X509_ALGOR_free(ret->sig_alg);
   107 	ASN1_STRING_free(ret->signature);
   105 		ret->sig_alg = CreateX509_ALGORL(sig_alg_ptr);
   108 	ret->signature = CreateASN1_STRING(sig.Length(), V_ASN1_BIT_STRING, (unsigned char *)sig.Ptr(), ASN1_STRING_FLAG_BITS_LEFT);
   106 		
   109 	
   107 //signature
   110 	CleanupStack::Pop(ret);
   108 		const TPtrC8 sig = X509Cert->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);
       
   111 	}
       
   112 		
       
   113 	return ret;
   111 	return ret;
   114 }
   112 }
   115 
   113 
   116 
   114 
   117 X509_ALGOR* CX509_Initializer::CreateX509_ALGORL(const TPtrC8* ptr)
   115 X509_ALGOR* CX509_Initializer::CreateX509_ALGORL(const TPtrC8* ptr)
   118 {
   116 {
   119 	X509_ALGOR* ret = X509_ALGOR_new();
       
   120 	
       
   121 	TASN1DecGeneric dec((TDesC8 &)*ptr);	
   117 	TASN1DecGeneric dec((TDesC8 &)*ptr);	
   122 	dec.InitL();
   118 	dec.InitL();
   123 	
   119 	
   124 	TASN1DecSequence encSeq;
   120 	TASN1DecSequence encSeq;
   125 	CArrayPtrFlat<TASN1DecGeneric>* seq = encSeq.DecodeDERLC(dec);
   121 	CArrayPtrFlat<TASN1DecGeneric>* seq = encSeq.DecodeDERLC(dec);
   126 	
   122 	
   127 	TASN1DecGeneric& AlgorEncSeq = *(seq->At(0));
   123 	TASN1DecGeneric& AlgorEncSeq = *(seq->At(0));
   128 
   124 
       
   125 	X509_ALGOR* ret = X509_ALGOR_new();
       
   126 		
   129 	if (dec.LengthDERContent() > AlgorEncSeq.LengthDER()) // can also check for (seq->Count() > 1) alternatively
   127 	if (dec.LengthDERContent() > AlgorEncSeq.LengthDER()) // can also check for (seq->Count() > 1) alternatively
   130 	{
   128 	{
   131 		// parameter part is present in the encoding.
   129 		// parameter part is present in the encoding.
   132 		TASN1DecGeneric& ParameterEncSeq = *(seq->At(1));
   130 		TASN1DecGeneric& ParameterEncSeq = *(seq->At(1));
   133 		
   131 		
   168 
   166 
   169 
   167 
   170 
   168 
   171 X509_NAME* CX509_Initializer::CreateX509_NAMEL(const CX500DistinguishedName& DistName)
   169 X509_NAME* CX509_Initializer::CreateX509_NAMEL(const CX500DistinguishedName& DistName)
   172 {
   170 {
   173 	X509_NAME* ret = X509_NAME_new();
       
   174 	
       
   175 	CASN1EncSequence * Asn1Seq = DistName.EncodeASN1LC();			
   171 	CASN1EncSequence * Asn1Seq = DistName.EncodeASN1LC();			
   176 
   172 
   177 	HBufC8* octetData = HBufC8::NewMaxLC(5000);
   173 	HBufC8* octetData = HBufC8::NewMaxLC(5000);
   178 	TPtr8 oct(octetData->Des());
   174 	TPtr8 oct(octetData->Des());
   179 	oct.FillZ();
   175 	oct.FillZ();
   180 	oct.SetLength(KMaxNameLength);
   176 	oct.SetLength(KMaxNameLength);
   181 	TUint writePos = 0;
   177 	TUint writePos = 0;
   182 	Asn1Seq->WriteDERL(oct, writePos);			
   178 	Asn1Seq->WriteDERL(oct, writePos);			
   183 
   179 
       
   180 	X509_NAME* ret = X509_NAME_new();
       
   181 	// push the object to a cleanup stack as it is passed to a leaving function.
       
   182 	TCleanupItem item((void(*)(void*))X509_NAME_free,(void*)ret);
       
   183 	CleanupStack::PushL(item);
       
   184 	
   184   	TInt len = Fill_X509_NAME_ENTRYL(ret, octetData->Des());
   185   	TInt len = Fill_X509_NAME_ENTRYL(ret, octetData->Des());
   185 	
   186 	
       
   187   	CleanupStack::Pop(ret);
   186 	char *p = (char *)oct.PtrZ();
   188 	char *p = (char *)oct.PtrZ();
   187 
   189 
   188     ret->bytes->data = (char *)OPENSSL_malloc(len);        // no need to free this. BUF_MEM_free will free if not NULL
   190     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)
   191     if(ret->bytes->data)
   190      memcpy(ret->bytes->data, p, len);
   192      memcpy(ret->bytes->data, p, len);