common.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* $OpenBSD: common.c,v 1.4 2020/01/26 00:09:50 djm Exp $ */
  2. /*
  3. * Helpers for key API tests
  4. *
  5. * Placed in the public domain
  6. */
  7. #include "includes.h"
  8. #include <sys/types.h>
  9. #include <sys/param.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #ifdef HAVE_STDINT_H
  14. #include <stdint.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #ifdef WITH_OPENSSL
  20. #include <openssl/bn.h>
  21. #include <openssl/rsa.h>
  22. #include <openssl/dsa.h>
  23. #include <openssl/objects.h>
  24. #ifdef OPENSSL_HAS_NISTP256
  25. # include <openssl/ec.h>
  26. #endif /* OPENSSL_HAS_NISTP256 */
  27. #endif /* WITH_OPENSSL */
  28. #include "openbsd-compat/openssl-compat.h"
  29. #include "../test_helper/test_helper.h"
  30. #include "ssherr.h"
  31. #include "authfile.h"
  32. #include "sshkey.h"
  33. #include "sshbuf.h"
  34. #include "common.h"
  35. struct sshbuf *
  36. load_file(const char *name)
  37. {
  38. struct sshbuf *ret = NULL;
  39. ASSERT_INT_EQ(sshbuf_load_file(test_data_file(name), &ret), 0);
  40. ASSERT_PTR_NE(ret, NULL);
  41. return ret;
  42. }
  43. struct sshbuf *
  44. load_text_file(const char *name)
  45. {
  46. struct sshbuf *ret = load_file(name);
  47. const u_char *p;
  48. /* Trim whitespace at EOL */
  49. for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) {
  50. if (p[sshbuf_len(ret) - 1] == '\r' ||
  51. p[sshbuf_len(ret) - 1] == '\t' ||
  52. p[sshbuf_len(ret) - 1] == ' ' ||
  53. p[sshbuf_len(ret) - 1] == '\n')
  54. ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0);
  55. else
  56. break;
  57. }
  58. /* \0 terminate */
  59. ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0);
  60. return ret;
  61. }
  62. #ifdef WITH_OPENSSL
  63. BIGNUM *
  64. load_bignum(const char *name)
  65. {
  66. BIGNUM *ret = NULL;
  67. struct sshbuf *buf;
  68. buf = load_text_file(name);
  69. ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0);
  70. sshbuf_free(buf);
  71. return ret;
  72. }
  73. const BIGNUM *
  74. rsa_n(struct sshkey *k)
  75. {
  76. const BIGNUM *n = NULL;
  77. ASSERT_PTR_NE(k, NULL);
  78. ASSERT_PTR_NE(k->rsa, NULL);
  79. RSA_get0_key(k->rsa, &n, NULL, NULL);
  80. return n;
  81. }
  82. const BIGNUM *
  83. rsa_e(struct sshkey *k)
  84. {
  85. const BIGNUM *e = NULL;
  86. ASSERT_PTR_NE(k, NULL);
  87. ASSERT_PTR_NE(k->rsa, NULL);
  88. RSA_get0_key(k->rsa, NULL, &e, NULL);
  89. return e;
  90. }
  91. const BIGNUM *
  92. rsa_p(struct sshkey *k)
  93. {
  94. const BIGNUM *p = NULL;
  95. ASSERT_PTR_NE(k, NULL);
  96. ASSERT_PTR_NE(k->rsa, NULL);
  97. RSA_get0_factors(k->rsa, &p, NULL);
  98. return p;
  99. }
  100. const BIGNUM *
  101. rsa_q(struct sshkey *k)
  102. {
  103. const BIGNUM *q = NULL;
  104. ASSERT_PTR_NE(k, NULL);
  105. ASSERT_PTR_NE(k->rsa, NULL);
  106. RSA_get0_factors(k->rsa, NULL, &q);
  107. return q;
  108. }
  109. const BIGNUM *
  110. dsa_g(struct sshkey *k)
  111. {
  112. const BIGNUM *g = NULL;
  113. ASSERT_PTR_NE(k, NULL);
  114. ASSERT_PTR_NE(k->dsa, NULL);
  115. DSA_get0_pqg(k->dsa, NULL, NULL, &g);
  116. return g;
  117. }
  118. const BIGNUM *
  119. dsa_pub_key(struct sshkey *k)
  120. {
  121. const BIGNUM *pub_key = NULL;
  122. ASSERT_PTR_NE(k, NULL);
  123. ASSERT_PTR_NE(k->dsa, NULL);
  124. DSA_get0_key(k->dsa, &pub_key, NULL);
  125. return pub_key;
  126. }
  127. const BIGNUM *
  128. dsa_priv_key(struct sshkey *k)
  129. {
  130. const BIGNUM *priv_key = NULL;
  131. ASSERT_PTR_NE(k, NULL);
  132. ASSERT_PTR_NE(k->dsa, NULL);
  133. DSA_get0_key(k->dsa, NULL, &priv_key);
  134. return priv_key;
  135. }
  136. #endif /* WITH_OPENSSL */