1*19261079SEd Maste /* $OpenBSD: common.c,v 1.4 2020/01/26 00:09:50 djm Exp $ */ 2a0ee8cc6SDag-Erling Smørgrav /* 3a0ee8cc6SDag-Erling Smørgrav * Helpers for key API tests 4a0ee8cc6SDag-Erling Smørgrav * 5a0ee8cc6SDag-Erling Smørgrav * Placed in the public domain 6a0ee8cc6SDag-Erling Smørgrav */ 7a0ee8cc6SDag-Erling Smørgrav 8a0ee8cc6SDag-Erling Smørgrav #include "includes.h" 9a0ee8cc6SDag-Erling Smørgrav 10a0ee8cc6SDag-Erling Smørgrav #include <sys/types.h> 11a0ee8cc6SDag-Erling Smørgrav #include <sys/param.h> 12a0ee8cc6SDag-Erling Smørgrav #include <sys/stat.h> 13a0ee8cc6SDag-Erling Smørgrav #include <fcntl.h> 14a0ee8cc6SDag-Erling Smørgrav #include <stdio.h> 15a0ee8cc6SDag-Erling Smørgrav #ifdef HAVE_STDINT_H 16a0ee8cc6SDag-Erling Smørgrav #include <stdint.h> 17a0ee8cc6SDag-Erling Smørgrav #endif 18a0ee8cc6SDag-Erling Smørgrav #include <stdlib.h> 19a0ee8cc6SDag-Erling Smørgrav #include <string.h> 20a0ee8cc6SDag-Erling Smørgrav #include <unistd.h> 21a0ee8cc6SDag-Erling Smørgrav 22*19261079SEd Maste #ifdef WITH_OPENSSL 23a0ee8cc6SDag-Erling Smørgrav #include <openssl/bn.h> 24a0ee8cc6SDag-Erling Smørgrav #include <openssl/rsa.h> 25a0ee8cc6SDag-Erling Smørgrav #include <openssl/dsa.h> 26a0ee8cc6SDag-Erling Smørgrav #include <openssl/objects.h> 27a0ee8cc6SDag-Erling Smørgrav #ifdef OPENSSL_HAS_NISTP256 28a0ee8cc6SDag-Erling Smørgrav # include <openssl/ec.h> 29*19261079SEd Maste #endif /* OPENSSL_HAS_NISTP256 */ 30*19261079SEd Maste #endif /* WITH_OPENSSL */ 31a0ee8cc6SDag-Erling Smørgrav 322f513db7SEd Maste #include "openbsd-compat/openssl-compat.h" 332f513db7SEd Maste 34a0ee8cc6SDag-Erling Smørgrav #include "../test_helper/test_helper.h" 35a0ee8cc6SDag-Erling Smørgrav 36a0ee8cc6SDag-Erling Smørgrav #include "ssherr.h" 37a0ee8cc6SDag-Erling Smørgrav #include "authfile.h" 38a0ee8cc6SDag-Erling Smørgrav #include "sshkey.h" 39a0ee8cc6SDag-Erling Smørgrav #include "sshbuf.h" 40a0ee8cc6SDag-Erling Smørgrav 41a0ee8cc6SDag-Erling Smørgrav #include "common.h" 42a0ee8cc6SDag-Erling Smørgrav 43a0ee8cc6SDag-Erling Smørgrav struct sshbuf * 44a0ee8cc6SDag-Erling Smørgrav load_file(const char *name) 45a0ee8cc6SDag-Erling Smørgrav { 46*19261079SEd Maste struct sshbuf *ret = NULL; 47a0ee8cc6SDag-Erling Smørgrav 48*19261079SEd Maste ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0); 49*19261079SEd Maste ASSERT_PTR_NE(ret, NULL); 50a0ee8cc6SDag-Erling Smørgrav return ret; 51a0ee8cc6SDag-Erling Smørgrav } 52a0ee8cc6SDag-Erling Smørgrav 53a0ee8cc6SDag-Erling Smørgrav struct sshbuf * 54a0ee8cc6SDag-Erling Smørgrav load_text_file(const char *name) 55a0ee8cc6SDag-Erling Smørgrav { 56a0ee8cc6SDag-Erling Smørgrav struct sshbuf *ret = load_file(name); 57a0ee8cc6SDag-Erling Smørgrav const u_char *p; 58a0ee8cc6SDag-Erling Smørgrav 59a0ee8cc6SDag-Erling Smørgrav /* Trim whitespace at EOL */ 60a0ee8cc6SDag-Erling Smørgrav for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) { 61a0ee8cc6SDag-Erling Smørgrav if (p[sshbuf_len(ret) - 1] == '\r' || 62a0ee8cc6SDag-Erling Smørgrav p[sshbuf_len(ret) - 1] == '\t' || 63a0ee8cc6SDag-Erling Smørgrav p[sshbuf_len(ret) - 1] == ' ' || 64a0ee8cc6SDag-Erling Smørgrav p[sshbuf_len(ret) - 1] == '\n') 65a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0); 66a0ee8cc6SDag-Erling Smørgrav else 67a0ee8cc6SDag-Erling Smørgrav break; 68a0ee8cc6SDag-Erling Smørgrav } 69a0ee8cc6SDag-Erling Smørgrav /* \0 terminate */ 70a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0); 71a0ee8cc6SDag-Erling Smørgrav return ret; 72a0ee8cc6SDag-Erling Smørgrav } 73a0ee8cc6SDag-Erling Smørgrav 74*19261079SEd Maste #ifdef WITH_OPENSSL 75a0ee8cc6SDag-Erling Smørgrav BIGNUM * 76a0ee8cc6SDag-Erling Smørgrav load_bignum(const char *name) 77a0ee8cc6SDag-Erling Smørgrav { 78a0ee8cc6SDag-Erling Smørgrav BIGNUM *ret = NULL; 79a0ee8cc6SDag-Erling Smørgrav struct sshbuf *buf; 80a0ee8cc6SDag-Erling Smørgrav 81a0ee8cc6SDag-Erling Smørgrav buf = load_text_file(name); 82a0ee8cc6SDag-Erling Smørgrav ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0); 83a0ee8cc6SDag-Erling Smørgrav sshbuf_free(buf); 84a0ee8cc6SDag-Erling Smørgrav return ret; 85a0ee8cc6SDag-Erling Smørgrav } 86a0ee8cc6SDag-Erling Smørgrav 872a01feabSEd Maste const BIGNUM * 882a01feabSEd Maste rsa_n(struct sshkey *k) 892a01feabSEd Maste { 902a01feabSEd Maste const BIGNUM *n = NULL; 912a01feabSEd Maste 922a01feabSEd Maste ASSERT_PTR_NE(k, NULL); 932a01feabSEd Maste ASSERT_PTR_NE(k->rsa, NULL); 942a01feabSEd Maste RSA_get0_key(k->rsa, &n, NULL, NULL); 952a01feabSEd Maste return n; 962a01feabSEd Maste } 972a01feabSEd Maste 982a01feabSEd Maste const BIGNUM * 992a01feabSEd Maste rsa_e(struct sshkey *k) 1002a01feabSEd Maste { 1012a01feabSEd Maste const BIGNUM *e = NULL; 1022a01feabSEd Maste 1032a01feabSEd Maste ASSERT_PTR_NE(k, NULL); 1042a01feabSEd Maste ASSERT_PTR_NE(k->rsa, NULL); 1052a01feabSEd Maste RSA_get0_key(k->rsa, NULL, &e, NULL); 1062a01feabSEd Maste return e; 1072a01feabSEd Maste } 1082a01feabSEd Maste 1092a01feabSEd Maste const BIGNUM * 1102a01feabSEd Maste rsa_p(struct sshkey *k) 1112a01feabSEd Maste { 1122a01feabSEd Maste const BIGNUM *p = NULL; 1132a01feabSEd Maste 1142a01feabSEd Maste ASSERT_PTR_NE(k, NULL); 1152a01feabSEd Maste ASSERT_PTR_NE(k->rsa, NULL); 1162a01feabSEd Maste RSA_get0_factors(k->rsa, &p, NULL); 1172a01feabSEd Maste return p; 1182a01feabSEd Maste } 1192a01feabSEd Maste 1202a01feabSEd Maste const BIGNUM * 1212a01feabSEd Maste rsa_q(struct sshkey *k) 1222a01feabSEd Maste { 1232a01feabSEd Maste const BIGNUM *q = NULL; 1242a01feabSEd Maste 1252a01feabSEd Maste ASSERT_PTR_NE(k, NULL); 1262a01feabSEd Maste ASSERT_PTR_NE(k->rsa, NULL); 1272a01feabSEd Maste RSA_get0_factors(k->rsa, NULL, &q); 1282a01feabSEd Maste return q; 1292a01feabSEd Maste } 1302a01feabSEd Maste 1312a01feabSEd Maste const BIGNUM * 1322a01feabSEd Maste dsa_g(struct sshkey *k) 1332a01feabSEd Maste { 1342a01feabSEd Maste const BIGNUM *g = NULL; 1352a01feabSEd Maste 1362a01feabSEd Maste ASSERT_PTR_NE(k, NULL); 1372a01feabSEd Maste ASSERT_PTR_NE(k->dsa, NULL); 1382a01feabSEd Maste DSA_get0_pqg(k->dsa, NULL, NULL, &g); 1392a01feabSEd Maste return g; 1402a01feabSEd Maste } 1412a01feabSEd Maste 1422a01feabSEd Maste const BIGNUM * 1432a01feabSEd Maste dsa_pub_key(struct sshkey *k) 1442a01feabSEd Maste { 1452a01feabSEd Maste const BIGNUM *pub_key = NULL; 1462a01feabSEd Maste 1472a01feabSEd Maste ASSERT_PTR_NE(k, NULL); 1482a01feabSEd Maste ASSERT_PTR_NE(k->dsa, NULL); 1492a01feabSEd Maste DSA_get0_key(k->dsa, &pub_key, NULL); 1502a01feabSEd Maste return pub_key; 1512a01feabSEd Maste } 1522a01feabSEd Maste 1532a01feabSEd Maste const BIGNUM * 1542a01feabSEd Maste dsa_priv_key(struct sshkey *k) 1552a01feabSEd Maste { 1562a01feabSEd Maste const BIGNUM *priv_key = NULL; 1572a01feabSEd Maste 1582a01feabSEd Maste ASSERT_PTR_NE(k, NULL); 1592a01feabSEd Maste ASSERT_PTR_NE(k->dsa, NULL); 1602a01feabSEd Maste DSA_get0_key(k->dsa, NULL, &priv_key); 1612a01feabSEd Maste return priv_key; 1622a01feabSEd Maste } 163*19261079SEd Maste #endif /* WITH_OPENSSL */ 1642a01feabSEd Maste 165