From 2abd91d9ff1fd7511532fadc5ef1743b096ebb8d Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Wed, 16 Dec 2020 20:09:52 +0100 Subject: [PATCH 1/2] Add PKey sign test case Signed-off-by: Patrick Uiterwijk --- src/tests/mod.rs | 1 + src/tests/pkey.rs | 65 ++++++++++++++++++++++++++++++++++++++++++ src/tests/privkey.pk8 | Bin 0 -> 1217 bytes 3 files changed, 66 insertions(+) create mode 100644 src/tests/pkey.rs create mode 100644 src/tests/privkey.pk8 diff --git a/src/tests/mod.rs b/src/tests/mod.rs index d03ded6..462e313 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -37,6 +37,7 @@ mod keytype; mod link; mod newring; mod permitting; +mod pkey; mod reading; mod revoke; mod search; diff --git a/src/tests/pkey.rs b/src/tests/pkey.rs new file mode 100644 index 0000000..187d8ae --- /dev/null +++ b/src/tests/pkey.rs @@ -0,0 +1,65 @@ +// Copyright (c) 2020, Patrick Uiterwijk +// Copyright (c) 2019, Ben Boeckel +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of this project nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +use crate::{keytypes::Asymmetric, KeyctlEncoding, KeyctlHash, PublicKeyOptions}; + +use super::utils; + +#[test] +fn sign() { + let mut keyring = utils::new_test_keyring(); + let payload = include_bytes!("privkey.pk8"); + let key = keyring + .add_key::("sign_key", &payload[..]) + .unwrap(); + + let expected_signature = &[ + 155, 73, 224, 97, 40, 120, 205, 218, 243, 105, 36, 130, 122, 189, 13, 99, 211, 12, 210, 23, + 14, 8, 71, 162, 133, 65, 104, 241, 9, 94, 240, 85, 41, 170, 246, 111, 129, 107, 21, 133, + 24, 231, 144, 71, 195, 171, 162, 80, 143, 159, 143, 99, 195, 255, 18, 74, 32, 87, 66, 176, + 181, 127, 207, 146, 223, 195, 45, 80, 96, 2, 77, 71, 63, 116, 227, 11, 196, 151, 40, 158, + 215, 211, 53, 14, 132, 189, 201, 52, 25, 120, 159, 221, 42, 35, 224, 12, 109, 205, 225, + 159, 100, 230, 155, 59, 179, 73, 168, 68, 27, 172, 65, 220, 189, 177, 53, 46, 29, 70, 62, + 219, 210, 243, 36, 7, 149, 61, 154, 100, 227, 169, 97, 127, 187, 182, 187, 112, 170, 26, + 11, 81, 247, 111, 210, 128, 48, 85, 237, 84, 231, 171, 106, 197, 216, 16, 10, 169, 198, + 131, 68, 22, 229, 136, 180, 94, 180, 97, 136, 87, 50, 253, 150, 96, 185, 248, 42, 164, 1, + 200, 156, 250, 244, 160, 221, 231, 148, 108, 12, 114, 200, 244, 159, 219, 175, 19, 117, + 171, 252, 150, 192, 237, 124, 26, 167, 193, 43, 113, 102, 171, 13, 255, 26, 177, 83, 254, + 235, 242, 251, 249, 90, 58, 5, 79, 167, 76, 131, 236, 167, 40, 35, 140, 44, 179, 225, 94, + 108, 253, 162, 202, 227, 205, 11, 110, 46, 240, 83, 56, 108, 111, 137, 251, 86, 130, 190, + 1, 172, 109, + ]; + let digest = &[0x1, 0x2, 0x3, 0x4]; + let options = PublicKeyOptions { + encoding: Some(KeyctlEncoding::RsassaPkcs1V15), + hash: Some(KeyctlHash::Sha256), + }; + + let signature = key.sign(&options, digest).unwrap(); + + assert_eq!(signature.to_vec(), expected_signature.to_vec()); +} diff --git a/src/tests/privkey.pk8 b/src/tests/privkey.pk8 new file mode 100644 index 0000000000000000000000000000000000000000..1bcd1c1c5c39bfee81d8f46179d961e21fbe702a GIT binary patch literal 1217 zcmV;y1U~yPf&{$+0RS)!1_>&LNQUrr!ay9qXGc{0)hbn0LJk%#Z^x` zChQk${W=$Ehn&Vdo%=4PWfRCN%_uu zg7CU zv*l>dHjj9noWd-M752C!3f~*Y<>w&Tfktl^EL|V!AUG3k&&>>%aWT@Y6MSN5&H&pt z*5GCv&hg*!v{&ggQ+PBM3lh_^zEs-9uNjU}-2cx+wj66t!tr1fz79noh@k?3fdKR$ zqz$n`3f(BM3rJV&03sbWe46(~R;ZzpWFm z?2|Bvf*GH^1vyfS+fBZk1}{J6+|^HWcSu=(+NhSUeB7i2SL$z{>3s2_#Iwr@G&WpD zIK_qL09OxSHfK*r)05D)hn^qPbeRHyfdJ5+-8Ls(KqNImnyO-nPbkNhyd!s)>n(c{ zRTsIG>lz3}6`f>v^yZD{MrKkREAmT0Qtyc{4j9h`B!0-;#waN$P>PmA=0+wx*M}`r zse0Mc12N+5R(13=-x@M#{HzDlqP!{LKjtxW8u2zGiH`57#X84xd~!5N>n-qqOn511*VW%J|#8LE=wV8@kQ0_K!58vUFRC`vF02;rshNIRxhwDFT6k0FGYlPD#A%$_WBBfAjGQ@uZ9F=QDMj z%#;D;%48o2_Tdlgc~%%ZETBl{6>Jg5xB0!H(9p@(#Qb{T_8BwzlWfZqbP%dj+bO%~ zds^m>am9A}F)&e*K)WE_EXOcv8@X)oXgJbJZ! zy8?lL4@IWYxr%vh2{NUcJB8twAsEI>V($@E8hqm+36@cs$Uyt#x6=<*{TG=U#n$1S zr00Bz$>aBxaodKW;}ru)1j-f~>ED1kt~45ji96DulVK=IO_M#5STjG|X} Date: Wed, 16 Dec 2020 20:11:29 +0100 Subject: [PATCH 2/2] Fix Key.sign by setting buffer length The truncate() method doesn't do anything if the "len" is greater than the current length, which is the case here. So instead, use the Vec.set_len method. Signed-off-by: Patrick Uiterwijk --- src/api.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index 39ca0d0..3ecd46c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -935,7 +935,9 @@ impl Key { let mut buffer = Vec::with_capacity(support.max_sig_size as usize); let write_buffer = buffer.get_backing_buffer(); let sz = keyctl_pkey_sign(self.id, &info, data, write_buffer)?; - buffer.truncate(sz); + unsafe { + buffer.set_len(sz); + } Ok(buffer) }