Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) LibDNS: Let's make >90% of the websites fail to resolve because dnssec #3883

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AK/CountingStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

namespace AK {

CountingStream::CountingStream(MaybeOwned<Stream> stream)
CountingStream::CountingStream(MaybeOwned<Stream> stream, size_t offset)
: m_stream(move(stream))
, m_read_bytes(offset)
{
}

Expand Down
2 changes: 1 addition & 1 deletion AK/CountingStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AK {

class CountingStream : public Stream {
public:
CountingStream(MaybeOwned<Stream>);
CountingStream(MaybeOwned<Stream>, size_t offset = 0);

u64 read_bytes() const;

Expand Down
38 changes: 37 additions & 1 deletion Libraries/LibCore/Promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ class Promise : public EventReceiver {
Function<ErrorOr<void>(Result&)> on_resolution;
Function<void(ErrorType&)> on_rejection;

static NonnullRefPtr<Promise> after(Vector<NonnullRefPtr<Promise>>&& promises)
{
auto promise = Promise::construct();
struct Resolved : RefCounted<Resolved> {
explicit Resolved(size_t n)
: needed(n)
{
}

size_t count { 0 };
size_t needed { 0 };
};

auto resolved = make_ref_counted<Resolved>(promises.size());
auto weak_promise = promise->template make_weak_ptr<Promise>();
for (auto p : promises) {
p->when_resolved([weak_promise, resolved](Result&) -> ErrorOr<void> {
if (weak_promise->is_rejected())
return {};

if (++resolved->count == resolved->needed)
weak_promise->resolve({});
return {};
});

p->when_rejected([weak_promise, resolved](ErrorType& error) {
++resolved->count;
weak_promise->reject(move(error));
});

promise->add_child(*p);
}

return promise;
}

void resolve(Result&& result)
{
m_result_or_rejection = move(result);
Expand Down Expand Up @@ -82,7 +118,7 @@ class Promise : public EventReceiver {
template<CallableAs<void, Result&> F>
Promise& when_resolved(F handler)
{
return when_resolved([handler = move(handler)](Result& result) -> ErrorOr<void> {
return when_resolved([handler = move(handler)](Result& result) mutable -> ErrorOr<void> {
handler(result);
return {};
});
Expand Down
23 changes: 23 additions & 0 deletions Libraries/LibCrypto/Curves/SECPxxxr1.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,29 @@ struct SECPxxxr1Signature {
return SECPxxxr1Signature { r_big_int, s_big_int, scalar_size };
}

static ErrorOr<SECPxxxr1Signature> from_raw(Span<int const> curve_oid, ReadonlyBytes signature)
{
size_t scalar_size;
if (curve_oid == ASN1::secp256r1_oid) {
scalar_size = ceil_div(256, 8);
} else if (curve_oid == ASN1::secp384r1_oid) {
scalar_size = ceil_div(384, 8);
} else if (curve_oid == ASN1::secp521r1_oid) {
scalar_size = ceil_div(521, 8);
} else {
return Error::from_string_literal("Unknown SECPxxxr1 curve");
}

if (signature.size() != scalar_size * 2)
return Error::from_string_literal("Invalid SECPxxxr1 signature");

return SECPxxxr1Signature {
UnsignedBigInteger::import_data(signature.slice(0, scalar_size)),
UnsignedBigInteger::import_data(signature.slice(scalar_size, scalar_size)),
scalar_size,
};
}

ErrorOr<ByteBuffer> r_bytes() const
{
return SECPxxxr1Point::scalar_to_bytes(r, size);
Expand Down
14 changes: 12 additions & 2 deletions Libraries/LibCrypto/PK/EC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,19 @@ static ErrorOr<ECPublicKey<>> read_ec_public_key(ReadonlyBytes bytes, Vector<Str
UnsignedBigInteger::import_data(bytes.slice(1 + half_size, half_size)),
half_size,
};
} else {
ERROR_WITH_SCOPE("Unsupported public key format");
}

if (bytes.size() % 2 == 0) {
// Raw public key, without the 0x04 prefix
auto half_size = bytes.size() / 2;
return ::Crypto::PK::ECPublicKey<> {
UnsignedBigInteger::import_data(bytes.slice(0, half_size)),
UnsignedBigInteger::import_data(bytes.slice(half_size, half_size)),
half_size,
};
}

ERROR_WITH_SCOPE("Unsupported public key format");
}

// https://www.rfc-editor.org/rfc/rfc5915#section-3
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibDNS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set(SOURCES
)

serenity_lib(LibDNS dns)
target_link_libraries(LibDNS PRIVATE LibCore)
target_link_libraries(LibDNS PRIVATE LibCore PUBLIC LibCrypto)
Loading
Loading