_sslverify.py 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. # -*- test-case-name: twisted.test.test_sslverify -*-
  2. # Copyright (c) 2005 Divmod, Inc.
  3. # Copyright (c) Twisted Matrix Laboratories.
  4. # See LICENSE for details.
  5. from __future__ import division, absolute_import
  6. import warnings
  7. from constantly import Names, NamedConstant
  8. from hashlib import md5
  9. from OpenSSL import SSL, crypto
  10. from OpenSSL._util import lib as pyOpenSSLlib
  11. from twisted.internet.abstract import isIPAddress, isIPv6Address
  12. from twisted.python import log
  13. from twisted.python.randbytes import secureRandom
  14. from twisted.python._oldstyle import _oldStyle
  15. from ._idna import _idnaBytes
  16. from zope.interface import Interface, implementer
  17. from constantly import Flags, FlagConstant
  18. from incremental import Version
  19. from twisted.internet.defer import Deferred
  20. from twisted.internet.error import VerifyError, CertificateError
  21. from twisted.internet.interfaces import (
  22. IAcceptableCiphers, ICipher, IOpenSSLClientConnectionCreator,
  23. IOpenSSLContextFactory
  24. )
  25. from twisted.python import util
  26. from twisted.python.deprecate import _mutuallyExclusiveArguments
  27. from twisted.python.compat import nativeString, unicode
  28. from twisted.python.failure import Failure
  29. from twisted.python.util import FancyEqMixin
  30. from twisted.python.deprecate import deprecated
  31. class TLSVersion(Names):
  32. """
  33. TLS versions that we can negotiate with the client/server.
  34. """
  35. SSLv3 = NamedConstant()
  36. TLSv1_0 = NamedConstant()
  37. TLSv1_1 = NamedConstant()
  38. TLSv1_2 = NamedConstant()
  39. TLSv1_3 = NamedConstant()
  40. _tlsDisableFlags = {
  41. TLSVersion.SSLv3: SSL.OP_NO_SSLv3,
  42. TLSVersion.TLSv1_0: SSL.OP_NO_TLSv1,
  43. TLSVersion.TLSv1_1: SSL.OP_NO_TLSv1_1,
  44. TLSVersion.TLSv1_2: SSL.OP_NO_TLSv1_2,
  45. # If we don't have TLS v1.3 yet, we can't disable it -- this is just so
  46. # when it makes it into OpenSSL, connections knowingly bracketed to v1.2
  47. # don't end up going to v1.3
  48. TLSVersion.TLSv1_3: getattr(SSL, "OP_NO_TLSv1_3", 0x00),
  49. }
  50. def _getExcludedTLSProtocols(oldest, newest):
  51. """
  52. Given a pair of L{TLSVersion} constants, figure out what versions we want
  53. to disable (as OpenSSL is an exclusion based API).
  54. @param oldest: The oldest L{TLSVersion} we want to allow.
  55. @type oldest: L{TLSVersion} constant
  56. @param newest: The newest L{TLSVersion} we want to allow, or L{None} for no
  57. upper limit.
  58. @type newest: L{TLSVersion} constant or L{None}
  59. @return: The versions we want to disable.
  60. @rtype: L{list} of L{TLSVersion} constants.
  61. """
  62. versions = list(TLSVersion.iterconstants())
  63. excludedVersions = [x for x in versions[:versions.index(oldest)]]
  64. if newest:
  65. excludedVersions.extend([x for x in versions[versions.index(newest):]])
  66. return excludedVersions
  67. class SimpleVerificationError(Exception):
  68. """
  69. Not a very useful verification error.
  70. """
  71. def simpleVerifyHostname(connection, hostname):
  72. """
  73. Check only the common name in the certificate presented by the peer and
  74. only for an exact match.
  75. This is to provide I{something} in the way of hostname verification to
  76. users who haven't installed C{service_identity}. This check is overly
  77. strict, relies on a deprecated TLS feature (you're supposed to ignore the
  78. commonName if the subjectAlternativeName extensions are present, I
  79. believe), and lots of valid certificates will fail.
  80. @param connection: the OpenSSL connection to verify.
  81. @type connection: L{OpenSSL.SSL.Connection}
  82. @param hostname: The hostname expected by the user.
  83. @type hostname: L{unicode}
  84. @raise twisted.internet.ssl.VerificationError: if the common name and
  85. hostname don't match.
  86. """
  87. commonName = connection.get_peer_certificate().get_subject().commonName
  88. if commonName != hostname:
  89. raise SimpleVerificationError(repr(commonName) + "!=" +
  90. repr(hostname))
  91. def simpleVerifyIPAddress(connection, hostname):
  92. """
  93. Always fails validation of IP addresses
  94. @param connection: the OpenSSL connection to verify.
  95. @type connection: L{OpenSSL.SSL.Connection}
  96. @param hostname: The hostname expected by the user.
  97. @type hostname: L{unicode}
  98. @raise twisted.internet.ssl.VerificationError: Always raised
  99. """
  100. raise SimpleVerificationError("Cannot verify certificate IP addresses")
  101. def _usablePyOpenSSL(version):
  102. """
  103. Check pyOpenSSL version string whether we can use it for host verification.
  104. @param version: A pyOpenSSL version string.
  105. @type version: L{str}
  106. @rtype: L{bool}
  107. """
  108. major, minor = (int(part) for part in version.split(".")[:2])
  109. return (major, minor) >= (0, 12)
  110. def _selectVerifyImplementation():
  111. """
  112. Determine if C{service_identity} is installed. If so, use it. If not, use
  113. simplistic and incorrect checking as implemented in
  114. L{simpleVerifyHostname}.
  115. @return: 2-tuple of (C{verify_hostname}, C{VerificationError})
  116. @rtype: L{tuple}
  117. """
  118. whatsWrong = (
  119. "Without the service_identity module, Twisted can perform only "
  120. "rudimentary TLS client hostname verification. Many valid "
  121. "certificate/hostname mappings may be rejected."
  122. )
  123. try:
  124. from service_identity import VerificationError
  125. from service_identity.pyopenssl import (
  126. verify_hostname, verify_ip_address
  127. )
  128. return verify_hostname, verify_ip_address, VerificationError
  129. except ImportError as e:
  130. warnings.warn_explicit(
  131. "You do not have a working installation of the "
  132. "service_identity module: '" + str(e) + "'. "
  133. "Please install it from "
  134. "<https://pypi.python.org/pypi/service_identity> and make "
  135. "sure all of its dependencies are satisfied. "
  136. + whatsWrong,
  137. # Unfortunately the lineno is required.
  138. category=UserWarning, filename="", lineno=0)
  139. return simpleVerifyHostname, simpleVerifyIPAddress, SimpleVerificationError
  140. verifyHostname, verifyIPAddress, VerificationError = \
  141. _selectVerifyImplementation()
  142. class ProtocolNegotiationSupport(Flags):
  143. """
  144. L{ProtocolNegotiationSupport} defines flags which are used to indicate the
  145. level of NPN/ALPN support provided by the TLS backend.
  146. @cvar NOSUPPORT: There is no support for NPN or ALPN. This is exclusive
  147. with both L{NPN} and L{ALPN}.
  148. @cvar NPN: The implementation supports Next Protocol Negotiation.
  149. @cvar ALPN: The implementation supports Application Layer Protocol
  150. Negotiation.
  151. """
  152. NPN = FlagConstant(0x0001)
  153. ALPN = FlagConstant(0x0002)
  154. # FIXME: https://twistedmatrix.com/trac/ticket/8074
  155. # Currently flags with literal zero values behave incorrectly. However,
  156. # creating a flag by NOTing a flag with itself appears to work totally fine, so
  157. # do that instead.
  158. ProtocolNegotiationSupport.NOSUPPORT = (
  159. ProtocolNegotiationSupport.NPN ^ ProtocolNegotiationSupport.NPN
  160. )
  161. def protocolNegotiationMechanisms():
  162. """
  163. Checks whether your versions of PyOpenSSL and OpenSSL are recent enough to
  164. support protocol negotiation, and if they are, what kind of protocol
  165. negotiation is supported.
  166. @return: A combination of flags from L{ProtocolNegotiationSupport} that
  167. indicate which mechanisms for protocol negotiation are supported.
  168. @rtype: L{constantly.FlagConstant}
  169. """
  170. support = ProtocolNegotiationSupport.NOSUPPORT
  171. ctx = SSL.Context(SSL.SSLv23_METHOD)
  172. try:
  173. ctx.set_npn_advertise_callback(lambda c: None)
  174. except (AttributeError, NotImplementedError):
  175. pass
  176. else:
  177. support |= ProtocolNegotiationSupport.NPN
  178. try:
  179. ctx.set_alpn_select_callback(lambda c: None)
  180. except (AttributeError, NotImplementedError):
  181. pass
  182. else:
  183. support |= ProtocolNegotiationSupport.ALPN
  184. return support
  185. _x509names = {
  186. 'CN': 'commonName',
  187. 'commonName': 'commonName',
  188. 'O': 'organizationName',
  189. 'organizationName': 'organizationName',
  190. 'OU': 'organizationalUnitName',
  191. 'organizationalUnitName': 'organizationalUnitName',
  192. 'L': 'localityName',
  193. 'localityName': 'localityName',
  194. 'ST': 'stateOrProvinceName',
  195. 'stateOrProvinceName': 'stateOrProvinceName',
  196. 'C': 'countryName',
  197. 'countryName': 'countryName',
  198. 'emailAddress': 'emailAddress'}
  199. class DistinguishedName(dict):
  200. """
  201. Identify and describe an entity.
  202. Distinguished names are used to provide a minimal amount of identifying
  203. information about a certificate issuer or subject. They are commonly
  204. created with one or more of the following fields::
  205. commonName (CN)
  206. organizationName (O)
  207. organizationalUnitName (OU)
  208. localityName (L)
  209. stateOrProvinceName (ST)
  210. countryName (C)
  211. emailAddress
  212. A L{DistinguishedName} should be constructed using keyword arguments whose
  213. keys can be any of the field names above (as a native string), and the
  214. values are either Unicode text which is encodable to ASCII, or L{bytes}
  215. limited to the ASCII subset. Any fields passed to the constructor will be
  216. set as attributes, accessible using both their extended name and their
  217. shortened acronym. The attribute values will be the ASCII-encoded
  218. bytes. For example::
  219. >>> dn = DistinguishedName(commonName=b'www.example.com',
  220. ... C='US')
  221. >>> dn.C
  222. b'US'
  223. >>> dn.countryName
  224. b'US'
  225. >>> hasattr(dn, "organizationName")
  226. False
  227. L{DistinguishedName} instances can also be used as dictionaries; the keys
  228. are extended name of the fields::
  229. >>> dn.keys()
  230. ['countryName', 'commonName']
  231. >>> dn['countryName']
  232. b'US'
  233. """
  234. __slots__ = ()
  235. def __init__(self, **kw):
  236. for k, v in kw.items():
  237. setattr(self, k, v)
  238. def _copyFrom(self, x509name):
  239. for name in _x509names:
  240. value = getattr(x509name, name, None)
  241. if value is not None:
  242. setattr(self, name, value)
  243. def _copyInto(self, x509name):
  244. for k, v in self.items():
  245. setattr(x509name, k, nativeString(v))
  246. def __repr__(self):
  247. return '<DN %s>' % (dict.__repr__(self)[1:-1])
  248. def __getattr__(self, attr):
  249. try:
  250. return self[_x509names[attr]]
  251. except KeyError:
  252. raise AttributeError(attr)
  253. def __setattr__(self, attr, value):
  254. if attr not in _x509names:
  255. raise AttributeError("%s is not a valid OpenSSL X509 name field" % (attr,))
  256. realAttr = _x509names[attr]
  257. if not isinstance(value, bytes):
  258. value = value.encode("ascii")
  259. self[realAttr] = value
  260. def inspect(self):
  261. """
  262. Return a multi-line, human-readable representation of this DN.
  263. @rtype: L{str}
  264. """
  265. l = []
  266. lablen = 0
  267. def uniqueValues(mapping):
  268. return set(mapping.values())
  269. for k in sorted(uniqueValues(_x509names)):
  270. label = util.nameToLabel(k)
  271. lablen = max(len(label), lablen)
  272. v = getattr(self, k, None)
  273. if v is not None:
  274. l.append((label, nativeString(v)))
  275. lablen += 2
  276. for n, (label, attr) in enumerate(l):
  277. l[n] = (label.rjust(lablen)+': '+ attr)
  278. return '\n'.join(l)
  279. DN = DistinguishedName
  280. @_oldStyle
  281. class CertBase:
  282. """
  283. Base class for public (certificate only) and private (certificate + key
  284. pair) certificates.
  285. @ivar original: The underlying OpenSSL certificate object.
  286. @type original: L{OpenSSL.crypto.X509}
  287. """
  288. def __init__(self, original):
  289. self.original = original
  290. def _copyName(self, suffix):
  291. dn = DistinguishedName()
  292. dn._copyFrom(getattr(self.original, 'get_'+suffix)())
  293. return dn
  294. def getSubject(self):
  295. """
  296. Retrieve the subject of this certificate.
  297. @return: A copy of the subject of this certificate.
  298. @rtype: L{DistinguishedName}
  299. """
  300. return self._copyName('subject')
  301. def __conform__(self, interface):
  302. """
  303. Convert this L{CertBase} into a provider of the given interface.
  304. @param interface: The interface to conform to.
  305. @type interface: L{zope.interface.interfaces.IInterface}
  306. @return: an L{IOpenSSLTrustRoot} provider or L{NotImplemented}
  307. @rtype: L{IOpenSSLTrustRoot} or L{NotImplemented}
  308. """
  309. if interface is IOpenSSLTrustRoot:
  310. return OpenSSLCertificateAuthorities([self.original])
  311. return NotImplemented
  312. def _handleattrhelper(Class, transport, methodName):
  313. """
  314. (private) Helper for L{Certificate.peerFromTransport} and
  315. L{Certificate.hostFromTransport} which checks for incompatible handle types
  316. and null certificates and raises the appropriate exception or returns the
  317. appropriate certificate object.
  318. """
  319. method = getattr(transport.getHandle(),
  320. "get_%s_certificate" % (methodName,), None)
  321. if method is None:
  322. raise CertificateError(
  323. "non-TLS transport %r did not have %s certificate" % (transport, methodName))
  324. cert = method()
  325. if cert is None:
  326. raise CertificateError(
  327. "TLS transport %r did not have %s certificate" % (transport, methodName))
  328. return Class(cert)
  329. class Certificate(CertBase):
  330. """
  331. An x509 certificate.
  332. """
  333. def __repr__(self):
  334. return '<%s Subject=%s Issuer=%s>' % (self.__class__.__name__,
  335. self.getSubject().commonName,
  336. self.getIssuer().commonName)
  337. def __eq__(self, other):
  338. if isinstance(other, Certificate):
  339. return self.dump() == other.dump()
  340. return False
  341. def __ne__(self, other):
  342. return not self.__eq__(other)
  343. @classmethod
  344. def load(Class, requestData, format=crypto.FILETYPE_ASN1, args=()):
  345. """
  346. Load a certificate from an ASN.1- or PEM-format string.
  347. @rtype: C{Class}
  348. """
  349. return Class(crypto.load_certificate(format, requestData), *args)
  350. # We can't use super() because it is old style still, so we have to hack
  351. # around things wanting to call the parent function
  352. _load = load
  353. def dumpPEM(self):
  354. """
  355. Dump this certificate to a PEM-format data string.
  356. @rtype: L{str}
  357. """
  358. return self.dump(crypto.FILETYPE_PEM)
  359. @classmethod
  360. def loadPEM(Class, data):
  361. """
  362. Load a certificate from a PEM-format data string.
  363. @rtype: C{Class}
  364. """
  365. return Class.load(data, crypto.FILETYPE_PEM)
  366. @classmethod
  367. def peerFromTransport(Class, transport):
  368. """
  369. Get the certificate for the remote end of the given transport.
  370. @param transport: an L{ISystemHandle} provider
  371. @rtype: C{Class}
  372. @raise: L{CertificateError}, if the given transport does not have a peer
  373. certificate.
  374. """
  375. return _handleattrhelper(Class, transport, 'peer')
  376. @classmethod
  377. def hostFromTransport(Class, transport):
  378. """
  379. Get the certificate for the local end of the given transport.
  380. @param transport: an L{ISystemHandle} provider; the transport we will
  381. @rtype: C{Class}
  382. @raise: L{CertificateError}, if the given transport does not have a host
  383. certificate.
  384. """
  385. return _handleattrhelper(Class, transport, 'host')
  386. def getPublicKey(self):
  387. """
  388. Get the public key for this certificate.
  389. @rtype: L{PublicKey}
  390. """
  391. return PublicKey(self.original.get_pubkey())
  392. def dump(self, format=crypto.FILETYPE_ASN1):
  393. return crypto.dump_certificate(format, self.original)
  394. def serialNumber(self):
  395. """
  396. Retrieve the serial number of this certificate.
  397. @rtype: L{int}
  398. """
  399. return self.original.get_serial_number()
  400. def digest(self, method='md5'):
  401. """
  402. Return a digest hash of this certificate using the specified hash
  403. algorithm.
  404. @param method: One of C{'md5'} or C{'sha'}.
  405. @return: The digest of the object, formatted as b":"-delimited hex
  406. pairs
  407. @rtype: L{bytes}
  408. """
  409. return self.original.digest(method)
  410. def _inspect(self):
  411. return '\n'.join(['Certificate For Subject:',
  412. self.getSubject().inspect(),
  413. '\nIssuer:',
  414. self.getIssuer().inspect(),
  415. '\nSerial Number: %d' % self.serialNumber(),
  416. 'Digest: %s' % nativeString(self.digest())])
  417. def inspect(self):
  418. """
  419. Return a multi-line, human-readable representation of this
  420. Certificate, including information about the subject, issuer, and
  421. public key.
  422. """
  423. return '\n'.join((self._inspect(), self.getPublicKey().inspect()))
  424. def getIssuer(self):
  425. """
  426. Retrieve the issuer of this certificate.
  427. @rtype: L{DistinguishedName}
  428. @return: A copy of the issuer of this certificate.
  429. """
  430. return self._copyName('issuer')
  431. def options(self, *authorities):
  432. raise NotImplementedError('Possible, but doubtful we need this yet')
  433. class CertificateRequest(CertBase):
  434. """
  435. An x509 certificate request.
  436. Certificate requests are given to certificate authorities to be signed and
  437. returned resulting in an actual certificate.
  438. """
  439. @classmethod
  440. def load(Class, requestData, requestFormat=crypto.FILETYPE_ASN1):
  441. req = crypto.load_certificate_request(requestFormat, requestData)
  442. dn = DistinguishedName()
  443. dn._copyFrom(req.get_subject())
  444. if not req.verify(req.get_pubkey()):
  445. raise VerifyError("Can't verify that request for %r is self-signed." % (dn,))
  446. return Class(req)
  447. def dump(self, format=crypto.FILETYPE_ASN1):
  448. return crypto.dump_certificate_request(format, self.original)
  449. class PrivateCertificate(Certificate):
  450. """
  451. An x509 certificate and private key.
  452. """
  453. def __repr__(self):
  454. return Certificate.__repr__(self) + ' with ' + repr(self.privateKey)
  455. def _setPrivateKey(self, privateKey):
  456. if not privateKey.matches(self.getPublicKey()):
  457. raise VerifyError(
  458. "Certificate public and private keys do not match.")
  459. self.privateKey = privateKey
  460. return self
  461. def newCertificate(self, newCertData, format=crypto.FILETYPE_ASN1):
  462. """
  463. Create a new L{PrivateCertificate} from the given certificate data and
  464. this instance's private key.
  465. """
  466. return self.load(newCertData, self.privateKey, format)
  467. @classmethod
  468. def load(Class, data, privateKey, format=crypto.FILETYPE_ASN1):
  469. return Class._load(data, format)._setPrivateKey(privateKey)
  470. def inspect(self):
  471. return '\n'.join([Certificate._inspect(self),
  472. self.privateKey.inspect()])
  473. def dumpPEM(self):
  474. """
  475. Dump both public and private parts of a private certificate to
  476. PEM-format data.
  477. """
  478. return self.dump(crypto.FILETYPE_PEM) + self.privateKey.dump(crypto.FILETYPE_PEM)
  479. @classmethod
  480. def loadPEM(Class, data):
  481. """
  482. Load both private and public parts of a private certificate from a
  483. chunk of PEM-format data.
  484. """
  485. return Class.load(data, KeyPair.load(data, crypto.FILETYPE_PEM),
  486. crypto.FILETYPE_PEM)
  487. @classmethod
  488. def fromCertificateAndKeyPair(Class, certificateInstance, privateKey):
  489. privcert = Class(certificateInstance.original)
  490. return privcert._setPrivateKey(privateKey)
  491. def options(self, *authorities):
  492. """
  493. Create a context factory using this L{PrivateCertificate}'s certificate
  494. and private key.
  495. @param authorities: A list of L{Certificate} object
  496. @return: A context factory.
  497. @rtype: L{CertificateOptions <twisted.internet.ssl.CertificateOptions>}
  498. """
  499. options = dict(privateKey=self.privateKey.original,
  500. certificate=self.original)
  501. if authorities:
  502. options.update(dict(trustRoot=OpenSSLCertificateAuthorities(
  503. [auth.original for auth in authorities]
  504. )))
  505. return OpenSSLCertificateOptions(**options)
  506. def certificateRequest(self, format=crypto.FILETYPE_ASN1,
  507. digestAlgorithm='sha256'):
  508. return self.privateKey.certificateRequest(
  509. self.getSubject(),
  510. format,
  511. digestAlgorithm)
  512. def signCertificateRequest(self,
  513. requestData,
  514. verifyDNCallback,
  515. serialNumber,
  516. requestFormat=crypto.FILETYPE_ASN1,
  517. certificateFormat=crypto.FILETYPE_ASN1):
  518. issuer = self.getSubject()
  519. return self.privateKey.signCertificateRequest(
  520. issuer,
  521. requestData,
  522. verifyDNCallback,
  523. serialNumber,
  524. requestFormat,
  525. certificateFormat)
  526. def signRequestObject(self, certificateRequest, serialNumber,
  527. secondsToExpiry=60 * 60 * 24 * 365, # One year
  528. digestAlgorithm='sha256'):
  529. return self.privateKey.signRequestObject(self.getSubject(),
  530. certificateRequest,
  531. serialNumber,
  532. secondsToExpiry,
  533. digestAlgorithm)
  534. @_oldStyle
  535. class PublicKey:
  536. """
  537. A L{PublicKey} is a representation of the public part of a key pair.
  538. You can't do a whole lot with it aside from comparing it to other
  539. L{PublicKey} objects.
  540. @note: If constructing a L{PublicKey} manually, be sure to pass only a
  541. L{OpenSSL.crypto.PKey} that does not contain a private key!
  542. @ivar original: The original private key.
  543. """
  544. def __init__(self, osslpkey):
  545. """
  546. @param osslpkey: The underlying pyOpenSSL key object.
  547. @type osslpkey: L{OpenSSL.crypto.PKey}
  548. """
  549. self.original = osslpkey
  550. def matches(self, otherKey):
  551. """
  552. Does this L{PublicKey} contain the same value as another L{PublicKey}?
  553. @param otherKey: The key to compare C{self} to.
  554. @type otherKey: L{PublicKey}
  555. @return: L{True} if these keys match, L{False} if not.
  556. @rtype: L{bool}
  557. """
  558. return self.keyHash() == otherKey.keyHash()
  559. def __repr__(self):
  560. return '<%s %s>' % (self.__class__.__name__, self.keyHash())
  561. def keyHash(self):
  562. """
  563. Compute a hash of the underlying PKey object.
  564. The purpose of this method is to allow you to determine if two
  565. certificates share the same public key; it is not really useful for
  566. anything else.
  567. In versions of Twisted prior to 15.0, C{keyHash} used a technique
  568. involving certificate requests for computing the hash that was not
  569. stable in the face of changes to the underlying OpenSSL library.
  570. @return: Return a 32-character hexadecimal string uniquely identifying
  571. this public key, I{for this version of Twisted}.
  572. @rtype: native L{str}
  573. """
  574. raw = crypto.dump_publickey(crypto.FILETYPE_ASN1, self.original)
  575. h = md5()
  576. h.update(raw)
  577. return h.hexdigest()
  578. def inspect(self):
  579. return 'Public Key with Hash: %s' % (self.keyHash(),)
  580. class KeyPair(PublicKey):
  581. @classmethod
  582. def load(Class, data, format=crypto.FILETYPE_ASN1):
  583. return Class(crypto.load_privatekey(format, data))
  584. def dump(self, format=crypto.FILETYPE_ASN1):
  585. return crypto.dump_privatekey(format, self.original)
  586. def __getstate__(self):
  587. return self.dump()
  588. def __setstate__(self, state):
  589. self.__init__(crypto.load_privatekey(crypto.FILETYPE_ASN1, state))
  590. def inspect(self):
  591. t = self.original.type()
  592. if t == crypto.TYPE_RSA:
  593. ts = 'RSA'
  594. elif t == crypto.TYPE_DSA:
  595. ts = 'DSA'
  596. else:
  597. ts = '(Unknown Type!)'
  598. L = (self.original.bits(), ts, self.keyHash())
  599. return '%s-bit %s Key Pair with Hash: %s' % L
  600. @classmethod
  601. def generate(Class, kind=crypto.TYPE_RSA, size=2048):
  602. pkey = crypto.PKey()
  603. pkey.generate_key(kind, size)
  604. return Class(pkey)
  605. def newCertificate(self, newCertData, format=crypto.FILETYPE_ASN1):
  606. return PrivateCertificate.load(newCertData, self, format)
  607. def requestObject(self, distinguishedName, digestAlgorithm='sha256'):
  608. req = crypto.X509Req()
  609. req.set_pubkey(self.original)
  610. distinguishedName._copyInto(req.get_subject())
  611. req.sign(self.original, digestAlgorithm)
  612. return CertificateRequest(req)
  613. def certificateRequest(self, distinguishedName,
  614. format=crypto.FILETYPE_ASN1,
  615. digestAlgorithm='sha256'):
  616. """
  617. Create a certificate request signed with this key.
  618. @return: a string, formatted according to the 'format' argument.
  619. """
  620. return self.requestObject(distinguishedName, digestAlgorithm).dump(format)
  621. def signCertificateRequest(self,
  622. issuerDistinguishedName,
  623. requestData,
  624. verifyDNCallback,
  625. serialNumber,
  626. requestFormat=crypto.FILETYPE_ASN1,
  627. certificateFormat=crypto.FILETYPE_ASN1,
  628. secondsToExpiry=60 * 60 * 24 * 365, # One year
  629. digestAlgorithm='sha256'):
  630. """
  631. Given a blob of certificate request data and a certificate authority's
  632. DistinguishedName, return a blob of signed certificate data.
  633. If verifyDNCallback returns a Deferred, I will return a Deferred which
  634. fires the data when that Deferred has completed.
  635. """
  636. hlreq = CertificateRequest.load(requestData, requestFormat)
  637. dn = hlreq.getSubject()
  638. vval = verifyDNCallback(dn)
  639. def verified(value):
  640. if not value:
  641. raise VerifyError("DN callback %r rejected request DN %r" % (verifyDNCallback, dn))
  642. return self.signRequestObject(issuerDistinguishedName, hlreq,
  643. serialNumber, secondsToExpiry, digestAlgorithm).dump(certificateFormat)
  644. if isinstance(vval, Deferred):
  645. return vval.addCallback(verified)
  646. else:
  647. return verified(vval)
  648. def signRequestObject(self,
  649. issuerDistinguishedName,
  650. requestObject,
  651. serialNumber,
  652. secondsToExpiry=60 * 60 * 24 * 365, # One year
  653. digestAlgorithm='sha256'):
  654. """
  655. Sign a CertificateRequest instance, returning a Certificate instance.
  656. """
  657. req = requestObject.original
  658. cert = crypto.X509()
  659. issuerDistinguishedName._copyInto(cert.get_issuer())
  660. cert.set_subject(req.get_subject())
  661. cert.set_pubkey(req.get_pubkey())
  662. cert.gmtime_adj_notBefore(0)
  663. cert.gmtime_adj_notAfter(secondsToExpiry)
  664. cert.set_serial_number(serialNumber)
  665. cert.sign(self.original, digestAlgorithm)
  666. return Certificate(cert)
  667. def selfSignedCert(self, serialNumber, **kw):
  668. dn = DN(**kw)
  669. return PrivateCertificate.fromCertificateAndKeyPair(
  670. self.signRequestObject(dn, self.requestObject(dn), serialNumber),
  671. self)
  672. KeyPair.__getstate__ = deprecated(Version("Twisted", 15, 0, 0),
  673. "a real persistence system")(KeyPair.__getstate__)
  674. KeyPair.__setstate__ = deprecated(Version("Twisted", 15, 0, 0),
  675. "a real persistence system")(KeyPair.__setstate__)
  676. class IOpenSSLTrustRoot(Interface):
  677. """
  678. Trust settings for an OpenSSL context.
  679. Note that this interface's methods are private, so things outside of
  680. Twisted shouldn't implement it.
  681. """
  682. def _addCACertsToContext(context):
  683. """
  684. Add certificate-authority certificates to an SSL context whose
  685. connections should trust those authorities.
  686. @param context: An SSL context for a connection which should be
  687. verified by some certificate authority.
  688. @type context: L{OpenSSL.SSL.Context}
  689. @return: L{None}
  690. """
  691. @implementer(IOpenSSLTrustRoot)
  692. class OpenSSLCertificateAuthorities(object):
  693. """
  694. Trust an explicitly specified set of certificates, represented by a list of
  695. L{OpenSSL.crypto.X509} objects.
  696. """
  697. def __init__(self, caCerts):
  698. """
  699. @param caCerts: The certificate authorities to trust when using this
  700. object as a C{trustRoot} for L{OpenSSLCertificateOptions}.
  701. @type caCerts: L{list} of L{OpenSSL.crypto.X509}
  702. """
  703. self._caCerts = caCerts
  704. def _addCACertsToContext(self, context):
  705. store = context.get_cert_store()
  706. for cert in self._caCerts:
  707. store.add_cert(cert)
  708. def trustRootFromCertificates(certificates):
  709. """
  710. Builds an object that trusts multiple root L{Certificate}s.
  711. When passed to L{optionsForClientTLS}, connections using those options will
  712. reject any server certificate not signed by at least one of the
  713. certificates in the `certificates` list.
  714. @since: 16.0
  715. @param certificates: All certificates which will be trusted.
  716. @type certificates: C{iterable} of L{CertBase}
  717. @rtype: L{IOpenSSLTrustRoot}
  718. @return: an object suitable for use as the trustRoot= keyword argument to
  719. L{optionsForClientTLS}
  720. """
  721. certs = []
  722. for cert in certificates:
  723. # PrivateCertificate or Certificate are both okay
  724. if isinstance(cert, CertBase):
  725. cert = cert.original
  726. else:
  727. raise TypeError(
  728. "certificates items must be twisted.internet.ssl.CertBase"
  729. " instances"
  730. )
  731. certs.append(cert)
  732. return OpenSSLCertificateAuthorities(certs)
  733. @implementer(IOpenSSLTrustRoot)
  734. class OpenSSLDefaultPaths(object):
  735. """
  736. Trust the set of default verify paths that OpenSSL was built with, as
  737. specified by U{SSL_CTX_set_default_verify_paths
  738. <https://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html>}.
  739. """
  740. def _addCACertsToContext(self, context):
  741. context.set_default_verify_paths()
  742. def platformTrust():
  743. """
  744. Attempt to discover a set of trusted certificate authority certificates
  745. (or, in other words: trust roots, or root certificates) whose trust is
  746. managed and updated by tools outside of Twisted.
  747. If you are writing any client-side TLS code with Twisted, you should use
  748. this as the C{trustRoot} argument to L{CertificateOptions
  749. <twisted.internet.ssl.CertificateOptions>}.
  750. The result of this function should be like the up-to-date list of
  751. certificates in a web browser. When developing code that uses
  752. C{platformTrust}, you can think of it that way. However, the choice of
  753. which certificate authorities to trust is never Twisted's responsibility.
  754. Unless you're writing a very unusual application or library, it's not your
  755. code's responsibility either. The user may use platform-specific tools for
  756. defining which server certificates should be trusted by programs using TLS.
  757. The purpose of using this API is to respect that decision as much as
  758. possible.
  759. This should be a set of trust settings most appropriate for I{client} TLS
  760. connections; i.e. those which need to verify a server's authenticity. You
  761. should probably use this by default for any client TLS connection that you
  762. create. For servers, however, client certificates are typically not
  763. verified; or, if they are, their verification will depend on a custom,
  764. application-specific certificate authority.
  765. @since: 14.0
  766. @note: Currently, L{platformTrust} depends entirely upon your OpenSSL build
  767. supporting a set of "L{default verify paths <OpenSSLDefaultPaths>}"
  768. which correspond to certificate authority trust roots. Unfortunately,
  769. whether this is true of your system is both outside of Twisted's
  770. control and difficult (if not impossible) for Twisted to detect
  771. automatically.
  772. Nevertheless, this ought to work as desired by default on:
  773. - Ubuntu Linux machines with the U{ca-certificates
  774. <https://launchpad.net/ubuntu/+source/ca-certificates>} package
  775. installed,
  776. - macOS when using the system-installed version of OpenSSL (i.e.
  777. I{not} one installed via MacPorts or Homebrew),
  778. - any build of OpenSSL which has had certificate authority
  779. certificates installed into its default verify paths (by default,
  780. C{/usr/local/ssl/certs} if you've built your own OpenSSL), or
  781. - any process where the C{SSL_CERT_FILE} environment variable is
  782. set to the path of a file containing your desired CA certificates
  783. bundle.
  784. Hopefully soon, this API will be updated to use more sophisticated
  785. trust-root discovery mechanisms. Until then, you can follow tickets in
  786. the Twisted tracker for progress on this implementation on U{Microsoft
  787. Windows <https://twistedmatrix.com/trac/ticket/6371>}, U{macOS
  788. <https://twistedmatrix.com/trac/ticket/6372>}, and U{a fallback for
  789. other platforms which do not have native trust management tools
  790. <https://twistedmatrix.com/trac/ticket/6934>}.
  791. @return: an appropriate trust settings object for your platform.
  792. @rtype: L{IOpenSSLTrustRoot}
  793. @raise NotImplementedError: if this platform is not yet supported by
  794. Twisted. At present, only OpenSSL is supported.
  795. """
  796. return OpenSSLDefaultPaths()
  797. def _tolerateErrors(wrapped):
  798. """
  799. Wrap up an C{info_callback} for pyOpenSSL so that if something goes wrong
  800. the error is immediately logged and the connection is dropped if possible.
  801. This wrapper exists because some versions of pyOpenSSL don't handle errors
  802. from callbacks at I{all}, and those which do write tracebacks directly to
  803. stderr rather than to a supplied logging system. This reports unexpected
  804. errors to the Twisted logging system.
  805. Also, this terminates the connection immediately if possible because if
  806. you've got bugs in your verification logic it's much safer to just give up.
  807. @param wrapped: A valid C{info_callback} for pyOpenSSL.
  808. @type wrapped: L{callable}
  809. @return: A valid C{info_callback} for pyOpenSSL that handles any errors in
  810. C{wrapped}.
  811. @rtype: L{callable}
  812. """
  813. def infoCallback(connection, where, ret):
  814. try:
  815. return wrapped(connection, where, ret)
  816. except:
  817. f = Failure()
  818. log.err(f, "Error during info_callback")
  819. connection.get_app_data().failVerification(f)
  820. return infoCallback
  821. @implementer(IOpenSSLClientConnectionCreator)
  822. class ClientTLSOptions(object):
  823. """
  824. Client creator for TLS.
  825. Private implementation type (not exposed to applications) for public
  826. L{optionsForClientTLS} API.
  827. @ivar _ctx: The context to use for new connections.
  828. @type _ctx: L{OpenSSL.SSL.Context}
  829. @ivar _hostname: The hostname to verify, as specified by the application,
  830. as some human-readable text.
  831. @type _hostname: L{unicode}
  832. @ivar _hostnameBytes: The hostname to verify, decoded into IDNA-encoded
  833. bytes. This is passed to APIs which think that hostnames are bytes,
  834. such as OpenSSL's SNI implementation.
  835. @type _hostnameBytes: L{bytes}
  836. @ivar _hostnameASCII: The hostname, as transcoded into IDNA ASCII-range
  837. unicode code points. This is pre-transcoded because the
  838. C{service_identity} package is rather strict about requiring the
  839. C{idna} package from PyPI for internationalized domain names, rather
  840. than working with Python's built-in (but sometimes broken) IDNA
  841. encoding. ASCII values, however, will always work.
  842. @type _hostnameASCII: L{unicode}
  843. @ivar _hostnameIsDnsName: Whether or not the C{_hostname} is a DNSName.
  844. Will be L{False} if C{_hostname} is an IP address or L{True} if
  845. C{_hostname} is a DNSName
  846. @type _hostnameIsDnsName: L{bool}
  847. """
  848. def __init__(self, hostname, ctx):
  849. """
  850. Initialize L{ClientTLSOptions}.
  851. @param hostname: The hostname to verify as input by a human.
  852. @type hostname: L{unicode}
  853. @param ctx: an L{OpenSSL.SSL.Context} to use for new connections.
  854. @type ctx: L{OpenSSL.SSL.Context}.
  855. """
  856. self._ctx = ctx
  857. self._hostname = hostname
  858. if isIPAddress(hostname) or isIPv6Address(hostname):
  859. self._hostnameBytes = hostname.encode('ascii')
  860. self._hostnameIsDnsName = False
  861. else:
  862. self._hostnameBytes = _idnaBytes(hostname)
  863. self._hostnameIsDnsName = True
  864. self._hostnameASCII = self._hostnameBytes.decode("ascii")
  865. ctx.set_info_callback(
  866. _tolerateErrors(self._identityVerifyingInfoCallback)
  867. )
  868. def clientConnectionForTLS(self, tlsProtocol):
  869. """
  870. Create a TLS connection for a client.
  871. @note: This will call C{set_app_data} on its connection. If you're
  872. delegating to this implementation of this method, don't ever call
  873. C{set_app_data} or C{set_info_callback} on the returned connection,
  874. or you'll break the implementation of various features of this
  875. class.
  876. @param tlsProtocol: the TLS protocol initiating the connection.
  877. @type tlsProtocol: L{twisted.protocols.tls.TLSMemoryBIOProtocol}
  878. @return: the configured client connection.
  879. @rtype: L{OpenSSL.SSL.Connection}
  880. """
  881. context = self._ctx
  882. connection = SSL.Connection(context, None)
  883. connection.set_app_data(tlsProtocol)
  884. return connection
  885. def _identityVerifyingInfoCallback(self, connection, where, ret):
  886. """
  887. U{info_callback
  888. <http://pythonhosted.org/pyOpenSSL/api/ssl.html#OpenSSL.SSL.Context.set_info_callback>
  889. } for pyOpenSSL that verifies the hostname in the presented certificate
  890. matches the one passed to this L{ClientTLSOptions}.
  891. @param connection: the connection which is handshaking.
  892. @type connection: L{OpenSSL.SSL.Connection}
  893. @param where: flags indicating progress through a TLS handshake.
  894. @type where: L{int}
  895. @param ret: ignored
  896. @type ret: ignored
  897. """
  898. # Literal IPv4 and IPv6 addresses are not permitted
  899. # as host names according to the RFCs
  900. if where & SSL.SSL_CB_HANDSHAKE_START and self._hostnameIsDnsName:
  901. connection.set_tlsext_host_name(self._hostnameBytes)
  902. elif where & SSL.SSL_CB_HANDSHAKE_DONE:
  903. try:
  904. if self._hostnameIsDnsName:
  905. verifyHostname(connection, self._hostnameASCII)
  906. else:
  907. verifyIPAddress(connection, self._hostnameASCII)
  908. except VerificationError:
  909. f = Failure()
  910. transport = connection.get_app_data()
  911. transport.failVerification(f)
  912. def optionsForClientTLS(hostname, trustRoot=None, clientCertificate=None,
  913. acceptableProtocols=None, **kw):
  914. """
  915. Create a L{client connection creator <IOpenSSLClientConnectionCreator>} for
  916. use with APIs such as L{SSL4ClientEndpoint
  917. <twisted.internet.endpoints.SSL4ClientEndpoint>}, L{connectSSL
  918. <twisted.internet.interfaces.IReactorSSL.connectSSL>}, and L{startTLS
  919. <twisted.internet.interfaces.ITLSTransport.startTLS>}.
  920. @since: 14.0
  921. @param hostname: The expected name of the remote host. This serves two
  922. purposes: first, and most importantly, it verifies that the certificate
  923. received from the server correctly identifies the specified hostname.
  924. The second purpose is to use the U{Server Name Indication extension
  925. <https://en.wikipedia.org/wiki/Server_Name_Indication>} to indicate to
  926. the server which certificate should be used.
  927. @type hostname: L{unicode}
  928. @param trustRoot: Specification of trust requirements of peers. This may be
  929. a L{Certificate} or the result of L{platformTrust}. By default it is
  930. L{platformTrust} and you probably shouldn't adjust it unless you really
  931. know what you're doing. Be aware that clients using this interface
  932. I{must} verify the server; you cannot explicitly pass L{None} since
  933. that just means to use L{platformTrust}.
  934. @type trustRoot: L{IOpenSSLTrustRoot}
  935. @param clientCertificate: The certificate and private key that the client
  936. will use to authenticate to the server. If unspecified, the client will
  937. not authenticate.
  938. @type clientCertificate: L{PrivateCertificate}
  939. @param acceptableProtocols: The protocols this peer is willing to speak
  940. after the TLS negotiation has completed, advertised over both ALPN and
  941. NPN. If this argument is specified, and no overlap can be found with
  942. the other peer, the connection will fail to be established. If the
  943. remote peer does not offer NPN or ALPN, the connection will be
  944. established, but no protocol wil be negotiated. Protocols earlier in
  945. the list are preferred over those later in the list.
  946. @type acceptableProtocols: L{list} of L{bytes}
  947. @param extraCertificateOptions: keyword-only argument; this is a dictionary
  948. of additional keyword arguments to be presented to
  949. L{CertificateOptions}. Please avoid using this unless you absolutely
  950. need to; any time you need to pass an option here that is a bug in this
  951. interface.
  952. @type extraCertificateOptions: L{dict}
  953. @param kw: (Backwards compatibility hack to allow keyword-only arguments on
  954. Python 2. Please ignore; arbitrary keyword arguments will be errors.)
  955. @type kw: L{dict}
  956. @return: A client connection creator.
  957. @rtype: L{IOpenSSLClientConnectionCreator}
  958. """
  959. extraCertificateOptions = kw.pop('extraCertificateOptions', None) or {}
  960. if trustRoot is None:
  961. trustRoot = platformTrust()
  962. if kw:
  963. raise TypeError(
  964. "optionsForClientTLS() got an unexpected keyword argument"
  965. " '{arg}'".format(
  966. arg=kw.popitem()[0]
  967. )
  968. )
  969. if not isinstance(hostname, unicode):
  970. raise TypeError(
  971. "optionsForClientTLS requires text for host names, not "
  972. + hostname.__class__.__name__
  973. )
  974. if clientCertificate:
  975. extraCertificateOptions.update(
  976. privateKey=clientCertificate.privateKey.original,
  977. certificate=clientCertificate.original
  978. )
  979. certificateOptions = OpenSSLCertificateOptions(
  980. trustRoot=trustRoot,
  981. acceptableProtocols=acceptableProtocols,
  982. **extraCertificateOptions
  983. )
  984. return ClientTLSOptions(hostname, certificateOptions.getContext())
  985. @implementer(IOpenSSLContextFactory)
  986. class OpenSSLCertificateOptions(object):
  987. """
  988. A L{CertificateOptions <twisted.internet.ssl.CertificateOptions>} specifies
  989. the security properties for a client or server TLS connection used with
  990. OpenSSL.
  991. @ivar _options: Any option flags to set on the L{OpenSSL.SSL.Context}
  992. object that will be created.
  993. @type _options: L{int}
  994. @ivar _cipherString: An OpenSSL-specific cipher string.
  995. @type _cipherString: L{unicode}
  996. @ivar _defaultMinimumTLSVersion: The default TLS version that will be
  997. negotiated. This should be a "safe default", with wide client and
  998. server support, vs an optimally secure one that excludes a large number
  999. of users. As of late 2016, TLSv1.0 is that safe default.
  1000. @type _defaultMinimumTLSVersion: L{TLSVersion} constant
  1001. """
  1002. # Factory for creating contexts. Configurable for testability.
  1003. _contextFactory = SSL.Context
  1004. _context = None
  1005. _OP_NO_TLSv1_3 = _tlsDisableFlags[TLSVersion.TLSv1_3]
  1006. _defaultMinimumTLSVersion = TLSVersion.TLSv1_0
  1007. @_mutuallyExclusiveArguments([
  1008. ['trustRoot', 'requireCertificate'],
  1009. ['trustRoot', 'verify'],
  1010. ['trustRoot', 'caCerts'],
  1011. ['method', 'insecurelyLowerMinimumTo'],
  1012. ['method', 'raiseMinimumTo'],
  1013. ['raiseMinimumTo', 'insecurelyLowerMinimumTo'],
  1014. ['method', 'lowerMaximumSecurityTo'],
  1015. ])
  1016. def __init__(self,
  1017. privateKey=None,
  1018. certificate=None,
  1019. method=None,
  1020. verify=False,
  1021. caCerts=None,
  1022. verifyDepth=9,
  1023. requireCertificate=True,
  1024. verifyOnce=True,
  1025. enableSingleUseKeys=True,
  1026. enableSessions=True,
  1027. fixBrokenPeers=False,
  1028. enableSessionTickets=False,
  1029. extraCertChain=None,
  1030. acceptableCiphers=None,
  1031. dhParameters=None,
  1032. trustRoot=None,
  1033. acceptableProtocols=None,
  1034. raiseMinimumTo=None,
  1035. insecurelyLowerMinimumTo=None,
  1036. lowerMaximumSecurityTo=None,
  1037. ):
  1038. """
  1039. Create an OpenSSL context SSL connection context factory.
  1040. @param privateKey: A PKey object holding the private key.
  1041. @param certificate: An X509 object holding the certificate.
  1042. @param method: Deprecated, use a combination of
  1043. C{insecurelyLowerMinimumTo}, C{raiseMinimumTo}, or
  1044. C{lowerMaximumSecurityTo} instead. The SSL protocol to use, one of
  1045. C{SSLv23_METHOD}, C{SSLv2_METHOD}, C{SSLv3_METHOD}, C{TLSv1_METHOD}
  1046. (or any other method constants provided by pyOpenSSL). By default,
  1047. a setting will be used which allows TLSv1.0, TLSv1.1, and TLSv1.2.
  1048. Can not be used with C{insecurelyLowerMinimumTo},
  1049. C{raiseMinimumTo}, or C{lowerMaximumSecurityTo}
  1050. @param verify: Please use a C{trustRoot} keyword argument instead,
  1051. since it provides the same functionality in a less error-prone way.
  1052. By default this is L{False}.
  1053. If L{True}, verify certificates received from the peer and fail the
  1054. handshake if verification fails. Otherwise, allow anonymous
  1055. sessions and sessions with certificates which fail validation.
  1056. @param caCerts: Please use a C{trustRoot} keyword argument instead,
  1057. since it provides the same functionality in a less error-prone way.
  1058. List of certificate authority certificate objects to use to verify
  1059. the peer's certificate. Only used if verify is L{True} and will be
  1060. ignored otherwise. Since verify is L{False} by default, this is
  1061. L{None} by default.
  1062. @type caCerts: L{list} of L{OpenSSL.crypto.X509}
  1063. @param verifyDepth: Depth in certificate chain down to which to verify.
  1064. If unspecified, use the underlying default (9).
  1065. @param requireCertificate: Please use a C{trustRoot} keyword argument
  1066. instead, since it provides the same functionality in a less
  1067. error-prone way.
  1068. If L{True}, do not allow anonymous sessions; defaults to L{True}.
  1069. @param verifyOnce: If True, do not re-verify the certificate on session
  1070. resumption.
  1071. @param enableSingleUseKeys: If L{True}, generate a new key whenever
  1072. ephemeral DH and ECDH parameters are used to prevent small subgroup
  1073. attacks and to ensure perfect forward secrecy.
  1074. @param enableSessions: If True, set a session ID on each context. This
  1075. allows a shortened handshake to be used when a known client
  1076. reconnects.
  1077. @param fixBrokenPeers: If True, enable various non-spec protocol fixes
  1078. for broken SSL implementations. This should be entirely safe,
  1079. according to the OpenSSL documentation, but YMMV. This option is
  1080. now off by default, because it causes problems with connections
  1081. between peers using OpenSSL 0.9.8a.
  1082. @param enableSessionTickets: If L{True}, enable session ticket
  1083. extension for session resumption per RFC 5077. Note there is no
  1084. support for controlling session tickets. This option is off by
  1085. default, as some server implementations don't correctly process
  1086. incoming empty session ticket extensions in the hello.
  1087. @param extraCertChain: List of certificates that I{complete} your
  1088. verification chain if the certificate authority that signed your
  1089. C{certificate} isn't widely supported. Do I{not} add
  1090. C{certificate} to it.
  1091. @type extraCertChain: C{list} of L{OpenSSL.crypto.X509}
  1092. @param acceptableCiphers: Ciphers that are acceptable for connections.
  1093. Uses a secure default if left L{None}.
  1094. @type acceptableCiphers: L{IAcceptableCiphers}
  1095. @param dhParameters: Key generation parameters that are required for
  1096. Diffie-Hellman key exchange. If this argument is left L{None},
  1097. C{EDH} ciphers are I{disabled} regardless of C{acceptableCiphers}.
  1098. @type dhParameters: L{DiffieHellmanParameters
  1099. <twisted.internet.ssl.DiffieHellmanParameters>}
  1100. @param trustRoot: Specification of trust requirements of peers. If
  1101. this argument is specified, the peer is verified. It requires a
  1102. certificate, and that certificate must be signed by one of the
  1103. certificate authorities specified by this object.
  1104. Note that since this option specifies the same information as
  1105. C{caCerts}, C{verify}, and C{requireCertificate}, specifying any of
  1106. those options in combination with this one will raise a
  1107. L{TypeError}.
  1108. @type trustRoot: L{IOpenSSLTrustRoot}
  1109. @param acceptableProtocols: The protocols this peer is willing to speak
  1110. after the TLS negotiation has completed, advertised over both ALPN
  1111. and NPN. If this argument is specified, and no overlap can be
  1112. found with the other peer, the connection will fail to be
  1113. established. If the remote peer does not offer NPN or ALPN, the
  1114. connection will be established, but no protocol wil be negotiated.
  1115. Protocols earlier in the list are preferred over those later in the
  1116. list.
  1117. @type acceptableProtocols: L{list} of L{bytes}
  1118. @param raiseMinimumTo: The minimum TLS version that you want to use, or
  1119. Twisted's default if it is higher. Use this if you want to make
  1120. your client/server more secure than Twisted's default, but will
  1121. accept Twisted's default instead if it moves higher than this
  1122. value. You probably want to use this over
  1123. C{insecurelyLowerMinimumTo}.
  1124. @type raiseMinimumTo: L{TLSVersion} constant
  1125. @param insecurelyLowerMinimumTo: The minimum TLS version to use,
  1126. possibly lower than Twisted's default. If not specified, it is a
  1127. generally considered safe default (TLSv1.0). If you want to raise
  1128. your minimum TLS version to above that of this default, use
  1129. C{raiseMinimumTo}. DO NOT use this argument unless you are
  1130. absolutely sure this is what you want.
  1131. @type insecurelyLowerMinimumTo: L{TLSVersion} constant
  1132. @param lowerMaximumSecurityTo: The maximum TLS version to use. If not
  1133. specified, it is the most recent your OpenSSL supports. You only
  1134. want to set this if the peer that you are communicating with has
  1135. problems with more recent TLS versions, it lowers your security
  1136. when communicating with newer peers. DO NOT use this argument
  1137. unless you are absolutely sure this is what you want.
  1138. @type lowerMaximumSecurityTo: L{TLSVersion} constant
  1139. @raise ValueError: when C{privateKey} or C{certificate} are set without
  1140. setting the respective other.
  1141. @raise ValueError: when C{verify} is L{True} but C{caCerts} doesn't
  1142. specify any CA certificates.
  1143. @raise ValueError: when C{extraCertChain} is passed without specifying
  1144. C{privateKey} or C{certificate}.
  1145. @raise ValueError: when C{acceptableCiphers} doesn't yield any usable
  1146. ciphers for the current platform.
  1147. @raise TypeError: if C{trustRoot} is passed in combination with
  1148. C{caCert}, C{verify}, or C{requireCertificate}. Please prefer
  1149. C{trustRoot} in new code, as its semantics are less tricky.
  1150. @raise TypeError: if C{method} is passed in combination with
  1151. C{tlsProtocols}. Please prefer the more explicit C{tlsProtocols}
  1152. in new code.
  1153. @raises NotImplementedError: If acceptableProtocols were provided but
  1154. no negotiation mechanism is available.
  1155. """
  1156. if (privateKey is None) != (certificate is None):
  1157. raise ValueError(
  1158. "Specify neither or both of privateKey and certificate")
  1159. self.privateKey = privateKey
  1160. self.certificate = certificate
  1161. # Set basic security options: disallow insecure SSLv2, disallow TLS
  1162. # compression to avoid CRIME attack, make the server choose the
  1163. # ciphers.
  1164. self._options = (
  1165. SSL.OP_NO_SSLv2 | SSL.OP_NO_COMPRESSION |
  1166. SSL.OP_CIPHER_SERVER_PREFERENCE
  1167. )
  1168. # Set the mode to Release Buffers, which demallocs send/recv buffers on
  1169. # idle TLS connections to save memory
  1170. self._mode = SSL.MODE_RELEASE_BUFFERS
  1171. if method is None:
  1172. self.method = SSL.SSLv23_METHOD
  1173. if raiseMinimumTo:
  1174. if (lowerMaximumSecurityTo and
  1175. raiseMinimumTo > lowerMaximumSecurityTo):
  1176. raise ValueError(
  1177. ("raiseMinimumTo needs to be lower than "
  1178. "lowerMaximumSecurityTo"))
  1179. if raiseMinimumTo > self._defaultMinimumTLSVersion:
  1180. insecurelyLowerMinimumTo = raiseMinimumTo
  1181. if insecurelyLowerMinimumTo is None:
  1182. insecurelyLowerMinimumTo = self._defaultMinimumTLSVersion
  1183. # If you set the max lower than the default, but don't set the
  1184. # minimum, pull it down to that
  1185. if (lowerMaximumSecurityTo and
  1186. insecurelyLowerMinimumTo > lowerMaximumSecurityTo):
  1187. insecurelyLowerMinimumTo = lowerMaximumSecurityTo
  1188. if (lowerMaximumSecurityTo and
  1189. insecurelyLowerMinimumTo > lowerMaximumSecurityTo):
  1190. raise ValueError(
  1191. ("insecurelyLowerMinimumTo needs to be lower than "
  1192. "lowerMaximumSecurityTo"))
  1193. excludedVersions = _getExcludedTLSProtocols(
  1194. insecurelyLowerMinimumTo, lowerMaximumSecurityTo)
  1195. for version in excludedVersions:
  1196. self._options |= _tlsDisableFlags[version]
  1197. else:
  1198. warnings.warn(
  1199. ("Passing method to twisted.internet.ssl.CertificateOptions "
  1200. "was deprecated in Twisted 17.1.0. Please use a combination "
  1201. "of insecurelyLowerMinimumTo, raiseMinimumTo, and "
  1202. "lowerMaximumSecurityTo instead, as Twisted will correctly "
  1203. "configure the method."),
  1204. DeprecationWarning, stacklevel=3)
  1205. # Otherwise respect the application decision.
  1206. self.method = method
  1207. if verify and not caCerts:
  1208. raise ValueError("Specify client CA certificate information if and"
  1209. " only if enabling certificate verification")
  1210. self.verify = verify
  1211. if extraCertChain is not None and None in (privateKey, certificate):
  1212. raise ValueError("A private key and a certificate are required "
  1213. "when adding a supplemental certificate chain.")
  1214. if extraCertChain is not None:
  1215. self.extraCertChain = extraCertChain
  1216. else:
  1217. self.extraCertChain = []
  1218. self.caCerts = caCerts
  1219. self.verifyDepth = verifyDepth
  1220. self.requireCertificate = requireCertificate
  1221. self.verifyOnce = verifyOnce
  1222. self.enableSingleUseKeys = enableSingleUseKeys
  1223. if enableSingleUseKeys:
  1224. self._options |= SSL.OP_SINGLE_DH_USE | SSL.OP_SINGLE_ECDH_USE
  1225. self.enableSessions = enableSessions
  1226. self.fixBrokenPeers = fixBrokenPeers
  1227. if fixBrokenPeers:
  1228. self._options |= SSL.OP_ALL
  1229. self.enableSessionTickets = enableSessionTickets
  1230. if not enableSessionTickets:
  1231. self._options |= SSL.OP_NO_TICKET
  1232. self.dhParameters = dhParameters
  1233. self._ecChooser = _ChooseDiffieHellmanEllipticCurve(
  1234. SSL.OPENSSL_VERSION_NUMBER,
  1235. openSSLlib=pyOpenSSLlib,
  1236. openSSLcrypto=crypto,
  1237. )
  1238. if acceptableCiphers is None:
  1239. acceptableCiphers = defaultCiphers
  1240. # This needs to run when method and _options are finalized.
  1241. self._cipherString = u':'.join(
  1242. c.fullName
  1243. for c in acceptableCiphers.selectCiphers(
  1244. _expandCipherString(u'ALL', self.method, self._options)
  1245. )
  1246. )
  1247. if self._cipherString == u'':
  1248. raise ValueError(
  1249. 'Supplied IAcceptableCiphers yielded no usable ciphers '
  1250. 'on this platform.'
  1251. )
  1252. if trustRoot is None:
  1253. if self.verify:
  1254. trustRoot = OpenSSLCertificateAuthorities(caCerts)
  1255. else:
  1256. self.verify = True
  1257. self.requireCertificate = True
  1258. trustRoot = IOpenSSLTrustRoot(trustRoot)
  1259. self.trustRoot = trustRoot
  1260. if acceptableProtocols is not None and not protocolNegotiationMechanisms():
  1261. raise NotImplementedError(
  1262. "No support for protocol negotiation on this platform."
  1263. )
  1264. self._acceptableProtocols = acceptableProtocols
  1265. def __getstate__(self):
  1266. d = self.__dict__.copy()
  1267. try:
  1268. del d['_context']
  1269. except KeyError:
  1270. pass
  1271. return d
  1272. def __setstate__(self, state):
  1273. self.__dict__ = state
  1274. def getContext(self):
  1275. """
  1276. Return an L{OpenSSL.SSL.Context} object.
  1277. """
  1278. if self._context is None:
  1279. self._context = self._makeContext()
  1280. return self._context
  1281. def _makeContext(self):
  1282. ctx = self._contextFactory(self.method)
  1283. ctx.set_options(self._options)
  1284. ctx.set_mode(self._mode)
  1285. if self.certificate is not None and self.privateKey is not None:
  1286. ctx.use_certificate(self.certificate)
  1287. ctx.use_privatekey(self.privateKey)
  1288. for extraCert in self.extraCertChain:
  1289. ctx.add_extra_chain_cert(extraCert)
  1290. # Sanity check
  1291. ctx.check_privatekey()
  1292. verifyFlags = SSL.VERIFY_NONE
  1293. if self.verify:
  1294. verifyFlags = SSL.VERIFY_PEER
  1295. if self.requireCertificate:
  1296. verifyFlags |= SSL.VERIFY_FAIL_IF_NO_PEER_CERT
  1297. if self.verifyOnce:
  1298. verifyFlags |= SSL.VERIFY_CLIENT_ONCE
  1299. self.trustRoot._addCACertsToContext(ctx)
  1300. # It'd be nice if pyOpenSSL let us pass None here for this behavior (as
  1301. # the underlying OpenSSL API call allows NULL to be passed). It
  1302. # doesn't, so we'll supply a function which does the same thing.
  1303. def _verifyCallback(conn, cert, errno, depth, preverify_ok):
  1304. return preverify_ok
  1305. ctx.set_verify(verifyFlags, _verifyCallback)
  1306. if self.verifyDepth is not None:
  1307. ctx.set_verify_depth(self.verifyDepth)
  1308. if self.enableSessions:
  1309. # 32 bytes is the maximum length supported
  1310. # Unfortunately pyOpenSSL doesn't provide SSL_MAX_SESSION_ID_LENGTH
  1311. sessionName = secureRandom(32)
  1312. ctx.set_session_id(sessionName)
  1313. if self.dhParameters:
  1314. ctx.load_tmp_dh(self.dhParameters._dhFile.path)
  1315. ctx.set_cipher_list(self._cipherString.encode('ascii'))
  1316. self._ecChooser.configureECDHCurve(ctx)
  1317. if self._acceptableProtocols:
  1318. # Try to set NPN and ALPN. _acceptableProtocols cannot be set by
  1319. # the constructor unless at least one mechanism is supported.
  1320. _setAcceptableProtocols(ctx, self._acceptableProtocols)
  1321. return ctx
  1322. OpenSSLCertificateOptions.__getstate__ = deprecated(
  1323. Version("Twisted", 15, 0, 0),
  1324. "a real persistence system")(OpenSSLCertificateOptions.__getstate__)
  1325. OpenSSLCertificateOptions.__setstate__ = deprecated(
  1326. Version("Twisted", 15, 0, 0),
  1327. "a real persistence system")(OpenSSLCertificateOptions.__setstate__)
  1328. @implementer(ICipher)
  1329. class OpenSSLCipher(FancyEqMixin, object):
  1330. """
  1331. A representation of an OpenSSL cipher.
  1332. """
  1333. compareAttributes = ('fullName',)
  1334. def __init__(self, fullName):
  1335. """
  1336. @param fullName: The full name of the cipher. For example
  1337. C{u"ECDHE-RSA-AES256-GCM-SHA384"}.
  1338. @type fullName: L{unicode}
  1339. """
  1340. self.fullName = fullName
  1341. def __repr__(self):
  1342. """
  1343. A runnable representation of the cipher.
  1344. """
  1345. return 'OpenSSLCipher({0!r})'.format(self.fullName)
  1346. def _expandCipherString(cipherString, method, options):
  1347. """
  1348. Expand C{cipherString} according to C{method} and C{options} to a list
  1349. of explicit ciphers that are supported by the current platform.
  1350. @param cipherString: An OpenSSL cipher string to expand.
  1351. @type cipherString: L{unicode}
  1352. @param method: An OpenSSL method like C{SSL.TLSv1_METHOD} used for
  1353. determining the effective ciphers.
  1354. @param options: OpenSSL options like C{SSL.OP_NO_SSLv3} ORed together.
  1355. @type options: L{int}
  1356. @return: The effective list of explicit ciphers that results from the
  1357. arguments on the current platform.
  1358. @rtype: L{list} of L{ICipher}
  1359. """
  1360. ctx = SSL.Context(method)
  1361. ctx.set_options(options)
  1362. try:
  1363. ctx.set_cipher_list(cipherString.encode('ascii'))
  1364. except SSL.Error as e:
  1365. # OpenSSL 1.1.1 turns an invalid cipher list into TLS 1.3
  1366. # ciphers, so pyOpenSSL >= 19.0.0 raises an artificial Error
  1367. # that lacks a corresponding OpenSSL error if the cipher list
  1368. # consists only of these after a call to set_cipher_list.
  1369. if not e.args[0]:
  1370. return []
  1371. if e.args[0][0][2] == 'no cipher match':
  1372. return []
  1373. else:
  1374. raise
  1375. conn = SSL.Connection(ctx, None)
  1376. ciphers = conn.get_cipher_list()
  1377. if isinstance(ciphers[0], unicode):
  1378. return [OpenSSLCipher(cipher) for cipher in ciphers]
  1379. else:
  1380. return [OpenSSLCipher(cipher.decode('ascii')) for cipher in ciphers]
  1381. @implementer(IAcceptableCiphers)
  1382. class OpenSSLAcceptableCiphers(object):
  1383. """
  1384. A representation of ciphers that are acceptable for TLS connections.
  1385. """
  1386. def __init__(self, ciphers):
  1387. self._ciphers = ciphers
  1388. def selectCiphers(self, availableCiphers):
  1389. return [cipher
  1390. for cipher in self._ciphers
  1391. if cipher in availableCiphers]
  1392. @classmethod
  1393. def fromOpenSSLCipherString(cls, cipherString):
  1394. """
  1395. Create a new instance using an OpenSSL cipher string.
  1396. @param cipherString: An OpenSSL cipher string that describes what
  1397. cipher suites are acceptable.
  1398. See the documentation of U{OpenSSL
  1399. <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS>} or
  1400. U{Apache
  1401. <http://httpd.apache.org/docs/2.4/mod/mod_ssl.html#sslciphersuite>}
  1402. for details.
  1403. @type cipherString: L{unicode}
  1404. @return: Instance representing C{cipherString}.
  1405. @rtype: L{twisted.internet.ssl.AcceptableCiphers}
  1406. """
  1407. return cls(_expandCipherString(
  1408. nativeString(cipherString),
  1409. SSL.SSLv23_METHOD, SSL.OP_NO_SSLv2 | SSL.OP_NO_SSLv3)
  1410. )
  1411. # A secure default.
  1412. # Sources for more information on TLS ciphers:
  1413. #
  1414. # - https://wiki.mozilla.org/Security/Server_Side_TLS
  1415. # - https://www.ssllabs.com/projects/best-practices/index.html
  1416. # - https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
  1417. #
  1418. # The general intent is:
  1419. # - Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE),
  1420. # - prefer ECDHE over DHE for better performance,
  1421. # - prefer any AES-GCM and ChaCha20 over any AES-CBC for better performance and
  1422. # security,
  1423. # - prefer AES-GCM to ChaCha20 because AES hardware support is common,
  1424. # - disable NULL authentication, MD5 MACs and DSS for security reasons.
  1425. #
  1426. defaultCiphers = OpenSSLAcceptableCiphers.fromOpenSSLCipherString(
  1427. "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:"
  1428. "TLS13-AES-128-GCM-SHA256:"
  1429. "ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:"
  1430. "ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:"
  1431. "!aNULL:!MD5:!DSS"
  1432. )
  1433. _defaultCurveName = u"prime256v1"
  1434. class _ChooseDiffieHellmanEllipticCurve(object):
  1435. """
  1436. Chooses the best elliptic curve for Elliptic Curve Diffie-Hellman
  1437. key exchange, and provides a C{configureECDHCurve} method to set
  1438. the curve, when appropriate, on a new L{OpenSSL.SSL.Context}.
  1439. The C{configureECDHCurve} method will be set to one of the
  1440. following based on the provided OpenSSL version and configuration:
  1441. - L{_configureOpenSSL110}
  1442. - L{_configureOpenSSL102}
  1443. - L{_configureOpenSSL101}
  1444. - L{_configureOpenSSL101NoCurves}.
  1445. @param openSSLVersion: The OpenSSL version number.
  1446. @type openSSLVersion: L{int}
  1447. @see: L{OpenSSL.SSL.OPENSSL_VERSION_NUMBER}
  1448. @param openSSLlib: The OpenSSL C{cffi} library module.
  1449. @param openSSLlib: The OpenSSL L{crypto} module.
  1450. @see: L{crypto}
  1451. """
  1452. def __init__(self, openSSLVersion, openSSLlib, openSSLcrypto):
  1453. self._openSSLlib = openSSLlib
  1454. self._openSSLcrypto = openSSLcrypto
  1455. if openSSLVersion >= 0x10100000:
  1456. self.configureECDHCurve = self._configureOpenSSL110
  1457. elif openSSLVersion >= 0x10002000:
  1458. self.configureECDHCurve = self._configureOpenSSL102
  1459. else:
  1460. try:
  1461. self._ecCurve = openSSLcrypto.get_elliptic_curve(
  1462. _defaultCurveName)
  1463. except ValueError:
  1464. # The get_elliptic_curve method raises a ValueError
  1465. # when the curve does not exist.
  1466. self.configureECDHCurve = self._configureOpenSSL101NoCurves
  1467. else:
  1468. self.configureECDHCurve = self._configureOpenSSL101
  1469. def _configureOpenSSL110(self, ctx):
  1470. """
  1471. OpenSSL 1.1.0 Contexts are preconfigured with an optimal set
  1472. of ECDH curves. This method does nothing.
  1473. @param ctx: L{OpenSSL.SSL.Context}
  1474. """
  1475. def _configureOpenSSL102(self, ctx):
  1476. """
  1477. Have the context automatically choose elliptic curves for
  1478. ECDH. Run on OpenSSL 1.0.2 and OpenSSL 1.1.0+, but only has
  1479. an effect on OpenSSL 1.0.2.
  1480. @param ctx: The context which .
  1481. @type ctx: L{OpenSSL.SSL.Context}
  1482. """
  1483. ctxPtr = ctx._context
  1484. try:
  1485. self._openSSLlib.SSL_CTX_set_ecdh_auto(ctxPtr, True)
  1486. except:
  1487. pass
  1488. def _configureOpenSSL101(self, ctx):
  1489. """
  1490. Set the default elliptic curve for ECDH on the context. Only
  1491. run on OpenSSL 1.0.1.
  1492. @param ctx: The context on which to set the ECDH curve.
  1493. @type ctx: L{OpenSSL.SSL.Context}
  1494. """
  1495. try:
  1496. ctx.set_tmp_ecdh(self._ecCurve)
  1497. except:
  1498. pass
  1499. def _configureOpenSSL101NoCurves(self, ctx):
  1500. """
  1501. No elliptic curves are available on OpenSSL 1.0.1. We can't
  1502. set anything, so do nothing.
  1503. @param ctx: The context on which to set the ECDH curve.
  1504. @type ctx: L{OpenSSL.SSL.Context}
  1505. """
  1506. class OpenSSLDiffieHellmanParameters(object):
  1507. """
  1508. A representation of key generation parameters that are required for
  1509. Diffie-Hellman key exchange.
  1510. """
  1511. def __init__(self, parameters):
  1512. self._dhFile = parameters
  1513. @classmethod
  1514. def fromFile(cls, filePath):
  1515. """
  1516. Load parameters from a file.
  1517. Such a file can be generated using the C{openssl} command line tool as
  1518. following:
  1519. C{openssl dhparam -out dh_param_2048.pem -2 2048}
  1520. Please refer to U{OpenSSL's C{dhparam} documentation
  1521. <http://www.openssl.org/docs/apps/dhparam.html>} for further details.
  1522. @param filePath: A file containing parameters for Diffie-Hellman key
  1523. exchange.
  1524. @type filePath: L{FilePath <twisted.python.filepath.FilePath>}
  1525. @return: An instance that loads its parameters from C{filePath}.
  1526. @rtype: L{DiffieHellmanParameters
  1527. <twisted.internet.ssl.DiffieHellmanParameters>}
  1528. """
  1529. return cls(filePath)
  1530. def _setAcceptableProtocols(context, acceptableProtocols):
  1531. """
  1532. Called to set up the L{OpenSSL.SSL.Context} for doing NPN and/or ALPN
  1533. negotiation.
  1534. @param context: The context which is set up.
  1535. @type context: L{OpenSSL.SSL.Context}
  1536. @param acceptableProtocols: The protocols this peer is willing to speak
  1537. after the TLS negotiation has completed, advertised over both ALPN and
  1538. NPN. If this argument is specified, and no overlap can be found with
  1539. the other peer, the connection will fail to be established. If the
  1540. remote peer does not offer NPN or ALPN, the connection will be
  1541. established, but no protocol wil be negotiated. Protocols earlier in
  1542. the list are preferred over those later in the list.
  1543. @type acceptableProtocols: L{list} of L{bytes}
  1544. """
  1545. def protoSelectCallback(conn, protocols):
  1546. """
  1547. NPN client-side and ALPN server-side callback used to select
  1548. the next protocol. Prefers protocols found earlier in
  1549. C{_acceptableProtocols}.
  1550. @param conn: The context which is set up.
  1551. @type conn: L{OpenSSL.SSL.Connection}
  1552. @param conn: Protocols advertised by the other side.
  1553. @type conn: L{list} of L{bytes}
  1554. """
  1555. overlap = set(protocols) & set(acceptableProtocols)
  1556. for p in acceptableProtocols:
  1557. if p in overlap:
  1558. return p
  1559. else:
  1560. return b''
  1561. # If we don't actually have protocols to negotiate, don't set anything up.
  1562. # Depending on OpenSSL version, failing some of the selection callbacks can
  1563. # cause the handshake to fail, which is presumably not what was intended
  1564. # here.
  1565. if not acceptableProtocols:
  1566. return
  1567. supported = protocolNegotiationMechanisms()
  1568. if supported & ProtocolNegotiationSupport.NPN:
  1569. def npnAdvertiseCallback(conn):
  1570. return acceptableProtocols
  1571. context.set_npn_advertise_callback(npnAdvertiseCallback)
  1572. context.set_npn_select_callback(protoSelectCallback)
  1573. if supported & ProtocolNegotiationSupport.ALPN:
  1574. context.set_alpn_select_callback(protoSelectCallback)
  1575. context.set_alpn_protos(acceptableProtocols)