From d8857b08117ed441825725294032a14539f880b2 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Sat, 26 Apr 2014 09:23:11 +0200 Subject: Clean up data types. --- src/plop.erl | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'src/plop.erl') diff --git a/src/plop.erl b/src/plop.erl index 90a5249..4515d25 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -13,7 +13,8 @@ -export([start_link/0, start_link/2, stop/0]). -export([add/1, sth/0]). %% gen_server callbacks. --export([init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2, code_change/3]). +-export([init/1, handle_call/3, terminate/2, + handle_cast/2, handle_info/2, code_change/3]). -include("plop.hrl"). -include_lib("public_key/include/public_key.hrl"). @@ -79,22 +80,8 @@ handle_call(sth, _From, Plop = #plop{hashtree = Tree}) -> %%%%%%%%%%%%%%%%%%%% --spec serialise(plop_entry() | plop_data()) -> iolist(). -serialise(#plop_entry{type = EntryType, entry = Entry}) -> - [<>, Entry]; -serialise(#plop_data{version = Version, - signature_type = Sigtype, - timestamp = Timestamp, - entry = Entry}) -> - [<>, serialise(Entry)]. - %% @doc Signed Plop Timestamp according to RFC6962 3.2 and RFC5246 4.7. -spt(LogID, - PrivKey, - #plop_data{version = Version, % >= 1 - signature_type = Sigtype, % >= 0 - timestamp = Timestamp_in, - entry = Entry = #plop_entry{}}) when is_binary(LogID) -> +spt(LogID, PrivKey, Data = #plop_data{timestamp = Timestamp_in}) -> Timestamp = case Timestamp_in of now -> @@ -104,11 +91,7 @@ spt(LogID, + NowMicroSec / 1.0e3); _ -> Timestamp_in end, - BinToSign = list_to_binary( - serialise(#plop_data{version = Version, - signature_type = Sigtype, - timestamp = Timestamp, - entry = Entry})), + BinToSign = list_to_binary(serialise(Data)), %% Was going to just sign/3 the hash but looking at %% digitally_signed() in lib/ssl/src/ssl_handshake.erl it seems @@ -144,13 +127,31 @@ read_keyfile(Filename, Passphrase) -> public_key(#'RSAPrivateKey'{modulus = Mod, publicExponent = Exp}) -> #'RSAPublicKey'{modulus = Mod, publicExponent = Exp}. +-spec serialise(plop_data() | plop_entry()) -> iolist(). +serialise(#plop_data{version = Version, + signature_type = SigtypeAtom, + timestamp = Timestamp, + entry = Entry}) -> + Sigtype = signature_type(SigtypeAtom), + [<>, serialise(Entry)]; +serialise(#plop_entry{type = TypeAtom, data = Data}) -> + Type = entry_type(TypeAtom), + [<>, Data]. + +signature_type(certificate_timestamp) -> 0; +signature_type(tree_hash) -> 1; +signature_type(test) -> 2. +entry_type(x509) -> 0; +entry_type(precert) -> 1; +entry_type(test) -> 2. + %%%%%%%%%%%%%%%%%%%% %% Tests. serialise_test_() -> - Entry = #plop_entry{type = ?PLOP_ENTRY_TYPE_X509, entry = <<"foo">>}, - Entry_serialised = <<0:16, "foo">>, - [?_assertEqual(Entry_serialised, list_to_binary(serialise(Entry))), - ?_assertEqual(<<1:8, 0:8, 0:64, Entry_serialised/binary>>, - list_to_binary(serialise(#plop_data{signature_type = 0, - timestamp = 0, - entry = Entry})))]. + [?_assertEqual( + <<1:8, 0:8, 0:64, 0:16, "foo">>, + list_to_binary(serialise(#plop_data{ + signature_type = certificate_timestamp, + timestamp = 0, + entry = #plop_entry{type = x509, + data = <<"foo">>}})))]. -- cgit v1.1