~ubuntu-branches/ubuntu/oneiric/xca/oneiric

« back to all changes in this revision

Viewing changes to lib/asn1time.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Tino Keitel
  • Date: 2009-09-14 20:55:44 UTC
  • Revision ID: james.westby@ubuntu.com-20090914205544-l4qsm2tjimhd62vo
Tags: upstream-0.7.0
ImportĀ upstreamĀ versionĀ 0.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vi: set sw=4 ts=4:
 
2
 *
 
3
 * Copyright (C) 2001 - 2007 Christian Hohnstaedt.
 
4
 *
 
5
 * All rights reserved.
 
6
 */
 
7
 
 
8
#include "base.h"
 
9
#include "asn1time.h"
 
10
#include <openssl/x509.h>
 
11
#include <openssl/err.h>
 
12
#include <openssl/opensslv.h>
 
13
 
 
14
a1time::a1time()
 
15
{
 
16
        time = ASN1_GENERALIZEDTIME_new();
 
17
        now();
 
18
}
 
19
 
 
20
a1time::a1time(const ASN1_TIME *a)
 
21
{
 
22
        time = NULL;
 
23
        set(a);
 
24
}
 
25
 
 
26
a1time::a1time(const a1time &a)
 
27
{
 
28
        time = NULL;
 
29
        set(a.time);
 
30
}
 
31
 
 
32
a1time::~a1time()
 
33
{
 
34
        ASN1_TIME_free(time);
 
35
}
 
36
 
 
37
ASN1_TIME *a1time::get() const
 
38
{
 
39
        return M_ASN1_TIME_dup(time);
 
40
}
 
41
 
 
42
ASN1_TIME *a1time::get_utc() const
 
43
{
 
44
        return toUTCtime();
 
45
}
 
46
 
 
47
a1time &a1time::set(const ASN1_TIME *a)
 
48
{
 
49
        if (a == NULL) {
 
50
                set((time_t)0);
 
51
        }
 
52
        else {
 
53
                ASN1_TIME *siko = time;
 
54
                time = ASN1_TIME_to_generalizedtime((ASN1_TIME *)a, &siko);
 
55
                if (!time) {
 
56
                        if (siko) ASN1_TIME_free(siko);
 
57
                        time=M_ASN1_TIME_dup(a);
 
58
                }
 
59
        }
 
60
        return *this;
 
61
}
 
62
 
 
63
a1time &a1time::set(time_t t)
 
64
{
 
65
        ASN1_GENERALIZEDTIME_set(time, t);
 
66
        return *this;
 
67
}
 
68
 
 
69
a1time &a1time::set(int y, int mon, int d, int h, int m, int s)
 
70
{
 
71
        QString gt;
 
72
        gt.sprintf("%04d%02d%02d%02d%02d%02dZ", y, mon, d, h, m ,s);
 
73
        return set(gt);
 
74
}
 
75
 
 
76
 
 
77
QString a1time::toPretty() const
 
78
{
 
79
        QString t = "";
 
80
        if (!time) return t;
 
81
        BIO * bio = BIO_new(BIO_s_mem());
 
82
        char buf[200];
 
83
        ASN1_TIME_print(bio, time);
 
84
        BIO_gets(bio, buf, 200);
 
85
        t = buf;
 
86
        BIO_free(bio);
 
87
        return t;
 
88
}
 
89
 
 
90
QString a1time::toPlain() const
 
91
{
 
92
        QString t = "";
 
93
        char b[15];
 
94
        if (!time) return t;
 
95
        memcpy(b, time->data, time->length);
 
96
        b[time->length] = '\0';
 
97
        t = b;
 
98
        return t;
 
99
}
 
100
 
 
101
QString a1time::toSortable() const
 
102
{
 
103
        int y,m,d,g;
 
104
        QString t = "";
 
105
        if (!time) return t;
 
106
        if (ymdg( &y ,&m ,&d ,&g)) {
 
107
                // openssl_error("time error");
 
108
        }
 
109
        char buf[20];
 
110
        sprintf(buf, "%04d-%02d-%02d %s",y,m,d,(g==1)?"GMT":"");
 
111
        t = buf;
 
112
        return t;
 
113
}
 
114
 
 
115
a1time &a1time::set(const QString &s)
 
116
{
 
117
        const char *x = s.toAscii();
 
118
        ASN1_GENERALIZEDTIME_set_string(time, (char*)x);
 
119
        return *this;
 
120
}
 
121
 
 
122
int a1time::ymdg(int *y, int *m, int *d, int *g) const
 
