1 /* $OpenBSD: common.c,v 1.3 2018/09/13 09:03:20 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 #include <openssl/bn.h> 23 #include <openssl/rsa.h> 24 #include <openssl/dsa.h> 25 #include <openssl/objects.h> 26 #ifdef OPENSSL_HAS_NISTP256 27 # include <openssl/ec.h> 28 #endif 29 30 #include "openbsd-compat/openssl-compat.h" 31 32 #include "../test_helper/test_helper.h" 33 34 #include "ssherr.h" 35 #include "authfile.h" 36 #include "sshkey.h" 37 #include "sshbuf.h" 38 39 #include "common.h" 40 41 struct sshbuf * 42 load_file(const char *name) 43 { 44 int fd; 45 struct sshbuf *ret; 46 47 ASSERT_PTR_NE(ret = sshbuf_new(), NULL); 48 ASSERT_INT_NE(fd = open(test_data_file(name), O_RDONLY), -1); 49 ASSERT_INT_EQ(sshkey_load_file(fd, ret), 0); 50 close(fd); 51 return ret; 52 } 53 54 struct sshbuf * 55 load_text_file(const char *name) 56 { 57 struct sshbuf *ret = load_file(name); 58 const u_char *p; 59 60 /* Trim whitespace at EOL */ 61 for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) { 62 if (p[sshbuf_len(ret) - 1] == '\r' || 63 p[sshbuf_len(ret) - 1] == '\t' || 64 p[sshbuf_len(ret) - 1] == ' ' || 65 p[sshbuf_len(ret) - 1] == '\n') 66 ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0); 67 else 68 break; 69 } 70 /* \0 terminate */ 71 ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0); 72 return ret; 73 } 74 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 164