From 85a504e310666f270a3a67edf4cdac06c34c61b9 Mon Sep 17 00:00:00 2001 From: lloyd Date: Wed, 13 Oct 2010 12:37:12 +0000 Subject: Split some of the ASN.1 types into their own headers --- src/asn1/asn1_dn.cpp | 304 -------------------------------------------------- src/asn1/asn1_obj.h | 56 +--------- src/asn1/asn1_str.cpp | 2 +- src/asn1/asn1_str.h | 38 +++++++ src/asn1/info.txt | 4 +- src/asn1/x509_dn.cpp | 304 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/asn1/x509_dn.h | 54 +++++++++ 7 files changed, 401 insertions(+), 361 deletions(-) delete mode 100644 src/asn1/asn1_dn.cpp create mode 100644 src/asn1/asn1_str.h create mode 100644 src/asn1/x509_dn.cpp create mode 100644 src/asn1/x509_dn.h (limited to 'src/asn1') diff --git a/src/asn1/asn1_dn.cpp b/src/asn1/asn1_dn.cpp deleted file mode 100644 index 1de8b8c7f..000000000 --- a/src/asn1/asn1_dn.cpp +++ /dev/null @@ -1,304 +0,0 @@ -/* -* X509_DN -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include -#include -#include -#include -#include -#include - -namespace Botan { - -/* -* Create an empty X509_DN -*/ -X509_DN::X509_DN() - { - } - -/* -* Create an X509_DN -*/ -X509_DN::X509_DN(const std::multimap& args) - { - std::multimap::const_iterator j; - for(j = args.begin(); j != args.end(); ++j) - add_attribute(j->first, j->second); - } - -/* -* Create an X509_DN -*/ -X509_DN::X509_DN(const std::multimap& args) - { - std::multimap::const_iterator j; - for(j = args.begin(); j != args.end(); ++j) - add_attribute(OIDS::lookup(j->first), j->second); - } - -/* -* Add an attribute to a X509_DN -*/ -void X509_DN::add_attribute(const std::string& type, - const std::string& str) - { - OID oid = OIDS::lookup(type); - add_attribute(oid, str); - } - -/* -* Add an attribute to a X509_DN -*/ -void X509_DN::add_attribute(const OID& oid, const std::string& str) - { - if(str == "") - return; - - typedef std::multimap::iterator rdn_iter; - - std::pair range = dn_info.equal_range(oid); - for(rdn_iter j = range.first; j != range.second; ++j) - if(j->second.value() == str) - return; - - multimap_insert(dn_info, oid, ASN1_String(str)); - dn_bits.clear(); - } - -/* -* Get the attributes of this X509_DN -*/ -std::multimap X509_DN::get_attributes() const - { - typedef std::multimap::const_iterator rdn_iter; - - std::multimap retval; - for(rdn_iter j = dn_info.begin(); j != dn_info.end(); ++j) - multimap_insert(retval, j->first, j->second.value()); - return retval; - } - -/* -* Get the contents of this X.500 Name -*/ -std::multimap X509_DN::contents() const - { - typedef std::multimap::const_iterator rdn_iter; - - std::multimap retval; - for(rdn_iter j = dn_info.begin(); j != dn_info.end(); ++j) - multimap_insert(retval, OIDS::lookup(j->first), j->second.value()); - return retval; - } - -/* -* Get a single attribute type -*/ -std::vector X509_DN::get_attribute(const std::string& attr) const - { - typedef std::multimap::const_iterator rdn_iter; - - const OID oid = OIDS::lookup(deref_info_field(attr)); - std::pair range = dn_info.equal_range(oid); - - std::vector values; - for(rdn_iter j = range.first; j != range.second; ++j) - values.push_back(j->second.value()); - return values; - } - -/* -* Handle the decoding operation of a DN -*/ -void X509_DN::do_decode(const MemoryRegion& bits) - { - BER_Decoder sequence(bits); - - while(sequence.more_items()) - { - BER_Decoder rdn = sequence.start_cons(SET); - - while(rdn.more_items()) - { - OID oid; - ASN1_String str; - - rdn.start_cons(SEQUENCE) - .decode(oid) - .decode(str) - .verify_end() - .end_cons(); - - add_attribute(oid, str.value()); - } - } - - dn_bits = bits; - } - -/* -* Return the BER encoded data, if any -*/ -MemoryVector X509_DN::get_bits() const - { - return dn_bits; - } - -/* -* Deref aliases in a subject/issuer info request -*/ -std::string X509_DN::deref_info_field(const std::string& info) - { - if(info == "Name" || info == "CommonName") return "X520.CommonName"; - if(info == "SerialNumber") return "X520.SerialNumber"; - if(info == "Country") return "X520.Country"; - if(info == "Organization") return "X520.Organization"; - if(info == "Organizational Unit" || info == "OrgUnit") - return "X520.OrganizationalUnit"; - if(info == "Locality") return "X520.Locality"; - if(info == "State" || info == "Province") return "X520.State"; - if(info == "Email") return "RFC822"; - return info; - } - -/* -* Compare two X509_DNs for equality -*/ -bool operator==(const X509_DN& dn1, const X509_DN& dn2) - { - typedef std::multimap::const_iterator rdn_iter; - - std::multimap attr1 = dn1.get_attributes(); - std::multimap attr2 = dn2.get_attributes(); - - if(attr1.size() != attr2.size()) return false; - - rdn_iter p1 = attr1.begin(); - rdn_iter p2 = attr2.begin(); - - while(true) - { - if(p1 == attr1.end() && p2 == attr2.end()) - break; - if(p1 == attr1.end()) return false; - if(p2 == attr2.end()) return false; - if(p1->first != p2->first) return false; - if(!x500_name_cmp(p1->second, p2->second)) - return false; - ++p1; - ++p2; - } - return true; - } - -/* -* Compare two X509_DNs for inequality -*/ -bool operator!=(const X509_DN& dn1, const X509_DN& dn2) - { - return !(dn1 == dn2); - } - -/* -* Compare two X509_DNs -*/ -bool operator<(const X509_DN& dn1, const X509_DN& dn2) - { - typedef std::multimap::const_iterator rdn_iter; - - std::multimap attr1 = dn1.get_attributes(); - std::multimap attr2 = dn2.get_attributes(); - - if(attr1.size() < attr2.size()) return true; - if(attr1.size() > attr2.size()) return false; - - for(rdn_iter p1 = attr1.begin(); p1 != attr1.end(); ++p1) - { - std::multimap::const_iterator p2; - p2 = attr2.find(p1->first); - if(p2 == attr2.end()) return false; - if(p1->second > p2->second) return false; - if(p1->second < p2->second) return true; - } - return false; - } - -namespace { - -/* -* DER encode a RelativeDistinguishedName -*/ -void do_ava(DER_Encoder& encoder, - const std::multimap& dn_info, - ASN1_Tag string_type, const std::string& oid_str, - bool must_exist = false) - { - typedef std::multimap::const_iterator rdn_iter; - - const OID oid = OIDS::lookup(oid_str); - const bool exists = (dn_info.find(oid) != dn_info.end()); - - if(!exists && must_exist) - throw Encoding_Error("X509_DN: No entry for " + oid_str); - if(!exists) return; - - std::pair range = dn_info.equal_range(oid); - - for(rdn_iter j = range.first; j != range.second; ++j) - { - encoder.start_cons(SET) - .start_cons(SEQUENCE) - .encode(oid) - .encode(ASN1_String(j->second, string_type)) - .end_cons() - .end_cons(); - } - } - -} - -/* -* DER encode a DistinguishedName -*/ -void X509_DN::encode_into(DER_Encoder& der) const - { - std::multimap dn_info = get_attributes(); - - der.start_cons(SEQUENCE); - - if(!dn_bits.empty()) - der.raw_bytes(dn_bits); - else - { - do_ava(der, dn_info, PRINTABLE_STRING, "X520.Country", true); - do_ava(der, dn_info, DIRECTORY_STRING, "X520.State"); - do_ava(der, dn_info, DIRECTORY_STRING, "X520.Locality"); - do_ava(der, dn_info, DIRECTORY_STRING, "X520.Organization"); - do_ava(der, dn_info, DIRECTORY_STRING, "X520.OrganizationalUnit"); - do_ava(der, dn_info, DIRECTORY_STRING, "X520.CommonName", true); - do_ava(der, dn_info, PRINTABLE_STRING, "X520.SerialNumber"); - } - - der.end_cons(); - } - -/* -* Decode a BER encoded DistinguishedName -*/ -void X509_DN::decode_from(BER_Decoder& source) - { - dn_info.clear(); - - source.start_cons(SEQUENCE) - .raw_bytes(dn_bits) - .end_cons(); - - do_decode(dn_bits); - } - -} diff --git a/src/asn1/asn1_obj.h b/src/asn1/asn1_obj.h index 068ed1565..8a7e6cea2 100644 --- a/src/asn1/asn1_obj.h +++ b/src/asn1/asn1_obj.h @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -61,57 +62,6 @@ class BOTAN_DLL X509_Time : public ASN1_Object ASN1_Tag tag; }; -/** -* Simple String -*/ -class BOTAN_DLL ASN1_String : public ASN1_Object - { - public: - void encode_into(class DER_Encoder&) const; - void decode_from(class BER_Decoder&); - - std::string value() const; - std::string iso_8859() const; - - ASN1_Tag tagging() const; - - ASN1_String(const std::string& = ""); - ASN1_String(const std::string&, ASN1_Tag); - private: - std::string iso_8859_str; - ASN1_Tag tag; - }; - -/** -* Distinguished Name -*/ -class BOTAN_DLL X509_DN : public ASN1_Object - { - public: - void encode_into(class DER_Encoder&) const; - void decode_from(class BER_Decoder&); - - std::multimap get_attributes() const; - std::vector get_attribute(const std::string&) const; - - std::multimap contents() const; - - void add_attribute(const std::string&, const std::string&); - void add_attribute(const OID&, const std::string&); - - static std::string deref_info_field(const std::string&); - - void do_decode(const MemoryRegion&); - MemoryVector get_bits() const; - - X509_DN(); - X509_DN(const std::multimap&); - X509_DN(const std::multimap&); - private: - std::multimap dn_info; - MemoryVector dn_bits; - }; - /** * Alternative Name */ @@ -148,10 +98,6 @@ bool BOTAN_DLL operator>=(const X509_Time&, const X509_Time&); bool BOTAN_DLL operator<(const X509_Time&, const X509_Time&); bool BOTAN_DLL operator>(const X509_Time&, const X509_Time&); -bool BOTAN_DLL operator==(const X509_DN&, const X509_DN&); -bool BOTAN_DLL operator!=(const X509_DN&, const X509_DN&); -bool BOTAN_DLL operator<(const X509_DN&, const X509_DN&); - /* * Helper Functions */ diff --git a/src/asn1/asn1_str.cpp b/src/asn1/asn1_str.cpp index 3d79674ec..efdf1a9b3 100644 --- a/src/asn1/asn1_str.cpp +++ b/src/asn1/asn1_str.cpp @@ -5,7 +5,7 @@ * Distributed under the terms of the Botan license */ -#include +#include #include #include #include diff --git a/src/asn1/asn1_str.h b/src/asn1/asn1_str.h new file mode 100644 index 000000000..ba43b0e94 --- /dev/null +++ b/src/asn1/asn1_str.h @@ -0,0 +1,38 @@ +/* +* ASN.1 string type +* (C) 1999-2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ASN1_STRING_H__ +#define BOTAN_ASN1_STRING_H__ + +#include + +namespace Botan { + +/** +* Simple String +*/ +class BOTAN_DLL ASN1_String : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + std::string value() const; + std::string iso_8859() const; + + ASN1_Tag tagging() const; + + ASN1_String(const std::string& = ""); + ASN1_String(const std::string&, ASN1_Tag); + private: + std::string iso_8859_str; + ASN1_Tag tag; + }; + +} + +#endif diff --git a/src/asn1/info.txt b/src/asn1/info.txt index 7ed025e68..e190ad9ea 100644 --- a/src/asn1/info.txt +++ b/src/asn1/info.txt @@ -6,13 +6,13 @@ load_on auto alg_id.cpp asn1_alt.cpp asn1_att.cpp -asn1_dn.cpp asn1_int.cpp asn1_oid.cpp asn1_str.cpp asn1_tm.cpp ber_dec.cpp der_enc.cpp +x509_dn.cpp @@ -20,8 +20,10 @@ alg_id.h asn1_int.h asn1_obj.h asn1_oid.h +asn1_str.h ber_dec.h der_enc.h +x509_dn.h diff --git a/src/asn1/x509_dn.cpp b/src/asn1/x509_dn.cpp new file mode 100644 index 000000000..0a4760391 --- /dev/null +++ b/src/asn1/x509_dn.cpp @@ -0,0 +1,304 @@ +/* +* X509_DN +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include +#include +#include +#include + +namespace Botan { + +/* +* Create an empty X509_DN +*/ +X509_DN::X509_DN() + { + } + +/* +* Create an X509_DN +*/ +X509_DN::X509_DN(const std::multimap& args) + { + std::multimap::const_iterator j; + for(j = args.begin(); j != args.end(); ++j) + add_attribute(j->first, j->second); + } + +/* +* Create an X509_DN +*/ +X509_DN::X509_DN(const std::multimap& args) + { + std::multimap::const_iterator j; + for(j = args.begin(); j != args.end(); ++j) + add_attribute(OIDS::lookup(j->first), j->second); + } + +/* +* Add an attribute to a X509_DN +*/ +void X509_DN::add_attribute(const std::string& type, + const std::string& str) + { + OID oid = OIDS::lookup(type); + add_attribute(oid, str); + } + +/* +* Add an attribute to a X509_DN +*/ +void X509_DN::add_attribute(const OID& oid, const std::string& str) + { + if(str == "") + return; + + typedef std::multimap::iterator rdn_iter; + + std::pair range = dn_info.equal_range(oid); + for(rdn_iter j = range.first; j != range.second; ++j) + if(j->second.value() == str) + return; + + multimap_insert(dn_info, oid, ASN1_String(str)); + dn_bits.clear(); + } + +/* +* Get the attributes of this X509_DN +*/ +std::multimap X509_DN::get_attributes() const + { + typedef std::multimap::const_iterator rdn_iter; + + std::multimap retval; + for(rdn_iter j = dn_info.begin(); j != dn_info.end(); ++j) + multimap_insert(retval, j->first, j->second.value()); + return retval; + } + +/* +* Get the contents of this X.500 Name +*/ +std::multimap X509_DN::contents() const + { + typedef std::multimap::const_iterator rdn_iter; + + std::multimap retval; + for(rdn_iter j = dn_info.begin(); j != dn_info.end(); ++j) + multimap_insert(retval, OIDS::lookup(j->first), j->second.value()); + return retval; + } + +/* +* Get a single attribute type +*/ +std::vector X509_DN::get_attribute(const std::string& attr) const + { + typedef std::multimap::const_iterator rdn_iter; + + const OID oid = OIDS::lookup(deref_info_field(attr)); + std::pair range = dn_info.equal_range(oid); + + std::vector values; + for(rdn_iter j = range.first; j != range.second; ++j) + values.push_back(j->second.value()); + return values; + } + +/* +* Handle the decoding operation of a DN +*/ +void X509_DN::do_decode(const MemoryRegion& bits) + { + BER_Decoder sequence(bits); + + while(sequence.more_items()) + { + BER_Decoder rdn = sequence.start_cons(SET); + + while(rdn.more_items()) + { + OID oid; + ASN1_String str; + + rdn.start_cons(SEQUENCE) + .decode(oid) + .decode(str) + .verify_end() + .end_cons(); + + add_attribute(oid, str.value()); + } + } + + dn_bits = bits; + } + +/* +* Return the BER encoded data, if any +*/ +MemoryVector X509_DN::get_bits() const + { + return dn_bits; + } + +/* +* Deref aliases in a subject/issuer info request +*/ +std::string X509_DN::deref_info_field(const std::string& info) + { + if(info == "Name" || info == "CommonName") return "X520.CommonName"; + if(info == "SerialNumber") return "X520.SerialNumber"; + if(info == "Country") return "X520.Country"; + if(info == "Organization") return "X520.Organization"; + if(info == "Organizational Unit" || info == "OrgUnit") + return "X520.OrganizationalUnit"; + if(info == "Locality") return "X520.Locality"; + if(info == "State" || info == "Province") return "X520.State"; + if(info == "Email") return "RFC822"; + return info; + } + +/* +* Compare two X509_DNs for equality +*/ +bool operator==(const X509_DN& dn1, const X509_DN& dn2) + { + typedef std::multimap::const_iterator rdn_iter; + + std::multimap attr1 = dn1.get_attributes(); + std::multimap attr2 = dn2.get_attributes(); + + if(attr1.size() != attr2.size()) return false; + + rdn_iter p1 = attr1.begin(); + rdn_iter p2 = attr2.begin(); + + while(true) + { + if(p1 == attr1.end() && p2 == attr2.end()) + break; + if(p1 == attr1.end()) return false; + if(p2 == attr2.end()) return false; + if(p1->first != p2->first) return false; + if(!x500_name_cmp(p1->second, p2->second)) + return false; + ++p1; + ++p2; + } + return true; + } + +/* +* Compare two X509_DNs for inequality +*/ +bool operator!=(const X509_DN& dn1, const X509_DN& dn2) + { + return !(dn1 == dn2); + } + +/* +* Compare two X509_DNs +*/ +bool operator<(const X509_DN& dn1, const X509_DN& dn2) + { + typedef std::multimap::const_iterator rdn_iter; + + std::multimap attr1 = dn1.get_attributes(); + std::multimap attr2 = dn2.get_attributes(); + + if(attr1.size() < attr2.size()) return true; + if(attr1.size() > attr2.size()) return false; + + for(rdn_iter p1 = attr1.begin(); p1 != attr1.end(); ++p1) + { + std::multimap::const_iterator p2; + p2 = attr2.find(p1->first); + if(p2 == attr2.end()) return false; + if(p1->second > p2->second) return false; + if(p1->second < p2->second) return true; + } + return false; + } + +namespace { + +/* +* DER encode a RelativeDistinguishedName +*/ +void do_ava(DER_Encoder& encoder, + const std::multimap& dn_info, + ASN1_Tag string_type, const std::string& oid_str, + bool must_exist = false) + { + typedef std::multimap::const_iterator rdn_iter; + + const OID oid = OIDS::lookup(oid_str); + const bool exists = (dn_info.find(oid) != dn_info.end()); + + if(!exists && must_exist) + throw Encoding_Error("X509_DN: No entry for " + oid_str); + if(!exists) return; + + std::pair range = dn_info.equal_range(oid); + + for(rdn_iter j = range.first; j != range.second; ++j) + { + encoder.start_cons(SET) + .start_cons(SEQUENCE) + .encode(oid) + .encode(ASN1_String(j->second, string_type)) + .end_cons() + .end_cons(); + } + } + +} + +/* +* DER encode a DistinguishedName +*/ +void X509_DN::encode_into(DER_Encoder& der) const + { + std::multimap dn_info = get_attributes(); + + der.start_cons(SEQUENCE); + + if(!dn_bits.empty()) + der.raw_bytes(dn_bits); + else + { + do_ava(der, dn_info, PRINTABLE_STRING, "X520.Country", true); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.State"); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.Locality"); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.Organization"); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.OrganizationalUnit"); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.CommonName", true); + do_ava(der, dn_info, PRINTABLE_STRING, "X520.SerialNumber"); + } + + der.end_cons(); + } + +/* +* Decode a BER encoded DistinguishedName +*/ +void X509_DN::decode_from(BER_Decoder& source) + { + dn_info.clear(); + + source.start_cons(SEQUENCE) + .raw_bytes(dn_bits) + .end_cons(); + + do_decode(dn_bits); + } + +} diff --git a/src/asn1/x509_dn.h b/src/asn1/x509_dn.h new file mode 100644 index 000000000..c4fc2d17b --- /dev/null +++ b/src/asn1/x509_dn.h @@ -0,0 +1,54 @@ +/* +* X.509 Distinguished Name +* (C) 1999-2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_DN_H__ +#define BOTAN_X509_DN_H__ + +#include +#include +#include +#include + +namespace Botan { + +/** +* Distinguished Name +*/ +class BOTAN_DLL X509_DN : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + std::multimap get_attributes() const; + std::vector get_attribute(const std::string&) const; + + std::multimap contents() const; + + void add_attribute(const std::string&, const std::string&); + void add_attribute(const OID&, const std::string&); + + static std::string deref_info_field(const std::string&); + + void do_decode(const MemoryRegion&); + MemoryVector get_bits() const; + + X509_DN(); + X509_DN(const std::multimap&); + X509_DN(const std::multimap&); + private: + std::multimap dn_info; + MemoryVector dn_bits; + }; + +bool BOTAN_DLL operator==(const X509_DN&, const X509_DN&); +bool BOTAN_DLL operator!=(const X509_DN&, const X509_DN&); +bool BOTAN_DLL operator<(const X509_DN&, const X509_DN&); + +} + +#endif -- cgit v1.2.3