From aa2480e50d713b90805336ab48477ba596aef531 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Mon, 17 Mar 2014 23:30:21 +0100 Subject: build: Fix test failures due to the nix -> guix directory name change. Reported by Nikita Karetnikov and Pierre-Antoine Rault . * test-env.in: Change 'NIX_LOG_DIR' in accordance with f5768af. * tests/guix-register.sh: Likewise for 'NIX_LOG_DIR' and 'NIX_DB_DIR'. --- tests/guix-register.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/guix-register.sh b/tests/guix-register.sh index ee633af4f9..019a451b3b 100644 --- a/tests/guix-register.sh +++ b/tests/guix-register.sh @@ -84,8 +84,8 @@ guix-register --prefix "$new_store" "$closure" NIX_IGNORE_SYMLINK_STORE=1 NIX_STORE_DIR="$new_store_dir" NIX_STATE_DIR="$new_store$localstatedir" -NIX_LOG_DIR="$new_store$localstatedir/log/nix" -NIX_DB_DIR="$new_store$localstatedir/nix/db" +NIX_LOG_DIR="$new_store$localstatedir/log/guix" +NIX_DB_DIR="$new_store$localstatedir/guix/db" export NIX_IGNORE_SYMLINK_STORE NIX_STORE_DIR NIX_STATE_DIR \ NIX_LOG_DIR NIX_DB_DIR -- cgit v1.2.3 From e297d8fc565861794c7d85cf7d158a846ab25aac Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Tue, 18 Mar 2014 23:59:57 +0100 Subject: tests: Test recovery from 'valid-path?' RPCs with an invalid parameter. * tests/store.scm ("valid-path? live", "valid-path? false", "valid-path? error", "valid-path? recovery"): New tests. --- tests/store.scm | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/store.scm b/tests/store.scm index 78023a423d..d23024bcbc 100644 --- a/tests/store.scm +++ b/tests/store.scm @@ -87,7 +87,39 @@ (%store-prefix) "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile"))))) -(test-skip (if %store 0 11)) +(test-skip (if %store 0 13)) + +(test-assert "valid-path? live" + (let ((p (add-text-to-store %store "hello" "hello, world"))) + (valid-path? %store p))) + +(test-assert "valid-path? false" + (not (valid-path? %store + (string-append (%store-prefix) "/" + (make-string 32 #\e) "-foobar")))) + +(test-assert "valid-path? error" + (with-store s + (guard (c ((nix-protocol-error? c) #t)) + (valid-path? s "foo") + #f))) + +(test-assert "valid-path? recovery" + ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately + ;; close the connection after receiving a 'valid-path?' RPC with a non-store + ;; file name. See + ;; for + ;; details. + (with-store s + (let-syntax ((true-if-error (syntax-rules () + ((_ exp) + (guard (c ((nix-protocol-error? c) #t)) + exp #f))))) + (and (true-if-error (valid-path? s "foo")) + (true-if-error (valid-path? s "bar")) + (true-if-error (valid-path? s "baz")) + (true-if-error (valid-path? s "chbouib")) + (valid-path? s (add-text-to-store s "valid" "yeah")))))) (test-assert "hash-part->path" (let ((p (add-text-to-store %store "hello" "hello, world"))) -- cgit v1.2.3 From 32a1eb802519179eab8ff687e73f26edab28922f Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Wed, 19 Mar 2014 21:40:10 +0100 Subject: pk-crypto: Use RFC6979 when signing with an ECC or DSA key. * guix/pk-crypto.scm (bytevector->hash-data): Add #:key-type parameter. Use the 'pkcs1' flag when KEY-TYPE is 'rsa', and 'rfc6979' when KEY-TYPE is 'ecc' or 'dsa'. (key-type): New procedure. * guix/scripts/authenticate.scm (read-hash-data): Add 'key-type' parameter. Pass it to 'bytevector->hash-data'. Adjust caller accordingly. * tests/pk-crypto.scm (%ecc-key-pair): New variable. ("key-type"): New test. ("sign + verify"): Pass #:key-type to 'bytevector->hash-data'. ("sign + verify, Ed25519"): New test. --- guix/pk-crypto.scm | 27 ++++++++++++++++++++++----- guix/scripts/authenticate.scm | 9 +++++---- tests/pk-crypto.scm | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/guix/pk-crypto.scm b/guix/pk-crypto.scm index 50f709418c..b9ab02861c 100644 --- a/guix/pk-crypto.scm +++ b/guix/pk-crypto.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2013 Ludovic Courtès +;;; Copyright © 2013, 2014 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -39,6 +39,7 @@ canonical-sexp-list? bytevector->hash-data hash-data->bytevector + key-type sign verify generate-key @@ -232,15 +233,31 @@ Return #f if that element does not exist, or if it's a list." "Return an s-expression representing NUMBER." (string->canonical-sexp (string-append "#" (number->string number 16) "#"))) -(define* (bytevector->hash-data bv #:optional (hash-algo "sha256")) +(define* (bytevector->hash-data bv + #:optional + (hash-algo "sha256") + #:key (key-type 'ecc)) "Given BV, a bytevector containing a hash, return an s-expression suitable -for use as the data for 'sign'." +for use as the data for 'sign'. KEY-TYPE must be a symbol: 'dsa, 'ecc, or +'rsa." (string->canonical-sexp - (format #f "(data (flags pkcs1) (hash \"~a\" #~a#))" + (format #f "(data (flags ~a) (hash \"~a\" #~a#))" + (case key-type + ((ecc dsa) "rfc6979") + ((rsa) "pkcs1") + (else (error "unknown key type" key-type))) hash-algo (bytevector->base16-string bv)))) -(define (hash-data->bytevector data) +(define (key-type sexp) + "Return a symbol denoting the type of key representing by SEXP--e.g., 'rsa', +'ecc'--or #f if SEXP does not denote a valid key." + (case (canonical-sexp-nth-data sexp 0) + ((public-key private-key) + (canonical-sexp-nth-data (canonical-sexp-nth sexp 1) 0)) + (else #f))) + +(define* (hash-data->bytevector data) "Return two values: the hash value (a bytevector), and the hash algorithm (a string) extracted from DATA, an sexp as returned by 'bytevector->hash-data'. Return #f if DATA does not conform." diff --git a/guix/scripts/authenticate.scm b/guix/scripts/authenticate.scm index 27580dedff..927dbe8afc 100644 --- a/guix/scripts/authenticate.scm +++ b/guix/scripts/authenticate.scm @@ -39,11 +39,12 @@ (call-with-input-file file (compose string->canonical-sexp get-string-all))) -(define (read-hash-data file) - "Read sha256 hash data from FILE and return it as a gcrypt sexp." +(define (read-hash-data file key-type) + "Read sha256 hash data from FILE and return it as a gcrypt sexp. KEY-TYPE +is a symbol representing the type of public key algo being used." (let* ((hex (call-with-input-file file get-string-all)) (bv (base16-string->bytevector (string-trim-both hex)))) - (bytevector->hash-data bv))) + (bytevector->hash-data bv #:key-type key-type))) ;;; @@ -64,7 +65,7 @@ (leave (_ "cannot find public key for secret key '~a'~%") key))) - (data (read-hash-data hash-file)) + (data (read-hash-data hash-file (key-type public-key))) (signature (signature-sexp data secret-key public-key))) (display (canonical-sexp->string signature)) #t)) diff --git a/tests/pk-crypto.scm b/tests/pk-crypto.scm index 6774dd4157..4d498020f5 100644 --- a/tests/pk-crypto.scm +++ b/tests/pk-crypto.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2013 Ludovic Courtès +;;; Copyright © 2013, 2014 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -31,7 +31,7 @@ ;; Test the (guix pk-crypto) module. (define %key-pair - ;; Key pair that was generated with: + ;; RSA key pair that was generated with: ;; (generate-key (string->canonical-sexp "(genkey (rsa (nbits 4:1024)))")) ;; which takes a bit of time. "(key-data @@ -48,6 +48,20 @@ (q #00E9AD22F158060BC9AE3601DA623AFC60FFF3058795802CA92371C00097335CF9A23D7782DE353C9DBA93D7BB99E6A24A411107605E722481C5C191F80D7EB77F#) (u #59B45B95AE01A7A7370FAFDB08FE73A4793CE37F228961B09B1B1E7DDAD9F8D3E28F5C5E8B4B067E6B8E0BBF3F690B42991A79E46108DDCDA2514323A66964DE#))))") +(define %ecc-key-pair + ;; Ed25519 key pair generated with: + ;; (generate-key (string->canonical-sexp "(genkey (ecdsa (curve Ed25519) (flags rfc6979 transient)))")) + "(key-data + (public-key + (ecc + (curve Ed25519) + (q #94869C1B9E69DB8DD910B7F7F4D6E56A63A964A59AE8F90F6703ACDDF6F50C81#))) + (private-key + (ecc + (curve Ed25519) + (q #94869C1B9E69DB8DD910B7F7F4D6E56A63A964A59AE8F90F6703ACDDF6F50C81#) + (d #6EFB32D0B4EC6B3237B523539F1979379B82726AAA605EB2FBA6775B2B777B78#))))") + (test-begin "pk-crypto") (let ((sexps '("(foo bar)" @@ -148,8 +162,30 @@ (and (string=? algo "sha256") (bytevector=? value bv)))))) +(test-equal "key-type" + '(rsa ecc) + (map (compose key-type + (cut find-sexp-token <> 'public-key) + string->canonical-sexp) + (list %key-pair %ecc-key-pair))) + (test-assert "sign + verify" (let* ((pair (string->canonical-sexp %key-pair)) + (secret (find-sexp-token pair 'private-key)) + (public (find-sexp-token pair 'public-key)) + (data (bytevector->hash-data + (sha256 (string->utf8 "Hello, world.")) + #:key-type (key-type public))) + (sig (sign data secret))) + (and (verify sig data public) + (not (verify sig + (bytevector->hash-data + (sha256 (string->utf8 "Hi!")) + #:key-type (key-type public)) + public))))) + +(test-assert "sign + verify, Ed25519" + (let* ((pair (string->canonical-sexp %ecc-key-pair)) (secret (find-sexp-token pair 'private-key)) (public (find-sexp-token pair 'public-key)) (data (bytevector->hash-data -- cgit v1.2.3 From 1fda6840a892e55b579dfeb8f4590b5cd2874662 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Thu, 20 Mar 2014 22:33:52 +0100 Subject: pk-crypto: Don't use Ed25519 when libgcrypt is older than 1.6.0. * guix/pk-crypto.scm (gcrypt-version): New procedure. * guix/scripts/archive.scm (%key-generation-parameters): New variable. (%options) : Use it. * tests/pk-crypto.scm ("sign + verify, Ed25519"): Skip if using gcrypt < 1.6.0. --- guix/pk-crypto.scm | 14 +++++++++++++- guix/scripts/archive.scm | 10 ++++++++-- tests/pk-crypto.scm | 2 ++ 3 files changed, 23 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/guix/pk-crypto.scm b/guix/pk-crypto.scm index b9ab02861c..481d3f2463 100644 --- a/guix/pk-crypto.scm +++ b/guix/pk-crypto.scm @@ -24,7 +24,8 @@ #:use-module (system foreign) #:use-module (rnrs bytevectors) #:use-module (ice-9 match) - #:export (canonical-sexp? + #:export (gcrypt-version + canonical-sexp? error-source error-string string->canonical-sexp @@ -86,6 +87,17 @@ "Return a pointer to symbol FUNC in libgcrypt." (dynamic-func func lib)))) +(define gcrypt-version + ;; According to the manual, this function must be called before any other, + ;; and it's not clear whether it can be called more than once. So call it + ;; right here from the top level. + (let* ((ptr (libgcrypt-func "gcry_check_version")) + (proc (pointer->procedure '* ptr '(*))) + (version (pointer->string (proc %null-pointer)))) + (lambda () + "Return the version number of libgcrypt as a string." + version))) + (define finalize-canonical-sexp! (libgcrypt-func "gcry_sexp_release")) diff --git a/guix/scripts/archive.scm b/guix/scripts/archive.scm index 0e67321026..c900fcecb9 100644 --- a/guix/scripts/archive.scm +++ b/guix/scripts/archive.scm @@ -87,6 +87,13 @@ Export/import one or more packages from/to the store.\n")) (newline) (show-bug-report-information)) +(define %key-generation-parameters + ;; Default key generation parameters. We prefer Ed25519, but it was + ;; introduced in libgcrypt 1.6.0. + (if (version>? (gcrypt-version) "1.6.0") + "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))" + "(genkey (rsa (nbits 4:4096)))")) + (define %options ;; Specifications of the command-line options. (cons* (option '(#\h "help") #f #f @@ -114,8 +121,7 @@ Export/import one or more packages from/to the store.\n")) ;; libgcrypt 1.6.0. (let ((params (string->canonical-sexp - (or arg "\ - (genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))) + (or arg %key-generation-parameters)))) (alist-cons 'generate-key params result))) (lambda (key err) (leave (_ "invalid key generation parameters: ~a: ~a~%") diff --git a/tests/pk-crypto.scm b/tests/pk-crypto.scm index 4d498020f5..294c7f3df8 100644 --- a/tests/pk-crypto.scm +++ b/tests/pk-crypto.scm @@ -184,6 +184,8 @@ #:key-type (key-type public)) public))))) +;; Ed25519 appeared in libgcrypt 1.6.0. +(test-skip (if (version>? (gcrypt-version) "1.6.0") 0 1)) (test-assert "sign + verify, Ed25519" (let* ((pair (string->canonical-sexp %ecc-key-pair)) (secret (find-sexp-token pair 'private-key)) -- cgit v1.2.3