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