1 /* $OpenBSD: common.c,v 1.3 2018/09/13 09:03:20 djm Exp $ */
3 * Helpers for key API tests
5 * Placed in the public domain
10 #include <sys/types.h>
11 #include <sys/param.h>
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>
30 #include "../test_helper/test_helper.h"
40 load_file(const char *name)
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);
53 load_text_file(const char *name)
55 struct sshbuf *ret = load_file(name);
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);
69 ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0);
74 load_bignum(const char *name)
79 buf = load_text_file(name);
80 ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0);
86 rsa_n(struct sshkey *k)
88 const BIGNUM *n = NULL;
90 ASSERT_PTR_NE(k, NULL);
91 ASSERT_PTR_NE(k->rsa, NULL);
92 RSA_get0_key(k->rsa, &n, NULL, NULL);
97 rsa_e(struct sshkey *k)
99 const BIGNUM *e = NULL;
101 ASSERT_PTR_NE(k, NULL);
102 ASSERT_PTR_NE(k->rsa, NULL);
103 RSA_get0_key(k->rsa, NULL, &e, NULL);
108 rsa_p(struct sshkey *k)
110 const BIGNUM *p = NULL;
112 ASSERT_PTR_NE(k, NULL);
113 ASSERT_PTR_NE(k->rsa, NULL);
114 RSA_get0_factors(k->rsa, &p, NULL);
119 rsa_q(struct sshkey *k)
121 const BIGNUM *q = NULL;
123 ASSERT_PTR_NE(k, NULL);
124 ASSERT_PTR_NE(k->rsa, NULL);
125 RSA_get0_factors(k->rsa, NULL, &q);
130 dsa_g(struct sshkey *k)
132 const BIGNUM *g = NULL;
134 ASSERT_PTR_NE(k, NULL);
135 ASSERT_PTR_NE(k->dsa, NULL);
136 DSA_get0_pqg(k->dsa, NULL, NULL, &g);
141 dsa_pub_key(struct sshkey *k)
143 const BIGNUM *pub_key = NULL;
145 ASSERT_PTR_NE(k, NULL);
146 ASSERT_PTR_NE(k->dsa, NULL);
147 DSA_get0_key(k->dsa, &pub_key, NULL);
152 dsa_priv_key(struct sshkey *k)
154 const BIGNUM *priv_key = NULL;
156 ASSERT_PTR_NE(k, NULL);
157 ASSERT_PTR_NE(k->dsa, NULL);
158 DSA_get0_key(k->dsa, NULL, &priv_key);