gadgets.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_SSL_GADGETS_H
10 #define SQUID_SRC_SSL_GADGETS_H
11 
12 #if USE_OPENSSL
13 
14 #include "anyp/forward.h"
15 #include "base/HardFun.h"
16 #include "compat/openssl.h"
17 #include "sbuf/forward.h"
18 #include "security/forward.h"
19 #include "ssl/crtd_message.h"
20 
21 #include <optional>
22 #include <string>
23 
24 #if HAVE_OPENSSL_ASN1_H
25 #include <openssl/asn1.h>
26 #endif
27 #if HAVE_OPENSSL_PEM_H
28 #include <openssl/pem.h>
29 #endif
30 #if HAVE_OPENSSL_TXT_DB_H
31 #include <openssl/txt_db.h>
32 #endif
33 #if HAVE_OPENSSL_X509V3_H
34 #include <openssl/x509v3.h>
35 #endif
36 
37 namespace Ssl
38 {
45 #if !defined(SQUID_SSL_SIGN_HASH_IF_NONE)
46 #define SQUID_SSL_SIGN_HASH_IF_NONE "sha256"
47 #endif
48 
52 sk_dtor_wrapper(sk_X509, STACK_OF(X509) *, X509_free);
53 typedef std::unique_ptr<STACK_OF(X509), sk_X509_free_wrapper> X509_STACK_Pointer;
54 
55 typedef std::unique_ptr<BIGNUM, HardFun<void, BIGNUM*, &BN_free>> BIGNUM_Pointer;
56 
57 typedef std::unique_ptr<BIO, HardFun<void, BIO*, &BIO_vfree>> BIO_Pointer;
58 
59 typedef std::unique_ptr<ASN1_INTEGER, HardFun<void, ASN1_INTEGER*, &ASN1_INTEGER_free>> ASN1_INT_Pointer;
60 
61 typedef std::unique_ptr<ASN1_OCTET_STRING, HardFun<void, ASN1_OCTET_STRING*, &ASN1_OCTET_STRING_free>> ASN1_OCTET_STRING_Pointer;
62 
63 typedef std::unique_ptr<TXT_DB, HardFun<void, TXT_DB*, &TXT_DB_free>> TXT_DB_Pointer;
64 
65 typedef std::unique_ptr<X509_NAME, HardFun<void, X509_NAME*, &X509_NAME_free>> X509_NAME_Pointer;
66 
67 using EVP_PKEY_CTX_Pointer = std::unique_ptr<EVP_PKEY_CTX, HardFun<void, EVP_PKEY_CTX*, &EVP_PKEY_CTX_free>>;
68 
69 typedef std::unique_ptr<X509_REQ, HardFun<void, X509_REQ*, &X509_REQ_free>> X509_REQ_Pointer;
70 
71 typedef std::unique_ptr<AUTHORITY_KEYID, HardFun<void, AUTHORITY_KEYID*, &AUTHORITY_KEYID_free>> AUTHORITY_KEYID_Pointer;
72 
73 sk_dtor_wrapper(sk_GENERAL_NAME, STACK_OF(GENERAL_NAME) *, GENERAL_NAME_free);
74 typedef std::unique_ptr<STACK_OF(GENERAL_NAME), sk_GENERAL_NAME_free_wrapper> GENERAL_NAME_STACK_Pointer;
75 
76 typedef std::unique_ptr<GENERAL_NAME, HardFun<void, GENERAL_NAME*, &GENERAL_NAME_free>> GENERAL_NAME_Pointer;
77 
78 typedef std::unique_ptr<X509_EXTENSION, HardFun<void, X509_EXTENSION*, &X509_EXTENSION_free>> X509_EXTENSION_Pointer;
79 
80 typedef std::unique_ptr<X509_STORE_CTX, HardFun<void, X509_STORE_CTX *, &X509_STORE_CTX_free>> X509_STORE_CTX_Pointer;
81 
82 // not using CtoCpp1() here because OpenSSL_free() takes void* rather than char*
83 inline void OPENSSL_free_for_c_strings(char * const string) { OPENSSL_free(string); }
84 using UniqueCString = std::unique_ptr<char, HardFun<void, char *, &OPENSSL_free_for_c_strings> >;
85 
87 void ForgetErrors();
88 
93 std::ostream &ReportAndForgetErrors(std::ostream &);
94 
99 bool writeCertAndPrivateKeyToMemory(Security::CertPointer const & cert, Security::PrivateKeyPointer const & pkey, std::string & bufferToWrite);
100 
105 bool appendCertToMemory(Security::CertPointer const & cert, std::string & bufferToWrite);
106 
111 bool readCertAndPrivateKeyFromMemory(Security::CertPointer & cert, Security::PrivateKeyPointer & pkey, char const * bufferToRead);
112 
115 BIO_Pointer ReadOnlyBioTiedTo(const char *);
116 
121 void ReadPrivateKeyFromFile(char const * keyFilename, Security::PrivateKeyPointer &pkey, pem_password_cb *passwd_callback);
122 
127 bool OpenCertsFileForReading(BIO_Pointer &bio, const char *filename);
128 
132 
136 
141 bool ReadPrivateKey(BIO_Pointer &bio, Security::PrivateKeyPointer &pkey, pem_password_cb *passwd_callback);
142 
148 bool OpenCertsFileForWriting(BIO_Pointer &bio, const char *filename);
149 
155 
160 bool WritePrivateKey(BIO_Pointer &bio, const Security::PrivateKeyPointer &pkey);
161 
163 UniqueCString OneLineSummary(X509_NAME &);
164 
170 
176 extern const char *CertSignAlgorithmStr[];
177 
182 inline const char *certSignAlgorithm(int sg)
183 {
184  if (sg >=0 && sg < Ssl::algSignEnd)
185  return Ssl::CertSignAlgorithmStr[sg];
186 
187  return nullptr;
188 }
189 
195 {
196  for (int i = 0; i < algSignEnd && Ssl::CertSignAlgorithmStr[i] != nullptr; i++)
197  if (strcmp(Ssl::CertSignAlgorithmStr[i], sg) == 0)
198  return (CertSignAlgorithm)i;
199 
200  return algSignEnd;
201 }
202 
208 
213 extern const char *CertAdaptAlgorithmStr[];
214 
219 inline const char *sslCertAdaptAlgoritm(int alg)
220 {
221  if (alg >=0 && alg < Ssl::algSetEnd)
222  return Ssl::CertAdaptAlgorithmStr[alg];
223 
224  return nullptr;
225 }
226 
232 {
233 public:
237  Security::PrivateKeyPointer signWithPkey;
241  std::string commonName;
243  const EVP_MD *signHash;
244 private:
247 };
248 
251 std::string & OnDiskCertificateDbKey(const CertificateProperties &);
252 
260 bool generateSslCertificate(Security::CertPointer & cert, Security::PrivateKeyPointer & pkey, CertificateProperties const &properties);
261 
267 bool sslDateIsInTheFuture(char const * date);
268 
275 bool certificateMatchesProperties(X509 *peer_cert, CertificateProperties const &properties);
276 
282 const char *CommonHostName(X509 *x509);
283 
285 SBuf AsnToSBuf(const ASN1_STRING &);
286 
288 std::optional<AnyP::Host> ParseCommonNameAt(X509_NAME &, int);
289 
292 std::optional<AnyP::Host> ParseAsSimpleDomainNameOrIp(const SBuf &);
293 
299 const char *getOrganization(X509 *x509);
300 
303 bool CertificatesCmp(const Security::CertPointer &cert1, const Security::CertPointer &cert2);
304 
307 const ASN1_BIT_STRING *X509_get_signature(const Security::CertPointer &);
308 
309 } // namespace Ssl
310 
311 #endif // USE_OPENSSL
312 #endif /* SQUID_SRC_SSL_GADGETS_H */
313 
bool appendCertToMemory(Security::CertPointer const &cert, std::string &bufferToWrite)
Definition: gadgets.cc:123
std::optional< AnyP::Host > ParseCommonNameAt(X509_NAME &, int)
interprets X.509 Subject or Issuer name entry (at the given position) as CN
Definition: gadgets.cc:504
std::unique_ptr< ASN1_INTEGER, HardFun< void, ASN1_INTEGER *, &ASN1_INTEGER_free > > ASN1_INT_Pointer
Definition: gadgets.h:59
Security::CertPointer ReadCertificate(const BIO_Pointer &)
Definition: gadgets.cc:816
CertSignAlgorithm
Definition: gadgets.h:169
Security::CertPointer mimicCert
Certificate to mimic.
Definition: gadgets.h:235
const char * CommonHostName(X509 *x509)
Definition: gadgets.cc:1027
Security::CertPointer signWithX509
Certificate to sign the generated request.
Definition: gadgets.h:236
const EVP_MD * signHash
The signing hash to use.
Definition: gadgets.h:243
std::unique_ptr< AUTHORITY_KEYID, HardFun< void, AUTHORITY_KEYID *, &AUTHORITY_KEYID_free > > AUTHORITY_KEYID_Pointer
Definition: gadgets.h:71
SBuf AsnToSBuf(const ASN1_STRING &)
converts ASN1_STRING to SBuf
Definition: gadgets.cc:473
std::unique_ptr< STACK_OF(GENERAL_NAME), sk_GENERAL_NAME_free_wrapper > GENERAL_NAME_STACK_Pointer
Definition: gadgets.h:74
std::unique_ptr< BIO, HardFun< void, BIO *, &BIO_vfree > > BIO_Pointer
Definition: gadgets.h:57
std::unique_ptr< X509_NAME, HardFun< void, X509_NAME *, &X509_NAME_free > > X509_NAME_Pointer
Definition: gadgets.h:65
const char * sslCertAdaptAlgoritm(int alg)
Definition: gadgets.h:219
std::unique_ptr< ASN1_OCTET_STRING, HardFun< void, ASN1_OCTET_STRING *, &ASN1_OCTET_STRING_free > > ASN1_OCTET_STRING_Pointer
Definition: gadgets.h:61
Definition: SBuf.h:93
std::unique_ptr< STACK_OF(X509), sk_X509_free_wrapper > X509_STACK_Pointer
Definition: gadgets.h:53
const char * certSignAlgorithm(int sg)
Definition: gadgets.h:182
bool ReadPrivateKey(BIO_Pointer &bio, Security::PrivateKeyPointer &pkey, pem_password_cb *passwd_callback)
Definition: gadgets.cc:826
BIO_Pointer ReadOnlyBioTiedTo(const char *)
Definition: gadgets.cc:172
bool certificateMatchesProperties(X509 *peer_cert, CertificateProperties const &properties)
Definition: gadgets.cc:936
bool CertificatesCmp(const Security::CertPointer &cert1, const Security::CertPointer &cert2)
Definition: gadgets.cc:1038
bool WriteX509Certificate(BIO_Pointer &bio, const Security::CertPointer &cert)
Definition: gadgets.cc:859
const ASN1_BIT_STRING * X509_get_signature(const Security::CertPointer &)
Definition: gadgets.cc:1063
const char * CertAdaptAlgorithmStr[]
Definition: gadgets.cc:239
std::unique_ptr< GENERAL_NAME, HardFun< void, GENERAL_NAME *, &GENERAL_NAME_free > > GENERAL_NAME_Pointer
Definition: gadgets.h:76
std::ostream & ReportAndForgetErrors(std::ostream &)
Definition: gadgets.cc:36
Security::CertPointer ReadOptionalCertificate(const BIO_Pointer &)
Definition: gadgets.cc:791
bool OpenCertsFileForReading(BIO_Pointer &bio, const char *filename)
Definition: gadgets.cc:780
CertAdaptAlgorithm
Definition: gadgets.h:207
@ algSignTrusted
Definition: gadgets.h:169
void OPENSSL_free_for_c_strings(char *const string)
Definition: gadgets.h:83
std::unique_ptr< X509_EXTENSION, HardFun< void, X509_EXTENSION *, &X509_EXTENSION_free > > X509_EXTENSION_Pointer
Definition: gadgets.h:78
@ algSignEnd
Definition: gadgets.h:169
bool generateSslCertificate(Security::CertPointer &cert, Security::PrivateKeyPointer &pkey, CertificateProperties const &properties)
Definition: gadgets.cc:769
Definition: Xaction.cc:39
CertificateProperties & operator=(CertificateProperties const &)
const char * getOrganization(X509 *x509)
Definition: gadgets.cc:1032
const char * CertSignAlgorithmStr[]
Definition: gadgets.cc:232
@ algSetValidAfter
Definition: gadgets.h:207
std::unique_ptr< char, HardFun< void, char *, &OPENSSL_free_for_c_strings > > UniqueCString
Definition: gadgets.h:84
bool setValidBefore
Do not mimic "Not Valid Before" field.
Definition: gadgets.h:239
std::unique_ptr< TXT_DB, HardFun< void, TXT_DB *, &TXT_DB_free > > TXT_DB_Pointer
Definition: gadgets.h:63
@ algSetCommonName
Definition: gadgets.h:207
@ algSignSelf
Definition: gadgets.h:169
Security::PrivateKeyPointer signWithPkey
The key of the signing certificate.
Definition: gadgets.h:237
UniqueCString OneLineSummary(X509_NAME &)
a RAII wrapper for the memory-allocating flavor of X509_NAME_oneline()
Definition: gadgets.cc:879
void ReadPrivateKeyFromFile(char const *keyFilename, Security::PrivateKeyPointer &pkey, pem_password_cb *passwd_callback)
Definition: gadgets.cc:837
@ algSignUntrusted
Definition: gadgets.h:169
bool sslDateIsInTheFuture(char const *date)
Definition: gadgets.cc:884
std::optional< AnyP::Host > ParseAsSimpleDomainNameOrIp(const SBuf &)
Definition: gadgets.cc:496
@ algSetValidBefore
Definition: gadgets.h:207
std::unique_ptr< X509_REQ, HardFun< void, X509_REQ *, &X509_REQ_free > > X509_REQ_Pointer
Definition: gadgets.h:69
std::string commonName
A CN to use for the generated certificate.
Definition: gadgets.h:241
sk_dtor_wrapper(sk_X509, STACK_OF(X509) *, X509_free)
bool OpenCertsFileForWriting(BIO_Pointer &bio, const char *filename)
Definition: gadgets.cc:848
std::unique_ptr< X509_STORE_CTX, HardFun< void, X509_STORE_CTX *, &X509_STORE_CTX_free > > X509_STORE_CTX_Pointer
Definition: gadgets.h:80
void ForgetErrors()
Clear any errors accumulated by OpenSSL in its global storage.
Definition: gadgets.cc:19
bool WritePrivateKey(BIO_Pointer &bio, const Security::PrivateKeyPointer &pkey)
Definition: gadgets.cc:869
@ algSetEnd
Definition: gadgets.h:207
CertSignAlgorithm signAlgorithm
The signing algorithm to use.
Definition: gadgets.h:242
CertSignAlgorithm certSignAlgorithmId(const char *sg)
Definition: gadgets.h:194
std::unique_ptr< EVP_PKEY_CTX, HardFun< void, EVP_PKEY_CTX *, &EVP_PKEY_CTX_free > > EVP_PKEY_CTX_Pointer
Definition: gadgets.h:67
bool setValidAfter
Do not mimic "Not Valid After" field.
Definition: gadgets.h:238
std::unique_ptr< BIGNUM, HardFun< void, BIGNUM *, &BN_free > > BIGNUM_Pointer
Definition: gadgets.h:55
bool writeCertAndPrivateKeyToMemory(Security::CertPointer const &cert, Security::PrivateKeyPointer const &pkey, std::string &bufferToWrite)
Definition: gadgets.cc:99
std::string & OnDiskCertificateDbKey(const CertificateProperties &)
Definition: gadgets.cc:269
STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx)
Definition: openssl.h:237
bool setCommonName
Replace the CN field of the mimicking subject with the given.
Definition: gadgets.h:240
bool readCertAndPrivateKeyFromMemory(Security::CertPointer &cert, Security::PrivateKeyPointer &pkey, char const *bufferToRead)
Definition: gadgets.cc:147

 

Introduction

Documentation

Support

Miscellaneous