Botan  1.11.15
src/lib/cert/x509/crl_ent.cpp
Go to the documentation of this file.
00001 /*
00002 * CRL Entry
00003 * (C) 1999-2010 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/crl_ent.h>
00009 #include <botan/x509_ext.h>
00010 #include <botan/der_enc.h>
00011 #include <botan/ber_dec.h>
00012 #include <botan/bigint.h>
00013 #include <botan/oids.h>
00014 
00015 namespace Botan {
00016 
00017 /*
00018 * Create a CRL_Entry
00019 */
00020 CRL_Entry::CRL_Entry(bool t_on_unknown_crit) :
00021    throw_on_unknown_critical(t_on_unknown_crit)
00022    {
00023    reason = UNSPECIFIED;
00024    }
00025 
00026 /*
00027 * Create a CRL_Entry
00028 */
00029 CRL_Entry::CRL_Entry(const X509_Certificate& cert, CRL_Code why) :
00030    throw_on_unknown_critical(false)
00031    {
00032    serial = cert.serial_number();
00033    time = X509_Time(std::chrono::system_clock::now());
00034    reason = why;
00035    }
00036 
00037 /*
00038 * Compare two CRL_Entrys for equality
00039 */
00040 bool operator==(const CRL_Entry& a1, const CRL_Entry& a2)
00041    {
00042    if(a1.serial_number() != a2.serial_number())
00043       return false;
00044    if(a1.expire_time() != a2.expire_time())
00045       return false;
00046    if(a1.reason_code() != a2.reason_code())
00047       return false;
00048    return true;
00049    }
00050 
00051 /*
00052 * Compare two CRL_Entrys for inequality
00053 */
00054 bool operator!=(const CRL_Entry& a1, const CRL_Entry& a2)
00055    {
00056    return !(a1 == a2);
00057    }
00058 
00059 /*
00060 * DER encode a CRL_Entry
00061 */
00062 void CRL_Entry::encode_into(DER_Encoder& der) const
00063    {
00064    Extensions extensions;
00065 
00066    extensions.add(new Cert_Extension::CRL_ReasonCode(reason));
00067 
00068    der.start_cons(SEQUENCE)
00069       .encode(BigInt::decode(serial))
00070          .encode(time)
00071          .start_cons(SEQUENCE)
00072             .encode(extensions)
00073           .end_cons()
00074       .end_cons();
00075    }
00076 
00077 /*
00078 * Decode a BER encoded CRL_Entry
00079 */
00080 void CRL_Entry::decode_from(BER_Decoder& source)
00081    {
00082    BigInt serial_number_bn;
00083    reason = UNSPECIFIED;
00084 
00085    BER_Decoder entry = source.start_cons(SEQUENCE);
00086 
00087    entry.decode(serial_number_bn).decode(time);
00088 
00089    if(entry.more_items())
00090       {
00091       Extensions extensions(throw_on_unknown_critical);
00092       entry.decode(extensions);
00093       Data_Store info;
00094       extensions.contents_to(info, info);
00095       reason = CRL_Code(info.get1_u32bit("X509v3.CRLReasonCode"));
00096       }
00097 
00098    entry.end_cons();
00099 
00100    serial = BigInt::encode(serial_number_bn);
00101    }
00102 
00103 }