759 // contributed by Jay Case of Sarvega, Inc.; http://sarvega.com/ |
759 // contributed by Jay Case of Sarvega, Inc.; http://sarvega.com/ |
760 // Based on X509_cmp_time() for intitial buffer hacking. |
760 // Based on X509_cmp_time() for intitial buffer hacking. |
761 //============================================================================== |
761 //============================================================================== |
762 QDateTime q_getTimeFromASN1(const ASN1_TIME *aTime) |
762 QDateTime q_getTimeFromASN1(const ASN1_TIME *aTime) |
763 { |
763 { |
764 char lBuffer[24]; |
|
765 char *pBuffer = lBuffer; |
|
766 |
|
767 size_t lTimeLength = aTime->length; |
764 size_t lTimeLength = aTime->length; |
768 char *pString = (char *) aTime->data; |
765 char *pString = (char *) aTime->data; |
769 |
766 |
770 if (aTime->type == V_ASN1_UTCTIME) { |
767 if (aTime->type == V_ASN1_UTCTIME) { |
|
768 |
|
769 char lBuffer[24]; |
|
770 char *pBuffer = lBuffer; |
|
771 |
771 if ((lTimeLength < 11) || (lTimeLength > 17)) |
772 if ((lTimeLength < 11) || (lTimeLength > 17)) |
772 return QDateTime(); |
773 return QDateTime(); |
773 |
774 |
774 memcpy(pBuffer, pString, 10); |
775 memcpy(pBuffer, pString, 10); |
775 pBuffer += 10; |
776 pBuffer += 10; |
776 pString += 10; |
777 pString += 10; |
|
778 |
|
779 if ((*pString == 'Z') || (*pString == '-') || (*pString == '+')) { |
|
780 *pBuffer++ = '0'; |
|
781 *pBuffer++ = '0'; |
|
782 } else { |
|
783 *pBuffer++ = *pString++; |
|
784 *pBuffer++ = *pString++; |
|
785 // Skip any fractional seconds... |
|
786 if (*pString == '.') { |
|
787 pString++; |
|
788 while ((*pString >= '0') && (*pString <= '9')) |
|
789 pString++; |
|
790 } |
|
791 } |
|
792 |
|
793 *pBuffer++ = 'Z'; |
|
794 *pBuffer++ = '\0'; |
|
795 |
|
796 time_t lSecondsFromUCT; |
|
797 if (*pString == 'Z') { |
|
798 lSecondsFromUCT = 0; |
|
799 } else { |
|
800 if ((*pString != '+') && (*pString != '-')) |
|
801 return QDateTime(); |
|
802 |
|
803 lSecondsFromUCT = ((pString[1] - '0') * 10 + (pString[2] - '0')) * 60; |
|
804 lSecondsFromUCT += (pString[3] - '0') * 10 + (pString[4] - '0'); |
|
805 lSecondsFromUCT *= 60; |
|
806 if (*pString == '-') |
|
807 lSecondsFromUCT = -lSecondsFromUCT; |
|
808 } |
|
809 |
|
810 tm lTime; |
|
811 lTime.tm_sec = ((lBuffer[10] - '0') * 10) + (lBuffer[11] - '0'); |
|
812 lTime.tm_min = ((lBuffer[8] - '0') * 10) + (lBuffer[9] - '0'); |
|
813 lTime.tm_hour = ((lBuffer[6] - '0') * 10) + (lBuffer[7] - '0'); |
|
814 lTime.tm_mday = ((lBuffer[4] - '0') * 10) + (lBuffer[5] - '0'); |
|
815 lTime.tm_mon = (((lBuffer[2] - '0') * 10) + (lBuffer[3] - '0')) - 1; |
|
816 lTime.tm_year = ((lBuffer[0] - '0') * 10) + (lBuffer[1] - '0'); |
|
817 if (lTime.tm_year < 50) |
|
818 lTime.tm_year += 100; // RFC 2459 |
|
819 |
|
820 QDate resDate(lTime.tm_year + 1900, lTime.tm_mon + 1, lTime.tm_mday); |
|
821 QTime resTime(lTime.tm_hour, lTime.tm_min, lTime.tm_sec); |
|
822 |
|
823 QDateTime result(resDate, resTime, Qt::UTC); |
|
824 result = result.addSecs(lSecondsFromUCT); |
|
825 return result; |
|
826 |
|
827 } else if (aTime->type == V_ASN1_GENERALIZEDTIME) { |
|
828 |
|
829 if (lTimeLength < 15) |
|
830 return QDateTime(); // hopefully never triggered |
|
831 |
|
832 // generalized time is always YYYYMMDDHHMMSSZ (RFC 2459, section 4.1.2.5.2) |
|
833 tm lTime; |
|
834 lTime.tm_sec = ((pString[12] - '0') * 10) + (pString[13] - '0'); |
|
835 lTime.tm_min = ((pString[10] - '0') * 10) + (pString[11] - '0'); |
|
836 lTime.tm_hour = ((pString[8] - '0') * 10) + (pString[9] - '0'); |
|
837 lTime.tm_mday = ((pString[6] - '0') * 10) + (pString[7] - '0'); |
|
838 lTime.tm_mon = (((pString[4] - '0') * 10) + (pString[5] - '0')); |
|
839 lTime.tm_year = ((pString[0] - '0') * 1000) + ((pString[1] - '0') * 100) + |
|
840 ((pString[2] - '0') * 10) + (pString[3] - '0'); |
|
841 |
|
842 QDate resDate(lTime.tm_year, lTime.tm_mon, lTime.tm_mday); |
|
843 QTime resTime(lTime.tm_hour, lTime.tm_min, lTime.tm_sec); |
|
844 |
|
845 QDateTime result(resDate, resTime, Qt::UTC); |
|
846 return result; |
|
847 |
777 } else { |
848 } else { |
778 if (lTimeLength < 13) |
849 qWarning("unsupported date format detected"); |
779 return QDateTime(); |
850 return QDateTime(); |
780 |
851 } |
781 memcpy(pBuffer, pString, 12); |
852 |
782 pBuffer += 12; |
|
783 pString += 12; |
|
784 } |
|
785 |
|
786 if ((*pString == 'Z') || (*pString == '-') || (*pString == '+')) { |
|
787 *pBuffer++ = '0'; |
|
788 *pBuffer++ = '0'; |
|
789 } else { |
|
790 *pBuffer++ = *pString++; |
|
791 *pBuffer++ = *pString++; |
|
792 // Skip any fractional seconds... |
|
793 if (*pString == '.') { |
|
794 pString++; |
|
795 while ((*pString >= '0') && (*pString <= '9')) |
|
796 pString++; |
|
797 } |
|
798 } |
|
799 |
|
800 *pBuffer++ = 'Z'; |
|
801 *pBuffer++ = '\0'; |
|
802 |
|
803 time_t lSecondsFromUCT; |
|
804 if (*pString == 'Z') { |
|
805 lSecondsFromUCT = 0; |
|
806 } else { |
|
807 if ((*pString != '+') && (*pString != '-')) |
|
808 return QDateTime(); |
|
809 |
|
810 lSecondsFromUCT = ((pString[1] - '0') * 10 + (pString[2] - '0')) * 60; |
|
811 lSecondsFromUCT += (pString[3] - '0') * 10 + (pString[4] - '0'); |
|
812 lSecondsFromUCT *= 60; |
|
813 if (*pString == '-') |
|
814 lSecondsFromUCT = -lSecondsFromUCT; |
|
815 } |
|
816 |
|
817 tm lTime; |
|
818 lTime.tm_sec = ((lBuffer[10] - '0') * 10) + (lBuffer[11] - '0'); |
|
819 lTime.tm_min = ((lBuffer[8] - '0') * 10) + (lBuffer[9] - '0'); |
|
820 lTime.tm_hour = ((lBuffer[6] - '0') * 10) + (lBuffer[7] - '0'); |
|
821 lTime.tm_mday = ((lBuffer[4] - '0') * 10) + (lBuffer[5] - '0'); |
|
822 lTime.tm_mon = (((lBuffer[2] - '0') * 10) + (lBuffer[3] - '0')) - 1; |
|
823 lTime.tm_year = ((lBuffer[0] - '0') * 10) + (lBuffer[1] - '0'); |
|
824 if (lTime.tm_year < 50) |
|
825 lTime.tm_year += 100; // RFC 2459 |
|
826 |
|
827 QDate resDate(lTime.tm_year + 1900, lTime.tm_mon + 1, lTime.tm_mday); |
|
828 QTime resTime(lTime.tm_hour, lTime.tm_min, lTime.tm_sec); |
|
829 QDateTime result(resDate, resTime, Qt::UTC); |
|
830 result = result.addSecs(lSecondsFromUCT); |
|
831 return result; |
|
832 } |
853 } |
833 |
854 |
834 QT_END_NAMESPACE |
855 QT_END_NAMESPACE |