File: ppb_x509_certificate.c

package info (click to toggle)
freshplayerplugin 0.3.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 5,324 kB
  • ctags: 13,257
  • sloc: ansic: 45,542; cpp: 26,176; yacc: 1,707; lex: 910; python: 176; sh: 87; makefile: 21
file content (476 lines) | stat: -rw-r--r-- 15,611 bytes parent folder | download | duplicates (2)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * Copyright © 2013-2015  Rinat Ibragimov
 *
 * This file is part of FreshPlayerPlugin.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "ppb_x509_certificate.h"
#include "ppb_var.h"
#include <stdlib.h>
#include "trace.h"
#include "tables.h"
#include "reverse_constant.h"
#include <openssl/x509.h>
#include <ctype.h>
#include "pp_interface.h"


PP_Resource
ppb_x509_certificate_create(PP_Instance instance)
{
    struct pp_instance_s *pp_i = tables_get_pp_instance(instance);
    if (!pp_i) {
        trace_error("%s, bad instance\n", __func__);
        return 0;
    }
    PP_Resource x509 = pp_resource_allocate(PP_RESOURCE_X509_CERTIFICATE, pp_i);
    return x509;
}

static
void
ppb_x509_certificate_destroy(void *ptr)
{
    struct pp_x509_certificate_s *xc = ptr;
    if (xc->cert) {
        X509_free(xc->cert);
        xc->cert = NULL;
    }
}

PP_Bool
ppb_x509_certificate_is_x509_certificate(PP_Resource resource)
{
    return pp_resource_get_type(resource) == PP_RESOURCE_X509_CERTIFICATE;
}

PP_Bool
ppb_x509_certificate_initialize(PP_Resource resource, const char *bytes, uint32_t length)
{
    const unsigned char **in = (const unsigned char **)&bytes;
    struct pp_x509_certificate_s *xc = pp_resource_acquire(resource, PP_RESOURCE_X509_CERTIFICATE);
    if (!xc) {
        return PP_FALSE;
    }

    PP_Bool retval = PP_TRUE;
    if (d2i_X509(&xc->cert, in, length) == NULL) {
        retval = PP_FALSE;
        goto done;
    }

    free_and_nullify(xc->raw_data);
    xc->raw_data = malloc(length);
    if (xc->raw_data) {
        memcpy(xc->raw_data, bytes, length);
        xc->raw_data_length = length;
    }

done:
    pp_resource_release(resource);
    return retval;
}

static struct PP_Var
get_xname_field_value(X509_NAME *xname, int nid)
{
    ASN1_STRING *asn1_str;
    int          loc;

    loc = X509_NAME_get_index_by_NID(xname, nid, -1);
    if (loc == -1)
        return PP_MakeNull();
    asn1_str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(xname, loc));
    return ppb_var_var_from_utf8((char *)asn1_str->data, asn1_str->length);
}

static struct PP_Var
get_cert_issuer_field_value(X509 *cert, int nid)
{
    X509_NAME *xname = X509_get_issuer_name(cert);
    if (!xname)
        return PP_MakeNull();
    return get_xname_field_value(xname, nid);
}

static struct PP_Var
get_cert_subject_field_value(X509 *cert, int nid)
{
    X509_NAME *xname = X509_get_subject_name(cert);
    if (!xname)
        return PP_MakeNull();
    return get_xname_field_value(xname, nid);
}

static struct PP_Var
convert_tm_to_pp_var(struct tm *t, double subsec, int hofs, int mofs)
{
    return PP_MakeDouble(timegm(t) - hofs * 3600 - mofs * 60 + subsec);
}

