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
dc9a211f
Commit
dc9a211f
authored
2 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
crypto: protect Id caching with atomic_bool
parent
d49b394e
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/opendht/crypto.h
+5
-0
5 additions, 0 deletions
include/opendht/crypto.h
src/crypto.cpp
+8
-4
8 additions, 4 deletions
src/crypto.cpp
with
13 additions
and
4 deletions
include/opendht/crypto.h
+
5
−
0
View file @
dc9a211f
...
@@ -32,6 +32,7 @@ extern "C" {
...
@@ -32,6 +32,7 @@ extern "C" {
#include
<vector>
#include
<vector>
#include
<memory>
#include
<memory>
#include
<atomic>
#ifdef _WIN32
#ifdef _WIN32
#include
<iso646.h>
#include
<iso646.h>
...
@@ -140,6 +141,8 @@ struct OPENDHT_PUBLIC PublicKey
...
@@ -140,6 +141,8 @@ struct OPENDHT_PUBLIC PublicKey
private
:
private
:
mutable
InfoHash
cachedId_
{};
mutable
InfoHash
cachedId_
{};
mutable
PkId
cachedLongId_
{};
mutable
PkId
cachedLongId_
{};
mutable
std
::
atomic_bool
idCached_
{
false
};
mutable
std
::
atomic_bool
longIdCached_
{
false
};
PublicKey
(
const
PublicKey
&
)
=
delete
;
PublicKey
(
const
PublicKey
&
)
=
delete
;
PublicKey
&
operator
=
(
const
PublicKey
&
)
=
delete
;
PublicKey
&
operator
=
(
const
PublicKey
&
)
=
delete
;
...
@@ -609,6 +612,8 @@ private:
...
@@ -609,6 +612,8 @@ private:
Certificate
&
operator
=
(
const
Certificate
&
)
=
delete
;
Certificate
&
operator
=
(
const
Certificate
&
)
=
delete
;
mutable
InfoHash
cachedId_
{};
mutable
InfoHash
cachedId_
{};
mutable
PkId
cachedLongId_
{};
mutable
PkId
cachedLongId_
{};
mutable
std
::
atomic_bool
idCached_
{
false
};
mutable
std
::
atomic_bool
longIdCached_
{
false
};
struct
crlNumberCmp
{
struct
crlNumberCmp
{
bool
operator
()
(
const
std
::
shared_ptr
<
RevocationList
>&
lhs
,
const
std
::
shared_ptr
<
RevocationList
>&
rhs
)
const
{
bool
operator
()
(
const
std
::
shared_ptr
<
RevocationList
>&
lhs
,
const
std
::
shared_ptr
<
RevocationList
>&
rhs
)
const
{
...
...
This diff is collapsed.
Click to expand it.
src/crypto.cpp
+
8
−
4
View file @
dc9a211f
...
@@ -529,7 +529,7 @@ PublicKey::encrypt(const uint8_t* data, size_t data_len) const
...
@@ -529,7 +529,7 @@ PublicKey::encrypt(const uint8_t* data, size_t data_len) const
const
InfoHash
&
const
InfoHash
&
PublicKey
::
getId
()
const
PublicKey
::
getId
()
const
{
{
if
(
pk
and
not
c
ached
Id_
)
{
if
(
pk
and
not
idC
ached
_
.
load
()
)
{
InfoHash
id
;
InfoHash
id
;
size_t
sz
=
id
.
size
();
size_t
sz
=
id
.
size
();
if
(
auto
err
=
gnutls_pubkey_get_key_id
(
pk
,
0
,
id
.
data
(),
&
sz
))
if
(
auto
err
=
gnutls_pubkey_get_key_id
(
pk
,
0
,
id
.
data
(),
&
sz
))
...
@@ -537,6 +537,7 @@ PublicKey::getId() const
...
@@ -537,6 +537,7 @@ PublicKey::getId() const
if
(
sz
!=
id
.
size
())
if
(
sz
!=
id
.
size
())
throw
CryptoException
(
"Can't get public key ID: wrong output length."
);
throw
CryptoException
(
"Can't get public key ID: wrong output length."
);
cachedId_
=
id
;
cachedId_
=
id
;
idCached_
.
store
(
true
);
}
}
return
cachedId_
;
return
cachedId_
;
}
}
...
@@ -544,7 +545,7 @@ PublicKey::getId() const
...
@@ -544,7 +545,7 @@ PublicKey::getId() const
const
PkId
&
const
PkId
&
PublicKey
::
getLongId
()
const
PublicKey
::
getLongId
()
const
{
{
if
(
pk
and
not
cachedLongId_
)
{
if
(
pk
and
not
longIdCached_
.
load
()
)
{
PkId
h
;
PkId
h
;
size_t
sz
=
h
.
size
();
size_t
sz
=
h
.
size
();
if
(
auto
err
=
gnutls_pubkey_get_key_id
(
pk
,
GNUTLS_KEYID_USE_SHA256
,
h
.
data
(),
&
sz
))
if
(
auto
err
=
gnutls_pubkey_get_key_id
(
pk
,
GNUTLS_KEYID_USE_SHA256
,
h
.
data
(),
&
sz
))
...
@@ -552,6 +553,7 @@ PublicKey::getLongId() const
...
@@ -552,6 +553,7 @@ PublicKey::getLongId() const
if
(
sz
!=
h
.
size
())
if
(
sz
!=
h
.
size
())
throw
CryptoException
(
"Can't get 256 bits public key ID: wrong output length."
);
throw
CryptoException
(
"Can't get 256 bits public key ID: wrong output length."
);
cachedLongId_
=
h
;
cachedLongId_
=
h
;
longIdCached_
.
store
(
true
);
}
}
return
cachedLongId_
;
return
cachedLongId_
;
}
}
...
@@ -833,7 +835,7 @@ Certificate::getPublicKey() const
...
@@ -833,7 +835,7 @@ Certificate::getPublicKey() const
const
InfoHash
&
const
InfoHash
&
Certificate
::
getId
()
const
Certificate
::
getId
()
const
{
{
if
(
cert
and
not
c
ached
Id_
)
{
if
(
cert
and
not
idC
ached
_
.
load
()
)
{
InfoHash
id
;
InfoHash
id
;
size_t
sz
=
id
.
size
();
size_t
sz
=
id
.
size
();
if
(
auto
err
=
gnutls_x509_crt_get_key_id
(
cert
,
0
,
id
.
data
(),
&
sz
))
if
(
auto
err
=
gnutls_x509_crt_get_key_id
(
cert
,
0
,
id
.
data
(),
&
sz
))
...
@@ -841,6 +843,7 @@ Certificate::getId() const
...
@@ -841,6 +843,7 @@ Certificate::getId() const
if
(
sz
!=
id
.
size
())
if
(
sz
!=
id
.
size
())
throw
CryptoException
(
"Can't get certificate public key ID: wrong output length."
);
throw
CryptoException
(
"Can't get certificate public key ID: wrong output length."
);
cachedId_
=
id
;
cachedId_
=
id
;
idCached_
.
store
(
true
);
}
}
return
cachedId_
;
return
cachedId_
;
}
}
...
@@ -848,7 +851,7 @@ Certificate::getId() const
...
@@ -848,7 +851,7 @@ Certificate::getId() const
const
PkId
&
const
PkId
&
Certificate
::
getLongId
()
const
Certificate
::
getLongId
()
const
{
{
if
(
cert
and
not
cachedLongId_
)
{
if
(
cert
and
not
longIdCached_
.
load
()
)
{
PkId
id
;
PkId
id
;
size_t
sz
=
id
.
size
();
size_t
sz
=
id
.
size
();
if
(
auto
err
=
gnutls_x509_crt_get_key_id
(
cert
,
GNUTLS_KEYID_USE_SHA256
,
id
.
data
(),
&
sz
))
if
(
auto
err
=
gnutls_x509_crt_get_key_id
(
cert
,
GNUTLS_KEYID_USE_SHA256
,
id
.
data
(),
&
sz
))
...
@@ -856,6 +859,7 @@ Certificate::getLongId() const
...
@@ -856,6 +859,7 @@ Certificate::getLongId() const
if
(
sz
!=
id
.
size
())
if
(
sz
!=
id
.
size
())
throw
CryptoException
(
"Can't get certificate 256 bits public key ID: wrong output length."
);
throw
CryptoException
(
"Can't get certificate 256 bits public key ID: wrong output length."
);
cachedLongId_
=
id
;
cachedLongId_
=
id
;
longIdCached_
.
store
(
true
);
}
}
return
cachedLongId_
;
return
cachedLongId_
;
}
}
...
...
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