2016-05-04 20:53:48 +02:00
|
|
|
// Copyright (C) 2016 Opsmate, Inc.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License, v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// This software is distributed WITHOUT A WARRANTY OF ANY KIND.
|
|
|
|
// See the Mozilla Public License for details.
|
|
|
|
|
2016-05-04 20:49:07 +02:00
|
|
|
package certspotter
|
2016-02-05 03:45:37 +01:00
|
|
|
|
|
|
|
import (
|
2016-07-28 20:55:46 +02:00
|
|
|
"fmt"
|
2016-02-05 03:45:37 +01:00
|
|
|
"math/big"
|
|
|
|
|
2016-05-04 21:19:59 +02:00
|
|
|
"software.sslmate.com/src/certspotter/ct"
|
2016-02-05 03:45:37 +01:00
|
|
|
)
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func IsPrecert(entry *ct.LogEntry) bool {
|
2016-03-17 00:58:00 +01:00
|
|
|
return entry.Leaf.TimestampedEntry.EntryType == ct.PrecertLogEntryType
|
2016-02-09 19:28:52 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
type CertInfo struct {
|
2016-07-28 20:55:46 +02:00
|
|
|
TBS *TBSCertificate
|
|
|
|
|
|
|
|
Subject RDNSequence
|
|
|
|
SubjectParseError error
|
|
|
|
Issuer RDNSequence
|
|
|
|
IssuerParseError error
|
|
|
|
SANs []SubjectAltName
|
|
|
|
SANsParseError error
|
|
|
|
SerialNumber *big.Int
|
|
|
|
SerialNumberParseError error
|
|
|
|
Validity *CertValidity
|
|
|
|
ValidityParseError error
|
|
|
|
IsCA *bool
|
|
|
|
IsCAParseError error
|
2021-10-29 15:28:39 +02:00
|
|
|
IsPreCert bool
|
2016-03-17 00:58:00 +01:00
|
|
|
}
|
2016-02-05 03:45:37 +01:00
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MakeCertInfoFromTBS(tbs *TBSCertificate) *CertInfo {
|
2016-03-17 00:58:00 +01:00
|
|
|
info := &CertInfo{TBS: tbs}
|
2016-02-05 03:45:37 +01:00
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
info.Subject, info.SubjectParseError = tbs.ParseSubject()
|
|
|
|
info.Issuer, info.IssuerParseError = tbs.ParseIssuer()
|
2016-05-02 20:59:55 +02:00
|
|
|
info.SANs, info.SANsParseError = tbs.ParseSubjectAltNames()
|
2016-03-17 00:58:00 +01:00
|
|
|
info.SerialNumber, info.SerialNumberParseError = tbs.ParseSerialNumber()
|
|
|
|
info.Validity, info.ValidityParseError = tbs.ParseValidity()
|
2016-04-27 03:06:59 +02:00
|
|
|
info.IsCA, info.IsCAParseError = tbs.ParseBasicConstraints()
|
2021-10-29 15:28:39 +02:00
|
|
|
info.IsPreCert = len(tbs.GetExtension(oidExtensionCTPoison)) > 0
|
2016-02-05 03:45:37 +01:00
|
|
|
|
2016-03-17 00:58:00 +01:00
|
|
|
return info
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MakeCertInfoFromRawTBS(tbsBytes []byte) (*CertInfo, error) {
|
2016-03-17 00:58:00 +01:00
|
|
|
tbs, err := ParseTBSCertificate(tbsBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
2016-03-17 00:58:00 +01:00
|
|
|
return MakeCertInfoFromTBS(tbs), nil
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MakeCertInfoFromRawCert(certBytes []byte) (*CertInfo, error) {
|
2016-03-17 00:58:00 +01:00
|
|
|
cert, err := ParseCertificate(certBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return MakeCertInfoFromRawTBS(cert.GetRawTBSCertificate())
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MakeCertInfoFromLogEntry(entry *ct.LogEntry) (*CertInfo, error) {
|
2016-02-09 19:28:52 +01:00
|
|
|
switch entry.Leaf.TimestampedEntry.EntryType {
|
|
|
|
case ct.X509LogEntryType:
|
2016-03-17 00:58:00 +01:00
|
|
|
return MakeCertInfoFromRawCert(entry.Leaf.TimestampedEntry.X509Entry)
|
|
|
|
|
2016-02-09 19:28:52 +01:00
|
|
|
case ct.PrecertLogEntryType:
|
2016-03-17 00:58:00 +01:00
|
|
|
return MakeCertInfoFromRawTBS(entry.Leaf.TimestampedEntry.PrecertEntry.TBSCertificate)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("MakeCertInfoFromCTEntry: unknown CT entry type (neither X509 nor precert)")
|
2016-02-05 03:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
func MatchesWildcard(dnsName string, pattern string) bool {
|
2016-05-10 19:37:10 +02:00
|
|
|
for len(pattern) > 0 {
|
|
|
|
if pattern[0] == '*' {
|
2016-05-10 23:29:04 +02:00
|
|
|
if len(dnsName) > 0 && dnsName[0] != '.' && MatchesWildcard(dnsName[1:], pattern) {
|
2016-05-10 19:37:10 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
pattern = pattern[1:]
|
|
|
|
} else {
|
|
|
|
if len(dnsName) == 0 || pattern[0] != dnsName[0] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
pattern = pattern[1:]
|
|
|
|
dnsName = dnsName[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(dnsName) == 0
|
|
|
|
}
|