static struct PP_Var
convert_asn1_time_to_pp_var(ASN1_TIME *asn1_time)
{
    struct tm t;
    int len = asn1_time->length;
    unsigned char *s = asn1_time->data;

    memset(&t, 0, sizeof(t));
    if (asn1_time->type == V_ASN1_GENERALIZEDTIME) {
        // "YYYYMMDDHH[MM[SS[.fff]]]", "YYYYMMDDHH[MM[SS[.fff]]]Z",
        // or "YYYYMMDDHH[MM[SS[.fff]]]+-HHMM"
        double subsec = 0;

        // YYYY
        if (len < 4 || !isdigit(s[0]) || !isdigit(s[1]) || !isdigit(s[2]) || !isdigit(s[3]))
            goto parse_error;
        t.tm_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
        t.tm_year -= 1900;
        len -= 4; s += 4;

        // MM
        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        t.tm_mon = (s[0] - '0') * 10 + (s[1] - '0') - 1;
        len -= 2; s += 2;

        // DD
        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        t.tm_mday = (s[0] - '0') * 10 + (s[1] - '0');
        len -= 2; s += 2;

        // HH
        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
        len -= 2; s += 2;

        if (len >= 2 && isdigit(s[0])) {
            // MM
            if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
                goto parse_error;
            t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
            len -= 2; s += 2;

            if (len >= 2 && isdigit(s[0])) {
                // SS
                if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
                    goto parse_error;
                t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
                len -= 2; s += 2;

                if (len >= 4 && s[0] == '.' && isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
                    // .fff
                    subsec = (s[1] - '0') * 0.1 + (s[2] - '0') * 0.01 + (s[3] - '0') * 0.001;
                    len -= 4; s += 4;
                }
            }
        }

        if (len == 0)
            return convert_tm_to_pp_var(&t, subsec, 0, 0);

        if (len == 1) {
            if (s[0] == 'Z')
                return convert_tm_to_pp_var(&t, subsec, 0, 0);
            else
                goto parse_error;
        }

        if (len < 1 || (s[0] != '+' && s[0] != '-'))
            goto parse_error;

        int tz_sign = (s[0] == '+') ? 1 : -1;
        len -= 1; s += 1;

        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        int tz_hours = (s[0] - '0') * 10 + (s[1] - '0');
        len -= 2; s += 2;

        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        int tz_minutes = (s[0] - '0') * 10 + (s[1] - '0');
        // keep as reminder: len -= 2; s += 2;

        return convert_tm_to_pp_var(&t, subsec, tz_sign * tz_hours, tz_minutes);
    } else if (asn1_time->type == V_ASN1_UTCTIME) {
        // either "YYMMDDhhmm[ss]Z" or "YYMMDDhhmm[ss](+|-)hhmm"

        // YY
        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        t.tm_year = (s[0] - '0') * 10 + (s[1] - '0');
        t.tm_year += (t.tm_year < 70) * 100;
        len -= 2; s += 2;

        // MM
        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        t.tm_mon = (s[0] - '0') * 10 + (s[1] - '0') - 1;
        len -= 2; s += 2;

        // DD
        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        t.tm_mday = (s[0] - '0') * 10 + (s[1] - '0');
        len -= 2; s += 2;

        // hh
        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
        len -= 2; s += 2;

        // mm
        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
        len -= 2; s += 2;

        if (len >= 2 && isdigit(s[0])) {
            // ss
            if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
                goto parse_error;
            t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
            len -= 2; s += 2;
        }

        if (len == 1 && s[0] == 'Z')
            return convert_tm_to_pp_var(&t, 0, 0, 0);

        if (len < 1 || (s[0] != '+' && s[0] != '-'))
            goto parse_error;

        int tz_sign = (s[0] == '+') ? 1 : -1;
        len -= 1; s += 1;

        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        int tz_hours = (s[0] - '0') * 10 + (s[1] - '0');
        len -= 2; s += 2;

        if (len < 2 || !isdigit(s[0]) || !isdigit(s[1]))
            goto parse_error;
        int tz_minutes = (s[0] - '0') * 10 + (s[1] - '0');
        // keep as reminder: len -= 2; s += 2;

        return convert_tm_to_pp_var(&t, 0, tz_sign * tz_hours, tz_minutes);
    } else {
        goto parse_error;
    }

parse_error:
    return PP_MakeNull();
}

