key.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ;;; key.scm -- Testing of Guile-SSH keys
  2. ;; Copyright (C) 2014, 2015 Artyom V. Poptsov <poptsov.artyom@gmail.com>
  3. ;;
  4. ;; This file is a part of Guile-SSH.
  5. ;;
  6. ;; Guile-SSH is free software: you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation, either version 3 of the
  9. ;; License, or (at your option) any later version.
  10. ;;
  11. ;; Guile-SSH 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 GNU
  14. ;; General Public License for more details.
  15. ;;
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
  18. (add-to-load-path (getenv "abs_top_srcdir"))
  19. (use-modules (srfi srfi-64)
  20. (ssh key)
  21. (ssh version)
  22. (tests common))
  23. ;;;
  24. ;; ECDSA doesn't work if libssh 0.6.3 was compiled GCrypt
  25. (define %openssl? (eq? (get-crypto-library) 'openssl))
  26. (define-syntax-rule (when-openssl test)
  27. (or (not %openssl?)
  28. test))
  29. (test-begin-with-log "key")
  30. (test-assert-with-log "private-key-from-file"
  31. (and (private-key-from-file %rsakey)
  32. (private-key-from-file %dsakey)
  33. (when-openssl
  34. (private-key-from-file %ecdsakey))))
  35. (test-assert-with-log "public-key-from-file"
  36. (and (public-key-from-file %rsakey-pub)
  37. (public-key-from-file %dsakey-pub)
  38. (when-openssl
  39. (public-key-from-file %ecdsakey-pub))))
  40. (define *rsa-key* (private-key-from-file %rsakey))
  41. (define *dsa-key* (private-key-from-file %dsakey))
  42. (define *ecdsa-key* (when-openssl
  43. (private-key-from-file %ecdsakey)))
  44. (define *rsa-pub-key* (public-key-from-file %rsakey-pub))
  45. (define *dsa-pub-key* (public-key-from-file %dsakey-pub))
  46. (define *ecdsa-pub-key* (when-openssl
  47. (public-key-from-file %ecdsakey-pub)))
  48. (test-assert "key?"
  49. (and (not (key? "not a key"))
  50. (key? *rsa-key*)
  51. (key? *dsa-key*)
  52. (when-openssl
  53. (key? *ecdsa-key*))
  54. (key? *rsa-pub-key*)
  55. (key? *dsa-pub-key*)
  56. (when-openssl
  57. (key? *ecdsa-pub-key*))))
  58. (test-assert "private-key?"
  59. (and (private-key? *rsa-key*)
  60. (not (private-key? *rsa-pub-key*))
  61. (not (private-key? "not a key"))))
  62. (test-assert-with-log "public-key?"
  63. (and (public-key? *rsa-pub-key*)
  64. ;; XXX: Currently a SSH key that has been read from a file
  65. ;; has both public and private flags.
  66. (public-key? *rsa-key*)
  67. (not (public-key? "not a key"))))
  68. (test-assert-with-log "private-key->public-key"
  69. (and (private-key->public-key *rsa-key*)
  70. (private-key->public-key *dsa-key*)
  71. (when-openssl
  72. (private-key->public-key *ecdsa-key*))))
  73. (test-assert-with-log "get-key-type"
  74. (and (eq? 'rsa (get-key-type *rsa-key*))
  75. (eq? 'dss (get-key-type *dsa-key*))
  76. (when-openssl
  77. (eq? 'ecdsa (get-key-type *ecdsa-key*)))))
  78. (test-assert-with-log "private-key-to-file"
  79. (when-openssl
  80. (let ((file-name "./tmp-rsa-key"))
  81. (private-key-to-file *rsa-key* file-name)
  82. (let ((key (private-key-from-file file-name)))
  83. (delete-file file-name)
  84. (and (key? key)
  85. (private-key? key))))))
  86. ;;; Converting between strings and keys
  87. (define %rsakey-pub-string
  88. "AAAAB3NzaC1yc2EAAAADAQABAAABAQC+8H9j5Yt3xeqaAxXAtSbBsW0JsJegngwfLveHA0ev3ndEKruylR6CZgf6OxshTwUeBaqn7jJMf+6RRQPTcxihgtZAfdyKdPGWDtmePBnG64+uGEaP8N3KvCzlANKf5tmxS8brJlQhxKL8t+3IE8w3QmCMnCGKWprsL/ygPA9koWauUqqKvOQbZXdUEfLvZfnsE1laRyK4dwLiiM2vyGZM/2yePLP4xYu/uYdPFaukxt3DMcgrEy9zuVcU8wbkJMKM57sambvituzMVVqRdeMX9exZv32qcXlpChl4XjFClQ0lqOb8S8CNTPXm3zQ2ZJrQtUHiD54RYhlXD7X0TO6v")
  89. (define %dsakey-pub-string
  90. "AAAAB3NzaC1kc3MAAACBAOpnJ64w3Qo3HkCCODTPpLqPUrDLg0bxWdoae2tsXFwhBthIlCV8N0hTzOj1Qrgnx/WiuDk5qXSKOHisyqVBv8sGLOUTBy0Fdz1SobZ9+WGu5+5EiJm78MZcgtHXHu1GPuImANifbSaDJpIGKItq0V5WhpLXyQC7o0Vt70sGQboVAAAAFQDeu+6APBWXtqq2Ch+nODn7VDSIhQAAAIA5iGHYbztSq8KnWj1J/6GTvsPp1JFqZ3hFX5wlGIV4XxBdeEZnCPrhYJumM7SRjYjWMpW5eqFNs5o3d+rJPFFwDo7yW10WC3Bfpo5xRxU35xf/aFAVbm3vi/HRQvv4cFrwTLvPHgNYGYdZiHXCXPoYIh+WoKT9n3MfrBXB4hpAmwAAAIEArkWuRnbjfPVFpXrWGw6kMPVdhOZr1ghdlG5bY31y4UKUlmHvXx5YZ776dSRSMJY2u4lS73+SFgwPdkmpgGma/rZdd9gly9T7SiSr/4qXJyS8Muh203xsAU3ukRocY8lsvllKEGiCJmrUTJWmj0UYEDsbqy2k/1Yz2Q/awygyk9c=")
  91. (define %ecdsakey-pub-string
  92. "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHcpje/fp21KjuZFKgmKAAwHeYJ6e3ny4LwEVjZr8hOCVlBvqj7/krVqxbwZI7EcowbpYI1F8ZszS7zfUhKT3U4=")
  93. (test-equal "public-key->string, RSA"
  94. (public-key->string *rsa-pub-key*)
  95. %rsakey-pub-string)
  96. (test-equal "public-key->string, DSA"
  97. (public-key->string *dsa-pub-key*)
  98. %dsakey-pub-string)
  99. (when-openssl
  100. (test-equal "public-key->string, ECDSA"
  101. (public-key->string *ecdsa-pub-key*)
  102. %ecdsakey-pub-string))
  103. (test-equal "string->public-key, RSA"
  104. (public-key->string (string->public-key %rsakey-pub-string 'rsa))
  105. %rsakey-pub-string)
  106. (test-equal "string->public-key, DSA"
  107. (public-key->string (string->public-key %dsakey-pub-string 'dss))
  108. %dsakey-pub-string)
  109. (when-openssl
  110. (test-equal "string->public-key, ECDSA"
  111. (public-key->string (string->public-key %ecdsakey-pub-string 'ecdsa))
  112. %ecdsakey-pub-string))
  113. (test-assert-with-log "string->public-key, RSA, gc test"
  114. (let ((max-keys 1000))
  115. (do ((idx 1 (+ idx 1)))
  116. ((> idx max-keys))
  117. (when (zero? (euclidean-remainder idx 100))
  118. (format-log/scm 'nolog "" (format #f "~d / ~d keys created ..."
  119. idx max-keys)))
  120. (public-key->string (string->public-key %rsakey-pub-string 'rsa)))
  121. #t))
  122. (test-assert-with-log "make-keypair"
  123. (and (let ((key (make-keypair 'rsa 1024)))
  124. (and (key? key)
  125. (eq? (get-key-type key) 'rsa)))
  126. (let ((key (make-keypair 'dss 1024)))
  127. (and (key? key)
  128. (eq? (get-key-type key) 'dss)))
  129. (when-openssl
  130. (let ((key (make-keypair 'ecdsa 256)))
  131. (and (key? key)
  132. (eq? (get-key-type key) 'ecdsa))))))
  133. ;;;
  134. (test-end "key")
  135. (exit (= (test-runner-fail-count (test-runner-current)) 0))
  136. ;;; key.scm ends here.