aboutsummaryrefslogtreecommitdiffstats
path: root/smime/lib/e-asn1-object.c
blob: 49002d1cfc48cf538ea2e3ff565da9ce5c35ff60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* e-cert.c
 *
 * Copyright (C) 2003 Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Chris Toshok (toshok@ximian.com)
 */

/* The following is the mozilla license blurb, as the bodies some of
   these functions were derived from the mozilla source. */

/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape security libraries.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-asn1-object.h"

#include "secasn1.h"

struct _EASN1ObjectPrivate {
    PRUint32 tag;
    PRUint32 type;
    gboolean valid_container;

    GList *children;

    char *display_name;
    char *value;

    char *data;
    guint data_len;
};

#define PARENT_TYPE G_TYPE_OBJECT
static GObjectClass *parent_class;

static void
e_asn1_object_dispose (GObject *object)
{
    EASN1Object *obj = E_ASN1_OBJECT (object);
    if (obj->priv) {

        if (obj->priv->display_name)
            g_free (obj->priv->display_name);

        if (obj->priv->value)
            g_free (obj->priv->value);

        g_list_foreach (obj->priv->children, (GFunc)g_object_unref, NULL);
        g_list_free (obj->priv->children);

        g_free (obj->priv);
        obj->priv = NULL;
    }
}

static void
e_asn1_object_class_init (EASN1ObjectClass *klass)
{
    GObjectClass *object_class;

    object_class = G_OBJECT_CLASS(klass);

    parent_class = g_type_class_ref (PARENT_TYPE);

    object_class->dispose = e_asn1_object_dispose;
}

static void
e_asn1_object_init (EASN1Object *asn1)
{
    asn1->priv = g_new0 (EASN1ObjectPrivate, 1);

    asn1->priv->valid_container = TRUE;
}

GType
e_asn1_object_get_type (void)
{
    static GType asn1_object_type = 0;

    if (!asn1_object_type) {
        static const GTypeInfo asn1_object_info =  {
            sizeof (EASN1ObjectClass),
            NULL,           /* base_init */
            NULL,           /* base_finalize */
            (GClassInitFunc) e_asn1_object_class_init,
            NULL,           /* class_finalize */
            NULL,           /* class_data */
            sizeof (EASN1Object),
            0,             /* n_preallocs */
            (GInstanceInitFunc) e_asn1_object_init,
        };

        asn1_object_type = g_type_register_static (PARENT_TYPE, "EASN1Object", &asn1_object_info, 0);
    }

    return asn1_object_type;
}


/* This function is used to interpret an integer that
   was encoded in a DER buffer. This function is used
   when converting a DER buffer into a nsIASN1Object 
   structure.  This interprets the buffer in data
   as defined by the DER (Distinguised Encoding Rules) of
   ASN1.
*/
static int
get_integer_256 (unsigned char *data, unsigned int nb)
{
    int val;

    switch (nb) {
    case 1:
        val = data[0];
        break;
    case 2:
        val = (data[0] << 8) | data[1];
        break;
    case 3:
        val = (data[0] << 16) | (data[1] << 8) | data[2];
        break;
    case 4:
        val = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
        break;
    default:
        return -1;
    }

    return val;
}

/* This function is used to retrieve the lenght of a DER encoded
   item.  It looks to see if this a multibyte length and then
   interprets the buffer accordingly to get the actual length value.
   This funciton is used mostly while parsing the DER headers.
   
   A DER encoded item has the following structure:

   <tag><length<data consisting of lenght bytes>
*/
static guint32
get_der_item_length (unsigned char *data, unsigned char *end,
             unsigned long *bytesUsed, gboolean *indefinite)
{
    unsigned char lbyte = *data++;
    PRInt32 length = -1;
  
    *indefinite = FALSE;
    if (lbyte >= 0x80) {
        /* Multibyte length */
        unsigned nb = (unsigned) (lbyte & 0x7f);
        if (nb > 4) {
            return -1;
        }
        if (nb > 0) {
            
            if ((data+nb) > end) {
                return -1;
            }
            length = get_integer_256 (data, nb);
            if (length < 0)
                return -1;
        } else {
            *indefinite = TRUE;
            length = 0;
        }
        *bytesUsed = nb+1;
    } else {
        length = lbyte;
        *bytesUsed = 1; 
    }
    return length;
}

