summaryrefslogtreecommitdiff
path: root/src/catlfish.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2015-06-11 16:38:30 +0200
committerLinus Nordberg <linus@nordu.net>2015-06-12 15:45:18 +0200
commit4e26b3679e9743690a85c9f72f7f4fc8ea8fd3f0 (patch)
tree6c445bb8644a7f2ece4084b731faedb726aea3f0 /src/catlfish.erl
parent3de0e0af3521f826e60468b2d6d19717fa0a53d7 (diff)
Implement rate limiting of add_chain
Diffstat (limited to 'src/catlfish.erl')
-rw-r--r--src/catlfish.erl62
1 files changed, 36 insertions, 26 deletions
diff --git a/src/catlfish.erl b/src/catlfish.erl
index dd25a84..e48f788 100644
--- a/src/catlfish.erl
+++ b/src/catlfish.erl
@@ -113,39 +113,49 @@ get_sct(Hash, TimestampedEntry) ->
calc_sct(TimestampedEntry)
end.
--spec add_chain(binary(), [binary()], normal|precert) -> {[{_,_},...]}.
-add_chain(LeafCert, CertChain, Type) ->
- CombinedChain = [LeafCert | CertChain],
- EntryHash = crypto:hash(sha256, CombinedChain),
+add_to_db(Type, LeafCert, CertChain, EntryHash) ->
EntryType = case Type of
normal -> x509_entry;
precert -> precert_entry
end,
+ Timestamp = plop:generate_timestamp(),
+ TSE = timestamped_entry(Timestamp, EntryType, LeafCert, CertChain),
+ MTLText = serialise(#mtl{leaf_version = v1,
+ leaf_type = timestamped_entry,
+ entry = TSE}),
+ MTLHash = ht:leaf_hash(MTLText),
+ ExtraData =
+ case Type of
+ normal -> CertChain;
+ precert -> [LeafCert | CertChain]
+ end,
+ LogEntry =
+ list_to_binary(
+ [encode_tls_vector(MTLText, 4),
+ encode_tls_vector(
+ encode_tls_vector(
+ list_to_binary(
+ [encode_tls_vector(C, 3) || C <- ExtraData]),
+ 3),
+ 4)]),
+ ok = plop:add(LogEntry, MTLHash, EntryHash),
+ {TSE, MTLHash}.
+
+get_ratelimit_token(Type) ->
+ ratelimit:get_token(Type).
+
+-spec add_chain(binary(), [binary()], normal|precert) -> {[{_,_},...]}.
+add_chain(LeafCert, CertChain, Type) ->
+ EntryHash = crypto:hash(sha256, [LeafCert | CertChain]),
{TimestampedEntry, Hash} =
case plop:get(EntryHash) of
notfound ->
- Timestamp = plop:generate_timestamp(),
- TSE = timestamped_entry(Timestamp, EntryType, LeafCert, CertChain),
- MTLText = serialise(#mtl{leaf_version = v1,
- leaf_type = timestamped_entry,
- entry = TSE}),
- MTLHash = ht:leaf_hash(MTLText),
- ExtraData =
- case Type of
- normal -> CertChain;
- precert -> CombinedChain
- end,
- LogEntry =
- list_to_binary(
- [encode_tls_vector(MTLText, 4),
- encode_tls_vector(
- encode_tls_vector(
- list_to_binary(
- [encode_tls_vector(C, 3) || C <- ExtraData]),
- 3),
- 4)]),
- ok = plop:add(LogEntry, MTLHash, EntryHash),
- {TSE, MTLHash};
+ case get_ratelimit_token(add_chain) of
+ ok ->
+ add_to_db(Type, LeafCert, CertChain, EntryHash);
+ _ ->
+ exit({internalerror, "Rate limiting"})
+ end;
{_Index, MTLHash, DBEntry} ->
{MTLText, _ExtraData} = unpack_entry(DBEntry),
MTL = deserialise_mtl(MTLText),