~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to lib/cert_sig.cpp

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// This file is part of BOINC.
2
 
// http://boinc.berkeley.edu
3
 
// Copyright (C) 2008 University of California
4
 
//
5
 
// BOINC is free software; you can redistribute it and/or modify it
6
 
// under the terms of the GNU Lesser General Public License
7
 
// as published by the Free Software Foundation,
8
 
// either version 3 of the License, or (at your option) any later version.
9
 
//
10
 
// BOINC is distributed in the hope that it will be useful,
11
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 
// See the GNU Lesser General Public License for more details.
14
 
//
15
 
// You should have received a copy of the GNU Lesser General Public License
16
 
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
#include "miofile.h"
19
 
#include "error_numbers.h"
20
 
#include "cert_sig.h"
21
 
 
22
 
CERT_SIG::CERT_SIG() {
23
 
    this->clear();
24
 
}
25
 
 
26
 
CERT_SIG::~CERT_SIG() {
27
 
    // TODO
28
 
}
29
 
 
30
 
void CERT_SIG::clear() {
31
 
    this->type = MD5_HASH;     // md5 hash by default
32
 
    memset(this->subject, 0, sizeof(this->subject));
33
 
    memset(this->signature, 0, sizeof(this->signature));    
34
 
}
35
 
 
36
 
CERT_SIGS::CERT_SIGS() {
37
 
    // TODO
38
 
}
39
 
 
40
 
CERT_SIGS::~CERT_SIGS() {
41
 
    // TODO
42
 
}
43
 
 
44
 
void CERT_SIGS::clear() {
45
 
    this->signatures.clear();
46
 
}
47
 
 
48
 
int CERT_SIGS::count() {
49
 
    return (int)(this->signatures.size());
50
 
}
51
 
 
52
 
int CERT_SIGS::parse(XML_PARSER &xp) {
53
 
    CERT_SIG sig;
54
 
    int is_tag = false;
55
 
    int in_entry = false;
56
 
    int in_sig = false;
57
 
    int parsed_one = false;
58
 
    char tag[4096];
59
 
    char buf[256];
60
 
    
61
 
    while (!xp.get(tag, sizeof(tag), (bool&)is_tag)) {
62
 
        if (!strcmp(tag, "/signatures")) {
63
 
            //printf("CERT_SIGS::parse() ends.\n");
64
 
            //fflush(stdout);
65
 
            return !in_entry && !in_sig && parsed_one;
66
 
        }
67
 
        if (in_sig) {
68
 
            in_sig = false;
69
 
            snprintf(sig.signature, sizeof(sig.signature), "%s", tag);
70
 
            continue;
71
 
        } 
72
 
        if (!is_tag) {
73
 
            printf("(CERT_SIGS): unexpected text: %s\n", tag);
74
 
            continue;
75
 
        }
76
 
        if (in_entry) {
77
 
            if (!strcmp(tag, "/entry")) {
78
 
                in_entry = false;
79
 
                in_sig = false;
80
 
                if (strlen(sig.subject) == 0) {
81
 
                    printf("ERROR: subject is not set.\n");
82
 
                    return false;
83
 
                }
84
 
                if (strlen(sig.signature) == 0) {
85
 
                    printf("ERROR: signature is not set.\n");
86
 
                    return false;
87
 
                }
88
 
                this->signatures.push_back(sig);
89
 
                parsed_one = true;
90
 
                sig.clear();                
91
 
                continue;
92
 
            }
93
 
            if (!strcmp(tag, "signature")) {
94
 
                in_sig = true;
95
 
                continue;
96
 
            }
97
 
            if (!strcmp(tag, "/signature")) {
98
 
                in_sig = false;
99
 
                continue;
100
 
            }
101
 
            if (xp.parse_str(tag, "subject", sig.subject, sizeof(sig.subject)))
102
 
                continue;
103
 
            else if (xp.parse_str(tag, "hash", sig.hash, sizeof(sig.hash)))
104
 
                continue;
105
 
            else if (xp.parse_str(tag, "type", buf, sizeof(buf))) {
106
 
                if ((!strcmp(buf,"md5")) || (!strcmp(buf,"MD5"))) {
107
 
                    sig.type = MD5_HASH;
108
 
                } else if ((!strcmp(buf,"sha1")) || (!strcmp(buf,"SHA1"))) {
109
 
                    sig.type = SHA1_HASH;                    
110
 
                }
111
 
                continue;
112
 
            }
113
 
        } else {
114
 
            if (strstr(tag, "entry")) {
115
 
                in_entry = true;
116
 
                continue;
117
 
            }
118
 
        }
119
 
    
120
 
    }
121
 
    return false;
122
 
}
123
 
 
124
 
