File indexing completed on 2024-04-21 14:56:09

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2000,2001 George Staikos <staikos@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "ksslutils.h"
0022 
0023 #include <QString>
0024 #include <klocalizedstring.h>
0025 #include <QDate>
0026 
0027 #include "kopenssl.h"
0028 
0029 #if KSSL_HAVE_SSL
0030 // This code is mostly taken from OpenSSL v0.9.5a
0031 // by Eric Young
0032 QDateTime ASN1_UTCTIME_QDateTime(ASN1_UTCTIME *tm, int *isGmt)
0033 {
0034     QDateTime qdt;
0035     char *v;
0036     int gmt = 0;
0037     int i;
0038     int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
0039     QDate qdate;
0040     QTime qtime;
0041 
0042     i = tm->length;
0043     v = (char *)tm->data;
0044 
0045     if (i < 10) {
0046         goto auq_err;
0047     }
0048     if (v[i - 1] == 'Z') {
0049         gmt = 1;
0050     }
0051     for (i = 0; i < 10; ++i)
0052         if ((v[i] > '9') || (v[i] < '0')) {
0053             goto auq_err;
0054         }
0055     y = (v[0] - '0') * 10 + (v[1] - '0');
0056     if (y < 50) {
0057         y += 100;
0058     }
0059     M = (v[2] - '0') * 10 + (v[3] - '0');
0060     if ((M > 12) || (M < 1)) {
0061         goto auq_err;
0062     }
0063     d = (v[4] - '0') * 10 + (v[5] - '0');
0064     h = (v[6] - '0') * 10 + (v[7] - '0');
0065     m = (v[8] - '0') * 10 + (v[9] - '0');
0066     if ((v[10] >= '0') && (v[10] <= '9') &&
0067             (v[11] >= '0') && (v[11] <= '9')) {
0068         s = (v[10] - '0') * 10 + (v[11] - '0');
0069     }
0070 
0071     // localize the date and display it.
0072     qdate.setDate(y + 1900, M, d);
0073     qtime.setHMS(h, m, s);
0074     qdt.setDate(qdate); qdt.setTime(qtime);
0075 auq_err:
0076     if (isGmt) {
0077         *isGmt = gmt;
0078     }
0079     return qdt;
0080 }
0081 
0082 QString ASN1_UTCTIME_QString(ASN1_UTCTIME *tm)
0083 {
0084     QString qstr;
0085     int gmt;
0086     QDateTime qdt = ASN1_UTCTIME_QDateTime(tm, &gmt);
0087     qstr = qdt.toString(Qt::DefaultLocaleLongDate);
0088     if (gmt) {
0089         qstr += ' ';
0090         qstr += i18n("GMT");
0091     }
0092     return qstr;
0093 }
0094 
0095 QString ASN1_INTEGER_QString(ASN1_INTEGER *aint)
0096 {
0097     char *rep = KOSSL::self()->i2s_ASN1_INTEGER(nullptr, aint);
0098     QString yy = rep;
0099     KOSSL::self()->OPENSSL_free(rep);
0100     return yy;
0101 }
0102 
0103 #endif
0104