31 #ifndef _SCC_CRYPTO_CERT_H
32 #define _SCC_CRYPTO_CERT_H
37 #include <system_error>
45 namespace scc::crypto {
117 void parse(
const BasePtr);
124 BasePtr
dump()
const;
127 std::string
str()
const;
264 parse(v.data(), v.size(), a, p);
268 parse(v.data(), v.size(), a, p);
338 DirectoryString::Type type;
341 DirectoryString(
const std::string& b, DirectoryString::Type t = Type::printable) : type(t)
357 return std::string::compare(b);
361 bool operator!=(
const DirectoryString& b)
const {
return compare(b) != 0; }
369 BasePtr dump()
const;
426 using RDNPair = std::pair<oid_value, DirectoryString>;
432 return lhs.first < rhs.first;
454 static const oid_value name;
455 static const oid_value surname;
456 static const oid_value given_name;
457 static const oid_value generation_qualifier;
458 static const oid_value common_name;
459 static const oid_value locality_name;
460 static const oid_value state_or_province_name;
461 static const oid_value organization_name;
462 static const oid_value organizational_unit_name;
463 static const oid_value title;
464 static const oid_value dn_qualifier;
465 static const oid_value country_name;
466 static const oid_value serial_number;
467 static const oid_value pseudonym;
468 static const oid_value organization_id;
469 static const oid_value street_address;
470 static const oid_value domain_component;
471 static const oid_value email_address;
512 enum class Type : int
520 uniform_resource_identifier = 6,
532 std::string string_val;
533 std::vector<RelativeDistinguishedName> name_val;
546 if (
type != b.type)
return false;
550 case Type::registered_id:
551 return oid_val == b.oid_val;
552 case Type::rfc822_name:
554 case Type::uniform_resource_identifier:
555 case Type::ip_address:
556 return string_val.compare(b.string_val) == 0;
557 case Type::directory_name:
558 return name_val == b.name_val;
559 case Type::other_name:
560 case Type::x400_address:
561 case Type::edi_party_name:
567 bool operator==(
const GeneralName& b)
const {
return compare(b); }
568 bool operator!=(
const GeneralName& b)
const {
return !compare(b); }
575 std::string
str(
bool =
false)
const;
580 subject_alternative_name,
581 authority_key_identifier,
582 subject_key_identifier,
583 issuer_alternative_name,
605 using ExtBasePtr = std::shared_ptr<ExtBase>;
630 virtual std::string
name()
const {
return "ExtUnknown"; }
632 virtual std::string
str(
bool =
false)
const;
673 std::string
name()
const {
return "ExtSubjectAlternativeName"; }
674 virtual std::string
str(
bool =
false)
const;
679 static bool is_castable(ExtBasePtr b)
683 static ExtSubjectAlternativeName& cast(ExtBasePtr b)
685 if (b ==
nullptr || !is_castable(b))
throw std::runtime_error(
"ExtSubjectAlternativeName: invalid cast");
686 return *
dynamic_cast<ExtSubjectAlternativeName*
>(b.get());
712 std::string
name()
const {
return "ExtAuthorityKeyIdentifier"; }
713 virtual std::string
str(
bool =
false)
const;
718 static bool is_castable(ExtBasePtr b)
722 static ExtAuthorityKeyIdentifier& cast(ExtBasePtr b)
724 if (b ==
nullptr || !is_castable(b))
throw std::runtime_error(
"ExtAuthorityKeyIdentifier: invalid cast");
725 return *
dynamic_cast<ExtAuthorityKeyIdentifier*
>(b.get());
748 std::string
name()
const {
return "ExtSubjectKeyIdentifier"; }
749 virtual std::string
str(
bool =
false)
const;
754 static bool is_castable(ExtBasePtr b)
758 static ExtSubjectKeyIdentifier& cast(ExtBasePtr b)
760 if (b ==
nullptr || !is_castable(b))
throw std::runtime_error(
"ExtSubjectKeyIdentifier: invalid cast");
761 return *
dynamic_cast<ExtSubjectKeyIdentifier*
>(b.get());
764 std::vector<char> key_identifier;
781 std::string
name()
const {
return "ExtIssuerAlternativeName"; }
782 virtual std::string
str(
bool =
false)
const;
787 static bool is_castable(ExtBasePtr b)
791 static ExtIssuerAlternativeName& cast(ExtBasePtr b)
793 if (b ==
nullptr || !is_castable(b))
throw std::runtime_error(
"ExtIssuerAlternativeName: invalid cast");
794 return *
dynamic_cast<ExtIssuerAlternativeName*
>(b.get());
797 std::vector<GeneralName> names;
818 std::string
name()
const {
return "ExtBasicConstraints"; }
819 virtual std::string
str(
bool =
false)
const;
824 static bool is_castable(ExtBasePtr b)
828 static ExtBasicConstraints& cast(ExtBasePtr b)
830 if (b ==
nullptr || !is_castable(b))
throw std::runtime_error(
"ExtBasicConstraints: invalid cast");
831 return *
dynamic_cast<ExtBasicConstraints*
>(b.get());
861 std::string
name()
const {
return "ExtKeyUsage"; }
862 virtual std::string
str(
bool =
false)
const;
867 static bool is_castable(ExtBasePtr b)
871 static ExtKeyUsage& cast(ExtBasePtr b)
873 if (b ==
nullptr || !is_castable(b))
throw std::runtime_error(
"ExtKeyUsage: invalid cast");
874 return *
dynamic_cast<ExtKeyUsage*
>(b.get());
920 std::string
name()
const {
return "ExtExtendedKeyUsage"; }
921 virtual std::string
str(
bool =
false)
const;
926 static bool is_castable(ExtBasePtr b)
930 static ExtExtendedKeyUsage& cast(ExtBasePtr b)
932 if (b ==
nullptr || !is_castable(b))
throw std::runtime_error(
"ExtExtendedKeyUsage: invalid cast");
933 return *
dynamic_cast<ExtExtendedKeyUsage*
>(b.get());
966 class ExtSignedCertificateTimestampList :
public ExtBase
968 static int struct_size(
const void*,
unsigned int);
969 bool add_sct(
const void*,
int, std::string&);
970 int dump_scts(std::vector<char>&);
972 std::string
name()
const {
return "ExtSignedCertificateTimestampList"; }
973 static const oid_value s_oid;
974 oid_value oid()
const {
return s_oid; }
976 std::vector<char> val;
978 enum class HashAlgorithm : int
989 enum class SigAlgorithm : int
999 std::vector<char> log_id;
1002 SigAlgorithm sig_algo;
1003 std::vector<char> signature;
1006 std::vector<SCT> sct_list;
1008 std::string str()
const;
1009 bool parse(std::string&);
1014 class ExtAuthorityInformationAccess :
public ExtBase
1017 std::string
name()
const {
return "ExtAuthorityInformationAccess"; }
1018 static const oid_value s_oid;
1019 oid_value oid()
const {
return s_oid; }
1023 std::string str()
const;
1024 bool parse(std::string&);
1060 class ExtCertificatePolicies :
public ExtBase
1063 std::string
name()
const {
return "ExtCertificatePolicies"; }
1064 static const oid_value s_oid;
1065 oid_value oid()
const {
return s_oid; }
1067 enum class PolicyQualifierId
1085 DisplayText() : type(Type::ia5_string) {}
1088 struct NoticeReference
1090 DisplayText organization;
1091 std::vector<Bignum> notice_numbers;
1096 NoticeReference notice_ref;
1097 DisplayText explicit_text;
1100 struct PolicyQualifierInfo
1102 PolicyQualifierId id;
1103 std::string cps_qual;
1104 UserNotice user_notice;
1109 oid_value policy_qualifier_id;
1110 std::vector<PolicyQualifierInfo> policy_qualifiers;
1113 std::vector<PolicyInfo> certificate_policies;
1117 std::string str()
const;
1118 bool parse(std::string&);
1123 class ExtPolicyMappings :
public ExtBase
1126 std::string
name()
const {
return "ExtPolicyMappings"; }
1127 static const oid_value s_oid;
1128 oid_value oid()
const {
return s_oid; }
1132 std::string str()
const;
1133 bool parse(std::string&);
1138 class ExtSubjectDirectoryAttributes :
public ExtBase
1141 std::string
name()
const {
return "ExtSubjectDirectoryAttributes"; }
1142 static const oid_value s_oid;
1143 oid_value oid()
const {
return s_oid; }
1145 std::vector<std::pair<AttributeType, DirectoryString>> directory_attributes;
1147 std::string str()
const;
1148 bool parse(std::string&);
1153 class ExtNameConstraints :
public ExtBase
1156 std::string
name()
const {
return "ExtNameConstraints"; }
1157 static const oid_value s_oid;
1158 oid_value oid()
const {
return s_oid; }
1162 std::string str()
const;
1163 bool parse(std::string&);
1168 class ExtPolicyConstraints :
public ExtBase
1171 std::string
name()
const {
return "ExtPolicyConstraints"; }
1172 static const oid_value s_oid;
1173 oid_value oid()
const {
return s_oid; }
1177 std::string str()
const;
1178 bool parse(std::string&);
1183 class ExtCrlDistributionPoints :
public ExtBase
1186 std::string
name()
const {
return "ExtCrlDistributionPoints"; }
1187 static const oid_value s_oid;
1188 oid_value oid()
const {
return s_oid; }
1192 std::string str()
const;
1193 bool parse(std::string&);
1198 class ExtInhibitAnyPolicy :
public ExtBase
1201 std::string
name()
const {
return "ExtInhibitAnyPolicy"; }
1202 static const oid_value s_oid;
1203 oid_value oid()
const {
return s_oid; }
1207 std::string str()
const;
1208 bool parse(std::string&);
1213 class ExtFreshestCrl :
public ExtBase
1216 std::string
name()
const {
return "ExtFreshestCrl"; }
1217 static const oid_value s_oid;
1218 oid_value oid()
const {
return s_oid; }
1222 std::string str()
const;
1223 bool parse(std::string&);
1229 class ExtSubjectInformationAccess :
public ExtBase
1232 std::string
name()
const {
return "ExtSubjectInformationAccess"; }
1233 static const oid_value s_oid;
1234 oid_value oid()
const {
return s_oid; }
1238 std::string str()
const;
1239 bool parse(std::string&);
1310 std::vector<RelativeDistinguishedName>
issuer;
1318 ExtBasePtr find_ext(ExtType t)
const
1322 return i->oid == ExtBase::find_oid(ExtType::subject_alternative_name);
1336 bool bin_compare(
const X509Cert& other)
const
1355 std::string
str(
bool =
false)
const;
BasePtr root_ptr() const
Return the root pointer.
Distinguished encoding rules (DER).
Elliptic curve cryptography.
std::pair< oid_value, DirectoryString > RDNPair
An x.509 relative distingushed name is a set of attribute / directory string names.
KeyAlgoType
Key algorithm type.
X509SignatureAlgo
Signature algorithms for X.509 certificates.
AttributeType
Attribute types.
@ ec_p192r1
parameter {1, 2, 840, 10045, 3, 1, 1}
@ ec_p256r1
parameter {1, 2, 840, 10045, 3, 1, 7}
@ ec_p384r1
parameter {1, 3, 132, 0, 34}
@ ec_p224r1
parameter {1, 3, 132, 0, 33}
@ ec_p521r1
parameter {1, 3, 132, 0, 35}
@ rsa_sha512
{1, 2, 840, 113549, 1, 1, 13}
@ ecdsa_sha512
{1, 2, 840, 10045, 4, 3, 4}
@ ecdsa_sha224
{1, 2, 840, 10045, 4, 3, 1}
@ rsa_sha224
{1, 2, 840, 113549, 1, 1, 14}
@ ecdsa_sha1
{1, 2, 840, 10045, 4, 1}
@ rsa_md5
{1, 2, 840, 113549, 1, 1, 4}
@ rsa_sha1
{1, 2, 840, 113549, 1, 1, 5}
@ ecdsa_sha256
{1, 2, 840, 10045, 4, 3, 2}
@ ecdsa_sha384
{1, 2, 840, 10045, 4, 3, 3}
@ rsa_sha384
{1, 2, 840, 113549, 1, 1, 12}
@ rsa_sha256
{1, 2, 840, 113549, 1, 1, 11}
@ pseudonym
{2, 5, 4, 65} },
@ organization_id
{2, 5, 4, 97} },
@ generation_qualifier
{2, 5, 4, 44} },
@ state_or_province_name
{2, 5, 4, 8} },
@ domain_component
{ 0, 9, 2342, 19200300, 100, 1, 25 } },
@ locality_name
{2, 5, 4, 7} },
@ serial_number
{2, 5, 4, 5} },
@ street_address
{2, 5, 4, 9} },
@ given_name
{2, 5, 4, 42} },
@ email_address
{1, 2, 840, 113549, 1, 9, 1} },
@ country_name
{2, 5, 4, 6} },
@ dn_qualifier
{2, 5, 4, 46} },
@ organizational_unit_name
{2, 5, 4, 11} },
@ organization_name
{2, 5, 4, 10} },
@ common_name
{2, 5, 4, 3} },
std::ostream & operator<<(std::ostream &, const scc::net::InetAddr &)
Print the socket address details to an output stream.
RSA public key cryptography.
void parse(std::istream &s)
Parse bundle from stream.
An x.509 directory string is used to store generic names.
std::string str() const
Descriptive string <chars> <type>.
int compare(const DirectoryString &b) const
Compare two directory strings.
void parse(BasePtr)
Parse the string from a base object.
From: https://tools.ietf.org/html/rfc3279#section-2.3.5.
static void parse(const BasePtr &, KeyAlgoType &)
Parse from an object id.
static BasePtr dump(const KeyAlgoType &)
Dump to an object id.
Private key certificate utility.
static BasePtr dump(const Bignum &, const KeyAlgoType &, const EccGfpPoint &)
Dump to a sequence.
static void parse(const BasePtr &, Bignum &, KeyAlgoType &, EccGfpPoint &)
Parse from a sequence.
Elliptic curve public key.
static void parse(const BasePtr &, const KeyAlgoType &, EccGfpPoint &)
Parse from a bit string for a specific curve.
static BasePtr dump(const EccGfpPoint &)
Dump to an uncompressed bit string.
Authority key identifier.
virtual std::string str(bool=false) const
Print to string, optionally printing the value.
virtual void dump()
Dump sub-class data into the value.
std::vector< char > key_identifier
Identifies the private key used to sign.
std::string name() const
Return the name of the extension.
virtual bool implemented() const
Is this implemented (sub-classed)?
Bignum authority_cert_serial_number
Serial number of the certificate.
std::vector< GeneralName > authority_cert_issuer
Certificate issuer.
virtual void parse()
Parse value into the the local sub-class data.
virtual void parse()
Parse value into the the local sub-class data.
oid_value oid
The oid of the extension.
BasePtr dump_seq()
Dump the extension into an Extension sequence.
static ExtBasePtr create(BasePtr)
Create an extension.
virtual std::string name() const
Return the name of the extension.
bool critical
Is the extension marked critical? If a CRL contains a critical extension that cannot be processed,...
virtual std::string str(bool=false) const
Print to string, optionally printing the value.
BasePtr value
Parsed extension value.
static oid_value find_oid(ExtType)
Find the oid associated with the extension type.
virtual void dump()
Dump sub-class data into the value.
virtual bool implemented() const
Is this implemented (sub-classed)?
virtual bool implemented() const
Is this implemented (sub-classed)?
std::string name() const
Return the name of the extension.
virtual std::string str(bool=false) const
Print to string, optionally printing the value.
virtual void dump()
Dump sub-class data into the value.
virtual void parse()
Parse value into the the local sub-class data.
bool conditional_access
Is this a conditional access certificate? If so, the public key can be used to verify certificate.
Bignum max_path_len
If conditional access, the maximum number of intermediate certificates in the certification path.
bool server_auth
TLS WWW server auth. Consistent with digital_signature, key_encipherment, key_agreement.
virtual bool implemented() const
Is this implemented (sub-classed)?
std::vector< oid_value > additional_usage_ids
Key usage ids not in the list above.
bool client_auth
TLS WWW client auth. Consistent with digital_signature, key_agreement.
virtual void dump()
Dump sub-class data into the value.
virtual void parse()
Parse value into the the local sub-class data.
std::string name() const
Return the name of the extension.
bool time_stamping
Binding the hash of an object to a time. Consistent with digital_signature, content_commitment.
virtual std::string str(bool=false) const
Print to string, optionally printing the value.
bool code_signing
Signing of downloadable code. Consistent with digital_signature.
bool ocsp_signing
Signing OCSP responses. Consistent with digital_signature, content_commitment.
bool email_protection
Email protection. Consistent with digital_signature, content_commitment, key_encipherment,...
bool permit_any
Permit any usage. Used for applications that must include this extension, but do not wish to specify ...
virtual std::string str(bool=false) const
Print to string, optionally printing the value.
virtual bool implemented() const
Is this implemented (sub-classed)?
virtual void dump()
Dump sub-class data into the value.
std::string name() const
Return the name of the extension.
virtual void parse()
Parse value into the the local sub-class data.
bool digital_signature
Public key is used for verifying digital signatures other than certificates and CRLs.
std::string name() const
Return the name of the extension.
virtual void dump()
Dump sub-class data into the value.
bool key_cert_sign
Public key is used for verifying signatures on public key certificates. ExtBasicConstraints condition...
bool data_encipherment
Public key is used to encipher data. This should be rare, as most applications will use key transport...
bool decipher_only
If key_agreement set, public key can only be used for deciphering data while performing key agreement...
virtual void parse()
Parse value into the the local sub-class data.
bool encipher_only
If key_agreement set, public key can only be used for enciphering data while performing key agreement...
bool key_encipherment
Public key is used to encipher private keys, e.g. in key transport.
bool key_agreement
Public key is used for key agreement, e.g. Diffie-Hellman key management.
virtual std::string str(bool=false) const
Print to string, optionally printing the value.
bool content_commitment
Public key is used for verifying digital signatures in a content commitment (non-repudiation) service...
bool crl_sign
Public key is used for verifying certificates on certificate revocation lists, e.g....
virtual bool implemented() const
Is this implemented (sub-classed)?
Subject alternative name.
std::vector< GeneralName > names
Alternative names.
std::string name() const
Return the name of the extension.
virtual void parse()
Parse value into the the local sub-class data.
virtual bool implemented() const
Is this implemented (sub-classed)?
virtual std::string str(bool=false) const
Print to string, optionally printing the value.
virtual void dump()
Dump sub-class data into the value.
std::string name() const
Return the name of the extension.
virtual std::string str(bool=false) const
Print to string, optionally printing the value.
virtual bool implemented() const
Is this implemented (sub-classed)?
virtual void dump()
Dump sub-class data into the value.
virtual void parse()
Parse value into the the local sub-class data.
GeneralName(Type t=Type::directory_name)
Create the base sequence.
void parse(BasePtr)
Parse an input as an implicit element (must have id corresponding to the type above.
BasePtr dump()
Reset the base pointer and dump the element to a context-class element.
std::string str(bool=false) const
Print contents.
Public key information certificate.
BasePtr dump() const
Dump to a sequence.
void parse(const BasePtr)
Parse from a sequence.
void set(const RsaPublicKey &key)
Set rsa public key.
KeyAlgoType type() const
Return the embedded public key type.
std::vector< uint8_t > public_key
The uninterpreted public key.
BasePtr parameters
The optional parameters (may be null)
std::string str() const
Print descriptive string.
oid_value algorithm_id
Algorithm id.
void get(RsaPublicKey &key) const
Get rsa public key.
void parse(BasePtr)
Parse from RelativeDistinguishedName.
BasePtr dump() const
Dump and return an element of type set.
std::string str() const
Print contents.
RSA private key certificate.
static BasePtr dump(const RsaPrivateKey &)
Dump to a sequence.
static void parse(const BasePtr &, RsaPrivateKey &)
Parse from a sequence.
RSA public key certificate.
static BasePtr dump(const RsaPublicKey &)
Dump to a sequence.
static void parse(const BasePtr &, RsaPublicKey &)
Parse from a sequence.
Bignum serial_number
The certificate serial number.
std::vector< char > subject_unique_id
Subject unique id. Optional: size 0 means not present.
std::vector< ExtBasePtr > extensions
Extensions.
std::vector< RelativeDistinguishedName > issuer
Issuer name.
std::vector< char > issuer_unique_id
Issuer unique id. Optional: size 0 means not present.
bool validate(const X509Cert &) const
Validate this certificate against another.
BasePtr dump_cert() const
Dump the certificate to a sequence.
std::string str(bool=false) const
Descriptive string.
bool validate(const EccGfpPoint &) const
Validate signature against an ECDSA algorithm and public key.
oid_value sig_algo_oid
Algorithm used to sign this certificate.
bool validate() const
Validate this certificate against it's own public key.
PublicKeyCert public_key
Certificate owner's public key.
void sign_and_dump(DerDocument &, const KeyAlgoType &, const Bignum &, Bignum &, const X509SignatureAlgo &)
Sign the certificate and dump to a document using the ECDSA signature algorithm.
std::chrono::system_clock::time_point valid_end
Time after which this certificate is invalid.
void sign_and_dump(DerDocument &, const RsaPrivateKey &, const X509SignatureAlgo &)
Sign the certificate and dump to a document using the RSA signature algorithm.
oid_value sig_algo_params
Signature algorithm parameters.
std::chrono::system_clock::time_point valid_start
Time before which this certificate is invalid.
bool validate(const RsaPublicKey &) const
Validate signature against an RSA public key.
std::vector< uint8_t > signature
Digital signature of this certificate signed using the issuer's private key.
void parse(const DerDocument &)
Parse from a document.
std::vector< RelativeDistinguishedName > subject
Subject name.
std::vector< char > cert_bin
Binary form of certificate from latest dump() or parse(), signed by the issuer.