SigningTimeAttribute.cpp
Go to the documentation of this file.00001
00009 #include "SigningTimeAttribute.h"
00010
00011 #include "PKIFTime.h"
00012 #include "ToolkitUtils.h"
00013 #include "Buffer.h"
00014 #include "OID.h"
00015 #include "ASN1Helper.h"
00016
00017 #include "CryptographicMessageSyntax2004.h"
00018
00019 #include "PKIFMessageException.h"
00020
00021
00022
00024 struct CPKIFSigningTimeAttributeImpl
00025 {
00026 CPKIFTimePtr m_time;
00027 bool m_bUseUtcTimeIfPossible;
00028
00036 CPKIFSigningTimeAttributeImpl()
00037 {
00038 m_bUseUtcTimeIfPossible = false;
00039 };
00040
00041 void ParseAttributes (const unsigned char *buf, const unsigned int length);
00042 };
00050 void CPKIFSigningTimeAttributeImpl::ParseAttributes (
00052 const unsigned char *buf,
00054 const unsigned int length)
00055 {
00056 CACASNWRAPPER_CREATE(CACCMSAttributeValues, attrsVals);
00057 CACCMSAttributeValues* attributesList = attrsVals.Decode(buf, length);
00058
00059 ASN1OpenType* ot = (ASN1OpenType*)((DListNode*)attributesList->head)->data;
00060 CPKIFASNWrapper<CACCMSTime> cmsTime(BEREncCACCMSTime, BERDecCACCMSTime);
00061
00062
00063
00064 cmsTime.Decode(ot->data, ot->numocts);
00065
00066
00067
00068 CPKIFTimePtr tmpTime;
00069 CACX509V3Time* tmpCACX509V3Time = (CACX509V3Time*)cmsTime.data();
00070 if(tmpCACX509V3Time->t == T_CACX509V3Time_utcTime)
00071 {
00072 tmpTime = CPKIFTimePtr(new CPKIFTime(tmpCACX509V3Time->u.utcTime, UTCTIME));
00073 }
00074 else
00075 {
00076 tmpTime = CPKIFTimePtr(new CPKIFTime(tmpCACX509V3Time->u.generalTime, GENERALIZEDTIME));
00077 }
00078 m_time = tmpTime;
00079 }
00080
00081
00083
00084 char CPKIFSigningTimeAttribute::extOID[] = "1.2.840.113549.1.9.5";
00092 CPKIFSigningTimeAttribute::CPKIFSigningTimeAttribute()
00093 :m_impl (new CPKIFSigningTimeAttributeImpl)
00094 {
00095 LOG_STRING_DEBUG("CPKIFSigningTimeAttribute::CPKIFSigningTimeAttribute()", TOOLKIT_MESSAGE_ASN, 0, this);
00096
00097 m_impl->m_bUseUtcTimeIfPossible = false;
00098 }
00099
00107
00109
00110
00111
00112
00113
00114
00115
00116
00117
00125 CPKIFSigningTimeAttribute::CPKIFSigningTimeAttribute(
00127 const CPKIFBufferPtr& buf)
00128 :m_impl (new CPKIFSigningTimeAttributeImpl)
00129 {
00130 LOG_STRING_DEBUG("CPKIFSigningTimeAttribute::CPKIFSigningTimeAttribute(const CACCMSAttribute& ext)", TOOLKIT_MESSAGE_ASN, 0, this);
00131
00132 m_impl->m_bUseUtcTimeIfPossible = false;
00133 m_impl->ParseAttributes (buf->GetBuffer(), buf->GetLength());
00134 }
00135
00143 CPKIFSigningTimeAttribute::~CPKIFSigningTimeAttribute()
00144 {
00145 LOG_STRING_DEBUG("CPKIFSigningTimeAttribute::~CPKIFSigningTimeAttribute()", TOOLKIT_MESSAGE_ASN, 0, this);
00146
00147 delete m_impl;
00148 m_impl = NULL;
00149 }
00157 CPKIFTimePtr CPKIFSigningTimeAttribute::GetTime() const
00158 {
00159 return m_impl->m_time;
00160 }
00170 void CPKIFSigningTimeAttribute::SetTime(
00172 CPKIFTimePtr& time)
00173 {
00174 LOG_STRING_DEBUG("CPKIFSigningTimeAttribute::SetTime(CPKIFTimePtr& time)", TOOLKIT_MESSAGE_ASN, 0, this);
00175
00176
00177 if(time == (CPKIFTime*)NULL)
00178 throw CPKIFMessageException(TOOLKIT_MESSAGE, COMMON_INVALID_INPUT);
00179
00180 m_impl->m_time = time;
00181 }
00182
00183
00191 CPKIFOIDPtr CPKIFSigningTimeAttribute::GetOID() const
00192 {
00193 return g_signingTimeAttribute;
00194 }
00202 void CPKIFSigningTimeAttribute::GetValues(
00204 CPKIFBufferList& values) const
00205 {
00206 LOG_STRING_DEBUG("CPKIFSigningTimeAttribute::GetValues(CPKIFBufferList& values)", TOOLKIT_MESSAGE_ASN, 0, this);
00207
00208 if(m_impl->m_time)
00209 {
00210 CACCMSTime theTime;
00211 const char* tmpTime = m_impl->m_time->GetTime();
00212
00213 if(!m_impl->m_bUseUtcTimeIfPossible || 2050 <= m_impl->m_time->year() || 1950 > m_impl->m_time->year())
00214 {
00215 theTime.t = 2;
00216 theTime.u.generalTime = tmpTime;
00217 }
00218 else
00219 {
00220 theTime.t = 1;
00221 theTime.u.utcTime = tmpTime + 2;
00222 }
00223
00224 CPKIFASNWrapper<CACCMSTime> objPDU(BEREncCACCMSTime, BERDecCACCMSTime);
00225 ASN1OpenType* data1 = objPDU.Encode(&theTime);
00226
00227 try
00228 {
00229
00230 CPKIFBufferPtr tmp(new CPKIFBuffer((unsigned char*)data1->data, data1->numocts));
00231 delete data1; data1 = NULL;
00232 values.push_back(tmp);
00233 }
00234 catch(std::bad_alloc& ba)
00235 {
00236 if(data1)
00237 delete data1;
00238 throw ba;
00239 }
00240 }
00241 }
00249 void CPKIFSigningTimeAttribute::SetUseUtcTimeIfPossible(
00251 bool bUseUtcIfPossible)
00252 {
00253 m_impl->m_bUseUtcTimeIfPossible = bUseUtcIfPossible;
00254 }
00262 bool CPKIFSigningTimeAttribute::GetUseUtcTimeIfPossible()
00263 {
00264 return m_impl->m_bUseUtcTimeIfPossible;
00265 }