int CERT_SIGS::parse_miofile_embed(MIOFILE &mf) {
125
 
    XML_PARSER xp(&mf);
126
 
    return this->parse(xp);    
127
 
}
128
 
 
129
 
int CERT_SIGS::parse_file(const char* filename) {
130
 
    FILE* f;
131
 
    int retval;
132
 
 
133
 
    f = fopen(filename, "r");
134
 
    if (!f) 
135
 
        return ERR_FOPEN;
136
 
    MIOFILE mf;
137
 
    mf.init_file(f);
138
 
    XML_PARSER xp(&mf);
139
 
    if (!xp.parse_start("signatures")) {
140
 
        return ERR_XML_PARSE;
141
 
    }
142
 
    retval = this->parse(xp);
143
 
    fclose(f);
144
 
    return retval;
145
 
}
146
 
 
147
 
int CERT_SIGS::parse_buffer(char* buf) {
148
 
    MIOFILE mf;
149
 
    int retval;
150
 
 
151
 
    mf.init_buf_read(buf);
152
 
    XML_PARSER xp(&mf);
153
 
    retval = this->parse(xp);
154
 
    return retval;
155
 
}
156
 
 
157
 
int CERT_SIGS::parse_buffer_embed(char* buf) {
158
 
    MIOFILE mf;
159
 
    char tag[4096];
160
 
    int is_tag;
161
 
    int s_found = false;
162
 
 
163
 
    mf.init_buf_read(buf);
164
 
    XML_PARSER xp(&mf);
165
 
    while (!xp.get(tag, sizeof(tag), (bool&)is_tag)) {
166
 
        if (!strcmp(tag, "signatures")) {
167
 
            s_found = true;
168
 
            break;
169
 
        }
170
 
    }
171
 
    if (s_found)
172
 
        return this->parse(xp);
173
 
    else
174
 
        return false;
175
 
}
176
 
 
177
 
int CERT_SIGS::write(MIOFILE &f) {
178
 
    if (this->signatures.size()==0) return true;
179
 
    f.printf("<signatures>\n");
180
 
    for(unsigned int i=0;i < this->signatures.size(); i++) {
181
 
        f.printf("  <entry>\n");
182
 
        f.printf("    <signature>\n%s\n", this->signatures.at(i).signature);
183
 
        f.printf("    </signature>\n");
184
 
        f.printf("    <subject>%s</subject>\n", this->signatures.at(i).subject);    
185
 
        f.printf("    <type>%s</type>\n", (this->signatures.at(i).type == MD5_HASH) ? "md5" : "sha1");
186
 
        f.printf("    <hash>%s</hash>\n", this->signatures.at(i).hash);
187
 
        f.printf("  </entry>\n");
188
 
    }
189
 
    f.printf("</signatures>\n");
190
 
    return true;
191
 
}
192
 
 
 
1
// This file is part of BOINC.
 
2
// http://boinc.berkeley.edu
 
3
// Copyright (C) 2008 University of California
 
4
//
 
5
// BOINC is free software; you can redistribute it and/or modify it
 
6
// under the terms of the GNU Lesser General Public License
 