struct PP_Var
ppb_x509_certificate_get_field(PP_Resource resource, PP_X509Certificate_Private_Field field)
{
    struct PP_Var    var = PP_MakeNull();
    ASN1_INTEGER    *asn1_int;
    ASN1_TIME       *asn1_time;

    struct pp_x509_certificate_s *xc = pp_resource_acquire(resource, PP_RESOURCE_X509_CERTIFICATE);
    if (!xc) {
        trace_error("%s, bad resource\n", __func__);
        return PP_MakeNull();
    }

    switch (field) {
    case PP_X509CERTIFICATE_PRIVATE_ISSUER_COMMON_NAME:
        var = get_cert_issuer_field_value(xc->cert, NID_commonName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_ISSUER_LOCALITY_NAME:
        var = get_cert_issuer_field_value(xc->cert, NID_localityName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_ISSUER_STATE_OR_PROVINCE_NAME:
        var = get_cert_issuer_field_value(xc->cert, NID_stateOrProvinceName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_ISSUER_COUNTRY_NAME:
        var = get_cert_issuer_field_value(xc->cert, NID_countryName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_NAME:
        var = get_cert_issuer_field_value(xc->cert, NID_organizationName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_UNIT_NAME:
        var = get_cert_issuer_field_value(xc->cert, NID_organizationalUnitName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_ISSUER_UNIQUE_ID:
        var = PP_MakeNull(); // see ppapi/c/private/ppb_x509_certificate_private.h for details
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_COMMON_NAME:
        var = get_cert_subject_field_value(xc->cert, NID_commonName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_LOCALITY_NAME:
        var = get_cert_subject_field_value(xc->cert, NID_localityName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_STATE_OR_PROVINCE_NAME:
        var = get_cert_subject_field_value(xc->cert, NID_stateOrProvinceName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_COUNTRY_NAME:
        var = get_cert_subject_field_value(xc->cert, NID_countryName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_NAME:
        var = get_cert_subject_field_value(xc->cert, NID_organizationName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_UNIT_NAME:
        var = get_cert_subject_field_value(xc->cert, NID_organizationalUnitName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_UNIQUE_ID:
        var = PP_MakeNull(); // see ppapi/c/private/ppb_x509_certificate_private.h for details
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_VERSION:
        var = PP_MakeNull(); // see ppapi/c/private/ppb_x509_certificate_private.h for details
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SERIAL_NUMBER:
        asn1_int = X509_get_serialNumber(xc->cert);
        if (!asn1_int)
            return PP_MakeNull();
        var = ppb_var_array_buffer_create(asn1_int->length);
        memcpy(ppb_var_array_buffer_map(var), asn1_int->data, asn1_int->length);
        ppb_var_array_buffer_unmap(var);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SIGNATURE_ALGORITHM_OID:
        var = PP_MakeNull(); // see ppapi/c/private/ppb_x509_certificate_private.h for details
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SIGNATURE_ALGORITHM_PARAMATERS_RAW:
        var = PP_MakeNull(); // see ppapi/c/private/ppb_x509_certificate_private.h for details
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_BEFORE:
        asn1_time = X509_get_notBefore(xc->cert);
        if (!asn1_time)
            return PP_MakeNull();
        var = convert_asn1_time_to_pp_var(asn1_time);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_AFTER:
        asn1_time = X509_get_notAfter(xc->cert);
        if (!asn1_time)
            return PP_MakeNull();
        var = convert_asn1_time_to_pp_var(asn1_time);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_PUBLIC_KEY_ALGORITHM_OID:
        trace_error("%s, not implemented path\n", __func__);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_PUBLIC_KEY:
        var = PP_MakeNull(); // see ppapi/c/private/ppb_x509_certificate_private.h for details
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_RAW:
        if (xc->raw_data) {
            var = ppb_var_array_buffer_create(xc->raw_data_length);
            memcpy(ppb_var_array_buffer_map(var), xc->raw_data, xc->raw_data_length);
            ppb_var_array_buffer_unmap(var);
        }
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_ISSUER_DISTINGUISHED_NAME:
        var = get_cert_issuer_field_value(xc->cert, NID_distinguishedName);
        goto done;

    case PP_X509CERTIFICATE_PRIVATE_SUBJECT_DISTINGUISHED_NAME:
        var = get_cert_subject_field_value(xc->cert, NID_distinguishedName);
        goto done;

    default:
        var = PP_MakeNull(); // unknown
        goto done;
    }

done:
    pp_resource_release(resource);
    return var;
}


// trace wrappers
TRACE_WRAPPER
PP_Resource
trace_ppb_x509_certificate_create(PP_Instance instance)
{
    trace_info("[PPB] {full} %s instance=%d\n", __func__+6, instance);
    return ppb_x509_certificate_create(instance);
}

TRACE_WRAPPER
PP_Bool
trace_ppb_x509_certificate_is_x509_certificate(PP_Resource resource)
{
    trace_info("[PPB] {full} %s resource=%d\n", __func__+6, resource);
    return ppb_x509_certificate_is_x509_certificate(resource);
}

TRACE_WRAPPER
PP_Bool
trace_ppb_x509_certificate_initialize(PP_Resource resource, const char *bytes, uint32_t length)
{
    trace_info("[PPB] {full} %s resource=%d, bytes=%p, length=%u\n", __func__+6, resource, bytes,
               length);
    return ppb_x509_certificate_initialize(resource, bytes, length);
}

TRACE_WRAPPER
struct PP_Var
trace_ppb_x509_certificate_get_field(PP_Resource resource, PP_X509Certificate_Private_Field field)
{
    trace_info("[PPB] {full} %s resource=%d, field=%s(%u)\n", __func__+6, resource,
               reverse_x509_certificate_field(field), field);
    return ppb_x509_certificate_get_field(resource, field);
}

const struct PPB_X509Certificate_Private_0_1 ppb_x509_certificate_interface_0_1 = {
    .Create =                   TWRAPF(ppb_x509_certificate_create),
    .IsX509CertificatePrivate = TWRAPF(ppb_x509_certificate_is_x509_certificate),
    .Initialize =               TWRAPF(ppb_x509_certificate_initialize),
    .GetField =                 TWRAPF(ppb_x509_certificate_get_field),
};

static
void
__attribute__((constructor))
constructor_ppb_x509_certificate(void)
{
    register_interface(PPB_X509CERTIFICATE_PRIVATE_INTERFACE_0_1,
                       &ppb_x509_certificate_interface_0_1);
    register_resource(PP_RESOURCE_X509_CERTIFICATE, ppb_x509_certificate_destroy);
}