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
e68a2fd9
Commit
e68a2fd9
authored
9 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
securedht: add public key cache
parent
cc18afd8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/opendht/securedht.h
+3
-0
3 additions, 0 deletions
include/opendht/securedht.h
src/securedht.cpp
+38
-4
38 additions, 4 deletions
src/securedht.cpp
with
41 additions
and
4 deletions
include/opendht/securedht.h
+
3
−
0
View file @
e68a2fd9
...
@@ -126,11 +126,13 @@ public:
...
@@ -126,11 +126,13 @@ public:
Value
decrypt
(
const
Value
&
v
);
Value
decrypt
(
const
Value
&
v
);
void
findCertificate
(
const
InfoHash
&
node
,
std
::
function
<
void
(
const
std
::
shared_ptr
<
crypto
::
Certificate
>
)
>
cb
);
void
findCertificate
(
const
InfoHash
&
node
,
std
::
function
<
void
(
const
std
::
shared_ptr
<
crypto
::
Certificate
>
)
>
cb
);
void
findPublicKey
(
const
InfoHash
&
node
,
std
::
function
<
void
(
const
std
::
shared_ptr
<
crypto
::
PublicKey
>
)
>
cb
);
const
std
::
shared_ptr
<
crypto
::
Certificate
>
registerCertificate
(
const
InfoHash
&
node
,
const
Blob
&
cert
);
const
std
::
shared_ptr
<
crypto
::
Certificate
>
registerCertificate
(
const
InfoHash
&
node
,
const
Blob
&
cert
);
void
registerCertificate
(
std
::
shared_ptr
<
crypto
::
Certificate
>&
cert
);
void
registerCertificate
(
std
::
shared_ptr
<
crypto
::
Certificate
>&
cert
);
const
std
::
shared_ptr
<
crypto
::
Certificate
>
getCertificate
(
const
InfoHash
&
node
)
const
;
const
std
::
shared_ptr
<
crypto
::
Certificate
>
getCertificate
(
const
InfoHash
&
node
)
const
;
const
std
::
shared_ptr
<
crypto
::
PublicKey
>
getPublicKey
(
const
InfoHash
&
node
)
const
;
...
@@ -159,6 +161,7 @@ private:
...
@@ -159,6 +161,7 @@ private:
// our certificate cache
// our certificate cache
std
::
map
<
InfoHash
,
std
::
shared_ptr
<
crypto
::
Certificate
>>
nodesCertificates_
{};
std
::
map
<
InfoHash
,
std
::
shared_ptr
<
crypto
::
Certificate
>>
nodesCertificates_
{};
std
::
map
<
InfoHash
,
std
::
shared_ptr
<
crypto
::
PublicKey
>>
nodesPubKeys_
{};
std
::
uniform_int_distribution
<
Value
::
Id
>
rand_id
{};
std
::
uniform_int_distribution
<
Value
::
Id
>
rand_id
{};
};
};
...
...
This diff is collapsed.
Click to expand it.
src/securedht.cpp
+
38
−
4
View file @
e68a2fd9
...
@@ -141,6 +141,18 @@ SecureDht::getCertificate(const InfoHash& node) const
...
@@ -141,6 +141,18 @@ SecureDht::getCertificate(const InfoHash& node) const
return
it
->
second
;
return
it
->
second
;
}
}
const
std
::
shared_ptr
<
crypto
::
PublicKey
>
SecureDht
::
getPublicKey
(
const
InfoHash
&
node
)
const
{
if
(
node
==
getId
())
return
std
::
make_shared
<
crypto
::
PublicKey
>
(
certificate_
->
getPublicKey
());
auto
it
=
nodesPubKeys_
.
find
(
node
);
if
(
it
==
nodesPubKeys_
.
end
())
return
nullptr
;
else
return
it
->
second
;
}
const
std
::
shared_ptr
<
crypto
::
Certificate
>
const
std
::
shared_ptr
<
crypto
::
Certificate
>
SecureDht
::
registerCertificate
(
const
InfoHash
&
node
,
const
Blob
&
data
)
SecureDht
::
registerCertificate
(
const
InfoHash
&
node
,
const
Blob
&
data
)
{
{
...
@@ -213,6 +225,26 @@ SecureDht::findCertificate(const InfoHash& node, std::function<void(const std::s
...
@@ -213,6 +225,26 @@ SecureDht::findCertificate(const InfoHash& node, std::function<void(const std::s
},
Value
::
TypeFilter
(
CERTIFICATE_TYPE
));
},
Value
::
TypeFilter
(
CERTIFICATE_TYPE
));
}
}
void
SecureDht
::
findPublicKey
(
const
InfoHash
&
node
,
std
::
function
<
void
(
const
std
::
shared_ptr
<
crypto
::
PublicKey
>
)
>
cb
)
{
auto
pk
=
getPublicKey
(
node
);
if
(
pk
&&
*
pk
)
{
DHT_LOG
.
DEBUG
(
"Found public key from cache for %s"
,
node
.
toString
().
c_str
());
if
(
cb
)
cb
(
pk
);
return
;
}
findCertificate
(
node
,
[
=
](
const
std
::
shared_ptr
<
crypto
::
Certificate
>
crt
)
{
if
(
crt
&&
*
crt
)
{
auto
pk
=
std
::
make_shared
<
crypto
::
PublicKey
>
(
crt
->
getPublicKey
());
nodesPubKeys_
[
pk
->
getId
()]
=
pk
;
if
(
cb
)
cb
(
pk
);
}
else
{
if
(
cb
)
cb
(
nullptr
);
}
});
}
GetCallback
GetCallback
SecureDht
::
getCallbackFilter
(
GetCallback
cb
,
Value
::
Filter
&&
filter
)
SecureDht
::
getCallbackFilter
(
GetCallback
cb
,
Value
::
Filter
&&
filter
)
...
@@ -227,6 +259,7 @@ SecureDht::getCallbackFilter(GetCallback cb, Value::Filter&& filter)
...
@@ -227,6 +259,7 @@ SecureDht::getCallbackFilter(GetCallback cb, Value::Filter&& filter)
try
{
try
{
Value
decrypted_val
(
decrypt
(
*
v
));
Value
decrypted_val
(
decrypt
(
*
v
));
if
(
decrypted_val
.
recipient
==
getId
())
{
if
(
decrypted_val
.
recipient
==
getId
())
{
nodesPubKeys_
[
decrypted_val
.
owner
->
getId
()]
=
decrypted_val
.
owner
;
if
(
not
filter
or
filter
(
decrypted_val
))
if
(
not
filter
or
filter
(
decrypted_val
))
tmpvals
.
push_back
(
std
::
make_shared
<
Value
>
(
std
::
move
(
decrypted_val
)));
tmpvals
.
push_back
(
std
::
make_shared
<
Value
>
(
std
::
move
(
decrypted_val
)));
}
}
...
@@ -238,6 +271,7 @@ SecureDht::getCallbackFilter(GetCallback cb, Value::Filter&& filter)
...
@@ -238,6 +271,7 @@ SecureDht::getCallbackFilter(GetCallback cb, Value::Filter&& filter)
// Check signed values
// Check signed values
else
if
(
v
->
isSigned
())
{
else
if
(
v
->
isSigned
())
{
if
(
v
->
owner
and
v
->
owner
->
checkSignature
(
v
->
getToSign
(),
v
->
signature
))
{
if
(
v
->
owner
and
v
->
owner
->
checkSignature
(
v
->
getToSign
(),
v
->
signature
))
{
nodesPubKeys_
[
v
->
owner
->
getId
()]
=
v
->
owner
;
if
(
not
filter
or
filter
(
*
v
))
if
(
not
filter
or
filter
(
*
v
))
tmpvals
.
push_back
(
v
);
tmpvals
.
push_back
(
v
);
}
}
...
@@ -308,15 +342,15 @@ SecureDht::putSigned(const InfoHash& hash, std::shared_ptr<Value> val, DoneCallb
...
@@ -308,15 +342,15 @@ SecureDht::putSigned(const InfoHash& hash, std::shared_ptr<Value> val, DoneCallb
void
void
SecureDht
::
putEncrypted
(
const
InfoHash
&
hash
,
const
InfoHash
&
to
,
std
::
shared_ptr
<
Value
>
val
,
DoneCallback
callback
)
SecureDht
::
putEncrypted
(
const
InfoHash
&
hash
,
const
InfoHash
&
to
,
std
::
shared_ptr
<
Value
>
val
,
DoneCallback
callback
)
{
{
find
Certificate
(
to
,
[
=
](
const
std
::
shared_ptr
<
crypto
::
Certificate
>
crt
)
{
find
PublicKey
(
to
,
[
=
](
const
std
::
shared_ptr
<
crypto
::
PublicKey
>
pk
)
{
if
(
!
crt
||
!*
crt
)
{
if
(
!
pk
||
!*
pk
)
{
if
(
callback
)
if
(
callback
)
callback
(
false
,
{});
callback
(
false
,
{});
return
;
return
;
}
}
DHT_LOG
.
WARN
(
"Encrypting data for PK: %s"
,
crt
->
getPublicKey
().
getId
().
toString
().
c_str
());
DHT_LOG
.
WARN
(
"Encrypting data for PK: %s"
,
pk
->
getId
().
toString
().
c_str
());
try
{
try
{
put
(
hash
,
encrypt
(
*
val
,
crt
->
getPublicKey
()
),
callback
);
put
(
hash
,
encrypt
(
*
val
,
*
pk
),
callback
);
}
catch
(
const
std
::
exception
&
e
)
{
}
catch
(
const
std
::
exception
&
e
)
{
DHT_LOG
.
ERROR
(
"Error putting encrypted data: %s"
,
e
.
what
());
DHT_LOG
.
ERROR
(
"Error putting encrypted data: %s"
,
e
.
what
());
if
(
callback
)
if
(
callback
)
...
...
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