Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

"""Tests for certbot.crypto_util.""" 

import logging 

import os 

import unittest 

 

import OpenSSL 

import mock 

import zope.component 

 

from certbot import errors 

from certbot import interfaces 

from certbot import util 

import certbot.tests.util as test_util 

 

 

RSA256_KEY = test_util.load_vector('rsa256_key.pem') 

RSA256_KEY_PATH = test_util.vector_path('rsa256_key.pem') 

RSA512_KEY = test_util.load_vector('rsa512_key.pem') 

RSA2048_KEY_PATH = test_util.vector_path('rsa2048_key.pem') 

CERT_PATH = test_util.vector_path('cert_512.pem') 

CERT = test_util.load_vector('cert_512.pem') 

SS_CERT_PATH = test_util.vector_path('cert_2048.pem') 

SS_CERT = test_util.load_vector('cert_2048.pem') 

P256_KEY = test_util.load_vector('nistp256_key.pem') 

P256_CERT_PATH = test_util.vector_path('cert-nosans_nistp256.pem') 

P256_CERT = test_util.load_vector('cert-nosans_nistp256.pem') 

 

class InitSaveKeyTest(test_util.TempDirTestCase): 

"""Tests for certbot.crypto_util.init_save_key.""" 

def setUp(self): 

super(InitSaveKeyTest, self).setUp() 

 

logging.disable(logging.CRITICAL) 

zope.component.provideUtility( 

mock.Mock(strict_permissions=True), interfaces.IConfig) 

 

def tearDown(self): 

super(InitSaveKeyTest, self).tearDown() 

 

logging.disable(logging.NOTSET) 

 

@classmethod 

def _call(cls, key_size, key_dir): 

from certbot.crypto_util import init_save_key 

return init_save_key(key_size, key_dir, 'key-certbot.pem') 

 

@mock.patch('certbot.crypto_util.make_key') 

def test_success(self, mock_make): 

mock_make.return_value = b'key_pem' 

key = self._call(1024, self.tempdir) 

self.assertEqual(key.pem, b'key_pem') 

self.assertTrue('key-certbot.pem' in key.file) 

self.assertTrue(os.path.exists(os.path.join(self.tempdir, key.file))) 

 

@mock.patch('certbot.crypto_util.make_key') 

def test_key_failure(self, mock_make): 

mock_make.side_effect = ValueError 

self.assertRaises(ValueError, self._call, 431, self.tempdir) 

 

 

class InitSaveCSRTest(test_util.TempDirTestCase): 

"""Tests for certbot.crypto_util.init_save_csr.""" 

 

def setUp(self): 

super(InitSaveCSRTest, self).setUp() 

 

zope.component.provideUtility( 

mock.Mock(strict_permissions=True), interfaces.IConfig) 

 

@mock.patch('acme.crypto_util.make_csr') 

@mock.patch('certbot.crypto_util.util.make_or_verify_dir') 

def test_it(self, unused_mock_verify, mock_csr): 

from certbot.crypto_util import init_save_csr 

 

mock_csr.return_value = b'csr_pem' 

 

csr = init_save_csr( 

mock.Mock(pem='dummy_key'), 'example.com', self.tempdir) 

 

self.assertEqual(csr.data, b'csr_pem') 

self.assertTrue('csr-certbot.pem' in csr.file) 

 

 

class ValidCSRTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.valid_csr.""" 

 

@classmethod 

def _call(cls, csr): 

from certbot.crypto_util import valid_csr 

return valid_csr(csr) 

 

def test_valid_pem_true(self): 

self.assertTrue(self._call(test_util.load_vector('csr_512.pem'))) 

 

def test_valid_pem_san_true(self): 

self.assertTrue(self._call(test_util.load_vector('csr-san_512.pem'))) 

 

def test_valid_der_false(self): 

self.assertFalse(self._call(test_util.load_vector('csr_512.der'))) 

 

def test_empty_false(self): 

self.assertFalse(self._call('')) 

 

def test_random_false(self): 

self.assertFalse(self._call('foo bar')) 

 

 

class CSRMatchesPubkeyTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.csr_matches_pubkey.""" 

 

@classmethod 

def _call(cls, *args, **kwargs): 

from certbot.crypto_util import csr_matches_pubkey 

return csr_matches_pubkey(*args, **kwargs) 

 

def test_valid_true(self): 

self.assertTrue(self._call( 

test_util.load_vector('csr_512.pem'), RSA512_KEY)) 

 

def test_invalid_false(self): 

self.assertFalse(self._call( 

test_util.load_vector('csr_512.pem'), RSA256_KEY)) 

 

 

class ImportCSRFileTest(unittest.TestCase): 

"""Tests for certbot.certbot_util.import_csr_file.""" 

 

@classmethod 

def _call(cls, *args, **kwargs): 

from certbot.crypto_util import import_csr_file 

return import_csr_file(*args, **kwargs) 

 

def test_der_csr(self): 

csrfile = test_util.vector_path('csr_512.der') 

data = test_util.load_vector('csr_512.der') 