7
// as published by the Free Software Foundation,
 
8
// either version 3 of the License, or (at your option) any later version.
 
9
//
 
10
// BOINC is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
// See the GNU Lesser General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU Lesser General Public License
 
16
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
#if   defined(_WIN32) && !defined(__STDWX_H__)
 
19
#include "boinc_win.h"
 
20
#elif defined(_WIN32) && defined(__STDWX_H__)
 
21
#include "stdwx.h"
 
22
#else
 
23
#include "config.h"
 
24
#ifdef _USING_FCGI_
 
25
#include "boinc_fcgi.h"
 
26
#else
 
27
#include <cstdio>
 
28
#endif
 
29
#endif
 
30
 
 
31
#ifdef _MSC_VER
 
32
#define snprintf _snprintf
 
33
#endif
 
34
 
 
35
#include "miofile.h"
 
36
#include "error_numbers.h"
 
37
#include "cert_sig.h"
 
38
 
 
39
CERT_SIG::CERT_SIG() {
 
40
    this->clear();
 
41
}
 
42
 
 
43
CERT_SIG::~CERT_SIG() {
 
44
    // TODO
 
45
}
 
46
 
 
47
void CERT_SIG::clear() {
 
48
    this->type = MD5_HASH;     // md5 hash by default
 
49
    memset(this->subject, 0, sizeof(this->subject));
 
50
    memset(this->signature, 0, sizeof(this->signature));    
 
51
}
 
52
 
 
53
CERT_SIGS::CERT_SIGS() {
 
54
    // TODO
 
55
}
 
56
 
 
57
CERT_SIGS::~CERT_SIGS() {
 
58
    // TODO
 
59
}
 
60
 
 
61
void CERT_SIGS::clear() {
 
62
    this->signatures.clear();
 
63
}
 
64
 
 
65
int CERT_SIGS::count() {
 
66
    return (int)(this->signatures.size());
 
67
}
 
68
 
 
69
int CERT_SIGS::parse(XML_PARSER &xp) {
 
70
    CERT_SIG sig;
 
71
    bool is_tag = false;
 
72
    bool in_entry = false;
 
73
    bool in_sig = false;
 
74
    bool parsed_one = false;
 
75
    char tag[4096];
 
76
    char buf[256];
 
77
    
 
78
    while (!xp.get(tag, sizeof(tag), is_tag)) {
 
79
        if (!strcmp(tag, "/signatures")) {
 
80
            //printf("CERT_SIGS::parse() ends.\n");
 
81
            //fflush(stdout);
 
82
            return !in_entry && !in_sig && parsed_one;
 
83
        }
 
84
        if (in_sig) {
 
85
            in_sig = false;
 
86
            snprintf(sig.signature, sizeof(sig.signature), "%s", tag);
 
87
            continue;
 
88
        } 
 
89
        if (!is_tag) {
 
90
            printf("(CERT_SIGS): unexpected text: %s\n", tag);
 
91
            continue;
 
92
        }
 
93
        if (in_entry) {
 
94
            if (!strcmp(tag, "/entry")) {
 
95
                in_entry = false;
 
96
                in_sig = false;
 
97
                if (strlen(sig.subject) == 0) {
 
98
                    printf("ERROR: subject is not set.\n");
 
99
                    return false;
 
100
                }
 
101
                if (strlen(sig.signature) == 0) {
 
102
                    printf("ERROR: signature is not set.\n");
 
103
                    return false;
 
104
                }
 
105
                this->signatures.push_back(sig);
 
106
                parsed_one = true;
 
107
                sig.clear();                
 
108
                continue;
 
109
            }
 
110
            if (!strcmp(tag, "signature")) {
 
111
                in_sig = true;
 
112
                continue;
 
113
            }
 
114
            if (!strcmp(tag, "/signature")) {
 
115
                in_sig = false;
 
116
                continue;
 
117
            }
 
118
            if (xp.parse_str(tag, "subject", sig.subject, sizeof(sig.subject)))
 
119
                continue;
 
120
            else if (xp.parse_str(tag, "hash", sig.hash, sizeof(sig.hash)))
 
121
                continue;
 
122
            else if (xp.parse_str(tag, "type", buf, sizeof(buf))) {
 
123
                if ((!strcmp(buf,"md5")) || (!strcmp(buf,"MD5"))) {
 
124
                    sig.type = MD5_HASH;
 
125
                } else if ((!strcmp(buf,"sha1")) || (!strcmp(buf,"SHA1"))) {
 
126
                    sig.type = SHA1_HASH;                    
 
127
                }
 
128
                continue;
 
129
            }
 
130
        } else {
 
131
            if (strstr(tag, "entry")) {
 
132
                in_entry = true;
 
133
                continue;
 
134
            }
 
135
        }
 
136
    
 
137
    }
 
138
    return false;
 
139
}
 
