crypt.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // This file is part of BOINC.
  2. // http://boinc.berkeley.edu
  3. // Copyright (C) 2008 University of California
  4. //
  5. // BOINC is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // BOINC is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef BOINC_CRYPT_H
  18. #define BOINC_CRYPT_H
  19. // We're set up to use either RSAEuro or the OpenSSL crypto library.
  20. // We use our own data structures (R_RSA_PUBLIC_KEY and R_RSA_PRIVATE_KEY)
  21. // to store keys in either case.
  22. #include <cstdio>
  23. #include <openssl/rsa.h>
  24. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) /* OpenSSL 1.1.0+ */
  25. #define HAVE_OPAQUE_EVP_PKEY 1 /* since 1.1.0 -pre3 */
  26. #define HAVE_OPAQUE_RSA_DSA_DH 1 /* since 1.1.0 -pre5 */
  27. #endif
  28. #define MAX_RSA_MODULUS_BITS 1024
  29. #define MAX_RSA_MODULUS_LEN ((MAX_RSA_MODULUS_BITS + 7) / 8)
  30. #define MAX_RSA_PRIME_BITS ((MAX_RSA_MODULUS_BITS + 1) / 2)
  31. #define MAX_RSA_PRIME_LEN ((MAX_RSA_PRIME_BITS + 7) / 8)
  32. typedef struct {
  33. unsigned short int bits; /* length in bits of modulus */
  34. unsigned char modulus[MAX_RSA_MODULUS_LEN]; /* modulus */
  35. unsigned char exponent[MAX_RSA_MODULUS_LEN]; /* public exponent */
  36. } R_RSA_PUBLIC_KEY;
  37. typedef struct {
  38. unsigned short int bits; /* length in bits of modulus */
  39. unsigned char modulus[MAX_RSA_MODULUS_LEN]; /* modulus */
  40. unsigned char publicExponent[MAX_RSA_MODULUS_LEN]; /* public exponent */
  41. unsigned char exponent[MAX_RSA_MODULUS_LEN]; /* private exponent */
  42. unsigned char prime[2][MAX_RSA_PRIME_LEN]; /* prime factors */
  43. unsigned char primeExponent[2][MAX_RSA_PRIME_LEN]; /* exponents for CRT */
  44. unsigned char coefficient[MAX_RSA_PRIME_LEN]; /* CRT coefficient */
  45. } R_RSA_PRIVATE_KEY;
  46. // functions to convert between OpenSSL's keys (using BIGNUMs)
  47. // and our binary format
  48. extern void openssl_to_keys(
  49. RSA* rp, int nbits, R_RSA_PRIVATE_KEY& priv, R_RSA_PUBLIC_KEY& pub
  50. );
  51. extern void private_to_openssl(R_RSA_PRIVATE_KEY& priv, RSA* rp);
  52. extern void public_to_openssl(R_RSA_PUBLIC_KEY& pub, RSA* rp);
  53. extern int openssl_to_private(RSA *from, R_RSA_PRIVATE_KEY *to);
  54. struct KEY {
  55. unsigned short int bits;
  56. unsigned char data[1];
  57. };
  58. struct DATA_BLOCK {
  59. unsigned char* data;
  60. unsigned int len;
  61. };
  62. #define MIN_OUT_BUFFER_SIZE (MAX_RSA_MODULUS_LEN+1)
  63. // the size of a binary signature (encrypted MD5)
  64. //
  65. #define SIGNATURE_SIZE_BINARY MIN_OUT_BUFFER_SIZE
  66. // size of text-encoded signature
  67. #define SIGNATURE_SIZE_TEXT (SIGNATURE_SIZE_BINARY*2+20)
  68. extern int sprint_hex_data(char* p, DATA_BLOCK&);
  69. #ifdef _USING_FCGI_
  70. #undef FILE
  71. #endif
  72. extern int print_hex_data(FILE* f, DATA_BLOCK&);
  73. extern int scan_hex_data(FILE* f, DATA_BLOCK&);
  74. extern int print_key_hex(FILE*, KEY* key, int len);
  75. extern int scan_key_hex(FILE*, KEY* key, int len);
  76. #ifdef _USING_FCGI_
  77. #define FILE FCGI_FILE
  78. #endif
  79. extern int sscan_key_hex(const char*, KEY* key, int len);
  80. extern int encrypt_private(
  81. R_RSA_PRIVATE_KEY& key, DATA_BLOCK& in, DATA_BLOCK& out
  82. );
  83. extern int decrypt_public(
  84. R_RSA_PUBLIC_KEY& key, DATA_BLOCK& in, DATA_BLOCK& out
  85. );
  86. extern int sign_file(
  87. const char* path, R_RSA_PRIVATE_KEY&, DATA_BLOCK& signature
  88. );
  89. extern int sign_block(
  90. DATA_BLOCK& data, R_RSA_PRIVATE_KEY&, DATA_BLOCK& signature
  91. );
  92. extern int check_file_signature(
  93. const char* md5, R_RSA_PUBLIC_KEY&, DATA_BLOCK& signature, bool&
  94. );
  95. extern int check_file_signature2(
  96. const char* md5, const char* signature, const char* key, bool&
  97. );
  98. extern int check_string_signature(
  99. const char* text, const char* signature, R_RSA_PUBLIC_KEY&, bool&
  100. );
  101. extern int check_string_signature2(
  102. const char* text, const char* signature, const char* key, bool&
  103. );
  104. extern int print_raw_data(FILE* f, DATA_BLOCK& x);
  105. extern int scan_raw_data(FILE *f, DATA_BLOCK& x);
  106. extern int read_key_file(const char* keyfile, R_RSA_PRIVATE_KEY& key);
  107. extern int generate_signature(
  108. char* text_to_sign, char* signature_hex, R_RSA_PRIVATE_KEY& key
  109. );
  110. // Check if sfileMsg (of length sfsize) has been created from sha1_md using the
  111. // private key beloning to the public key file cFile
  112. // Return:
  113. // 1: YES
  114. // 0: NO or error
  115. extern int check_validity_of_cert(
  116. const char *cFile, const unsigned char *sha1_md,
  117. unsigned char *sfileMsg, const int sfsize, const char* caPath
  118. );
  119. extern char *check_validity(const char *certPath, const char *origFile,
  120. unsigned char *signature, char* caPath
  121. );
  122. struct CERT_SIGS;
  123. int cert_verify_file(
  124. CERT_SIGS* signatures, const char* origFile, const char* trustLocation
  125. );
  126. #endif