1/*
2 * Copyright (C) 2012 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "PlatformCertificateInfo.h"
28
29#include "ArgumentDecoder.h"
30#include "ArgumentEncoder.h"
31#include "DataReference.h"
32#include <WebCore/ResourceError.h>
33#include <WebCore/ResourceResponse.h>
34#include <libsoup/soup.h>
35
36using namespace WebCore;
37
38namespace WebKit {
39
40PlatformCertificateInfo::PlatformCertificateInfo()
41    : m_tlsErrors(static_cast<GTlsCertificateFlags>(0))
42{
43}
44
45PlatformCertificateInfo::PlatformCertificateInfo(const ResourceResponse& response)
46    : m_certificate(response.soupMessageCertificate())
47    , m_tlsErrors(response.soupMessageTLSErrors())
48{
49}
50
51PlatformCertificateInfo::PlatformCertificateInfo(const ResourceError& resourceError)
52    : m_certificate(resourceError.certificate())
53    , m_tlsErrors(static_cast<GTlsCertificateFlags>(resourceError.tlsErrors()))
54{
55}
56
57PlatformCertificateInfo::~PlatformCertificateInfo()
58{
59}
60
61void PlatformCertificateInfo::encode(CoreIPC::ArgumentEncoder& encoder) const
62{
63    if (!m_certificate) {
64        encoder << false;
65        return;
66    }
67
68    GByteArray* certificateData = 0;
69    g_object_get(G_OBJECT(m_certificate.get()), "certificate", &certificateData, NULL);
70    if (!certificateData) {
71        encoder << false;
72        return;
73    }
74
75    encoder << true;
76    GRefPtr<GByteArray> certificate = adoptGRef(certificateData);
77    encoder.encodeVariableLengthByteArray(CoreIPC::DataReference(certificate->data, certificate->len));
78    encoder << static_cast<uint32_t>(m_tlsErrors);
79}
80
81bool PlatformCertificateInfo::decode(CoreIPC::ArgumentDecoder& decoder, PlatformCertificateInfo& certificateInfo)
82{
83    bool hasCertificate;
84    if (!decoder.decode(hasCertificate))
85        return false;
86
87    if (!hasCertificate)
88        return true;
89
90    CoreIPC::DataReference certificateDataReference;
91    if (!decoder.decodeVariableLengthByteArray(certificateDataReference))
92        return false;
93
94    GByteArray* certificateData = g_byte_array_sized_new(certificateDataReference.size());
95    certificateData = g_byte_array_append(certificateData, certificateDataReference.data(), certificateDataReference.size());
96    GRefPtr<GByteArray> certificate = adoptGRef(certificateData);
97
98    GTlsBackend* backend = g_tls_backend_get_default();
99    certificateInfo.m_certificate = adoptGRef(G_TLS_CERTIFICATE(g_initable_new(g_tls_backend_get_certificate_type(backend), 0, 0,
100                                                                               "certificate", certificate.get(), NULL)));
101
102    uint32_t tlsErrors;
103    if (!decoder.decode(tlsErrors))
104        return false;
105    certificateInfo.m_tlsErrors = static_cast<GTlsCertificateFlags>(tlsErrors);
106
107    return true;
108}
109
110} // namespace WebKit
111