140
 
 
141
int CERT_SIGS::parse_miofile_embed(MIOFILE &mf) {
 
142
    XML_PARSER xp(&mf);
 
143
    return this->parse(xp);    
 
144
}
 
145
 
 
146
int CERT_SIGS::parse_file(const char* filename) {
 
147
    int retval;
 
148
 
 
149
#ifndef _USING_FCGI_
 
150
    FILE *f = fopen(filename, "r");
 
151
#else
 
152
    FCGI_FILE *f = FCGI::fopen(filename, "r");
 
153
#endif
 
154
    if (!f) 
 
155
        return ERR_FOPEN;
 
156
    MIOFILE mf;
 
157
    mf.init_file(f);
 
158
    XML_PARSER xp(&mf);
 
159
    if (!xp.parse_start("signatures")) {
 
160
        return ERR_XML_PARSE;
 
161
    }
 
162
    retval = this->parse(xp);
 
163
    fclose(f);
 
164
    return retval;
 
165
}
 
166
 
 
167
int CERT_SIGS::parse_buffer(char* buf) {
 
168
    MIOFILE mf;
 
169
    int retval;
 
170
 
 
171
    mf.init_buf_read(buf);
 
172
    XML_PARSER xp(&mf);
 
173
    retval = this->parse(xp);
 
174
    return retval;
 
175
}
 
176
 
 
177
int CERT_SIGS::parse_buffer_embed(char* buf) {
 
178
    MIOFILE mf;
 
179
    char tag[4096];
 
180
    bool is_tag;
 
181
    int s_found = false;
 
182
 
 
183
    mf.init_buf_read(buf);
 
184
    XML_PARSER xp(&mf);
 
185
    while (!xp.get(tag, sizeof(tag), is_tag)) {
 
186
        if (!strcmp(tag, "signatures")) {
 
187
            s_found = true;
 
188
            break;
 
189
        }
 
190
    }
 
191
    if (s_found)
 
192
        return this->parse(xp);
 
193
    else
 
194
        return false;
 
195
}
 
196
 
 
197
int CERT_SIGS::write(MIOFILE &f) {
 
198
    if (this->signatures.size()==0) return true;
 
199
    f.printf("<signatures>\n");
 
200
    for(unsigned int i=0;i < this->signatures.size(); i++) {
 
201
        f.printf("  <entry>\n");
 
202
        f.printf("    <signature>\n%s\n", this->signatures.at(i).signature);
 
203
        f.printf("    </signature>\n");
 
204
        f.printf("    <subject>%s</subject>\n", this->signatures.at(i).subject);    
 
205
        f.printf("    <type>%s</type>\n", (this->signatures.at(i).type == MD5_HASH) ? "md5" : "sha1");
 
206
        f.printf("    <hash>%s</hash>\n", this->signatures.at(i).hash);
 
207
        f.printf("  </entry>\n");
 
208
    }
 
209
    f.printf("</signatures>\n");
 
210
    return true;
 
211
}
 
212