diff options
Diffstat (limited to 'src/catlfish.erl')
-rw-r--r-- | src/catlfish.erl | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/src/catlfish.erl b/src/catlfish.erl index 73066bb..5d96278 100644 --- a/src/catlfish.erl +++ b/src/catlfish.erl @@ -5,6 +5,7 @@ -export([add_chain/2, entries/2, entry_and_proof/2]). -export([known_roots/0, update_known_roots/0]). -include_lib("eunit/include/eunit.hrl"). +-include("catlfish.hrl"). -define(PROTOCOL_VERSION, 0). @@ -98,13 +99,11 @@ add_chain(LeafCert, CertChain) -> plop:spt(list_to_binary([<<?PROTOCOL_VERSION:8>>, serialise_signature_type(certificate_timestamp), serialise(TimestampedEntry)])), - binary_to_list( - jiffy:encode( - {[{sct_version, ?PROTOCOL_VERSION}, - {id, base64:encode(plop:get_logid())}, - {timestamp, TimestampedEntry#timestamped_entry.timestamp}, - {extensions, base64:encode(<<>>)}, - {signature, base64:encode(plop:serialise(SCT_sig))}]})). + {[{sct_version, ?PROTOCOL_VERSION}, + {id, base64:encode(plop:get_logid())}, + {timestamp, TimestampedEntry#timestamped_entry.timestamp}, + {extensions, base64:encode(<<>>)}, + {signature, base64:encode(plop:serialise(SCT_sig))}]}. -spec serialise_logentry(integer(), binary(), [binary()]) -> binary(). serialise_logentry(Timestamp, LeafCert, CertChain) -> @@ -118,24 +117,21 @@ serialise_logentry(Timestamp, LeafCert, CertChain) -> -spec entries(non_neg_integer(), non_neg_integer()) -> list(). entries(Start, End) -> - binary_to_list( - jiffy:encode({[{entries, x_entries(plop:get(Start, End))}]})). + {[{entries, x_entries(plop:get(Start, End))}]}. -spec entry_and_proof(non_neg_integer(), non_neg_integer()) -> list(). entry_and_proof(Index, TreeSize) -> - binary_to_list( - jiffy:encode( - case plop:inclusion_and_entry(Index, TreeSize) of - {ok, Entry, Path} -> - {Timestamp, LeafCertVector, CertChainVector} = unpack_entry(Entry), - MTL = build_mtl(Timestamp, LeafCertVector), - {[{leaf_input, base64:encode(MTL)}, - {extra_data, base64:encode(CertChainVector)}, - {audit_path, [base64:encode(X) || X <- Path]}]}; - {notfound, Msg} -> - {[{success, false}, - {error_message, list_to_binary(Msg)}]} - end)). + case plop:inclusion_and_entry(Index, TreeSize) of + {ok, Entry, Path} -> + {Timestamp, LeafCertVector, CertChainVector} = unpack_entry(Entry), + MTL = build_mtl(Timestamp, LeafCertVector), + {[{leaf_input, base64:encode(MTL)}, + {extra_data, base64:encode(CertChainVector)}, + {audit_path, [base64:encode(X) || X <- Path]}]}; + {notfound, Msg} -> + {[{success, false}, + {error_message, list_to_binary(Msg)}]} + end. %% Private functions. unpack_entry(Entry) -> @@ -164,7 +160,7 @@ decode_tls_vector(Binary, LengthLen) -> <<ExtractedBinary:Length/binary-unit:8, Rest2/binary>> = Rest, {ExtractedBinary, Rest2}. --define(ROOTS_TABLE, catlfish_roots). +-define(ROOTS_CACHE_KEY, roots). update_known_roots() -> case application:get_env(catlfish, known_roots_path) of @@ -183,22 +179,20 @@ known_roots() -> -spec known_roots(file:filename(), use_cache|update_tab) -> list(). known_roots(Directory, CacheUsage) -> - case ets:info(?ROOTS_TABLE) of - undefined -> - read_pemfiles_from_dir( - ets:new(?ROOTS_TABLE, [set, protected, named_table]), - Directory); - _ -> - case CacheUsage of - use_cache -> - ets:lookup_element(?ROOTS_TABLE, list, 2); - update_tab -> - read_pemfiles_from_dir(?ROOTS_TABLE, Directory) - end + case CacheUsage of + use_cache -> + case ets:lookup(?CACHE_TABLE, ?ROOTS_CACHE_KEY) of + [] -> + read_pemfiles_from_dir(Directory); + [{roots, DerList}] -> + DerList + end; + update_tab -> + read_pemfiles_from_dir(Directory) end. --spec read_pemfiles_from_dir(ets:tab(), file:filename()) -> list(). -read_pemfiles_from_dir(Tab, Dir) -> +-spec read_pemfiles_from_dir(file:filename()) -> list(). +read_pemfiles_from_dir(Dir) -> DerList = case file:list_dir(Dir) of {error, enoent} -> @@ -213,7 +207,7 @@ read_pemfiles_from_dir(Tab, Dir) -> Filenames), ders_from_pemfiles(Dir, Files) end, - true = ets:insert(Tab, {list, DerList}), + true = ets:insert(?CACHE_TABLE, {?ROOTS_CACHE_KEY, DerList}), DerList. ders_from_pemfiles(Dir, Filenames) -> @@ -256,7 +250,7 @@ read_pemfiles_test_() -> fun() -> {known_roots(?PEMFILES_DIR_OK, use_cache), known_roots(?PEMFILES_DIR_OK, use_cache)} end, - fun(_) -> ets:delete(?ROOTS_TABLE) end, + fun(_) -> ets:delete(?CACHE_TABLE, ?ROOTS_CACHE_KEY) end, fun({L, LCached}) -> [?_assertMatch(7, length(L)), ?_assertEqual(L, LCached)] @@ -265,5 +259,5 @@ read_pemfiles_test_() -> read_pemfiles_fail_test_() -> {setup, fun() -> known_roots(?PEMFILES_DIR_NONEXISTENT, use_cache) end, - fun(_) -> ets:delete(?ROOTS_TABLE) end, + fun(_) -> ets:delete(?CACHE_TABLE, ?ROOTS_CACHE_KEY) end, fun(Empty) -> [?_assertMatch([], Empty)] end}. |