all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob df67fe9cf6b9bb773a904b9cc734b2230b0781df 30395 bytes (raw)
name: lisp/net/nsm.el 	 # note: path name is non-authoritative(*)

  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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
 
;;; nsm.el --- Network Security Manager

;; Copyright (C) 2014-2018 Free Software Foundation, Inc.

;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: encryption, security, network

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'cl-lib)
(require 'rmc)                       ; read-multiple-choice
(require 'subr-x)

(defvar nsm-permanent-host-settings nil)
(defvar nsm-temporary-host-settings nil)

(defgroup nsm nil
  "Network Security Manager"
  :version "25.1"
  :group 'comm)

(defcustom network-security-level 'medium
  "How secure the network should be.
If a potential problem with the security of the network
connection is found, the user is asked to give input into how the
connection should be handled.

The following values are possible:

`low': Absolutely no checks are performed.
`medium': This is the default level, should be reasonable for most usage.
`high': This warns about additional things that many people would
not find useful.
`paranoid': On this level, the user is queried for most new connections.

See the Emacs manual for a description of all things that are
checked and warned against."
  :version "25.1"
  :group 'nsm
  :type '(choice (const :tag "Low" low)
		 (const :tag "Medium" medium)
		 (const :tag "High" high)
		 (const :tag "Paranoid" paranoid)))

(defcustom nsm-settings-file (expand-file-name "network-security.data"
						 user-emacs-directory)
  "The file the security manager settings will be stored in."
  :version "25.1"
  :group 'nsm
  :type 'file)

(defcustom nsm-save-host-names nil
  "If non-nil, always save host names in the structures in `nsm-settings-file'.
By default, only hosts that have exceptions have their names
stored in plain text."
  :version "25.1"
  :group 'nsm
  :type 'boolean)

(defvar nsm-noninteractive nil
  "If non-nil, the connection is opened in a non-interactive context.
This means that no queries should be performed.")

(declare-function gnutls-peer-status "gnutls.c" (proc))

(defun nsm-verify-connection (process host port &optional
				      save-fingerprint warn-unencrypted)
  "Verify the security status of PROCESS that's connected to HOST:PORT.
If PROCESS is a gnutls connection, the certificate validity will
be examined.  If it's a non-TLS connection, it may be compared
against previous connections.  If the function determines that
there is something odd about the connection, the user will be
queried about what to do about it.

The process is returned if everything is OK, and otherwise, the
process will be deleted and nil is returned.

If SAVE-FINGERPRINT, always save the fingerprint of the
server (if the connection is a TLS connection).  This is useful
to keep track of the TLS status of STARTTLS servers.

If WARN-UNENCRYPTED, query the user if the connection is
unencrypted."
  (if (eq network-security-level 'low)
      process
    (let* ((status (gnutls-peer-status process))
	   (id (nsm-id host port))
	   (settings (nsm-host-settings id)))
      (cond
       ((not (process-live-p process))
	nil)
       ((not status)
	;; This is a non-TLS connection.
	(nsm-check-plain-connection process host port settings
				    warn-unencrypted))
       (t
	(let ((process
	       (nsm-check-tls-connection process host port status settings)))
	  (when (and process save-fingerprint
		     (null (nsm-host-settings id)))
	    (nsm-save-host host port status 'fingerprint 'always))
	  process))))))

(defun nsm-check-tls-connection (process host port status settings)
  (when-let ((process
              (nsm-check-certificate process host port status settings)))
    ;; Do further protocol-level checks.
    (nsm-check-protocol process host port status settings)))

(declare-function gnutls-peer-status-warning-describe "gnutls.c"
		  (status-symbol))

(defun nsm-check-certificate (process host port status settings)
  (let ((warnings (plist-get status :warnings)))
    (cond

     ;; The certificate validated, but perhaps we want to do
     ;; certificate pinning.
     ((null warnings)
      (cond
       ((< (nsm-level network-security-level) (nsm-level 'high))
	process)
       ;; The certificate is fine, but if we're paranoid, we might
       ;; want to check whether it's changed anyway.
       ((and (>= (nsm-level network-security-level) (nsm-level 'high))
	     (not (nsm-fingerprint-ok-p host port status settings)))
	(delete-process process)
	nil)
       ;; We haven't seen this before, and we're paranoid.
       ((and (eq network-security-level 'paranoid)
	     (null settings)
	     (not (nsm-new-fingerprint-ok-p host port status)))
	(delete-process process)
	nil)
       ((>= (nsm-level network-security-level) (nsm-level 'high))
	;; Save the host fingerprint so that we can check it the
	;; next time we connect.
	(nsm-save-host host port status 'fingerprint 'always)
	process)
       (t
	process)))

     ;; The certificate did not validate.
     ((not (equal network-security-level 'low))
      ;; We always want to pin the certificate of invalid connections
      ;; to track man-in-the-middle or the like.
      (if (not (nsm-fingerprint-ok-p host port status settings))
	  (progn
	    (delete-process process)
	    nil)
	;; We have a warning, so query the user.
	(if (and (not (nsm-warnings-ok-p status settings))
		 (not (nsm-query
		       host port status 'conditions
                       (format-message
		        "The TLS connection to %s:%s is insecure for the following reason%s:\n\n%s"
		        host port
		        (if (> (length warnings) 1)
			    "s" "")
		        (mapconcat #'gnutls-peer-status-warning-describe
                                   warnings
                                   "\n")))))
	    (progn
	      (delete-process process)
	      nil)
	  process))))))

(defvar network-security-protocol-checks
  '((rsa-kx high)
    (dhe-kx medium)
    (anon-kx medium)
    (export-kx medium)
    (cbc-cipher high)
    (ecdsa-cbc-cipher medium)
    (3des-cipher medium)
    (des-cipher medium)
    (rc4-cipher medium)
    (rc2-cipher medium)
    (null-cipher medium)
    (sha1-sig medium)
    (md5-sig medium)
    (ssl medium))
  "This variable specifies what TLS connection checks to perform.
It's an alist where the first element is the name of the check,
the second is the security level where the check kicks in, and the
optional third element is a parameter supplied to the check.

An element like `(rc4 medium)' will result in the function
`nsm-protocol-check--rc4' being called with the parameters
HOST PORT STATUS OPTIONAL-PARAMETER.")

(defun nsm-check-protocol (process host port status settings)
  (let ((results
         (cl-remove-if-not
          #'cdr
          (cl-loop for check in network-security-protocol-checks
                   for type = (intern (format ":%s" (car check)) obarray)
                   ;; Skip the check if the user has already said that this
                   ;; host is OK for this type of "error".
                   when (and (not (memq type (plist-get settings :conditions)))
                             (>= (nsm-level network-security-level)
                                 (nsm-level (cadr check))))
                   collect (cons
                            type
                            (funcall (intern (format "nsm-protocol-check--%s"
                                                     (car check))
                                             obarray)
                                     host port status))))))
    (when (and results
               (not (nsm-query host port status
                               (sort (map-keys results) #'string<)
                               (format-message
		                "The TLS connection to %s:%s is insecure for the following reason%s:\n\n%s"
		                host port
		                (if (> (length results) 1)
			            "s" "")
		                (string-join (map-values results) "\n")))))
      (delete-process process)
      (setq process nil)))
  ;; If a test failed we return nil, otherwise the process object.
  process)

\f

;; Key exchange checks

(defun nsm-protocol-check--rsa-kx (host port status)
  "Check for static RSA key exchange.

Static RSA key exchange methods do not offer perfect forward
secrecy.

Reference:

IETF TLSWG (2014). \"[TLS] Confirming Consensus on removing RSA key
Transport from TLS 1.3\",
`https://www.ietf.org/mail-archive/web/tls/current/msg11621.html'"
  (let ((kx (plist-get status :key-exchange)))
    (and (string-match "^\\bRSA\\b" kx)
         (format-message
          "RSA key exchange method (%s) that does not offer perfect forward secrecy"
          kx))))

(defun nsm-protocol-check--dhe-kx (host port status)
  "Check for finite field ephemeral Diffie-Hellman key exchange.

If `network-security-level' is 'medium, and a DHE key exchange
method was used, this function queries the user if the prime bit
length is < 1024.

If `network-security-level' is 'high or above, and a DHE key
exchange method was used, this function queries the user even if
the prime bit length is >= 1024.

In 2014, the discovery of Logjam[1] had proven non-elliptic-curve
Diffie-Hellman key exchange with < 1024 prime bit length to be
unsafe. In subsequent years, it was discovered that there were
rampant use of small subgroup prime or composite number for DHE
by many servers, and thus allowed themselves to be vulnerable to
backdoors[2]. Given the difficulty in validating DHE parameters,
major browser vendors had started to remove DHE since
2016[3]. Emacs stops short of banning DHE and terminating
connection, but prompts the user instead.

References:

[1]: Adrian et al (2014). \"Imperfect Forward Secrecy: How
Diffie-Hellman Fails in Practice\", `https://weakdh.org/'

[2]: Dorey, Fong, and Essex (2016). \"Indiscreet Logs: Persistent
Diffie-Hellman Backdoors in TLS.\",
`https://eprint.iacr.org/2016/999.pdf'

[3]: Chrome Platform Status (2017). \"Remove DHE-based ciphers\",
`https://www.chromestatus.com/feature/5128908798164992'"
  (let* ((kx (plist-get status :key-exchange))
         (dhe-p (string-match "^\\bDHE\\b" kx))
         (prime-bits (plist-get status :diffie-hellman-prime-bits))
         msg)
    (when dhe-p
      (if (and (>= (nsm-level network-security-level) (nsm-level 'medium))
               (< prime-bits 1024))
          (setq msg (format-message
                     "Diffie-Hellman prime bits (%s) too low (%s)"
                     prime-bits gnutls-min-prime-bits)))
      (if (>= (nsm-level network-security-level) (nsm-level 'high))
          (setq msg (concat
                     msg
                     (format-message
                      "non-elliptic-curve ephemeral Diffie-Hellman key exchange method (%s) maybe using an unsafe prime"
                      kx))))
      msg)))

(defun nsm-protocol-check--anon-kx (host port status)
  "Check for anonymous key exchange.

Anonymouse key exchange exposes the connection to MITM attacks.

Reference:

GnuTLS authors (2018). \"GnuTLS Manual 4.3.3 Anonymous
authentication\",
`https://www.gnutls.org/manual/gnutls.html\#Anonymous-authentication'"
  (let ((kx (plist-get status :key-exchange)))
    (and (string-match "\\bANON\\b" kx)
         (format-message
          "anonymous key exchange method (%s) can be unsafe"
          kx))))

(defun nsm-protocol-check--export-kx (host port status)
  "Check for EXPORT key exchange.

EXPORT cipher suites are a family of 40-bit effective security
algorithms legally exportable by the United States in the early 90s.
They can be broken in seconds on 2018 hardware.

Recent version of GnuTLS does not enable this key exchange by default,
but can be enabled if requested.  This check is mainly provided to
secure Emacs built with older version of GnuTLS.

Reference:

Schneier, Bruce (1996). Applied Cryptography (Second ed.). John Wiley
& Sons. ISBN 0-471-11709-9."
  (let ((kx (plist-get status :key-exchange)))
    (and (string-match "\\bEXPORT\\b" kx)
         (format-message
          "EXPORT level key exchange method (%s) is insecure"
          kx))))

;; Cipher checks

(defun nsm-protocol-check--cbc-cipher (host port status)
  "Check for CBC mode ciphers.

CBC mode cipher in TLS versions earlier than 1.3 are problematic
because of MAC-then-encrypt. This construction is vulnerable to
padding oracle attacks[1].

Reference:

Sullivan (Feb 2016). \"Padding oracles and the decline of CBC-mode
cipher suites\",
`https://blog.cloudflare.com/padding-oracles-and-the-decline-of-cbc-mode-ciphersuites/'"
  (let ((cipher (plist-get status :cipher)))
    (and (string-match "\\bCBC\\b" cipher)
         (format-message
          "CBC mode cipher (%s) can be insecure"
          cipher))))

(defun nsm-protocol-check--ecdsa-cbc-cipher (host port status)
  "Check for CBC mode cipher usage under ECDSA key exchange.

CBC mode cipher in TLS versions earlier than 1.3 are problematic
because of MAC-then-encrypt. This construction is vulnerable to
padding oracle attacks[1].

Due to still widespread use of CBC mode ciphers by servers, this
function only checks for CBC mode cipher usage in combination with
ECDSA key exchange, which is virtually non-existent[2].

References:

[1]: Sullivan (Feb 2016). \"Padding oracles and the decline of
CBC-mode cipher suites\",
`https://blog.cloudflare.com/padding-oracles-and-the-decline-of-cbc-mode-ciphersuites/'

[2]: Chrome Platform Status (2017). \"Remove CBC-mode ECDSA ciphers in
TLS\", `https://www.chromestatus.com/feature/5740978103123968'"
  (let ((kx (plist-get status :key-exchange))
        (cipher (plist-get status :cipher)))
    (and (string-match "\\bECDSA\\b" kx)
         (string-match "\\bCBC\\b" cipher)
         (format-message
          "CBC mode cipher (%s) can be insecure"
          cipher))))

(defun nsm-protocol-check--3des-cipher (host port status)
  "Check for 3DES ciphers.

3DES is considered a weak cipher by NIST as it only has 80 bits
of security.

Reference:

Barker, Elaine (Jan 2016). \"NIST Special Publication 800-57:
Recommendation for Key Management Part 1: General\" (4 ed.),
`https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r4.pdf'
"
  (let ((cipher (plist-get status :cipher)))
    (and (string-match "\\b3DES\\b" cipher)
         (format-message
          "3DES cipher (%s) is insecure"
          cipher))))

(defun nsm-protocol-check--des-cipher (host port status)
  "Check for DES ciphers.

Using dedicated hardware costing less than $10k, a DES key used to
encrypt a message can be brute-forced in a matter of days.  RFC
5469[1] suggests TLS libraries should not enable this algorithm.

Recent version of GnuTLS does not enable this cipher by default, but
can be enabled if requested.  This check is mainly provided to secure
Emacs built with older version of GnuTLS.

Reference:

[1]: P. Eronen, Ed (2009). \"DES and IDEA Cipher Suites for Transport
Layer Security (TLS)\", `https://tools.ietf.org/html/rfc5469'"
  (let ((cipher (plist-get status :cipher)))
    (and (string-match "\\bDES\\b" cipher)
         (format-message
          "DES cipher (%s) is insecure"
          cipher))))

(defun nsm-protocol-check--rc4-cipher (host port status)
  "Check for RC4 ciphers.

RC4 cipher has been prohibited by RFC 7465.

Recent version of GnuTLS does not enable this cipher by default, but
can be enabled if requested.  This check is mainly provided to secure
Emacs built with older version of GnuTLS.

Reference:

Popov A (Feb 2015). \"Prohibiting RC4 Cipher Suites\",
`https://tools.ietf.org/html/rfc7465'"
  (let ((cipher (plist-get status :cipher)))
    (and (string-match "\\bARCFOUR\\b" cipher)
         (format-message
          "RC4 cipher (%s) is insecure"
          cipher))))

(defun nsm-protocol-check--rc2-cipher (host port status)
  "Check for RC2 ciphers.

\"RC2 can be broken with one related-key query and about 2^34 chosen
plaintexts\", which is a matter of seconds on 2018 hardware.

Recent version of GnuTLS does not enable this cipher by default, but
can be enabled if requested.  This check is mainly provided to secure
Emacs built with older version of GnuTLS.

Reference:

Kelsey, Schneier, Wagner (1997). \"Related-Key Cryptanalysis of 3-WAY,
Biham-DES,CAST, DES-X, NewDES, RC2, and TEA\",
`https://www.schneier.com/academic/paperfiles/paper-relatedkey.pdf'"
  (let ((cipher (plist-get status :cipher)))
    (and (string-match "\\bRC2\\b" cipher)
         (format-message
          "RC2 cipher (%s) is insecure"
          cipher))))

(defun nsm-protocol-check--null-cipher (host port status)
  "Check for NULL cipher suites.

This function checks for NULL key exchange, cipher and message
authentication code key derivation function.  As the name suggests, a
NULL assigned for any of the above disables an integral part of the
security properties that make up the TLS protocol."
  (let ((suite (format "%s-%s-%s"
                       (plist-get status :key-exchange)
	               (plist-get status :cipher)
	               (plist-get status :mac))))
    (and (string-match "\\bNULL\\b" suite)
         (format-message
          "NULL cipher suite (%s) violates authenticity, integrity, or confidentiality guarantees"
          suite))))

;; Signature checks

(defun nsm-protocol-check--sha1-sig (host port status)
  "Check for SHA1 signatures on certificates.

The first SHA1 collision was found in 2017[1], as a precaution against
events followed the discovery of cheap collisions in MD5, major
browsers[2][3][4] have sunsetted the use of SHA1 signatures in
certificates.

References:

[1]: Stevens M, Karpman P et al (2017). \"The first collision for full
SHA-1\", `https://shattered.io/static/shattered.pdf'
[2]: Chromium Security Education TLS/SSL. \"Deprecated and Removed
Features (SHA-1 Certificate Signatures)\",
`https://www.chromium.org/Home/chromium-security/education/tls\#TOC-SHA-1-Certificate-Signatures'
[3]: Jones J.C (2017). \"The end of SHA-1 on the Public Web\",
`https://blog.mozilla.org/security/2017/02/23/the-end-of-sha-1-on-the-public-web/'
[4]: Apple Support (2017). \"Move to SHA-256 signed certificates to
avoid connection failures\",
`https://support.apple.com/en-gb/HT207459'
[5]: Microsoft Security Advisory 4010323 (2017). \"Deprecation of
SHA-1 for SSL/TLS Certificates in Microsoft Edge and Internet Explorer
11\",
`https://docs.microsoft.com/en-us/security-updates/securityadvisories/2017/4010323'"
  (cl-loop for certificate in (plist-get status :certificates)
           for algo = (plist-get certificate :signature-algorithm)
           ;; Don't check root certificates -- root is always trusted.
           if (and (not (equal (plist-get certificate :issuer)
                                 (plist-get certificate :subject)))
                     (string-match "\\bSHA1\\b" algo))
           return (format-message
                   "SHA1 signature (%s) is prone to collisions"
                   algo)
           end))

(defun nsm-protocol-check--md5-sig (host port status)
  "Check for MD5 signatures on certificates.

In 2008, a group of researchers were able to forge an intermediate CA
certificate that appeared to be legitimate when checked by MD5[1].
RFC 6151 has recommended against the usage of MD5 for digital
signatures, which includes TLS certificate signatures.

References:

[1]: Sotirov A, Stevens M et al (2008). \"MD5 considered harmful today
- Creating a rogue CA certificate\",
`http://www.win.tue.nl/hashclash/rogue-ca/'
[2]: Turner S, Chen L (2011). \"Updated Security Considerations for
the MD5 Message-Digest and the HMAC-MD5 Algorithms\",
`https://tools.ietf.org/html/rfc6151'"
  (cl-loop for certificate in (plist-get status :certificates)
           for algo = (plist-get certificate :signature-algorithm)
           ;; Don't check root certificates -- root is always trusted.
           if (and (not (equal (plist-get certificate :issuer)
                               (plist-get certificate :subject)))
                   (string-match "\\bMD51\\b" algo))
           return (format-message
                   "MD5 signature (%s) is very prone to collisions"
                   algo)
           end))

;; Protocol level checks

(defun nsm-protocol-check--ssl (host port status)
  "Check for SSL protocol version.

This function guard against usage of SSL3.0, which has been deprecated
by RFC7568.

Reference:

IETF (2015). \"Deprecating Secure Sockets Layer Version 3.0\",
`https://tools.ietf.org/html/rfc7568'"
  (let ((protocol (plist-get status :protocol)))
    (and protocol
         (string-match "SSL" protocol)
         (format-message
	  " %s protocol is deprecated"
	  protocol))))

\f

(defun nsm-fingerprint (status)
  (plist-get (plist-get status :certificate) :public-key-id))

(defun nsm-fingerprint-ok-p (host port status settings)
  (let ((did-query nil))
    (if (and settings
	     (not (eq (plist-get settings :fingerprint) :none))
	     (not (equal (nsm-fingerprint status)
			 (plist-get settings :fingerprint)))
	     (not
	      (setq did-query
		    (nsm-query
		     host port status 'fingerprint
                     (format-message
		      "The fingerprint for the connection to %s:%s has changed from %s to %s"
		      host port
		      (plist-get settings :fingerprint)
		      (nsm-fingerprint status))))))
	;; Not OK.
	nil
      (when did-query
	;; Remove any exceptions that have been set on the previous
	;; certificate.
	(plist-put settings :conditions nil))
      t)))

(defun nsm-new-fingerprint-ok-p (host port status)
  (nsm-query
   host port status 'fingerprint
   (format-message
    "The fingerprint for the connection to %s:%s is new: %s"
    host port
    (nsm-fingerprint status))))

(defun nsm-check-plain-connection (process host port settings warn-unencrypted)
  ;; If this connection used to be TLS, but is now plain, then it's
  ;; possible that we're being Man-In-The-Middled by a proxy that's
  ;; stripping out STARTTLS announcements.
  (cond
   ((and (plist-get settings :fingerprint)
	 (not (eq (plist-get settings :fingerprint) :none))
	 (not
	  (nsm-query
	   host port nil 'conditions
           (format-message
	    "The connection to %s:%s used to be an encrypted connection, but is now unencrypted.  This might mean that there's a man-in-the-middle tapping this connection."
	    host port))))
    (delete-process process)
    nil)
   ((and warn-unencrypted
	 (not (memq :unencrypted (plist-get settings :conditions)))
	 (not (nsm-query
	       host port nil 'conditions
               (format-message
	        "The connection to %s:%s is unencrypted."
	        host port))))
    (delete-process process)
    nil)
   (t
    process)))

(defun nsm-query (host port status what message)
  ;; If there is no user to answer queries, then say `no' to everything.
  (if (or noninteractive
	  nsm-noninteractive)
      nil
    (let ((response
	   (condition-case nil
               (intern
                (car (split-string
                      (nsm-query-user message
                                      (nsm-format-certificate status))))
                obarray)
	     ;; Make sure we manage to close the process if the user hits
	     ;; `C-g'.
	     (quit 'no)
	     (error 'no))))
      (if (eq response 'no)
          (progn
            (message "Aborting connection to %s:%s" host port)
            nil)
        (message (if (eq response 'session)
                     "Accepting certificate for %s:%s this session only"
                   "Permanently accepting certificate for %s:%s")
                 host port)
	(nsm-save-host host port status what response)
	t))))

(defun nsm-query-user (message cert)
  (let ((buffer (get-buffer-create "*Network Security Manager*")))
    (save-window-excursion
      ;; First format the certificate and warnings.
      (with-help-window buffer
        (with-current-buffer buffer
          (erase-buffer)
          (when (> (length cert) 0)
            (insert cert "\n"))
          (let ((start (point)))
            (insert message)
            (goto-char start)
            ;; Fill the first line of the message, which usually
            ;; contains lots of explanatory text.
            (fill-region (point) (line-end-position)))))
      ;; Then ask the user what to do about it.
      (unwind-protect
          (cadr
           (read-multiple-choice
            "Continue connecting?"
            '((?a "always" "Accept this certificate this session and for all future sessions.")
              (?s "session only" "Accept this certificate this session only.")
              (?n "no" "Refuse to use this certificate, and close the connection."))))
        (kill-buffer buffer)))))

(defun nsm-save-host (host port status what permanency)
  (let* ((id (nsm-id host port))
         (what (if (listp what) what (list what)))
	 (saved
	  (list :id id
		:fingerprint (or (nsm-fingerprint status)
				 ;; Plain connection.
				 :none))))
    (when (or (memq 'conditions what)
	      nsm-save-host-names)
      (nconc saved (list :host (format "%s:%s" host port))))
    ;; We either want to save/update the fingerprint or the conditions
    ;; of the certificate/unencrypted connection.
    (cond
     ((memq 'conditions what)
      (cond
       ((not status)
	(nconc saved '(:conditions (:unencrypted))))
       ((plist-get status :warnings)
	(nconc saved
	       (list :conditions (plist-get status :warnings))))))
     ((not (memq 'fingerprint what))
      ;; Store additional protocol settings.
      (let ((settings (nsm-host-settings id)))
	(when settings
	  (setq saved settings))
	(if (plist-get saved :conditions)
	    (nconc (plist-get saved :conditions) what)
	  (nconc saved (list :conditions what))))))
    (if (eq permanency 'always)
	(progn
	  (nsm-remove-temporary-setting id)
	  (nsm-remove-permanent-setting id)
	  (push saved nsm-permanent-host-settings)
	  (nsm-write-settings))
      (nsm-remove-temporary-setting id)
      (push saved nsm-temporary-host-settings))))

(defun nsm-write-settings ()
  (with-temp-file nsm-settings-file
    (insert "(\n")
    (dolist (setting nsm-permanent-host-settings)
      (insert " ")
      (prin1 setting (current-buffer))
      (insert "\n"))
    (insert ")\n")))

(defun nsm-read-settings ()
  (setq nsm-permanent-host-settings
	(with-temp-buffer
	  (insert-file-contents nsm-settings-file)
	  (goto-char (point-min))
	  (ignore-errors (read (current-buffer))))))

(defun nsm-id (host port)
  (concat "sha1:" (sha1 (format "%s:%s" host port))))

(defun nsm-host-settings (id)
  (when (and (not nsm-permanent-host-settings)
	     (file-exists-p nsm-settings-file))
    (nsm-read-settings))
  (let ((result nil))
    (dolist (elem (append nsm-temporary-host-settings
			  nsm-permanent-host-settings))
      (when (and (not result)
		 (equal (plist-get elem :id) id))
	(setq result elem)))
    result))

(defun nsm-warnings-ok-p (status settings)
  (let ((ok t)
	(conditions (plist-get settings :conditions)))
    (dolist (warning (plist-get status :warnings))
      (unless (memq warning conditions)
	(setq ok nil)))
    ok))

(defun nsm-remove-permanent-setting (id)
  (setq nsm-permanent-host-settings
	(cl-delete-if
	 (lambda (elem)
	   (equal (plist-get elem :id) id))
	 nsm-permanent-host-settings)))

(defun nsm-remove-temporary-setting (id)
  (setq nsm-temporary-host-settings
	(cl-delete-if
	 (lambda (elem)
	   (equal (plist-get elem :id) id))
	 nsm-temporary-host-settings)))

(defun nsm-format-certificate (status)
  (let ((cert (plist-get status :certificate)))
    (when cert
      (with-temp-buffer
	(insert
	 "Certificate information\n"
	 "Issued by:"
	 (nsm-certificate-part (plist-get cert :issuer) "CN" t) "\n"
	 "Issued to:"
	 (or (nsm-certificate-part (plist-get cert :subject) "O")
	     (nsm-certificate-part (plist-get cert :subject) "OU" t))
	 "\n"
	 "Hostname:"
	 (nsm-certificate-part (plist-get cert :subject) "CN" t) "\n")
	(when (and (plist-get cert :public-key-algorithm)
		   (plist-get cert :signature-algorithm))
	  (insert
	   "Public key:" (plist-get cert :public-key-algorithm)
	   ", signature: " (plist-get cert :signature-algorithm) "\n"))
	(when (and (plist-get status :key-exchange)
		   (plist-get status :cipher)
		   (plist-get status :mac)
		   (plist-get status :protocol))
	  (insert
	   "Protocol:" (plist-get status :protocol)
	   ", key: " (plist-get status :key-exchange)
	   ", cipher: " (plist-get status :cipher)
	   ", mac: " (plist-get status :mac) "\n"))
	(when (plist-get cert :certificate-security-level)
	  (insert
	   "Security level:"
	   (propertize (plist-get cert :certificate-security-level)
		       'face 'bold)
	   "\n"))
	(insert
	 "Valid:From " (plist-get cert :valid-from)
	 " to " (plist-get cert :valid-to) "\n\n")
	(goto-char (point-min))
	(while (re-search-forward "^[^:]+:" nil t)
	  (insert (make-string (- 20 (current-column)) ? )))
	(buffer-string)))))

(defun nsm-certificate-part (string part &optional full)
  (let ((part (cadr (assoc part (nsm-parse-subject string)))))
    (cond
     (part part)
     (full string)
     (t nil))))

(defun nsm-parse-subject (string)
  (with-temp-buffer
    (insert string)
    (goto-char (point-min))
    (let ((start (point))
	  (result nil))
      (while (not (eobp))
	(push (replace-regexp-in-string
	       "[\\]\\(.\\)" "\\1"
	       (buffer-substring start
				 (if (re-search-forward "[^\\]," nil 'move)
				     (1- (point))
				   (point))))
	      result)
	(setq start (point)))
      (mapcar
       (lambda (elem)
	 (let ((pos (cl-position ?= elem)))
	   (if pos
	       (list (substring elem 0 pos)
		     (substring elem (1+ pos)))
	     elem)))
       (nreverse result)))))

(defun nsm-level (symbol)
  "Return a numerical level for SYMBOL for easier comparison."
  (cond
   ((eq symbol 'low) 0)
   ((eq symbol 'medium) 1)
   ((eq symbol 'high) 2)
   (t 3)))

(provide 'nsm)

;;; nsm.el ends here

debug log:

solving df67fe9cf6 ...
found df67fe9cf6 in https://yhetil.org/emacs/CAKDRQS4LWyfDB8Ao3ECNzPt6LWEHSa9_FaK_yhPtuMLgVXbdQA@mail.gmail.com/
found 0653cfbb1a in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 0653cfbb1a286904c663d6acca664cd9aff51ffa	lisp/net/nsm.el

applying [1/1] https://yhetil.org/emacs/CAKDRQS4LWyfDB8Ao3ECNzPt6LWEHSa9_FaK_yhPtuMLgVXbdQA@mail.gmail.com/
diff --git a/lisp/net/nsm.el b/lisp/net/nsm.el
index 0653cfbb1a..df67fe9cf6 100644

Checking patch lisp/net/nsm.el...
Applied patch lisp/net/nsm.el cleanly.

index at:
100644 df67fe9cf6b9bb773a904b9cc734b2230b0781df	lisp/net/nsm.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.