data_pem = test_util.load_vector('csr_512.pem') 

 

self.assertEqual( 

(OpenSSL.crypto.FILETYPE_PEM, 

util.CSR(file=csrfile, 

data=data_pem, 

form="pem"), 

["Example.com"]), 

self._call(csrfile, data)) 

 

def test_pem_csr(self): 

csrfile = test_util.vector_path('csr_512.pem') 

data = test_util.load_vector('csr_512.pem') 

 

self.assertEqual( 

(OpenSSL.crypto.FILETYPE_PEM, 

util.CSR(file=csrfile, 

data=data, 

form="pem"), 

["Example.com"],), 

self._call(csrfile, data)) 

 

def test_bad_csr(self): 

self.assertRaises(errors.Error, self._call, 

test_util.vector_path('cert_512.pem'), 

test_util.load_vector('cert_512.pem')) 

 

 

class MakeKeyTest(unittest.TestCase): # pylint: disable=too-few-public-methods 

"""Tests for certbot.crypto_util.make_key.""" 

 

def test_it(self): # pylint: disable=no-self-use 

from certbot.crypto_util import make_key 

# Do not test larger keys as it takes too long. 

OpenSSL.crypto.load_privatekey( 

OpenSSL.crypto.FILETYPE_PEM, make_key(1024)) 

 

 

class VerifyCertSetup(unittest.TestCase): 

"""Refactoring for verification tests.""" 

 

def setUp(self): 

super(VerifyCertSetup, self).setUp() 

 

self.renewable_cert = mock.MagicMock() 

self.renewable_cert.cert = SS_CERT_PATH 

self.renewable_cert.chain = SS_CERT_PATH 

self.renewable_cert.privkey = RSA2048_KEY_PATH 

self.renewable_cert.fullchain = test_util.vector_path('cert_fullchain_2048.pem') 

 

self.bad_renewable_cert = mock.MagicMock() 

self.bad_renewable_cert.chain = SS_CERT_PATH 

self.bad_renewable_cert.cert = SS_CERT_PATH 

self.bad_renewable_cert.fullchain = SS_CERT_PATH 

 

 

class VerifyRenewableCertTest(VerifyCertSetup): 

"""Tests for certbot.crypto_util.verify_renewable_cert.""" 

 

def setUp(self): 

super(VerifyRenewableCertTest, self).setUp() 

 

def _call(self, renewable_cert): 

from certbot.crypto_util import verify_renewable_cert 

return verify_renewable_cert(renewable_cert) 

 

def test_verify_renewable_cert(self): 

self.assertEqual(None, self._call(self.renewable_cert)) 

 

@mock.patch('certbot.crypto_util.verify_renewable_cert_sig', side_effect=errors.Error("")) 

def test_verify_renewable_cert_failure(self, unused_verify_renewable_cert_sign): 

self.assertRaises(errors.Error, self._call, self.bad_renewable_cert) 

 

 

class VerifyRenewableCertSigTest(VerifyCertSetup): 

"""Tests for certbot.crypto_util.verify_renewable_cert.""" 

 

def setUp(self): 

super(VerifyRenewableCertSigTest, self).setUp() 

 

def _call(self, renewable_cert): 

from certbot.crypto_util import verify_renewable_cert_sig 

return verify_renewable_cert_sig(renewable_cert) 

 

def test_cert_sig_match(self): 

self.assertEqual(None, self._call(self.renewable_cert)) 

 

def test_cert_sig_match_ec(self): 

renewable_cert = mock.MagicMock() 

renewable_cert.cert = P256_CERT_PATH 

renewable_cert.chain = P256_CERT_PATH 

renewable_cert.privkey = P256_KEY 

self.assertEqual(None, self._call(renewable_cert)) 

 

def test_cert_sig_mismatch(self): 

self.bad_renewable_cert.cert = test_util.vector_path('cert_512_bad.pem') 

self.assertRaises(errors.Error, self._call, self.bad_renewable_cert) 

 

 

class VerifyFullchainTest(VerifyCertSetup): 

"""Tests for certbot.crypto_util.verify_fullchain.""" 

 

def setUp(self): 

super(VerifyFullchainTest, self).setUp() 

 

def _call(self, renewable_cert): 

from certbot.crypto_util import verify_fullchain 

return verify_fullchain(renewable_cert) 

 

def test_fullchain_matches(self): 

self.assertEqual(None, self._call(self.renewable_cert)) 

 

def test_fullchain_mismatch(self): 

self.assertRaises(errors.Error, self._call, self.bad_renewable_cert) 

 

def test_fullchain_ioerror(self): 

self.bad_renewable_cert.chain = "dog" 

self.assertRaises(errors.Error, self._call, self.bad_renewable_cert) 

 

 

class VerifyCertMatchesPrivKeyTest(VerifyCertSetup): 

"""Tests for certbot.crypto_util.verify_cert_matches_priv_key.""" 

 

def setUp(self): 

super(VerifyCertMatchesPrivKeyTest, self).setUp() 

 

def _call(self, renewable_cert): 

