1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* -*- indent-tabs-mode: nil -*- */
#include "log/log_signer.h"
#include <glog/logging.h>
#include <openssl/evp.h>
#include <openssl/opensslv.h>
#include <stdint.h>
#include "merkletree/serial_hasher.h"
#include "proto/ct.pb.h"
#include "proto/serializer.h"
#include "util/util.h"
using cert_trans::Verifier;
using cert_trans::serialization::SerializeResult;
using cert_trans::serialization::DeserializeResult;
using ct::DigitallySigned;
using ct::LogEntry;
using ct::LogEntryType;
using ct::SignedCertificateTimestamp;
using ct::SignedTreeHead;
using std::string;
#if OPENSSL_VERSION_NUMBER < 0x10000000
#error "Need OpenSSL >= 1.0.0"
#endif
namespace {
LogSigVerifier::VerifyResult ConvertStatus(const Verifier::Status status) {
switch (status) {
case Verifier::OK:
return LogSigVerifier::OK;
case Verifier::HASH_ALGORITHM_MISMATCH:
return LogSigVerifier::HASH_ALGORITHM_MISMATCH;
case Verifier::SIGNATURE_ALGORITHM_MISMATCH:
return LogSigVerifier::SIGNATURE_ALGORITHM_MISMATCH;
case Verifier::INVALID_SIGNATURE:
return LogSigVerifier::INVALID_SIGNATURE;
}
LOG(FATAL) << "Unexpected status " << status;
}
} // namespace
LogSigner::LogSigner(EVP_PKEY* pkey) : cert_trans::Signer(pkey) {
}
LogSigner::~LogSigner() {
}
LogSigner::SignResult LogSigner::SignV1CertificateTimestamp(
uint64_t timestamp, const string& leaf_certificate,
const string& extensions, string* result) const {
SignedCertificateTimestamp sct;
sct.set_version(ct::V1);
sct.set_timestamp(timestamp);
sct.set_extensions(extensions);
LogEntry entry;
entry.set_type(ct::X509_ENTRY);
entry.mutable_x509_entry()->set_leaf_certificate(leaf_certificate);
string serialized_input;
SerializeResult res =
Serializer::SerializeSCTSignatureInput(sct, entry, &serialized_input);
if (res != SerializeResult::OK)
return GetSerializeError(res);
DigitallySigned signature;
Sign(serialized_input, &signature);
CHECK_EQ(SerializeResult::OK,
Serializer::SerializeDigitallySigned(signature, result));
return OK;
}
LogSigner::SignResult LogSigner::SignV1PrecertificateTimestamp(
uint64_t timestamp, const string& issuer_key_hash,
const string& tbs_certificate, const string& extensions,
string* result) const {
SignedCertificateTimestamp sct;
sct.set_version(ct::V1);
sct.set_timestamp(timestamp);
sct.set_extensions(extensions);
LogEntry entry;
entry.set_type(ct::PRECERT_ENTRY);
entry.mutable_precert_entry()->mutable_pre_cert()->set_issuer_key_hash(
issuer_key_hash);
entry.mutable_precert_entry()->mutable_pre_cert()->set_tbs_certificate(
tbs_certificate);
string serialized_input;
SerializeResult res =
Serializer::SerializeSCTSignatureInput(sct, entry, &serialized_input);
if (res != SerializeResult::OK)
return GetSerializeError(res);
DigitallySigned signature;
Sign(serialized_input, &signature);
CHECK_EQ(SerializeResult::OK,
Serializer::SerializeDigitallySigned(signature, result));
return OK;
}
LogSigner::SignResult LogSigner::SignCertificateTimestamp(
const LogEntry& entry, SignedCertificateTimestamp* sct) const {
CHECK(sct->has_timestamp())
<< "Attempt to sign an SCT with a missing timestamp";
string serialized_input;
SerializeResult res =
Serializer::SerializeSCTSignatureInput(*sct, entry, &serialized_input);
if (res != SerializeResult::OK)
return GetSerializeError(res);
Sign(serialized_input, sct->mutable_signature());
sct->mutable_id()->set_key_id(KeyID());
return OK;
}
LogSigner::SignResult LogSigner::SignV1TreeHead(uint64_t timestamp,
int64_t tree_size,
const string& root_hash,
string* result) const {
CHECK_GE(tree_size, 0);
string serialized_sth;
SerializeResult res =
Serializer::SerializeV1STHSignatureInput(timestamp, tree_size, root_hash,
&serialized_sth);
if (res != SerializeResult::OK)
return GetSerializeError(res);
DigitallySigned signature;
Sign(serialized_sth, &signature);
CHECK_EQ(SerializeResult::OK,
Serializer::SerializeDigitallySigned(signature, result));
return OK;
}
LogSigner::SignResult LogSigner::SignTreeHead(SignedTreeHead* sth) const {
string serialized_sth;
SerializeResult res =
Serializer::SerializeSTHSignatureInput(*sth, &serialized_sth);
if (res != SerializeResult::OK)
return GetSerializeError(res);
Sign(serialized_sth, sth->mutable_signature());
sth->mutable_id()->set_key_id(KeyID());
return OK;
}
// static
LogSigner::SignResult LogSigner::GetSerializeError(SerializeResult result) {
SignResult sign_result;
switch (result) {
case SerializeResult::INVALID_ENTRY_TYPE:
sign_result = INVALID_ENTRY_TYPE;
break;
case SerializeResult::EMPTY_CERTIFICATE:
sign_result = EMPTY_CERTIFICATE;
break;
case SerializeResult::CERTIFICATE_TOO_LONG:
sign_result = CERTIFICATE_TOO_LONG;
break;
case SerializeResult::INVALID_HASH_LENGTH:
sign_result = INVALID_HASH_LENGTH;
break;
case SerializeResult::UNSUPPORTED_VERSION:
sign_result = UNSUPPORTED_VERSION;
break;
case SerializeResult::EXTENSIONS_TOO_LONG:
sign_result = EXTENSIONS_TOO_LONG;
break;
default:
LOG(FATAL) << "Unexpected Serializer error code " << result;
}
return sign_result;
}
LogSigVerifier::LogSigVerifier(EVP_PKEY* pkey) : Verifier(pkey) {
}
LogSigVerifier::~LogSigVerifier() {
}
LogSigVerifier::VerifyResult LogSigVerifier::VerifyV1CertSCTSignature(
uint64_t timestamp, const string& leaf_cert, const string& extensions,
const string& serialized_sig) const {
DigitallySigned signature;
DeserializeResult result =
Deserializer::DeserializeDigitallySigned(serialized_sig, &signature);
if (result != DeserializeResult::OK) {
LOG(WARNING) << "DeserializeDigitallySigned returned " << result;
return GetDeserializeSignatureError(result);
}
SignedCertificateTimestamp sct;
sct.set_version(ct::V1);
sct.set_timestamp(timestamp);
sct.set_extensions(extensions);
LogEntry entry;
entry.set_type(ct::X509_ENTRY);
entry.mutable_x509_entry()->set_leaf_certificate(leaf_cert);
string serialized_sct;
SerializeResult serialize_result =
Serializer::SerializeSCTSignatureInput(sct, entry, &serialized_sct);
if (serialize_result != SerializeResult::OK)
return GetSerializeError(serialize_result);
return ConvertStatus(Verify(serialized_sct, signature));
}
LogSigVerifier::VerifyResult LogSigVerifier::VerifyV1PrecertSCTSignature(
uint64_t timestamp, const string& issuer_key_hash, const string& tbs_cert,
const string& extensions, const string& serialized_sig) const {
DigitallySigned signature;
DeserializeResult result =
Deserializer::DeserializeDigitallySigned(serialized_sig, &signature);
if (result != DeserializeResult::OK)
return GetDeserializeSignatureError(result);
SignedCertificateTimestamp sct;
sct.set_version(ct::V1);
sct.set_timestamp(timestamp);
sct.set_extensions(extensions);
LogEntry entry;
entry.set_type(ct::PRECERT_ENTRY);
entry.mutable_precert_entry()->mutable_pre_cert()->set_issuer_key_hash(
issuer_key_hash);
entry.mutable_precert_entry()->mutable_pre_cert()->set_tbs_certificate(
tbs_cert);
string serialized_sct;
SerializeResult serialize_result =
Serializer::SerializeSCTSignatureInput(sct, entry, &serialized_sct);
if (serialize_result != SerializeResult::OK)
return GetSerializeError(serialize_result);
return ConvertStatus(Verify(serialized_sct, signature));
}
LogSigVerifier::VerifyResult LogSigVerifier::VerifySCTSignature(
const LogEntry& entry, const SignedCertificateTimestamp& sct) const {
// Try to catch key mismatches early.
if (sct.id().has_key_id() && sct.id().key_id() != KeyID()) {
LOG(WARNING) << "Key ID mismatch, got: "
<< util::HexString(sct.id().key_id())
<< " expected: " << util::HexString(KeyID());
return KEY_ID_MISMATCH;
}
string serialized_input;
SerializeResult serialize_result =
Serializer::SerializeSCTSignatureInput(sct, entry, &serialized_input);
if (serialize_result != SerializeResult::OK)
return GetSerializeError(serialize_result);
return ConvertStatus(Verify(serialized_input, sct.signature()));
}
LogSigVerifier::VerifyResult LogSigVerifier::VerifyV1STHSignature(
uint64_t timestamp, int64_t tree_size, const string& root_hash,
const string& serialized_sig) const {
CHECK_GE(tree_size, 0);
DigitallySigned signature;
DeserializeResult result =
Deserializer::DeserializeDigitallySigned(serialized_sig, &signature);
if (result != DeserializeResult::OK)
return GetDeserializeSignatureError(result);
string serialized_sth;
SerializeResult serialize_result =
Serializer::SerializeV1STHSignatureInput(timestamp, tree_size, root_hash,
&serialized_sth);
if (serialize_result != SerializeResult::OK)
return GetSerializeError(serialize_result);
return ConvertStatus(Verify(serialized_sth, signature));
}
LogSigVerifier::VerifyResult LogSigVerifier::VerifySTHSignature(
const SignedTreeHead& sth) const {
if (sth.id().has_key_id() && sth.id().key_id() != KeyID())
return KEY_ID_MISMATCH;
string serialized_sth;
SerializeResult serialize_result =
Serializer::SerializeSTHSignatureInput(sth, &serialized_sth);
if (serialize_result != SerializeResult::OK)
return GetSerializeError(serialize_result);
return ConvertStatus(Verify(serialized_sth, sth.signature()));
}
// static
LogSigVerifier::VerifyResult LogSigVerifier::GetSerializeError(
SerializeResult result) {
VerifyResult verify_result;
switch (result) {
case SerializeResult::INVALID_ENTRY_TYPE:
verify_result = INVALID_ENTRY_TYPE;
break;
case SerializeResult::EMPTY_CERTIFICATE:
verify_result = EMPTY_CERTIFICATE;
break;
case SerializeResult::CERTIFICATE_TOO_LONG:
verify_result = CERTIFICATE_TOO_LONG;
break;
case SerializeResult::INVALID_HASH_LENGTH:
verify_result = INVALID_HASH_LENGTH;
break;
case SerializeResult::UNSUPPORTED_VERSION:
verify_result = UNSUPPORTED_VERSION;
break;
case SerializeResult::EXTENSIONS_TOO_LONG:
verify_result = EXTENSIONS_TOO_LONG;
break;
default:
LOG(FATAL) << "Unexpected Serializer error code " << result;
}
return verify_result;
}
// static
LogSigVerifier::VerifyResult LogSigVerifier::GetDeserializeSignatureError(
DeserializeResult result) {
VerifyResult verify_result;
switch (result) {
case DeserializeResult::INPUT_TOO_SHORT:
verify_result = SIGNATURE_TOO_SHORT;
break;
case DeserializeResult::INVALID_HASH_ALGORITHM:
verify_result = INVALID_HASH_ALGORITHM;
break;
case DeserializeResult::INVALID_SIGNATURE_ALGORITHM:
verify_result = INVALID_SIGNATURE_ALGORITHM;
break;
case DeserializeResult::INPUT_TOO_LONG:
verify_result = SIGNATURE_TOO_LONG;
break;
default:
LOG(FATAL) << "Unexpected Deserializer error code " << result;
}
return verify_result;
}
|