summaryrefslogtreecommitdiff
path: root/src/manager/common/pkcs12-impl.cpp
blob: 255a752a218eb33e4e1669dfe48e61f14926ca80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Copyright (c) 2014 Samsung Electronics Co.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 *
 * @file        pkcs12-impl.cpp
 * @author      Barlomiej Grzelewski (b.grzelewski@samsung.com)
 * @version     1.0
 * @brief       Certificate Implmentation.
 */
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/pkcs12.h>
#include <openssl/x509.h>

#include <dpl/log/log.h>

#include <pkcs12-impl.h>

#include <certificate-impl.h>
#include <generic-key.h>

namespace CKM {
namespace {

typedef std::unique_ptr<BIO, std::function<void(BIO*)>> BioUniquePtr;

} // anonymous namespace

PKCS12Impl::PKCS12Impl(const RawBuffer &buffer, const Password &password)
{
    EVP_PKEY *pkey = NULL;
    X509 *cert = NULL;
    STACK_OF(X509) *ca = NULL;
    ::PKCS12 *pkcs12 = NULL;

    BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
    LogDebug("Start to parse PKCS12");

    int result = BIO_write(bio.get(), buffer.data(), buffer.size());
    if (result != static_cast<int>(buffer.size())) {
        LogError("BIO_write failed. result = " << result << " Expected: " << buffer.size());
        return;
    }

    pkcs12 = d2i_PKCS12_bio(bio.get(), NULL);

    if (pkcs12 == NULL) {
        LogDebug("d2i_PKCS12_bio failed.");
        return;
    }

    if (!PKCS12_verify_mac(pkcs12, password.c_str(), password.size())) {
        LogDebug("Pkcs12 verify failed. Wrong password");
        return;
    }

    if (!PKCS12_parse(pkcs12, password.c_str(), &pkey, &cert, &ca)) {
        LogError("PKCS12_parse failed");
        return;
    }

    if (pkey) {
        GenericKey::EvpShPtr ptr(pkey, EVP_PKEY_free);
        if (EVP_PKEY_type(pkey->type) == EVP_PKEY_RSA) {
            m_pkey = std::make_shared<GenericKey>(ptr, KeyType::KEY_RSA_PRIVATE);
        } else if (EVP_PKEY_type(pkey->type) == EVP_PKEY_EC) {
            m_pkey = std::make_shared<GenericKey>(ptr, KeyType::KEY_ECDSA_PRIVATE);
        } else {
            LogError("Unsupported private key type.");
            EVP_PKEY_free(pkey);
        }
    }

    if (cert) {
        m_cert = std::make_shared<CertificateImpl>(cert, false);
    }

    if (ca) {
        while (sk_X509_num(ca) > 0) {
            X509 *top = sk_X509_pop(ca);
            m_ca.push_back(std::make_shared<CertificateImpl>(top, false));
        }

        sk_X509_pop_free(ca, X509_free);
    }
}

KeyShPtr PKCS12Impl::getKey() const {
    return m_pkey;
}

CertificateShPtr PKCS12Impl::getCertificate() const {
    return m_cert;
}

CertificateShPtrVector PKCS12Impl::getCaCertificateShPtrVector() const {
    return m_ca;
}

bool PKCS12Impl::empty() const {
    return m_pkey.get() == NULL && m_cert.get() == NULL && m_ca.empty();
}

PKCS12Impl::~PKCS12Impl()
{}

PKCS12ShPtr PKCS12::create(const RawBuffer &rawBuffer, const Password &password) {
    try {
        auto output = std::make_shared<PKCS12Impl>(rawBuffer, password);
        if (output->empty())
            output.reset();
        return output;
    } catch (const std::bad_alloc &e) {
        LogDebug("Bad alloc was caught during PKCS12 creation");
    } catch (...) {
        LogError("Critical error: Unknown exception was caught during PCKS12Impl creation!");
    }
    return PKCS12ShPtr();
}

} // namespace CKM