Botan  1.11.15
src/lib/tls/msg_certificate.cpp
Go to the documentation of this file.
00001 /*
00002 * Certificate Message
00003 * (C) 2004-2006,2012 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/internal/tls_messages.h>
00009 #include <botan/internal/tls_reader.h>
00010 #include <botan/internal/tls_extensions.h>
00011 #include <botan/internal/tls_handshake_io.h>
00012 #include <botan/der_enc.h>
00013 #include <botan/ber_dec.h>
00014 #include <botan/loadstor.h>
00015 
00016 namespace Botan {
00017 
00018 namespace TLS {
00019 
00020 /**
00021 * Create a new Certificate message
00022 */
00023 Certificate::Certificate(Handshake_IO& io,
00024                          Handshake_Hash& hash,
00025                          const std::vector<X509_Certificate>& cert_list) :
00026    m_certs(cert_list)
00027    {
00028    hash.update(io.send(*this));
00029    }
00030 
00031 /**
00032 * Deserialize a Certificate message
00033 */
00034 Certificate::Certificate(const std::vector<byte>& buf)
00035    {
00036    if(buf.size() < 3)
00037       throw Decoding_Error("Certificate: Message malformed");
00038 
00039    const size_t total_size = make_u32bit(0, buf[0], buf[1], buf[2]);
00040 
00041    if(total_size != buf.size() - 3)
00042       throw Decoding_Error("Certificate: Message malformed");
00043 
00044    const byte* certs = &buf[3];
00045 
00046    while(size_t remaining_bytes = &buf[buf.size()] - certs)
00047       {
00048       if(remaining_bytes < 3)
00049          throw Decoding_Error("Certificate: Message malformed");
00050 
00051       const size_t cert_size = make_u32bit(0, certs[0], certs[1], certs[2]);
00052 
00053       if(remaining_bytes < (3 + cert_size))
00054          throw Decoding_Error("Certificate: Message malformed");
00055 
00056       DataSource_Memory cert_buf(&certs[3], cert_size);
00057       m_certs.push_back(X509_Certificate(cert_buf));
00058 
00059       certs += cert_size + 3;
00060       }
00061    }
00062 
00063 /**
00064 * Serialize a Certificate message
00065 */
00066 std::vector<byte> Certificate::serialize() const
00067    {
00068    std::vector<byte> buf(3);
00069 
00070    for(size_t i = 0; i != m_certs.size(); ++i)
00071       {
00072       std::vector<byte> raw_cert = m_certs[i].BER_encode();
00073       const size_t cert_size = raw_cert.size();
00074       for(size_t i = 0; i != 3; ++i)
00075          buf.push_back(get_byte<u32bit>(i+1, cert_size));
00076       buf += raw_cert;
00077       }
00078 
00079    const size_t buf_size = buf.size() - 3;
00080    for(size_t i = 0; i != 3; ++i)
00081       buf[i] = get_byte<u32bit>(i+1, buf_size);
00082 
00083    return buf;
00084    }
00085 
00086 }
00087 
00088 }