from certbot.crypto_util import verify_cert_matches_priv_key 

return verify_cert_matches_priv_key(renewable_cert.cert, renewable_cert.privkey) 

 

def test_cert_priv_key_match(self): 

self.renewable_cert.cert = SS_CERT_PATH 

self.renewable_cert.privkey = RSA2048_KEY_PATH 

self.assertEqual(None, self._call(self.renewable_cert)) 

 

def test_cert_priv_key_mismatch(self): 

self.bad_renewable_cert.privkey = RSA256_KEY_PATH 

self.bad_renewable_cert.cert = SS_CERT_PATH 

 

self.assertRaises(errors.Error, self._call, self.bad_renewable_cert) 

 

 

class ValidPrivkeyTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.valid_privkey.""" 

 

@classmethod 

def _call(cls, privkey): 

from certbot.crypto_util import valid_privkey 

return valid_privkey(privkey) 

 

def test_valid_true(self): 

self.assertTrue(self._call(RSA512_KEY)) 

 

def test_empty_false(self): 

self.assertFalse(self._call('')) 

 

def test_random_false(self): 

self.assertFalse(self._call('foo bar')) 

 

 

class GetSANsFromCertTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.get_sans_from_cert.""" 

 

@classmethod 

def _call(cls, *args, **kwargs): 

from certbot.crypto_util import get_sans_from_cert 

return get_sans_from_cert(*args, **kwargs) 

 

def test_single(self): 

self.assertEqual([], self._call(test_util.load_vector('cert_512.pem'))) 

 

def test_san(self): 

self.assertEqual( 

['example.com', 'www.example.com'], 

self._call(test_util.load_vector('cert-san_512.pem'))) 

 

 

class GetNamesFromCertTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.get_names_from_cert.""" 

 

@classmethod 

def _call(cls, *args, **kwargs): 

from certbot.crypto_util import get_names_from_cert 

return get_names_from_cert(*args, **kwargs) 

 

def test_single(self): 

self.assertEqual( 

['example.com'], 

self._call(test_util.load_vector('cert_512.pem'))) 

 

def test_san(self): 

self.assertEqual( 

['example.com', 'www.example.com'], 

self._call(test_util.load_vector('cert-san_512.pem'))) 

 

def test_common_name_sans_order(self): 

# Tests that the common name comes first 

# followed by the SANS in alphabetical order 

self.assertEqual( 

['example.com'] + ['{0}.example.com'.format(c) for c in 'abcd'], 

self._call(test_util.load_vector('cert-5sans_512.pem'))) 

 

def test_parse_non_cert(self): 

self.assertRaises(OpenSSL.crypto.Error, self._call, "hello there") 

 

 

class CertLoaderTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.pyopenssl_load_certificate""" 

 

def test_load_valid_cert(self): 

from certbot.crypto_util import pyopenssl_load_certificate 

 

cert, file_type = pyopenssl_load_certificate(CERT) 

self.assertEqual(cert.digest('sha256'), 

OpenSSL.crypto.load_certificate(file_type, CERT).digest('sha256')) 

 

def test_load_invalid_cert(self): 

from certbot.crypto_util import pyopenssl_load_certificate 

bad_cert_data = CERT.replace(b"BEGIN CERTIFICATE", b"ASDFASDFASDF!!!") 

self.assertRaises( 

errors.Error, pyopenssl_load_certificate, bad_cert_data) 

 

 

class NotBeforeTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.notBefore""" 

 

def test_notBefore(self): 

from certbot.crypto_util import notBefore 

self.assertEqual(notBefore(CERT_PATH).isoformat(), 

'2014-12-11T22:34:45+00:00') 

 

 

class NotAfterTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.notAfter""" 

 

def test_notAfter(self): 

from certbot.crypto_util import notAfter 

self.assertEqual(notAfter(CERT_PATH).isoformat(), 

'2014-12-18T22:34:45+00:00') 

 

 

class Sha256sumTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.notAfter""" 

def test_sha256sum(self): 

from certbot.crypto_util import sha256sum 

self.assertEqual(sha256sum(CERT_PATH), 

'914ffed8daf9e2c99d90ac95c77d54f32cbd556672facac380f0c063498df84e') 

 

 

class CertAndChainFromFullchainTest(unittest.TestCase): 

"""Tests for certbot.crypto_util.cert_and_chain_from_fullchain""" 

 

def test_cert_and_chain_from_fullchain(self): 

cert_pem = CERT.decode() 

chain_pem = cert_pem + SS_CERT.decode() 

fullchain_pem = cert_pem + chain_pem 

spacey_fullchain_pem = cert_pem + u'\n' + chain_pem 

from certbot.crypto_util import cert_and_chain_from_fullchain 

for fullchain in (fullchain_pem, spacey_fullchain_pem): 

cert_out, chain_out = cert_and_chain_from_fullchain(fullchain) 

self.assertEqual(cert_out, cert_pem) 

self.assertEqual(chain_out, chain_pem) 

 

 

if __name__ == '__main__': 

unittest.main() # pragma: no cover