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