Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
3a6ce2f4
Commit
3a6ce2f4
authored
5 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
c wrapper: add initial dht_privatekey support
parent
299e6327
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
c/opendht.cpp
+24
-2
24 additions, 2 deletions
c/opendht.cpp
c/opendht_c.h
+5
-1
5 additions, 1 deletion
c/opendht_c.h
include/opendht/crypto.h
+2
-1
2 additions, 1 deletion
include/opendht/crypto.h
src/crypto.cpp
+3
-4
3 additions, 4 deletions
src/crypto.cpp
with
34 additions
and
8 deletions
c/opendht.cpp
+
24
−
2
View file @
3a6ce2f4
...
@@ -16,6 +16,11 @@ void dht_infohash_random(dht_infohash* h)
...
@@ -16,6 +16,11 @@ void dht_infohash_random(dht_infohash* h)
*
reinterpret_cast
<
dht
::
InfoHash
*>
(
h
)
=
dht
::
InfoHash
::
getRandom
();
*
reinterpret_cast
<
dht
::
InfoHash
*>
(
h
)
=
dht
::
InfoHash
::
getRandom
();
}
}
const
char
*
dht_pkid_print
(
const
dht_pkid
*
h
)
{
return
reinterpret_cast
<
const
dht
::
PkId
*>
(
h
)
->
to_c_str
();
}
// dht::Blob
// dht::Blob
void
dht_blob_delete
(
dht_blob
*
data
)
void
dht_blob_delete
(
dht_blob
*
data
)
{
{
...
@@ -26,7 +31,7 @@ dht_data_view dht_blob_get_data(const dht_blob* data)
...
@@ -26,7 +31,7 @@ dht_data_view dht_blob_get_data(const dht_blob* data)
{
{
dht_data_view
view
;
dht_data_view
view
;
view
.
data
=
reinterpret_cast
<
const
dht
::
Blob
*>
(
data
)
->
data
();
view
.
data
=
reinterpret_cast
<
const
dht
::
Blob
*>
(
data
)
->
data
();
view
.
length
=
reinterpret_cast
<
const
dht
::
Blob
*>
(
data
)
->
size
();
view
.
size
=
reinterpret_cast
<
const
dht
::
Blob
*>
(
data
)
->
size
();
return
view
;
return
view
;
}
}
...
@@ -35,7 +40,7 @@ dht_data_view dht_value_get_data(const dht_value* data)
...
@@ -35,7 +40,7 @@ dht_data_view dht_value_get_data(const dht_value* data)
{
{
dht_data_view
view
;
dht_data_view
view
;
view
.
data
=
reinterpret_cast
<
const
dht
::
Value
*>
(
data
)
->
data
.
data
();
view
.
data
=
reinterpret_cast
<
const
dht
::
Value
*>
(
data
)
->
data
.
data
();
view
.
length
=
reinterpret_cast
<
const
dht
::
Value
*>
(
data
)
->
data
.
size
();
view
.
size
=
reinterpret_cast
<
const
dht
::
Value
*>
(
data
)
->
data
.
size
();
return
view
;
return
view
;
}
}
...
@@ -86,6 +91,23 @@ dht_blob* dht_publickey_encrypt(const dht_publickey* pk, const char* data, size_
...
@@ -86,6 +91,23 @@ dht_blob* dht_publickey_encrypt(const dht_publickey* pk, const char* data, size_
return
(
dht_blob
*
)
rdata
;
return
(
dht_blob
*
)
rdata
;
}
}
dht_privatekey
*
dht_privatekey_generate
(
unsigned
key_length_bits
)
{
if
(
key_length_bits
==
0
)
key_length_bits
=
4096
;
return
reinterpret_cast
<
dht_privatekey
*>
(
new
dht
::
crypto
::
PrivateKey
(
dht
::
crypto
::
PrivateKey
::
generate
(
key_length_bits
)));
}
dht_privatekey
*
dht_privatekey_import
(
const
uint8_t
*
dat
,
size_t
dat_size
,
const
char
*
password
)
{
return
reinterpret_cast
<
dht_privatekey
*>
(
new
dht
::
crypto
::
PrivateKey
(
dat
,
dat_size
,
password
));
}
dht_publickey
*
dht_privatekey_get_publickey
(
const
dht_privatekey
*
key
)
{
return
reinterpret_cast
<
dht_publickey
*>
(
new
dht
::
crypto
::
PublicKey
(
reinterpret_cast
<
const
dht
::
crypto
::
PrivateKey
*>
(
key
)
->
getPublicKey
()));
}
// dht::DhtRunner
// dht::DhtRunner
dht_runner
*
dht_runner_new
()
{
dht_runner
*
dht_runner_new
()
{
return
reinterpret_cast
<
dht_runner
*>
(
new
dht
::
DhtRunner
);
return
reinterpret_cast
<
dht_runner
*>
(
new
dht
::
DhtRunner
);
...
...
This diff is collapsed.
Click to expand it.
c/opendht_c.h
+
5
−
1
View file @
3a6ce2f4
...
@@ -14,7 +14,7 @@ extern "C" {
...
@@ -14,7 +14,7 @@ extern "C" {
// Non-owning data view
// Non-owning data view
struct
OPENDHT_C_PUBLIC
dht_data_view
{
struct
OPENDHT_C_PUBLIC
dht_data_view
{
const
uint8_t
*
data
;
const
uint8_t
*
data
;
size_t
length
;
size_t
size
;
};
};
typedef
struct
dht_data_view
dht_data_view
;
typedef
struct
dht_data_view
dht_data_view
;
...
@@ -38,6 +38,7 @@ OPENDHT_C_PUBLIC const char* dht_infohash_print(const dht_infohash* h);
...
@@ -38,6 +38,7 @@ OPENDHT_C_PUBLIC const char* dht_infohash_print(const dht_infohash* h);
// dht::PkId
// dht::PkId
struct
OPENDHT_C_PUBLIC
dht_pkid
{
uint8_t
d
[
32
];
};
struct
OPENDHT_C_PUBLIC
dht_pkid
{
uint8_t
d
[
32
];
};
typedef
struct
dht_pkid
dht_pkid
;
typedef
struct
dht_pkid
dht_pkid
;
OPENDHT_C_PUBLIC
const
char
*
dht_pkid_print
(
const
dht_pkid
*
h
);
// dht::crypto::PublicKey
// dht::crypto::PublicKey
struct
OPENDHT_C_PUBLIC
dht_publickey
;
struct
OPENDHT_C_PUBLIC
dht_publickey
;
...
@@ -54,6 +55,9 @@ OPENDHT_C_PUBLIC dht_blob* dht_publickey_encrypt(const dht_publickey* pk, const
...
@@ -54,6 +55,9 @@ OPENDHT_C_PUBLIC dht_blob* dht_publickey_encrypt(const dht_publickey* pk, const
// dht::crypto::PrivateKey
// dht::crypto::PrivateKey
struct
OPENDHT_C_PUBLIC
dht_privatekey
;
struct
OPENDHT_C_PUBLIC
dht_privatekey
;
typedef
struct
dht_privatekey
dht_privatekey
;
typedef
struct
dht_privatekey
dht_privatekey
;
OPENDHT_C_PUBLIC
dht_privatekey
*
dht_privatekey_generate
(
unsigned
key_length_bits
);
OPENDHT_C_PUBLIC
dht_privatekey
*
dht_privatekey_import
(
const
uint8_t
*
dat
,
size_t
dat_size
,
const
char
*
password
);
OPENDHT_C_PUBLIC
dht_publickey
*
dht_privatekey_get_publickey
(
const
dht_privatekey
*
);
// dht::crypto::Certificate
// dht::crypto::Certificate
struct
OPENDHT_C_PUBLIC
dht_certificate
;
struct
OPENDHT_C_PUBLIC
dht_certificate
;
...
...
This diff is collapsed.
Click to expand it.
include/opendht/crypto.h
+
2
−
1
View file @
3a6ce2f4
...
@@ -147,7 +147,8 @@ struct OPENDHT_PUBLIC PrivateKey
...
@@ -147,7 +147,8 @@ struct OPENDHT_PUBLIC PrivateKey
PrivateKey
(
PrivateKey
&&
o
)
noexcept
;
PrivateKey
(
PrivateKey
&&
o
)
noexcept
;
PrivateKey
&
operator
=
(
PrivateKey
&&
o
)
noexcept
;
PrivateKey
&
operator
=
(
PrivateKey
&&
o
)
noexcept
;
PrivateKey
(
const
Blob
&
import
,
const
std
::
string
&
password
=
{});
PrivateKey
(
const
uint8_t
*
src
,
size_t
src_size
,
const
char
*
password
=
nullptr
);
PrivateKey
(
const
Blob
&
src
,
const
std
::
string
&
password
=
{})
:
PrivateKey
(
src
.
data
(),
src
.
size
(),
password
.
data
())
{}
~
PrivateKey
();
~
PrivateKey
();
explicit
operator
bool
()
const
{
return
key
;
}
explicit
operator
bool
()
const
{
return
key
;
}
...
...
This diff is collapsed.
Click to expand it.
src/crypto.cpp
+
3
−
4
View file @
3a6ce2f4
...
@@ -215,15 +215,14 @@ PrivateKey::PrivateKey(gnutls_x509_privkey_t k) : x509_key(k)
...
@@ -215,15 +215,14 @@ PrivateKey::PrivateKey(gnutls_x509_privkey_t k) : x509_key(k)
}
}
}
}
PrivateKey
::
PrivateKey
(
const
Blob
&
import
,
const
std
::
string
&
password
)
PrivateKey
::
PrivateKey
(
const
uint8_t
*
src
,
size_t
src_size
,
const
char
*
password
_ptr
)
{
{
int
err
=
gnutls_x509_privkey_init
(
&
x509_key
);
int
err
=
gnutls_x509_privkey_init
(
&
x509_key
);
if
(
err
!=
GNUTLS_E_SUCCESS
)
if
(
err
!=
GNUTLS_E_SUCCESS
)
throw
CryptoException
(
"Can't initialize private key !"
);
throw
CryptoException
(
"Can't initialize private key !"
);
const
gnutls_datum_t
dt
{(
uint8_t
*
)
import
.
data
(),
static_cast
<
unsigned
>
(
import
.
size
())};
const
gnutls_datum_t
dt
{(
uint8_t
*
)
src
,
static_cast
<
unsigned
>
(
src_size
)};
const
char
*
password_ptr
=
password
.
empty
()
?
nullptr
:
password
.
c_str
();
int
flags
=
password_ptr
?
GNUTLS_PKCS_PLAIN
int
flags
=
password
.
empty
()
?
GNUTLS_PKCS_PLAIN
:
(
GNUTLS_PKCS_PBES2_AES_128
|
GNUTLS_PKCS_PBES2_AES_192
|
GNUTLS_PKCS_PBES2_AES_256
:
(
GNUTLS_PKCS_PBES2_AES_128
|
GNUTLS_PKCS_PBES2_AES_192
|
GNUTLS_PKCS_PBES2_AES_256
|
GNUTLS_PKCS_PKCS12_3DES
|
GNUTLS_PKCS_PKCS12_ARCFOUR
|
GNUTLS_PKCS_PKCS12_RC2_40
);
|
GNUTLS_PKCS_PKCS12_3DES
|
GNUTLS_PKCS_PKCS12_ARCFOUR
|
GNUTLS_PKCS_PKCS12_RC2_40
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment