Repository navigation
Expand file tree
/
Copy pathrsa.c
More file actions
146 lines (116 loc) · 2.71 KB
/
Copy pathrsa.c
File metadata and controls
146 lines (116 loc) · 2.71 KB
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include "common.h"
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
#include <string.h>
#include <glib.h>
#include "rsa.h"
#include "utils.h"
RSA*
private_key_to_pub(RSA *priv)
{
RSA *pub = RSA_new();
pub->n = BN_dup(priv->n);
pub->e = BN_dup(priv->e);
return pub;
}
inline GString*
public_key_to_gstring(const RSA *rsa)
{
GString *buf = g_string_new(NULL);
public_key_append_to_gstring(rsa, buf);
return buf;
}
void
public_key_append_to_gstring(const RSA *rsa, GString *buf)
{
unsigned char *temp;
char *coded;
gsize len = BN_num_bytes(rsa->n);
temp = malloc(len);
BN_bn2bin(rsa->n, temp);
coded = g_base64_encode(temp, len);
g_string_append (buf, coded);
g_string_append_c (buf, ' ');
g_free(coded);
len = BN_num_bytes(rsa->e);
temp = realloc(temp, len);
BN_bn2bin(rsa->e, temp);
coded = g_base64_encode(temp, len);
g_string_append (buf, coded);
g_free(coded);
free(temp);
}
RSA*
public_key_from_string(char *str)
{
char *p;
unsigned char *num;
gsize len;
if (!str)
return NULL;
if ( !(p = strchr(str, ' ')) )
return NULL;
*p = '\0';
RSA *key = RSA_new();
num = g_base64_decode(str, &len);
key->n = BN_bin2bn(num, len, NULL);
if (!key->n)
goto err;
g_free(num);
num = g_base64_decode(p+1, &len);
key->e = BN_bin2bn(num, len, NULL);
if (!key->e)
goto err;
g_free(num);
*p = ' ';
return key;
err:
*p = ' ';
RSA_free (key);
g_free(num);
return NULL;
}
unsigned char *
private_key_decrypt(RSA *key, unsigned char *data, int len, int *decrypt_len)
{
int size;
unsigned char *buf;
size = RSA_size(key);
buf = g_malloc(size);
*decrypt_len = RSA_private_decrypt(len, data, buf, key, RSA_PKCS1_PADDING);
return buf;
}
unsigned char *
public_key_encrypt(RSA *key, unsigned char *data, int len, int *encrypt_len)
{
int size;
unsigned char *buf;
size = RSA_size(key);
buf = g_malloc(size);
*encrypt_len = RSA_public_encrypt(len, data, buf, key, RSA_PKCS1_PADDING);
return buf;
}
char *
id_from_pubkey (RSA *pubkey)
{
GString *buf;
unsigned char sha1[20];
char *id = g_malloc(41);
buf = public_key_to_gstring (pubkey);
calculate_sha1 (sha1, buf->str, -1);
sha1_to_hex (sha1, id);
g_string_free (buf, TRUE);
return id;
}
RSA *
generate_private_key(u_int bits)
{
RSA *private = NULL;
private = RSA_generate_key(bits, 35, NULL, NULL);
if (private == NULL)
g_error ("rsa_generate_private_key: key generation failed.");
return private;
}