You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * --
  19. * crypto.c:
  20. * front end to the OpenSSL cryptographic functions
  21. */
  22. #include <openssl/bio.h>
  23. #include <openssl/evp.h>
  24. #include <openssl/crypto.h>
  25. #include <openssl/md5.h>
  26. #include <openssl/x509.h>
  27. #include <openssl/err.h>
  28. #include <openssl/rand.h>
  29. #include <openssl/rsa.h>
  30. #include <openssl/pem.h>
  31. #include "crypto.h"
  32. #include "log.h"
  33. #include "xmalloc.h"
  34. void
  35. init_crypto(void)
  36. {
  37. RAND_load_file("/dev/urandom", 1024);
  38. ERR_load_crypto_strings();
  39. }
  40. void
  41. free_crypto(void)
  42. {
  43. ERR_free_strings();
  44. }
  45. char *
  46. ssl_strerr(void)
  47. {
  48. return ERR_error_string(ERR_get_error(), 0);
  49. }
  50. /*
  51. * genrsa: generates a new rsa key pair and returns the private key in the RSA
  52. * format. If `pub' is not null, it stores in it the pointer to a newly
  53. * allocated dump of the public key that is `*pub_len' bytes. The same is for
  54. * `priv' and `priv_len'.
  55. * On error null is returned.
  56. */
  57. RSA *
  58. genrsa(int key_bits, u_char ** pub, u_int * pub_len, u_char ** priv,
  59. u_int * priv_len)
  60. {
  61. RSA *rsa = 0;
  62. int len;
  63. rsa = RSA_generate_key(key_bits, RSA_F4, NULL, NULL);
  64. if (!rsa) {
  65. debug(DBG_SOFT, "RSA key generation failed");
  66. goto error;
  67. }
  68. if (priv) {
  69. *priv = 0;
  70. len = i2d_RSAPrivateKey(rsa, priv);
  71. if (priv_len)
  72. *priv_len = len;
  73. if (len <= 0) {
  74. debug(DBG_SOFT, "Cannot dump RSA public key: %s",
  75. ssl_strerr());
  76. goto error;
  77. }
  78. }
  79. if (pub) {
  80. *pub = 0;
  81. len = i2d_RSAPublicKey(rsa, pub);
  82. if (pub_len)
  83. *pub_len = len;
  84. if (len <= 0) {
  85. debug(DBG_SOFT, "Cannot dump RSA public key: %s",
  86. ssl_strerr());
  87. goto error;
  88. }
  89. }
  90. return rsa;
  91. error:
  92. if (rsa)
  93. RSA_free(rsa);
  94. return 0;
  95. }
  96. /*
  97. * get_rsa_pub
  98. *
  99. * Converts a dump of a rsa pub key to a RSA structure, which is returned.
  100. * Remeber to RSA_free() the returned key.
  101. */
  102. RSA *
  103. get_rsa_pub(const u_char ** pub_key, long length)
  104. {
  105. return d2i_RSAPublicKey(NULL, pub_key, length);
  106. }
  107. /*
  108. * get_rsa_priv
  109. *
  110. * Converts a dump of a rsa priv key to a RSA structure, which is returned.
  111. * Remeber to RSA_free() the returned key.
  112. */
  113. RSA *
  114. get_rsa_priv(const u_char ** priv_key, long length)
  115. {
  116. return d2i_RSAPrivateKey(NULL, priv_key, length);
  117. }
  118. u_char *
  119. hash_sha1(u_char * msg, u_int m_len, u_char * hash)
  120. {
  121. return SHA1(msg, m_len, hash);
  122. }
  123. u_char *
  124. hash_md5(u_char * msg, u_int m_len, u_char * hash)
  125. {
  126. return MD5(msg, m_len, hash);
  127. }
  128. /*
  129. * rsa_sign: It signs the given message `msg' and returns its newly allocated
  130. * signature. In `siglen' it stores the signature's lenght.
  131. * On error null is returned.
  132. */
  133. u_char *
  134. rsa_sign(u_char * msg, u_int m_len, RSA * priv, u_int * siglen)
  135. {
  136. u_char *signature;
  137. int ret, len;
  138. ret = RSA_size(priv);
  139. if (!ret)
  140. return 0;
  141. signature = (u_char *) xmalloc(ret);
  142. ret = RSA_sign(NID_sha1, hash_sha1(msg, m_len, 0), SHA_DIGEST_LENGTH,
  143. signature, (u_int *) & len, priv);
  144. if (siglen)
  145. *siglen = len;
  146. return !ret ? 0 : signature;
  147. }
  148. /*
  149. * verify_sign: verifies the rsa `signature' of `msg'.
  150. * It returns 1 if the signature is valid, otherwise 0 is returned.
  151. */
  152. int
  153. verify_sign(u_char * msg, u_int m_len, u_char * signature, u_int siglen,
  154. RSA * pub)
  155. {
  156. return RSA_verify(NID_sha1, hash_sha1(msg, m_len, 0),
  157. SHA_DIGEST_LENGTH, signature, siglen, pub);
  158. }