pki.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-pki)
  19. #:use-module (guix pki)
  20. #:use-module (gcrypt pk-crypto)
  21. #:use-module (gcrypt hash)
  22. #:use-module (rnrs io ports)
  23. #:use-module (srfi srfi-64))
  24. ;; Test the (guix pki) module.
  25. (define %public-key
  26. (call-with-input-file %public-key-file
  27. (compose string->canonical-sexp get-string-all)))
  28. (define %secret-key
  29. (call-with-input-file %private-key-file
  30. (compose string->canonical-sexp get-string-all)))
  31. (define %alternate-secret-key
  32. (string->canonical-sexp
  33. "
  34. (key-data
  35. (public-key
  36. (rsa
  37. (n #00FDBF170366AC43B7D95CF9085565C566FB1F21B17C0A36E68F35ABB500E7851E00B40D7B04C8CD25903371F38E4C298FACEFFC4C97E913B536A0672BAF99D04515AE98A1A56627CD7EB02502FCFBEEA21AF13CC1A853192AD6409B9EFBD9F549BDE32BD890AE01F9A221E81FEE1C407090550647790E0D60775B855E181C2FB5#)
  38. (e #010001#)))
  39. (private-key
  40. (rsa
  41. (n #00FDBF170366AC43B7D95CF9085565C566FB1F21B17C0A36E68F35ABB500E7851E00B40D7B04C8CD25903371F38E4C298FACEFFC4C97E913B536A0672BAF99D04515AE98A1A56627CD7EB02502FCFBEEA21AF13CC1A853192AD6409B9EFBD9F549BDE32BD890AE01F9A221E81FEE1C407090550647790E0D60775B855E181C2FB5#)
  42. (e #010001#)
  43. (d #2790250C2E74C2FD361A99288BBA19B878048F5A0F333F829CC71B3DD64582DB9DF3F4DB1EB0994DD7493225EDA4A1E1492F44D903617FA5643E47BFC7BA157EF48B492AB51229916B02DDBDA0E7DBC7B35A6B8332AB463DC61951CA694551A9760F5A836A375D39E3EA8F2C502A3B5D89CB8777A809B75D603BE7511CEB74E9#)
  44. (p #00FE15B1751E1C31125B724FF37462F9476239A2AFF4192FAB1550F76928C8D02407F4F5EFC83F7A0AF51BD93399DDC06A4B54DFA60A7079F160A9F618C0148AD9#)
  45. (q #00FFA8BE7005AAB7401B0926CD9D6AC30BC9BE7D12C8737C9438498A999F56BE9F5EA98B4D7F5364BEB6D550A5AEDDE34C1EC152C9DAF61A97FDE71740C73BAA3D#)
  46. (u #00FD4050EF4F31B41EC81C28E18D205DFFB3C188F15D8BBA300E30AD8B5C4D3E392EFE10269FC115A538B19F4025973AB09B6650A7FF97DA833FB726F3D8819319#))))"))
  47. (test-begin "pki")
  48. (test-assert "current-acl"
  49. (not (not (member (canonical-sexp->sexp %public-key)
  50. (map canonical-sexp->sexp
  51. (acl->public-keys (current-acl)))))))
  52. (test-assert "authorized-key? public-key current-acl"
  53. (authorized-key? %public-key))
  54. (test-assert "authorized-key? public-key empty-acl"
  55. (not (authorized-key? %public-key (public-keys->acl '()))))
  56. (test-assert "authorized-key? public-key singleton"
  57. (authorized-key? %public-key (public-keys->acl (list %public-key))))
  58. (test-equal "public-keys->acl deduplication"
  59. (public-keys->acl (list %public-key))
  60. (public-keys->acl (make-list 10 %public-key)))
  61. (test-assert "signature-case valid-signature"
  62. (let* ((hash (sha256 #vu8(1 2 3)))
  63. (data (bytevector->hash-data hash #:key-type (key-type %public-key)))
  64. (sig (signature-sexp data %secret-key %public-key)))
  65. (signature-case (sig hash (public-keys->acl (list %public-key)))
  66. (valid-signature #t)
  67. (else #f))))
  68. (test-eq "signature-case invalid-signature" 'i
  69. (let* ((hash (sha256 #vu8(1 2 3)))
  70. (data (bytevector->hash-data hash #:key-type (key-type %public-key)))
  71. (sig (signature-sexp data %alternate-secret-key %public-key)))
  72. (signature-case (sig hash (public-keys->acl (list %public-key)))
  73. (valid-signature 'v)
  74. (invalid-signature 'i)
  75. (hash-mismatch 'm)
  76. (unauthorized-key 'u)
  77. (corrupt-signature 'c))))
  78. (test-eq "signature-case hash-mismatch" 'm
  79. (let* ((hash (sha256 #vu8(1 2 3)))
  80. (data (bytevector->hash-data hash #:key-type (key-type %public-key)))
  81. (sig (signature-sexp data %secret-key %public-key)))
  82. (signature-case (sig (sha256 #vu8())
  83. (public-keys->acl (list %public-key)))
  84. (valid-signature 'v)
  85. (invalid-signature 'i)
  86. (hash-mismatch 'm)
  87. (unauthorized-key 'u)
  88. (corrupt-signature 'c))))
  89. (test-eq "signature-case unauthorized-key" 'u
  90. (let* ((hash (sha256 #vu8(1 2 3)))
  91. (data (bytevector->hash-data hash #:key-type (key-type %public-key)))
  92. (sig (signature-sexp data %secret-key %public-key)))
  93. (signature-case (sig hash (public-keys->acl '()))
  94. (valid-signature 'v)
  95. (invalid-signature 'i)
  96. (hash-mismatch 'm)
  97. (unauthorized-key 'u)
  98. (corrupt-signature 'c))))
  99. (test-eq "signature-case corrupt-signature" 'c
  100. (let* ((hash (sha256 #vu8(1 2 3)))
  101. (sig (string->canonical-sexp "(w tf)")))
  102. (signature-case (sig hash (public-keys->acl (list %public-key)))
  103. (valid-signature 'v)
  104. (invalid-signature 'i)
  105. (hash-mismatch 'm)
  106. (unauthorized-key 'u)
  107. (corrupt-signature 'c))))
  108. (test-end)