This repository has been archived by the owner on Mar 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signfile.c
101 lines (98 loc) · 2.4 KB
/
signfile.c
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
/*
* Copyright 2009 Christopher Breneman
*
* This file is part of ClueVPN.
*
* ClueVPN 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 3 of the License, or
* (at your option) any later version.
*
* ClueVPN 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 ClueVPN. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <arpa/inet.h>
int main(int argc, char **argv) {
EVP_MD_CTX ctx;
char buf[1024];
char cpbuf[1024];
FILE *cpf;
int num;
int r;
unsigned int signum, nsignum;
unsigned int inlen = 0;
EVP_PKEY *pkey;
FILE *f;
if(argc != 4) {
printf("Usage: %s <InFile> <OutFile> <KeyFile>\n", argv[0]);
return 1;
}
SSL_load_error_strings();
f = fopen(argv[1], "r");
if(!f) {
printf("Could not open input file.\n");
return 1;
}
EVP_SignInit(&ctx, EVP_dss1());
while(num = fread(buf, 1, 1024, f)) {
inlen += num;
r = EVP_SignUpdate(&ctx, buf, num);
if(!r) {
printf("Error updating signature.\n");
return 1;
}
}
fclose(f);
f = fopen(argv[3], "r");
if(!f) {
printf("Could not open key file.\n");
return 1;
}
pkey = NULL;
pkey = PEM_read_PrivateKey(f, &pkey, NULL, NULL);
if(!pkey) {
printf("Error reading private key.\n");
return 1;
}
fclose(f);
printf("Private key size: %d\n", EVP_PKEY_size(pkey));
r = EVP_SignFinal(&ctx, buf, &signum, pkey);
if(!r) {
printf("Error creating signature: %s\n", ERR_error_string(ERR_get_error(), NULL));
return 1;
}
EVP_PKEY_free(pkey);
f = fopen(argv[2], "w");
if(!f) {
printf("Could not open output file.\n");
return 1;
}
nsignum = htonl(signum);
cpf = fopen(argv[1], "r");
if(!cpf) {
printf("Could not open input file.\n");
return 1;
}
inlen = htonl(inlen);
fwrite(&inlen, sizeof(inlen), 1, f);
while(num = fread(cpbuf, 1, 1024, cpf)) {
fwrite(cpbuf, 1, num, f);
}
fclose(cpf);
fwrite(&nsignum, sizeof(nsignum), 1, f);
fwrite(buf, 1, signum, f);
fclose(f);
EVP_MD_CTX_cleanup(&ctx);
return 0;
}