gadgets.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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_SSL_GADGETS_H
10 #define SQUID_SSL_GADGETS_H
11 
12 #if USE_OPENSSL
13 
14 #include "base/HardFun.h"
15 #include "compat/openssl.h"
16 #include "security/forward.h"
17 #include "ssl/crtd_message.h"
18 
19 #include <string>
20 
21 #if HAVE_OPENSSL_ASN1_H
22 #include <openssl/asn1.h>
23 #endif
24 #if HAVE_OPENSSL_TXT_DB_H
25 #include <openssl/txt_db.h>
26 #endif
27 #if HAVE_OPENSSL_X509V3_H
28 #include <openssl/x509v3.h>
29 #endif
30 
31 namespace Ssl
32 {
39 #if !defined(SQUID_SSL_SIGN_HASH_IF_NONE)
40 #define SQUID_SSL_SIGN_HASH_IF_NONE "sha256"
41 #endif
42 
46 sk_dtor_wrapper(sk_X509, STACK_OF(X509) *, X509_free);
47 typedef std::unique_ptr<STACK_OF(X509), sk_X509_free_wrapper> X509_STACK_Pointer;
48 
49 typedef std::unique_ptr<BIGNUM, HardFun<void, BIGNUM*, &BN_free>> BIGNUM_Pointer;
50 
51 typedef std::unique_ptr<BIO, HardFun<void, BIO*, &BIO_vfree>> BIO_Pointer;
52 
53 typedef std::unique_ptr<ASN1_INTEGER, HardFun<void, ASN1_INTEGER*, &ASN1_INTEGER_free>> ASN1_INT_Pointer;
54 
55 typedef std::unique_ptr<ASN1_OCTET_STRING, HardFun<void, ASN1_OCTET_STRING*, &ASN1_OCTET_STRING_free>> ASN1_OCTET_STRING_Pointer;
56 
57 typedef std::unique_ptr<TXT_DB, HardFun<void, TXT_DB*, &TXT_DB_free>> TXT_DB_Pointer;
58 
59 typedef std::unique_ptr<X509_NAME, HardFun<void, X509_NAME*, &X509_NAME_free>> X509_NAME_Pointer;
60 
61 typedef std::unique_ptr<RSA, HardFun<void, RSA*, &RSA_free>> RSA_Pointer;
62 
63 typedef std::unique_ptr<X509_REQ, HardFun<void, X509_REQ*, &X509_REQ_free>> X509_REQ_Pointer;
64 
65 typedef std::unique_ptr<AUTHORITY_KEYID, HardFun<void, AUTHORITY_KEYID*, &AUTHORITY_KEYID_free>> AUTHORITY_KEYID_Pointer;
66 
67 sk_dtor_wrapper(sk_GENERAL_NAME, STACK_OF(GENERAL_NAME) *, GENERAL_NAME_free);
68 typedef std::unique_ptr<STACK_OF(GENERAL_NAME), sk_GENERAL_NAME_free_wrapper> GENERAL_NAME_STACK_Pointer;
69 
70 typedef std::unique_ptr<GENERAL_NAME, HardFun<void, GENERAL_NAME*, &GENERAL_NAME_free>> GENERAL_NAME_Pointer;
71 
72 typedef std::unique_ptr<X509_EXTENSION, HardFun<void, X509_EXTENSION*, &X509_EXTENSION_free>> X509_EXTENSION_Pointer;
73 
74 typedef std::unique_ptr<X509_STORE_CTX, HardFun<void, X509_STORE_CTX *, &X509_STORE_CTX_free>> X509_STORE_CTX_Pointer;
79 EVP_PKEY * createSslPrivateKey();
80 
85 bool writeCertAndPrivateKeyToMemory(Security::CertPointer const & cert, Security::PrivateKeyPointer const & pkey, std::string & bufferToWrite);
86 
91 bool appendCertToMemory(Security::CertPointer const & cert, std::string & bufferToWrite);
92 
97 bool readCertAndPrivateKeyFromMemory(Security::CertPointer & cert, Security::PrivateKeyPointer & pkey, char const * bufferToRead);
98 
103 bool readCertFromMemory(Security::CertPointer & cert, char const * bufferToRead);
104 
109 void ReadPrivateKeyFromFile(char const * keyFilename, Security::PrivateKeyPointer &pkey, pem_password_cb *passwd_callback);
110 
115 bool OpenCertsFileForReading(BIO_Pointer &bio, const char *filename);
116 
120 
125 bool ReadPrivateKey(BIO_Pointer &bio, Security::PrivateKeyPointer &pkey, pem_password_cb *passwd_callback);
126 
132 bool OpenCertsFileForWriting(BIO_Pointer &bio, const char *filename);
133 
139 
144 bool WritePrivateKey(BIO_Pointer &bio, const Security::PrivateKeyPointer &pkey);
145 
151 
157 extern const char *CertSignAlgorithmStr[];
158 
163 inline const char *certSignAlgorithm(int sg)
164 {
165  if (sg >=0 && sg < Ssl::algSignEnd)
166  return Ssl::CertSignAlgorithmStr[sg];
167 
168  return NULL;
169 }
170 
176 {
177  for (int i = 0; i < algSignEnd && Ssl::CertSignAlgorithmStr[i] != NULL; i++)
178  if (strcmp(Ssl::CertSignAlgorithmStr[i], sg) == 0)
179  return (CertSignAlgorithm)i;
180 
181  return algSignEnd;
182 }
183 
189 
194 extern const char *CertAdaptAlgorithmStr[];
195 
200 inline const char *sslCertAdaptAlgoritm(int alg)
201 {
202  if (alg >=0 && alg < Ssl::algSetEnd)
203  return Ssl::CertAdaptAlgorithmStr[alg];
204 
205  return NULL;
206 }
207 
213 {
214 public:
218  Security::PrivateKeyPointer signWithPkey;
222  std::string commonName;
224  const EVP_MD *signHash;
225 private:
228 };
229 
232 std::string & OnDiskCertificateDbKey(const CertificateProperties &);
233 
241 bool generateSslCertificate(Security::CertPointer & cert, Security::PrivateKeyPointer & pkey, CertificateProperties const &properties);
242 
248 bool sslDateIsInTheFuture(char const * date);
249 
256 bool certificateMatchesProperties(X509 *peer_cert, CertificateProperties const &properties);
257 
263 const char *CommonHostName(X509 *x509);
264 
270 const char *getOrganization(X509 *x509);
271 
274 bool CertificatesCmp(const Security::CertPointer &cert1, const Security::CertPointer &cert2);
275 
278 const ASN1_BIT_STRING *X509_get_signature(const Security::CertPointer &);
279 
280 } // namespace Ssl
281 
282 #endif // USE_OPENSSL
283 #endif // SQUID_SSL_GADGETS_H
284 
bool appendCertToMemory(Security::CertPointer const &cert, std::string &bufferToWrite)
Definition: gadgets.cc:92
std::unique_ptr< ASN1_INTEGER, HardFun< void, ASN1_INTEGER *, &ASN1_INTEGER_free > > ASN1_INT_Pointer
Definition: gadgets.h:53
CertSignAlgorithm
Definition: gadgets.h:150
Security::CertPointer mimicCert
Certificate to mimic.
Definition: gadgets.h:216
const char * CommonHostName(X509 *x509)
Definition: gadgets.cc:897
Security::CertPointer signWithX509
Certificate to sign the generated request.
Definition: gadgets.h:217
const EVP_MD * signHash
The signing hash to use.
Definition: gadgets.h:224
std::unique_ptr< AUTHORITY_KEYID, HardFun< void, AUTHORITY_KEYID *, &AUTHORITY_KEYID_free > > AUTHORITY_KEYID_Pointer
Definition: gadgets.h:65
std::unique_ptr< STACK_OF(GENERAL_NAME), sk_GENERAL_NAME_free_wrapper > GENERAL_NAME_STACK_Pointer
Definition: gadgets.h:68
std::unique_ptr< BIO, HardFun< void, BIO *, &BIO_vfree > > BIO_Pointer
Definition: gadgets.h:51
std::unique_ptr< X509_NAME, HardFun< void, X509_NAME *, &X509_NAME_free > > X509_NAME_Pointer
Definition: gadgets.h:59
const char * sslCertAdaptAlgoritm(int alg)
Definition: gadgets.h:200
std::unique_ptr< ASN1_OCTET_STRING, HardFun< void, ASN1_OCTET_STRING *, &ASN1_OCTET_STRING_free > > ASN1_OCTET_STRING_Pointer
Definition: gadgets.h:55
std::unique_ptr< STACK_OF(X509), sk_X509_free_wrapper > X509_STACK_Pointer
Definition: gadgets.h:47
const char * certSignAlgorithm(int sg)
Definition: gadgets.h:163
bool ReadPrivateKey(BIO_Pointer &bio, Security::PrivateKeyPointer &pkey, pem_password_cb *passwd_callback)
Definition: gadgets.cc:702
bool certificateMatchesProperties(X509 *peer_cert, CertificateProperties const &properties)
Definition: gadgets.cc:806
bool CertificatesCmp(const Security::CertPointer &cert1, const Security::CertPointer &cert2)
Definition: gadgets.cc:908
bool WriteX509Certificate(BIO_Pointer &bio, const Security::CertPointer &cert)
Definition: gadgets.cc:735
const ASN1_BIT_STRING * X509_get_signature(const Security::CertPointer &)
Definition: gadgets.cc:933
const char * CertAdaptAlgorithmStr[]
Definition: gadgets.cc:198
std::unique_ptr< GENERAL_NAME, HardFun< void, GENERAL_NAME *, &GENERAL_NAME_free > > GENERAL_NAME_Pointer
Definition: gadgets.h:70
Security::CertPointer ReadX509Certificate(const BIO_Pointer &)
Definition: gadgets.cc:695
bool OpenCertsFileForReading(BIO_Pointer &bio, const char *filename)
Definition: gadgets.cc:684
CertAdaptAlgorithm
Definition: gadgets.h:188
#define NULL
Definition: types.h:166
@ algSignTrusted
Definition: gadgets.h:150
std::unique_ptr< X509_EXTENSION, HardFun< void, X509_EXTENSION *, &X509_EXTENSION_free > > X509_EXTENSION_Pointer
Definition: gadgets.h:72
@ algSignEnd
Definition: gadgets.h:150
bool generateSslCertificate(Security::CertPointer &cert, Security::PrivateKeyPointer &pkey, CertificateProperties const &properties)
Definition: gadgets.cc:673
CertificateProperties(CertificateProperties &)
Definition: Xaction.cc:49
CertificateProperties & operator=(CertificateProperties const &)
const char * getOrganization(X509 *x509)
Definition: gadgets.cc:902
EVP_PKEY * createSslPrivateKey()
Definition: gadgets.cc:12
const char * CertSignAlgorithmStr[]
Definition: gadgets.cc:191
@ algSetValidAfter
Definition: gadgets.h:188
bool setValidBefore
Do not mimic "Not Valid Before" field.
Definition: gadgets.h:220
std::unique_ptr< TXT_DB, HardFun< void, TXT_DB *, &TXT_DB_free > > TXT_DB_Pointer
Definition: gadgets.h:57
@ algSetCommonName
Definition: gadgets.h:188
@ algSignSelf
Definition: gadgets.h:150
Security::PrivateKeyPointer signWithPkey
The key of the signing certificate.
Definition: gadgets.h:218
void ReadPrivateKeyFromFile(char const *keyFilename, Security::PrivateKeyPointer &pkey, pem_password_cb *passwd_callback)
Definition: gadgets.cc:713
bool readCertFromMemory(Security::CertPointer &cert, char const *bufferToRead)
Definition: gadgets.cc:132
@ algSignUntrusted
Definition: gadgets.h:150
bool sslDateIsInTheFuture(char const *date)
Definition: gadgets.cc:754
@ algSetValidBefore
Definition: gadgets.h:188
std::unique_ptr< X509_REQ, HardFun< void, X509_REQ *, &X509_REQ_free > > X509_REQ_Pointer
Definition: gadgets.h:63
std::string commonName
A CN to use for the generated certificate.
Definition: gadgets.h:222
sk_dtor_wrapper(sk_X509, STACK_OF(X509) *, X509_free)
bool OpenCertsFileForWriting(BIO_Pointer &bio, const char *filename)
Definition: gadgets.cc:724
std::unique_ptr< X509_STORE_CTX, HardFun< void, X509_STORE_CTX *, &X509_STORE_CTX_free > > X509_STORE_CTX_Pointer
Definition: gadgets.h:74
bool WritePrivateKey(BIO_Pointer &bio, const Security::PrivateKeyPointer &pkey)
Definition: gadgets.cc:745
@ algSetEnd
Definition: gadgets.h:188
CertSignAlgorithm signAlgorithm
The signing algorithm to use.
Definition: gadgets.h:223
CertSignAlgorithm certSignAlgorithmId(const char *sg)
Definition: gadgets.h:175
bool setValidAfter
Do not mimic "Not Valid After" field.
Definition: gadgets.h:219
std::unique_ptr< BIGNUM, HardFun< void, BIGNUM *, &BN_free > > BIGNUM_Pointer
Definition: gadgets.h:49
bool writeCertAndPrivateKeyToMemory(Security::CertPointer const &cert, Security::PrivateKeyPointer const &pkey, std::string &bufferToWrite)
Definition: gadgets.cc:68
std::string & OnDiskCertificateDbKey(const CertificateProperties &)
Definition: gadgets.cc:228
STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx)
Definition: openssl.h:237
std::unique_ptr< RSA, HardFun< void, RSA *, &RSA_free > > RSA_Pointer
Definition: gadgets.h:61
bool setCommonName
Replace the CN field of the mimicking subject with the given.
Definition: gadgets.h:221
bool readCertAndPrivateKeyFromMemory(Security::CertPointer &cert, Security::PrivateKeyPointer &pkey, char const *bufferToRead)
Definition: gadgets.cc:116

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors