Coverage Report

Created: 2026-07-25 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Crypto/include/Poco/Crypto/PKCS12Container.h
Line
Count
Source
1
//
2
// PKCS12Container.h
3
//
4
// Library: Crypto
5
// Package: Certificate
6
// Module:  PKCS12Container
7
//
8
// Definition of the PKCS12Container class.
9
//
10
// Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Crypto_PKCS12Container_INCLUDED
18
#define Crypto_PKCS12Container_INCLUDED
19
20
21
#include "Poco/Crypto/Crypto.h"
22
#include "Poco/Crypto/OpenSSLInitializer.h"
23
#include "Poco/Crypto/X509Certificate.h"
24
#include "Poco/Crypto/EVPPKey.h"
25
#include "Poco/Path.h"
26
#include <memory>
27
#include <istream>
28
#include <openssl/pkcs12.h>
29
30
31
namespace Poco::Crypto {
32
33
34
class Crypto_API PKCS12Container
35
  /// This class implements PKCS#12 container functionality.
36
{
37
public:
38
  using CAList = X509Certificate::List;
39
  using CANameList = std::vector<std::string>;
40
41
  explicit PKCS12Container(std::istream& istr, const std::string& password = "");
42
    /// Creates the PKCS12Container object from a stream.
43
44
  explicit PKCS12Container(const std::string& path, const std::string& password = "");
45
    /// Creates the PKCS12Container object from a file.
46
47
  PKCS12Container(const PKCS12Container& cont);
48
    /// Copy constructor.
49
50
  PKCS12Container(PKCS12Container&& cont) noexcept;
51
    /// Move constructor.
52
53
  PKCS12Container& operator = (const PKCS12Container& cont);
54
    /// Assignment operator.
55
56
  PKCS12Container& operator = (PKCS12Container&& cont) noexcept;
57
    /// Move assignment operator.
58
59
  ~PKCS12Container();
60
    /// Destroys the PKCS12Container.
61
62
  bool hasKey() const;
63
    /// Returns true if container contains the key.
64
65
  EVPPKey getKey() const;
66
    /// Return key as openssl EVP_PKEY wrapper object.
67
68
  bool hasX509Certificate() const;
69
    /// Returns true if container has X509 certificate.
70
71
  const X509Certificate& getX509Certificate() const;
72
    /// Returns the X509 certificate.
73
    /// Throws NotFoundException if there is no certificate.
74
75
  const CAList& getCACerts() const;
76
    /// Returns the list of CA certificates in this container.
77
78
  const std::string& getFriendlyName() const;
79
    /// Returns the friendly name of the certificate bag.
80
81
  const CANameList& getFriendlyNamesCA() const;
82
    /// Returns a list of CA certificates friendly names.
83
84
private:
85
  void load(PKCS12* pPKCS12, const std::string& password = "");
86
  std::string extractFriendlyName(X509* pCert);
87
88
  using CertPtr = std::unique_ptr<X509Certificate>;
89
90
  OpenSSLInitializer _openSSLInitializer;
91
  EVP_PKEY*          _pKey;
92
  CertPtr            _pX509Cert;
93
  CAList             _caCertList;
94
  CANameList         _caCertNames;
95
  std::string        _pkcsFriendlyName;
96
};
97
98
99
//
100
// inlines
101
//
102
103
inline bool PKCS12Container::hasX509Certificate() const
104
0
{
105
0
  return _pX509Cert.get() != nullptr;
106
0
}
107
108
109
inline const X509Certificate& PKCS12Container::getX509Certificate() const
110
0
{
111
0
  if (!hasX509Certificate())
112
0
    throw NotFoundException("PKCS12Container X509 certificate");
113
0
  return *_pX509Cert;
114
0
}
115
116
117
inline const std::string& PKCS12Container::getFriendlyName() const
118
0
{
119
0
  return _pkcsFriendlyName;
120
0
}
121
122
123
inline const PKCS12Container::CAList& PKCS12Container::getCACerts() const
124
0
{
125
0
  return _caCertList;
126
0
}
127
128
129
inline const PKCS12Container::CANameList& PKCS12Container::getFriendlyNamesCA() const
130
0
{
131
0
  return _caCertNames;
132
0
}
133
134
135
inline bool PKCS12Container::hasKey() const
136
0
{
137
0
  return _pKey != nullptr;
138
0
}
139
140
141
inline EVPPKey PKCS12Container::getKey() const
142
0
{
143
0
  return EVPPKey(_pKey);
144
0
}
145
146
147
} // namespace Poco::Crypto
148
149
150
#endif // Crypto_PKCS12Container_INCLUDED