123
{
 
124
        int h, M, s;
 
125
        return ymdg(y,m,d, &h, &M, &s, g);
 
126
}
 
127
 
 
128
int a1time::ymdg(int *y, int *m, int *d, int *h, int *M, int *s, int *g) const
 
129
{
 
130
        char *v;
 
131
        int i;
 
132
        *y=0, *m=0, *d=0, *g=0;
 
133
        if (!time) return 1;
 
134
        i=time->length;
 
135
        v=(char *)time->data;
 
136
 
 
137
        if (i < 14) return 1; /* it is at least 10 digits */
 
138
        if (v[i-1] == 'Z') *g=1;
 
139
        for (i=0; i<14; i++)
 
140
                if ((v[i] > '9') || (v[i] < '0')) return 1;
 
141
        *y= (v[0]-'0')*1000+(v[1]-'0')*100+(v[2]-'0')*10+(v[3]-'0');
 
142
        *m= (v[4]-'0')*10+(v[5]-'0');
 
143
        if ((*m > 12) || (*m < 1)) return 1;
 
144
        *d= (v[6]-'0')*10+(v[7]-'0');
 
145
        if ((*d > 31) || (*d < 1)) return 1;
 
146
        *h= (v[8]-'0')*10+(v[9]-'0');
 
147
        if ((*h > 23) || (*h < 0)) return 1;
 
148
        *M= (v[10]-'0')*10+(v[11]-'0');
 
149
        if ((*M > 59) || (*M < 0)) return 1;
 
150
        *s= (v[12]-'0')*10+(v[13]-'0');
 
151
        if ((*s > 59) || (*s < 0)) return 1;
 
152
        return 0;
 
153
}
 
154
 
 
155
a1time &a1time::now(int delta)
 
156
{
 
157
        X509_gmtime_adj(time, delta);
 
158
        return *this;
 
159
}
 
160
 
 
161
a1time &a1time::operator = (const a1time &a)
 
162
{
 
163
        set(a.time);
 
164
        return *this;
 
165
}
 
166
 
 
167
bool const a1time::operator > (const a1time &a)
 
168
{
 
169
        return (ASN1_STRING_cmp(time, a.time) == 1);
 
170
}
 
171
 
 
172
bool const a1time::operator < (const a1time &a)
 
173
{
 
174
        return (ASN1_STRING_cmp(time, a.time) == -1);
 
175
}
 
176
 
 
177
bool const a1time::operator == (const a1time &a)
 
178
{
 
179
        return (ASN1_STRING_cmp(time, a.time) == 0);
 
180
}
 
181
 
 
182
bool const a1time::operator != (const a1time &a)
 
183
{
 
184
        return (ASN1_STRING_cmp(time, a.time) != 0);
 
185
}
 
186
 
 
187
unsigned char *a1time::d2i(const unsigned char *p, int size)
 
188
{
 
189
        if (time)
 
190
                ASN1_TIME_free(time);
 
191
        time = D2I_CLASH(d2i_ASN1_TIME, NULL, &p, size);
 
192
        return (unsigned char *)p;
 
193
}
 
194
 
 
195
unsigned char *a1time::i2d(unsigned char *p)
 
196
{
 
197
        unsigned char *mp = p;
 
198
        i2d_ASN1_TIME(time, &mp);
 
199
        return mp;
 
200
}
 
201
 
 
202
int a1time::derSize() const
 
203
{
 
204
        return i2d_ASN1_TIME(time, NULL);
 
205
}
 
206
 
 
207
 
 
208
ASN1_UTCTIME *a1time::toUTCtime() const
 
209
{
 
210
        ASN1_UTCTIME *ret;
 
211
        int year=0,i ;
 
212
        // if (!ASN1_TIME_check(t)) return NULL;
 
213
        for (i=0; i<4; i++) {
 
214
                year *= 10;
 
215
                year += time->data[i] - '0';
 
216
        }
 
217
        if (year > 2049 || year <1950)
 
218
                return NULL;
 
219
 
 
220
        if (!(ret = ASN1_UTCTIME_new ()))
 
221
                return NULL;
 
222
 
 
223
        /* If already UTC Time just copy across */
 
224
        if (time->type == V_ASN1_UTCTIME) {
 
225
                if(!ASN1_STRING_set(ret, time->data, time->length))
 
226
                        return NULL;
 
227
                return ret;
 
228
        }
 
229
 
 
230
        /* copy w/o 19 or 20 */
 
231
        if (!ASN1_STRING_set(ret, time->data+2, time->length - 2))
 
232
                return NULL;
 
233
 
 
234
        return ret;
 
235
}