static gboolean
build_from_der (EASN1Object *parent, char *data, char *end)
{
    unsigned long bytesUsed;
    gboolean indefinite;
    PRInt32 len;
    PRUint32 type;
    unsigned char code, tagnum;
    EASN1Object *asn1object = NULL;

    if (data >= end)
        return TRUE;

    /*
      A DER item has the form of |tag|len|data
      tag is one byte and describes the type of elment
      we are dealing with.
      len is a DER encoded int telling us how long the data is
      data is a buffer that is len bytes long and has to be
      interpreted according to its type.
    */

    while (data < end) {
        code = *data;
        tagnum = code & SEC_ASN1_TAGNUM_MASK;

        /*
         * NOTE: This code does not (yet) handle the high-tag-number form!
         */
        if (tagnum == SEC_ASN1_HIGH_TAG_NUMBER) {
            return FALSE;
        }
        data++;
        len = get_der_item_length ((unsigned char *)data, (unsigned char *)end, &bytesUsed, &indefinite);
        data += bytesUsed;
        if ((len < 0) || ((data+len) > end))
            return FALSE;

        if (code & SEC_ASN1_CONSTRUCTED) {
            if (len > 0 || indefinite) {
                switch (code & SEC_ASN1_CLASS_MASK) {
                case SEC_ASN1_UNIVERSAL:
                    type = tagnum;
                    break;
                case SEC_ASN1_APPLICATION:
                    type = E_ASN1_OBJECT_TYPE_APPLICATION;
                    break;
                case SEC_ASN1_CONTEXT_SPECIFIC:
                    type = E_ASN1_OBJECT_TYPE_CONTEXT_SPECIFIC;
                    break;
                case SEC_ASN1_PRIVATE:
                    type = E_ASN1_OBJECT_TYPE_PRIVATE;
                    break;
                default:
                    g_warning ("bad DER");
                    return FALSE;
                }

                asn1object = e_asn1_object_new ();
                asn1object->priv->tag = tagnum;
                asn1object->priv->type = type;
                
                if (!build_from_der (asn1object, data, (len == 0) ? end : data + len)) {
                    g_object_unref (asn1object);
                    return FALSE;
                }
            }
        } else {
            asn1object = e_asn1_object_new ();

            asn1object->priv->type = tagnum;
            asn1object->priv->tag = tagnum;

            /*printableItem->SetData((char*)data, len);*/
        }
        data += len;

        parent->priv->children = g_list_append (parent->priv->children, asn1object);
    }

    return TRUE;
}

EASN1Object*
e_asn1_object_new_from_der (char *data, guint32 len)
{
    EASN1Object *obj = g_object_new (E_TYPE_ASN1_OBJECT, NULL);

    if (!build_from_der (obj, data, data + len)) {
        g_object_unref (obj);
        return NULL;
    }

    return obj;
}

EASN1Object*
e_asn1_object_new (void)
{
    return E_ASN1_OBJECT (g_object_new (E_TYPE_ASN1_OBJECT, NULL));
}


void
e_asn1_object_set_valid_container (EASN1Object *obj, gboolean flag)
{
    obj->priv->valid_container = flag;
}

gboolean
e_asn1_object_is_valid_container (EASN1Object *obj)
{
    return obj->priv->valid_container;
}

PRUint32
e_asn1_object_get_asn1_type (EASN1Object *obj)
{
    return obj->priv->type;
}

PRUint32
e_asn1_object_get_asn1_tag (EASN1Object *obj)
{
    return obj->priv->tag;
}

GList*
e_asn1_object_get_children (EASN1Object *obj)
{
    GList *children = g_list_copy (obj->priv->children);

    g_list_foreach (children, (GFunc)g_object_ref, NULL);

    return children;
}

void
e_asn1_object_append_child (EASN1Object *parent, EASN1Object *child)
{
    parent->priv->children = g_list_append (parent->priv->children, g_object_ref (child));
}

void
e_asn1_object_set_display_name (EASN1Object *obj, const char *name)
{
    g_free (obj->priv->display_name);
    obj->priv->display_name = g_strdup (name);
}

const char*
e_asn1_object_get_display_name (EASN1Object *obj)
{
    return obj->priv->display_name;
}

void
e_asn1_object_set_display_value (EASN1Object *obj, const char *value)
{
    g_free (obj->priv->value);
    obj->priv->value = g_strdup (value);
}

const char*
e_asn1_object_get_display_value (EASN1Object *obj)
{
    return obj->priv->value;
}

void
e_asn1_object_get_data (EASN1Object *obj, char **data, guint32 *len)
{
    *data = obj->priv->data;
    *len = obj->priv->data_len;
}