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