1 /* $OpenBSD: common.c,v 1.6 2024/08/15 00:52:23 djm Exp $ */ 2 /* 3 * Helpers for key API tests 4 * 5 * Placed in the public domain 6 */ 7 8 #include <sys/types.h> 9 #include <sys/stat.h> 10 #include <fcntl.h> 11 #include <stdio.h> 12 #include <stdint.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <unistd.h> 16 17 #include <openssl/bn.h> 18 #include <openssl/ec.h> 19 #include <openssl/rsa.h> 20 #include <openssl/dsa.h> 21 #include <openssl/objects.h> 22 23 #include "test_helper.h" 24 25 #include "ssherr.h" 26 #include "authfile.h" 27 #include "sshkey.h" 28 #include "sshbuf.h" 29 30 #include "common.h" 31 32 struct sshbuf * 33 load_file(const char *name) 34 { 35 struct sshbuf *ret = NULL; 36 37 ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0); 38 ASSERT_PTR_NE(ret, NULL); 39 return ret; 40 } 41 42 struct sshbuf * 43 load_text_file(const char *name) 44 { 45 struct sshbuf *ret = load_file(name); 46 const u_char *p; 47 48 /* Trim whitespace at EOL */ 49 for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) { 50 if (p[sshbuf_len(ret) - 1] == '\r' || 51 p[sshbuf_len(ret) - 1] == '\t' || 52 p[sshbuf_len(ret) - 1] == ' ' || 53 p[sshbuf_len(ret) - 1] == '\n') 54 ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0); 55 else 56 break; 57 } 58 /* \0 terminate */ 59 ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0); 60 return ret; 61 } 62 63 BIGNUM * 64 load_bignum(const char *name) 65 { 66 BIGNUM *ret = NULL; 67 struct sshbuf *buf; 68 69 buf = load_text_file(name); 70 ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0); 71 sshbuf_free(buf); 72 return ret; 73 } 74 75 const BIGNUM * 76 rsa_n(struct sshkey *k) 77 { 78 const BIGNUM *n = NULL; 79 80 ASSERT_PTR_NE(k, NULL); 81 ASSERT_PTR_NE(k->pkey, NULL); 82 RSA_get0_key(EVP_PKEY_get0_RSA(k->pkey), &n, NULL, NULL); 83 return n; 84 } 85 86 const BIGNUM * 87 rsa_e(struct sshkey *k) 88 { 89 const BIGNUM *e = NULL; 90 91 ASSERT_PTR_NE(k, NULL); 92 ASSERT_PTR_NE(k->pkey, NULL); 93 RSA_get0_key(EVP_PKEY_get0_RSA(k->pkey), NULL, &e, NULL); 94 return e; 95 } 96 97 const BIGNUM * 98 rsa_p(struct sshkey *k) 99 { 100 const BIGNUM *p = NULL; 101 102 ASSERT_PTR_NE(k, NULL); 103 ASSERT_PTR_NE(EVP_PKEY_get0_RSA(k->pkey), NULL); 104 RSA_get0_factors(EVP_PKEY_get0_RSA(k->pkey), &p, NULL); 105 return p; 106 } 107 108 const BIGNUM * 109 rsa_q(struct sshkey *k) 110 { 111 const BIGNUM *q = NULL; 112 113 ASSERT_PTR_NE(k, NULL); 114 ASSERT_PTR_NE(EVP_PKEY_get0_RSA(k->pkey), NULL); 115 RSA_get0_factors(EVP_PKEY_get0_RSA(k->pkey), NULL, &q); 116 return q; 117 } 118 119 const BIGNUM * 120 dsa_g(struct sshkey *k) 121 { 122 const BIGNUM *g = NULL; 123 124 ASSERT_PTR_NE(k, NULL); 125 ASSERT_PTR_NE(k->dsa, NULL); 126 DSA_get0_pqg(k->dsa, NULL, NULL, &g); 127 return g; 128 } 129 130 const BIGNUM * 131 dsa_pub_key(struct sshkey *k) 132 { 133 const BIGNUM *pub_key = NULL; 134 135 ASSERT_PTR_NE(k, NULL); 136 ASSERT_PTR_NE(k->dsa, NULL); 137 DSA_get0_key(k->dsa, &pub_key, NULL); 138 return pub_key; 139 } 140 141 const BIGNUM * 142 dsa_priv_key(struct sshkey *k) 143 { 144 const BIGNUM *priv_key = NULL; 145 146 ASSERT_PTR_NE(k, NULL); 147 ASSERT_PTR_NE(k->dsa, NULL); 148 DSA_get0_key(k->dsa, NULL, &priv_key); 149 return priv_key; 150 } 151 152