154aa6d63Sopenharmony_ci/*
254aa6d63Sopenharmony_ci * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
354aa6d63Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
454aa6d63Sopenharmony_ci * you may not use this file except in compliance with the License.
554aa6d63Sopenharmony_ci * You may obtain a copy of the License at
654aa6d63Sopenharmony_ci *
754aa6d63Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
854aa6d63Sopenharmony_ci *
954aa6d63Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1054aa6d63Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1154aa6d63Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1254aa6d63Sopenharmony_ci * See the License for the specific language governing permissions and
1354aa6d63Sopenharmony_ci * limitations under the License.
1454aa6d63Sopenharmony_ci */
1554aa6d63Sopenharmony_ci#ifndef SIGNATRUETOOLS_PKCS7_CONTEXT_H
1654aa6d63Sopenharmony_ci#define SIGNATRUETOOLS_PKCS7_CONTEXT_H
1754aa6d63Sopenharmony_ci
1854aa6d63Sopenharmony_ci#include <vector>
1954aa6d63Sopenharmony_ci#include <string>
2054aa6d63Sopenharmony_ci#include "openssl/pkcs7.h"
2154aa6d63Sopenharmony_ci#include "openssl/x509.h"
2254aa6d63Sopenharmony_ci#include "byte_buffer.h"
2354aa6d63Sopenharmony_ci#include "matching_result.h"
2454aa6d63Sopenharmony_ci
2554aa6d63Sopenharmony_cinamespace OHOS {
2654aa6d63Sopenharmony_cinamespace SignatureTools {
2754aa6d63Sopenharmony_ci
2854aa6d63Sopenharmony_ciusing CertChain = std::vector<X509*>;
2954aa6d63Sopenharmony_ciusing Pkcs7CertChains = std::vector<CertChain>;
3054aa6d63Sopenharmony_ci
3154aa6d63Sopenharmony_cistruct Pkcs7Context {
3254aa6d63Sopenharmony_ci    bool needWriteCrl;
3354aa6d63Sopenharmony_ci    int32_t digestAlgorithm = 0;
3454aa6d63Sopenharmony_ci    MatchingResult matchResult;
3554aa6d63Sopenharmony_ci    std::string certIssuer;
3654aa6d63Sopenharmony_ci    PKCS7* p7;
3754aa6d63Sopenharmony_ci    Pkcs7CertChains certChain;
3854aa6d63Sopenharmony_ci    ByteBuffer content;
3954aa6d63Sopenharmony_ci    Pkcs7Context()
4054aa6d63Sopenharmony_ci        : needWriteCrl(false), digestAlgorithm(0), matchResult(), certIssuer(),
4154aa6d63Sopenharmony_ci        p7(nullptr), certChain(), content()
4254aa6d63Sopenharmony_ci    {
4354aa6d63Sopenharmony_ci    }
4454aa6d63Sopenharmony_ci    ~Pkcs7Context()
4554aa6d63Sopenharmony_ci    {
4654aa6d63Sopenharmony_ci        if (p7 != nullptr) {
4754aa6d63Sopenharmony_ci            PKCS7_free(p7);
4854aa6d63Sopenharmony_ci            p7 = nullptr;
4954aa6d63Sopenharmony_ci        }
5054aa6d63Sopenharmony_ci        for (auto certChain : certChain) {
5154aa6d63Sopenharmony_ci            for (auto cert : certChain) {
5254aa6d63Sopenharmony_ci                X509_free(cert);
5354aa6d63Sopenharmony_ci            }
5454aa6d63Sopenharmony_ci        }
5554aa6d63Sopenharmony_ci        certChain.clear();
5654aa6d63Sopenharmony_ci    }
5754aa6d63Sopenharmony_ci};
5854aa6d63Sopenharmony_ci} // namespace SignatureTools
5954aa6d63Sopenharmony_ci} // namespace OHOS
6054aa6d63Sopenharmony_ci#endif // SIGNATRUETOOLS_PKCS7_CONTEXT_H
61