1 /* $OpenBSD: common.c,v 1.2 2015/01/08 13:10:58 djm Exp $ */ 2 /* 3 * Helpers for key API tests 4 * 5 * Placed in the public domain 6 */ 7 8 #include <sys/types.h> 9 #include <sys/param.h> 10 #include <sys/stat.h> 11 #include <fcntl.h> 12 #include <stdio.h> 13 #include <stdint.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <unistd.h> 17 18 #include <openssl/bn.h> 19 #include <openssl/ec.h> 20 #include <openssl/rsa.h> 21 #include <openssl/dsa.h> 22 #include <openssl/objects.h> 23 24 #include "test_helper.h" 25 26 #include "ssherr.h" 27 #include "authfile.h" 28 #include "sshkey.h" 29 #include "sshbuf.h" 30 31 #include "common.h" 32 33 struct sshbuf * 34 load_file(const char *name) 35 { 36 int fd; 37 struct sshbuf *ret; 38 39 ASSERT_PTR_NE(ret = sshbuf_new(), NULL); 40 ASSERT_INT_NE(fd = open(test_data_file(name), O_RDONLY), -1); 41 ASSERT_INT_EQ(sshkey_load_file(fd, ret), 0); 42 close(fd); 43 return ret; 44 } 45 46 struct sshbuf * 47 load_text_file(const char *name) 48 { 49 struct sshbuf *ret = load_file(name); 50 const u_char *p; 51 52 /* Trim whitespace at EOL */ 53 for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) { 54 if (p[sshbuf_len(ret) - 1] == '\r' || 55 p[sshbuf_len(ret) - 1] == '\t' || 56 p[sshbuf_len(ret) - 1] == ' ' || 57 p[sshbuf_len(ret) - 1] == '\n') 58 ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0); 59 else 60 break; 61 } 62 /* \0 terminate */ 63 ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0); 64 return ret; 65 } 66 67 BIGNUM * 68 load_bignum(const char *name) 69 { 70 BIGNUM *ret = NULL; 71 struct sshbuf *buf; 72 73 buf = load_text_file(name); 74 ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0); 75 sshbuf_free(buf); 76 return ret; 77 } 78 79