R Under development (unstable) (2024-06-14 r86747 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ## simple test suite - avoid testthat! It has an insane amount of > ## unnecessary dependencies. A test package should have exactly 0 > > assert <- function(msg, what) { + cat(" . ", msg,"\n") + stopifnot(what) + .GlobalEnv$ok <- .GlobalEnv$ok + 1L + } > > xfail <- function(...) suppressWarnings(tryCatch({ ...; FALSE }, error=function(e) TRUE)) > > ## none of these are fatal > info <- function(...) message(" -- ", ...) > err <- function(...) message(" ** ERROR: ", ...) > warn <- function(...) message(" !! ", ...) > > ## all warnings (unless suppressed) are errors > options(warn=2) > > library(PKI) Loading required package: base64enc > > .GlobalEnv$ok <- 0L > > ## Majority of tests are in th3 examples, > ## so we won't repeat those, but some special cases > ## not covered there as well as expected failures > > info("Checking failure paths") -- Checking failure paths > xfail(PKI.load.cert(what="foo", file="bar")) ## set both what and file [1] TRUE > xfail(PKI.load.cert("foo", "DER")) ## binary format with text [1] TRUE > xfail(PKI.load.cert("nothing")) ## invalid content [1] TRUE > xfail(PKI.digest("foo", "bar")) ## invalid hash spec [1] TRUE > xfail(PKI.load.key(what="foo", file="bar")) [1] TRUE > xfail(PKI:::PKI.decode.SSH2(fn.priv.der)) ## try to load invalid content [1] TRUE > > info("Checking key paths via files/connections") -- Checking key paths via files/connections > key <- PKI.genRSAkey(bits = 2048L) > fn.priv.pem <- tempfile() > PKI.save.key(key, target=fn.priv.pem) [1] "-----BEGIN RSA PRIVATE KEY-----" [2] "MIIEowIBAAKCAQEApZbrISyy7TDLuMUaWr1ouXEN+HG7uOB7n5Vzl50KRakxt76g" [3]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reYulC2/cTuebTu" [19] "es0n0vypCCZVqy+PPLb2idy+DHTD4DWpSzlj9K8kOcpJWZOHK9BIxTNr0UVoiW89" [20] "z3KLQkVI+P4AVW3GMLLBS3KfTOZ35mXeOHSK0GvBm3hg23wsxDKhKYxRYv9rsYo3" [21] "XLlAPJvHkgY/xb/buPo/bbECgYA06estWd6ZxIZnlimg6fQSVqTdtfc0sNzUP7w0" [22] "OjRW4IgnUAviSvPDxijGGxstr6Q4p10dmXJeT2cThxhO5DUS6C7mmc49dtDvU6Lw" [23] "OmcWbO4n7+HquUn87FnWXQ/LWdEUQIn3ycWTUIqHINy5fzP6ZOU5H34Z9yLlaUzd" [24] "1CZjPQKBgHo8cNLEKXb8jwIlZsx86jPXpsvVuL6dkF6Ln71Emyg9e7MO9w1WTzN3" [25] "hWGs/46ObORfLrUBCVbm/2Lvkn2Sj98EpOQHiqqW5Ufi5nmB8OB3ezd4iT5cZN7d" [26] "Q6ch2vPB9Jz5ZPNnZydl5M06oz+vNoQgLOxXTXMlPIhJqxjguZGX" [27] "-----END RSA PRIVATE KEY-----" > fn.priv.der <- tempfile() > PKI.save.key(key, "DER", target=fn.priv.der) [1] 30 82 04 a3 02 01 00 02 82 01 01 00 a5 96 eb 21 2c b2 ed 30 cb b8 c5 1a [25] 5a bd 68 b9 71 0d f8 71 bb b8 e0 7b 9f 95 73 97 9d 0a 45 a9 31 b7 be a0 [49] fe 35 7f f3 f9 da d2 4d 6e ab ac c8 6d 82 f7 c1 eb 8f b1 22 f5 e1 97 16 [73] 41 fa 52 47 36 02 cc a5 1c 2e c2 35 53 ce 42 1a 77 71 e6 57 9a 40 c0 32 [97] f1 0c 9d 7a 8c c0 85 65 1c d6 96 d3 9b 09 f0 a9 52 95 06 36 9d 78 8a 40 [121] 91 a4 5f fb bb c0 cf 76 cc 4d 21 eb 2a 1b 6c 5c 90 be e3 82 66 d9 21 df [145] 84 63 e9 77 74 ef 92 e9 38 a7 3f b9 6b be 39 1f db 76 a5 c3 7a 91 fb ae [169] e6 6e 81 ca b4 9f 02 3a ee ca cf 67 1b 16 a4 dc cc 18 8a 64 f3 ce 2b d7 [193] 2a 5d 71 a7 76 ea 0c 4f 71 f9 2e 1e df 5b 36 e6 8d e4 ce 0b a3 15 53 cc [217] 2d 61 4b 09 19 72 15 7c c4 01 af 5e ab 34 d9 1f a4 d4 cf 4a df 5c 2e 05 [241] a5 e7 21 44 e5 6b ba 94 0f 51 fa 82 00 52 90 56 8e 71 de fc 43 e8 e4 70 [265] 63 5c 63 1b 02 03 01 00 01 02 82 01 00 0d a4 86 ff c7 36 80 ab 46 05 95 [289] 9b ab c1 32 39 21 12 e5 16 bd c0 28 22 97 4d d1 65 ba e7 07 41 3e da e4 [313] 26 50 7b 68 ec d9 77 89 5f 1e d0 9f 1e 2e 43 9d 95 09 0a e0 2d c0 b3 52 [337] e6 df 0d b8 2c ac cb 03 40 51 37 9b 01 98 c3 84 17 b9 22 95 b7 3e fa b9 [361] bb 18 b8 10 db 71 70 45 2d 44 f8 7b 19 b9 0a d3 1c a8 34 a5 fb e6 f2 70 [385] f5 8b ea 4e 8c 27 76 65 c1 64 68 b0 74 37 16 66 51 a4 0d 4e 22 24 d4 13 [409] e8 c9 0a be a9 af ba 2c 5a 43 fd 72 9a 94 f2 1e c9 63 a0 fb e5 5c 72 16 [433] 41 43 17 65 b9 03 e7 d3 59 03 79 2a 53 c8 2b 4c e9 22 cd 60 7e cd 99 65 [457] e9 e7 3c b1 2a 88 d9 b0 46 06 15 fd 50 91 25 98 e5 e6 6e f2 7c 93 7a 43 [481] 90 dd f5 e6 c9 6d 69 84 61 1c ef 7c ec 9e b0 ca 8a d1 b8 bd 5d 1c 71 8c [505] c2 92 81 8f dd 06 1e 04 9d 78 b5 27 01 dd 16 07 52 a7 32 b1 87 d8 6e 37 [529] 4b dc 95 a4 91 02 81 81 00 d4 de f3 c6 4c 32 40 3d 6e 43 f4 7c fb b7 02 [553] c0 3f 50 8f 64 16 f5 ab dc 8b dd 24 3d 5d 73 a1 ce a2 9e ad d9 c5 25 95 [577] 8b 77 40 3d 05 68 80 95 28 b0 a4 f0 88 3d 21 c6 83 cd 5c 42 a2 00 3b db [601] 48 29 43 63 ac b1 9b 5c ca 88 80 a3 bf 41 e3 e1 2e ca 17 13 ad 53 b0 5a [625] 14 00 c3 14 42 37 65 4c 57 f6 8f a8 d6 7f 82 12 8d cf 73 34 b5 f7 5f a8 [649] a8 61 e5 ee 25 f0 73 9b 1f 60 b2 5e 5b ca 24 4d b1 02 81 81 00 c7 23 9b [673] 51 6d b2 5f d6 22 75 d9 9e 8c a6 b8 c3 9e 9c 2f ca 35 5f a4 fe b1 79 fe [697] 78 76 e8 95 32 59 ca 50 c8 ab ce 57 6b f6 30 1a df 0f 45 66 60 07 b2 76 [721] ea 13 1c 4f 69 12 89 65 76 6d f8 3c ca 20 13 08 cd 2d 4c 13 da 1a 78 49 [745] 35 44 1d 0f 9c ce b4 ca 8b b2 31 0d 69 07 11 4c 75 1b e8 80 9b ea f8 e3 [769] 56 95 7f 78 d7 a8 9d 0d f6 af 59 aa d1 4b 2c a1 c7 3e 52 d0 2d 1c 32 f5 [793] 05 38 12 74 8b 02 81 81 00 bf 40 64 a2 b7 98 ba 50 b6 fd c4 ee 79 b4 ee [817] 7a cd 27 d2 fc a9 08 26 55 ab 2f 8f 3c b6 f6 89 dc be 0c 74 c3 e0 35 a9 [841] 4b 39 63 f4 af 24 39 ca 49 59 93 87 2b d0 48 c5 33 6b d1 45 68 89 6f 3d [865] cf 72 8b 42 45 48 f8 fe 00 55 6d c6 30 b2 c1 4b 72 9f 4c e6 77 e6 65 de [889] 38 74 8a d0 6b c1 9b 78 60 db 7c 2c c4 32 a1 29 8c 51 62 ff 6b b1 8a 37 [913] 5c b9 40 3c 9b c7 92 06 3f c5 bf db b8 fa 3f 6d b1 02 81 80 34 e9 eb 2d [937] 59 de 99 c4 86 67 96 29 a0 e9 f4 12 56 a4 dd b5 f7 34 b0 dc d4 3f bc 34 [961] 3a 34 56 e0 88 27 50 0b e2 4a f3 c3 c6 28 c6 1b 1b 2d af a4 38 a7 5d 1d [985] 99 72 5e 4f 67 13 87 18 4e e4 35 12 e8 2e e6 99 ce 3d 76 d0 ef 53 a2 f0 [1009] 3a 67 16 6c ee 27 ef e1 ea b9 49 fc ec 59 d6 5d 0f cb 59 d1 14 40 89 f7 [1033] c9 c5 93 50 8a 87 20 dc b9 7f 33 fa 64 e5 39 1f 7e 19 f7 22 e5 69 4c dd [1057] d4 26 63 3d 02 81 80 7a 3c 70 d2 c4 29 76 fc 8f 02 25 66 cc 7c ea 33 d7 [1081] a6 cb d5 b8 be 9d 90 5e 8b 9f bd 44 9b 28 3d 7b b3 0e f7 0d 56 4f 33 77 [1105] 85 61 ac ff 8e 8e 6c e4 5f 2e b5 01 09 56 e6 ff 62 ef 92 7d 92 8f df 04 [1129] a4 e4 07 8a aa 96 e5 47 e2 e6 79 81 f0 e0 77 7b 37 78 89 3e 5c 64 de dd [1153] 43 a7 21 da f3 c1 f4 9c f9 64 f3 67 67 27 65 e4 cd 3a a3 3f af 36 84 20 [1177] 2c ec 57 4d 73 25 3c 88 49 ab 18 e0 b9 91 97 attr(,"class") [1] "private.key.DER" > PKI.load.key(file=fn.priv.pem) attr(,"class") [1] "private.key" > PKI.load.key(file=fn.priv.der, format="DER", private=TRUE) attr(,"class") [1] "private.key" > fn.pub.pem <- tempfile() > PKI.save.key(key, target=fn.pub.pem, private=FALSE) [1] "-----BEGIN PUBLIC KEY-----" [2] "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApZbrISyy7TDLuMUaWr1o" [3] "uXEN+HG7uOB7n5Vzl50KRakxt76g/jV/8/na0k1uq6zIbYL3weuPsSL14ZcWQfpS" [4] "RzYCzKUcLsI1U85CGndx5leaQMAy8QydeozAhWUc1pbTmwnwqVKVBjadeIpAkaRf" [5] "+7vAz3bMTSHrKhtsXJC+44Jm2SHfhGPpd3Tvkuk4pz+5a745H9t2pcN6kfuu5m6B" [6] "yrSfAjruys9nGxak3MwYimTzzivXKl1xp3bqDE9x+S4e31s25o3kzgujFVPMLWFL" [7] "CRlyFXzEAa9eqzTZH6TUz0rfXC4FpechROVrupQPUfqCAFKQVo5x3vxD6ORwY1xj" [8] "GwIDAQAB" [9] "-----END PUBLIC KEY-----" > fn.pub.der <- tempfile() > PKI.save.key(key, "DER", target=fn.pub.der, private=FALSE) [1] 30 82 01 22 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03 82 01 0f 00 30 [26] 82 01 0a 02 82 01 01 00 a5 96 eb 21 2c b2 ed 30 cb b8 c5 1a 5a bd 68 b9 71 [51] 0d f8 71 bb b8 e0 7b 9f 95 73 97 9d 0a 45 a9 31 b7 be a0 fe 35 7f f3 f9 da [76] d2 4d 6e ab ac c8 6d 82 f7 c1 eb 8f b1 22 f5 e1 97 16 41 fa 52 47 36 02 cc [101] a5 1c 2e c2 35 53 ce 42 1a 77 71 e6 57 9a 40 c0 32 f1 0c 9d 7a 8c c0 85 65 [126] 1c d6 96 d3 9b 09 f0 a9 52 95 06 36 9d 78 8a 40 91 a4 5f fb bb c0 cf 76 cc [151] 4d 21 eb 2a 1b 6c 5c 90 be e3 82 66 d9 21 df 84 63 e9 77 74 ef 92 e9 38 a7 [176] 3f b9 6b be 39 1f db 76 a5 c3 7a 91 fb ae e6 6e 81 ca b4 9f 02 3a ee ca cf [201] 67 1b 16 a4 dc cc 18 8a 64 f3 ce 2b d7 2a 5d 71 a7 76 ea 0c 4f 71 f9 2e 1e [226] df 5b 36 e6 8d e4 ce 0b a3 15 53 cc 2d 61 4b 09 19 72 15 7c c4 01 af 5e ab [251] 34 d9 1f a4 d4 cf 4a df 5c 2e 05 a5 e7 21 44 e5 6b ba 94 0f 51 fa 82 00 52 [276] 90 56 8e 71 de fc 43 e8 e4 70 63 5c 63 1b 02 03 01 00 01 attr(,"class") [1] "public.key.DER" > PKI.load.key(file=fn.pub.pem) attr(,"class") [1] "public.key" > PKI.load.key(file=fn.pub.der, format="DER", private=FALSE) attr(,"class") [1] "public.key" > > info("gmp") -- gmp > if (requireNamespace("gmp", quietly=TRUE)) { + PKI.mkRSApubkey(gmp::as.bigz("119445732379544598056145200053932732877863846799652384989588303737527328743970559883211146487286317168142202446955508902936035124709397221178664495721428029984726868375359168203283442617134197706515425366188396513684446494070223079865755643116690165578452542158755074958452695530623055205290232290667934914919")) + } else { + warn("gmp not found, skipping bignum tests") + } !! gmp not found, skipping bignum tests > > info("Ciphers") -- Ciphers > skey <- PKI.random(256) > for (cipher in c("aes256ecb", "aes256ofb", "bfcbc", "bfecb", "bfofb", "bfcfb")) + assert(cipher, all(PKI.decrypt(PKI.encrypt(charToRaw("foo!"), skey, cipher), skey, cipher)[1:4] == charToRaw("foo!"))) . aes256ecb . aes256ofb . bfcbc . bfecb . bfofb . bfcfb > iv <- PKI.random(256) > for (cipher in c("bfcbc", "bfecb", "bfofb", "bfcfb")) + assert(paste0(cipher, " (with IV)"), + all(PKI.decrypt(PKI.encrypt(charToRaw("foo!"), skey, cipher, iv=iv), skey, cipher, iv=iv)[1:4] == charToRaw("foo!"))) . bfcbc (with IV) . bfecb (with IV) . bfofb (with IV) . bfcfb (with IV) > > info("ASN.1") -- ASN.1 > > assert("ASN.1 encode/decode", + { d <- ASN1.decode(ASN1.encode(ASN1.item(0:255, 3L))) + ASN1.type(d) == 3L && all(d == as.raw(0:255)) }) . ASN.1 encode/decode > > info("Tar ball signing") -- Tar ball signing > tmpfn <- c(fn.pub.der, fn.pub.pem, fn.priv.der, fn.priv.pem) > fn <- tempfile() > ## on some systems using abs paths can break 100 byte limit > ## so we must do this in the tempdir > wd <- getwd() > td <- tempdir() > setwd(td) > tar(fn, basename(tmpfn), "none") > PKI.sign.tar(fn, key) > PKI.verify.tar(fn, key) [1] TRUE > setwd(wd) > > unlink(c(fn, tmpfn)) > > proc.time() user system elapsed 0.39 0.07 0.50