File: cms_verifier.cc

package info (click to toggle)
golang-github-google-certificate-transparency 0.0~git20160709.0.0f6e3d1~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,676 kB
  • sloc: cpp: 35,278; python: 11,838; java: 1,911; sh: 1,885; makefile: 950; xml: 520; ansic: 225
file content (233 lines) | stat: -rw-r--r-- 8,511 bytes parent folder | download
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
/* -*- indent-tabs-mode: nil -*- */
#include "log/cms_verifier.h"
#include "log/ct_extensions.h"
#include "util/cms_scoped_types.h"
#include "util/openssl_scoped_types.h"

using std::string;
using std::unique_ptr;
using util::Status;
using util::StatusOr;

namespace cert_trans {
util::StatusOr<bool> CmsVerifier::IsCmsSignedByCert(BIO* cms_bio_in,
                                                    const Cert& cert) const {
  CHECK_NOTNULL(cms_bio_in);

  ScopedCMS_ContentInfo cms_content_info(d2i_CMS_bio(cms_bio_in, nullptr));

  if (!cms_content_info) {
    LOG(ERROR) << "Could not parse CMS data";
    LOG_OPENSSL_ERRORS(WARNING);
    return Status(util::error::INVALID_ARGUMENT,
                  "CMS data could not be parsed");
  }

  // This stack must not be freed as it points into the CMS structure
  STACK_OF(CMS_SignerInfo) *
      const signers(CMS_get0_SignerInfos(cms_content_info.get()));

  if (signers) {
    for (int s = 0; s < sk_CMS_SignerInfo_num(signers); ++s) {
      CMS_SignerInfo* const signer = sk_CMS_SignerInfo_value(signers, s);

      if (CMS_SignerInfo_cert_cmp(signer, cert.x509_.get()) == 0) {
        return true;
      }
    }
  }

  return false;
}

StatusOr<bool> CmsVerifier::IsCmsSignedByCert(const string& cms_object,
                                              const Cert& cert) const {
  // Load a source bio with the CMS signed data object and parse it
  ScopedBIO source_bio(BIO_new(BIO_s_mem()));
  BIO_write(source_bio.get(), cms_object.c_str(), cms_object.length());

  ScopedCMS_ContentInfo cms_content_info(
      d2i_CMS_bio(source_bio.get(), nullptr));

  if (!cms_content_info) {
    LOG(ERROR) << "Could not parse CMS data";
    LOG_OPENSSL_ERRORS(WARNING);
    return Status(util::error::INVALID_ARGUMENT,
                  "CMS data could not be parsed");
  }

  // Now that we've got the CMS unpacked check it has a valid signature using
  // the same key as the cert. First create a certificate stack from our
  // expected signing cert that can be used by CMS_verify.
  ScopedWeakX509Stack validation_chain(sk_X509_new(nullptr));

  sk_X509_push(validation_chain.get(), CHECK_NOTNULL(cert.x509_.get()));

  // Must set CMS_NOINTERN as the RFC says certs SHOULD be omitted from the
  // message but the client might not have obeyed this. CMS_BINARY is required
  // to avoid MIME-related translation. CMS_NO_SIGNER_CERT_VERIFY because we
  // will do our own checks that the chain is valid and the message may not
  // be signed directly by a trusted cert. We don't check it's a signed data
  // object CMS type as OpenSSL does this.
  const int verified =
      CMS_verify(cms_content_info.get(), validation_chain.get(), nullptr,
                 nullptr, nullptr,
                 CMS_NO_SIGNER_CERT_VERIFY | CMS_NOINTERN | CMS_BINARY);

  if (verified != 1) {
    // Most likely, was not CMS signed by the precert
    return false;
  }

  // This stack must not be freed as it points into the CMS structure
  STACK_OF(CMS_SignerInfo) *
      const signers(CMS_get0_SignerInfos(cms_content_info.get()));

  if (signers) {
    for (int s = 0; s < sk_CMS_SignerInfo_num(signers); ++s) {
      CMS_SignerInfo* const signer = sk_CMS_SignerInfo_value(signers, s);

      if (CMS_SignerInfo_cert_cmp(signer, cert.x509_.get()) == 0) {
        return true;
      }
    }
  }

  return false;
}


util::Status CmsVerifier::UnpackCmsDerBio(BIO* cms_bio_in, const Cert& cert,
                                          BIO* cms_bio_out) {
  CHECK_NOTNULL(cms_bio_in);

  ScopedCMS_ContentInfo cms_content_info(d2i_CMS_bio(cms_bio_in, nullptr));

  if (!cms_content_info) {
    LOG(ERROR) << "Could not parse CMS data";
    LOG_OPENSSL_ERRORS(WARNING);
    return Status(util::error::INVALID_ARGUMENT,
                  "CMS data could not be parsed");
  }

  const ASN1_OBJECT* message_content_type(
      CMS_get0_eContentType(cms_content_info.get()));
  int content_type_nid = OBJ_obj2nid(message_content_type);
  // TODO: Enforce content type here. This is not yet defined in the RFC.
  if (content_type_nid != NID_ctV2CmsPayloadContentType) {
    LOG(WARNING) << "CMS message content has unexpected type: "
                 << content_type_nid;
  }

  // Create a certificate stack from our expected signing cert that can be used
  // by CMS_verify.
  ScopedWeakX509Stack validation_chain(sk_X509_new(nullptr));

  sk_X509_push(validation_chain.get(), cert.x509_.get());

  // Must set CMS_NOINTERN as the RFC says certs SHOULD be omitted from the
  // message but the client might not have obeyed this. CMS_BINARY is required
  // to avoid MIME-related translation. CMS_NO_SIGNER_CERT_VERIFY because we
  // will do our own checks that the chain is valid and the message may not
  // be signed directly by a trusted cert. We don't check it's a signed data
  // object CMS type as OpenSSL does this.
  int verified =
      CMS_verify(cms_content_info.get(), validation_chain.get(), nullptr,
                 nullptr, cms_bio_out,
                 CMS_NO_SIGNER_CERT_VERIFY | CMS_NOINTERN | CMS_BINARY);

  return (verified == 1) ? util::Status::OK
                         : util::Status(util::error::INVALID_ARGUMENT,
                                        "CMS verification failed");
}


util::Status CmsVerifier::UnpackCmsDerBio(BIO* cms_bio_in, BIO* cms_bio_out) {
  CHECK_NOTNULL(cms_bio_in);
  CHECK_NOTNULL(cms_bio_out);

  ScopedCMS_ContentInfo cms_content_info(d2i_CMS_bio(cms_bio_in, nullptr));

  if (!cms_content_info) {
    LOG(ERROR) << "Could not parse CMS data";
    LOG_OPENSSL_ERRORS(WARNING);
    return Status(util::error::INVALID_ARGUMENT,
                  "CMS data could not be parsed");
  }

  const ASN1_OBJECT* message_content_type(
      CMS_get0_eContentType(cms_content_info.get()));
  const int content_type_nid = OBJ_obj2nid(message_content_type);
  // TODO: Enforce content type here. This is not yet defined in the RFC.
  if (content_type_nid != NID_ctV2CmsPayloadContentType) {
    LOG(WARNING) << "CMS message content has unexpected type: "
                 << content_type_nid;
  }

  // Must set CMS_NOINTERN as the RFC says certs SHOULD be omitted from the
  // message but the client might not have obeyed this. CMS_BINARY is required
  // to avoid MIME-related translation. CMS_NO_SIGNER_CERT_VERIFY because we
  // will do our own checks that the chain is valid and the message may not
  // be signed directly by a trusted cert. CMS_NO_CONTENT_VERIFY because we
  // can't apply the RFC mandated signature checks until we have the unpacked
  // cert to examine. We don't check it's a signed data object CMS type as
  // OpenSSL does this.
  const int verified =
      CMS_verify(cms_content_info.get(), nullptr, nullptr, nullptr,
                 cms_bio_out, CMS_NO_SIGNER_CERT_VERIFY | CMS_NOINTERN |
                                  CMS_BINARY | CMS_NO_CONTENT_VERIFY);

  return (verified == 1) ? util::Status::OK
                         : util::Status(util::error::INVALID_ARGUMENT,
                                        "CMS unpack failed");
}


unique_ptr<Cert> CmsVerifier::UnpackCmsSignedCertificate(
    BIO* cms_bio_in, const Cert& verify_cert) {
  CHECK_NOTNULL(cms_bio_in);
  ScopedBIO unpacked_bio(BIO_new(BIO_s_mem()));
  unique_ptr<Cert> cert;

  if (UnpackCmsDerBio(cms_bio_in, verify_cert, unpacked_bio.get()).ok()) {
    // The unpacked data should be a valid DER certificate.
    // TODO: The RFC does not yet define this as the format so this may
    // need to change.
    cert = Cert::FromDerBio(unpacked_bio.get());

    if (!cert) {
      LOG(WARNING) << "Could not unpack cert from CMS DER encoded data";
    }
  } else {
    LOG_OPENSSL_ERRORS(ERROR);
  }

  return cert;
}

unique_ptr<Cert> CmsVerifier::UnpackCmsSignedCertificate(
    const string& cms_object) {
  // Load the source bio with the CMS signed data object
  ScopedBIO source_bio(BIO_new(BIO_s_mem()));
  BIO_write(source_bio.get(), cms_object.c_str(), cms_object.length());

  ScopedBIO unpacked_bio(BIO_new(BIO_s_mem()));
  unique_ptr<Cert> cert;

  if (UnpackCmsDerBio(source_bio.get(), unpacked_bio.get()).ok()) {
    // The unpacked data should be a valid DER certificate.
    // TODO: The RFC does not yet define this as the format so this may
    // need to change.
    cert = Cert::FromDerBio(unpacked_bio.get());

    if (!cert) {
      LOG(WARNING) << "Could not unpack cert from CMS DER encoded data";
    }
  } else {
    LOG_OPENSSL_ERRORS(ERROR);
  }

  return cert;
}

}  // namespace cert_trans