summaryrefslogtreecommitdiffstats
path: root/kio/kssl/kssl/cert_extract.c
blob: 095f810f4ff11a184db79c17154e627159eb8468 (plain)
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
//krazy:excludeall=license (program, not a library)
/*
**  cert_extract.c -- Extract CA Certs out of Netscape certN.db files
**
**  Copyright      Ariel Glenn <ariel@columbia.edu>
**  Copyright 1998 Ralf S. Engelschall <rse@engelschall.com>
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
**
**  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
**
**
**  Originally written and released under the GPL by 
**  Ariel Glenn from the AcIS R&D group at Columbia
**  as the two sources findoffset.c and dblist.c. See under
**  http://www.columbia.edu/~ariel/good-certs/ for more details.
**
**  Merged into one single program in August 1998 
**  by Ralf S. Engelschall for use in the mod_ssl project.
**  See under http://www.engelschall.com/sw/mod_ssl/ for more details.
**
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>

#include <db1/db.h>

#include "openssl/asn1.h"
#include "openssl/x509.h"

int findoffset(char *dbname);

int findoffset(char *dbname)
{
    DB *db;
    DBT dkey, dvalue;
    int result;
    int offset = 0;
    char *p;
    int ptag = 0, pclass, plen;
    X509 *mycert;

    if ((db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL)) == NULL) {
        fprintf(stderr, "Failed to open DB file '%s': %s\n", dbname, strerror(errno));
        exit(1);
    }
    while ((result = (db->seq(db, &dkey, &dvalue, R_NEXT))) == 0) {
        if ((dvalue.size) > 520) {
            while (offset < dvalue.size) {
                p = (char *)dvalue.data + offset - 1;
                ASN1_get_object((unsigned char **)&p, (long *)&plen, &ptag, &pclass, dvalue.size);
                if (ptag == V_ASN1_SEQUENCE) {  /* ok, it might be a cert then. */
                    /* include length of object header junk */
                    plen += p - ((char *) dvalue.data + offset - 1);
                    mycert = NULL;
                    p = (char *) dvalue.data + offset - 1;
                    d2i_X509(&mycert, (unsigned char **) &p, plen);
                    if (mycert == NULL) {       /* must be garbage after all */
                        offset++;
                        continue;
                    }
                    break;
                }
                else
                    offset++;
            }
            if (offset > 0)
                break;          /* found it, let's quit */
        }
    }
    db->close(db);
    return (offset);
}

int main(int argc, char **argv)
{
    char *dbname;
    DB *db;
    int j;
    int offset;
    DBT dkey, dvalue;
    int result;
    char oname[40];
    int fout;
    int find;
    char *p;
    int ptag = 0, pclass, plen;
    X509 *mycert;
    char *shortname;
    char byte1, byte2;

    if (argc != 2) {
        fprintf(stderr, "usage: %s /path/to/netscape/cert.db\n", argv[0]);
        exit(1);
    }

    dbname = argv[1];
    offset = findoffset(dbname);
    if (offset == 0) {
        fprintf(stderr, "Could not determine cert offset in DB file '%s'\n", dbname);
        exit(1);
    }
    else {
        fprintf(stderr, "Ok: certificates are at offset %d\n", offset);
    }

    if ((db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL)) == NULL) {
        fprintf(stderr, "Failed to open DB file '%s': %s\n", dbname, strerror(errno));
        exit(1);
    }
    if ((find = open("cert.index", O_WRONLY | O_CREAT | O_TRUNC, 0755)) == -1) {
        fprintf(stderr, "Failed to open Index file '%s': %s\n", "cert-index", strerror(errno));
        exit(1);
    }
    j = 0;
    byte1 = -1;
    byte2 = -1;
    while ((result = (db->seq(db, &dkey, &dvalue, R_NEXT))) == 0) {
        if (dvalue.size > offset && ((dvalue.size) - offset) > 500) {
            p = (char *)dvalue.data + offset - 1;
            if (byte1 != -1 && byte2 != -1)
                if (byte1 != p[0] || byte2 != p[1])
                    continue;
            ASN1_get_object((unsigned char **)&p, (long *)&plen, &ptag, &pclass, dvalue.size);
            if (ptag == V_ASN1_SEQUENCE) {      /* ok, it might be a cert then. */
                if (byte1 == -1 && byte2 == -1) {
                    byte1 = p[0];
                    byte2 = p[1];
                }
                /* include length of object header junk */
                plen += p - ((char *) dvalue.data + offset - 1);
                mycert = NULL;
                p = (char *) dvalue.data + offset - 1;
                d2i_X509(&mycert, (unsigned char **) &p, plen);
                if (mycert == NULL) {   /* must be garbage after all */
                    continue;
                }
                j++;
                sprintf(oname, "cert.%02d.der", j);
                if ((fout = open(oname, O_WRONLY | O_CREAT | O_TRUNC, 0755)) == -1) {
                    fprintf(stderr, "could not open %s\n", oname);
                    continue;
                }
                write(fout, (char *) dvalue.data + offset - 1, plen);
                close(fout);
                write(find, oname, strlen(oname));
                write(find, ": ", 2);
                shortname = (char *) dvalue.data + offset - 1 + plen;
                write(find, shortname, dvalue.size - plen - offset);
                write(find, "\n", 1);
                fprintf(stderr, "Extracted: %s (", oname);
                write(fileno(stderr), shortname, dvalue.size - plen - offset);
                fprintf(stderr, ")\n");
            }
            else {
                /* fprintf(stderr, "Hmmm... ptag is %d, plen is %d\n", ptag, plen); */
            }
        }
    }
    close(find);
    db->close(db);

    return (0);
}