pem_common.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // pem_common.h - commom PEM routines.
  2. // Written and placed in the public domain by Jeffrey Walton
  3. // pem_common.h is an internal header. Include pem.h instead.
  4. ///////////////////////////////////////////////////////////////////////////
  5. // For documentation on the PEM read and write routines, see
  6. // http://www.cryptopp.com/wiki/PEM_Pack
  7. ///////////////////////////////////////////////////////////////////////////
  8. #ifndef CRYPTOPP_PEM_COMMON_H
  9. #define CRYPTOPP_PEM_COMMON_H
  10. #include "cryptlib.h"
  11. #include "secblock.h"
  12. #include "osrng.h"
  13. #include "pem.h"
  14. #include <string>
  15. //////////////////////////////////////////////////////
  16. //////////////////////////////////////////////////////
  17. // By default, keys and parameters are validated after reading in Debug builds.
  18. // You will have to call key.Validate() yourself if desired. If you want automatic
  19. // validation, then uncomment the line below or set it on the command line.
  20. // #define PEM_KEY_OR_PARAMETER_VALIDATION 1
  21. #if defined(CRYPTOPP_DEBUG) && !defined(PEM_KEY_OR_PARAMETER_VALIDATION)
  22. # define PEM_KEY_OR_PARAMETER_VALIDATION 1
  23. #endif
  24. //////////////////////////////////////////////////////
  25. //////////////////////////////////////////////////////
  26. NAMESPACE_BEGIN(CryptoPP)
  27. NAMESPACE_BEGIN(PEM)
  28. typedef std::basic_string<char, std::char_traits<char>, AllocatorWithCleanup<char> > secure_string;
  29. inline const byte* byte_ptr(const char* cstr)
  30. {
  31. return reinterpret_cast<const byte*>(cstr);
  32. }
  33. inline byte* byte_ptr(char* cstr)
  34. {
  35. return reinterpret_cast<byte*>(cstr);
  36. }
  37. inline const byte* byte_ptr(const secure_string& str)
  38. {
  39. static const char empty[1] = {0};
  40. return str.empty() ?
  41. reinterpret_cast<const byte*>(empty) : reinterpret_cast<const byte*>(&str[0]);
  42. }
  43. inline byte* byte_ptr(secure_string& str)
  44. {
  45. static char empty[1] = {0};
  46. return str.empty() ?
  47. reinterpret_cast<byte*>(empty) : reinterpret_cast<byte*>(&str[0]);
  48. }
  49. inline const byte* byte_ptr(const std::string& str)
  50. {
  51. static const char empty[1] = {0};
  52. return str.empty() ?
  53. reinterpret_cast<const byte*>(empty) : reinterpret_cast<const byte*>(&str[0]);
  54. }
  55. inline byte* byte_ptr(std::string& str)
  56. {
  57. static char empty[1] = {0};
  58. return str.empty() ?
  59. reinterpret_cast<byte*>(empty) : reinterpret_cast<byte*>(&str[0]);
  60. }
  61. // Attempts to locate a control field in a line
  62. secure_string GetControlField(const secure_string& line);
  63. // Attempts to fetch the data from a control line
  64. secure_string GetControlFieldData(const secure_string& line);
  65. // Returns 0 if a match, non-0 otherwise
  66. int CompareNoCase(const secure_string& first, const secure_string& second);
  67. // Returns a string converted to lower-case
  68. secure_string ToLower(const secure_string& str);
  69. // Returns a string converted to upper-case
  70. secure_string ToUpper(const secure_string& str);
  71. // Base64 Encode
  72. void PEM_Base64Encode(BufferedTransformation& source, BufferedTransformation& dest);
  73. // Base64 Decode
  74. void PEM_Base64Decode(BufferedTransformation& source, BufferedTransformation& dest);
  75. // Write to a BufferedTransformation
  76. void PEM_WriteLine(BufferedTransformation& bt, const SecByteBlock& line);
  77. void PEM_WriteLine(BufferedTransformation& bt, const std::string& line);
  78. void PEM_WriteLine(BufferedTransformation& bt, const secure_string& line);
  79. // Signature changed a bit to match Crypto++. Salt must be PKCS5_SALT_LEN in length.
  80. // Salt, Data and Count are IN; Key and IV are OUT.
  81. int OPENSSL_EVP_BytesToKey(HashTransformation& hash,
  82. const unsigned char *salt, const unsigned char* data, size_t dlen,
  83. size_t count, unsigned char *key, size_t ksize,
  84. unsigned char *iv, size_t vsize);
  85. // From OpenSSL, crypto/evp/evp.h.
  86. static const unsigned int OPENSSL_PKCS5_SALT_LEN = 8;
  87. // Signals failure
  88. static const size_t PEM_INVALID = static_cast<size_t>(-1);
  89. // 64-character line length is required by RFC 1421.
  90. static const unsigned int PEM_LINE_BREAK = 64;
  91. extern const secure_string CR;
  92. extern const secure_string LF;
  93. extern const secure_string EOL;
  94. extern const secure_string CRLF;
  95. extern const secure_string COMMA;
  96. extern const secure_string SPACE;
  97. extern const secure_string COLON;
  98. extern const secure_string PEM_BEGIN;
  99. extern const secure_string PEM_TAIL;
  100. extern const secure_string PEM_END;
  101. extern const secure_string PUBLIC_BEGIN;
  102. extern const secure_string PUBLIC_END;
  103. extern const secure_string PRIVATE_BEGIN;
  104. extern const secure_string PRIVATE_END;
  105. extern const secure_string RSA_PUBLIC_BEGIN;
  106. extern const secure_string RSA_PUBLIC_END;
  107. extern const secure_string RSA_PRIVATE_BEGIN;
  108. extern const secure_string RSA_PRIVATE_END;
  109. extern const secure_string DSA_PUBLIC_BEGIN;
  110. extern const secure_string DSA_PUBLIC_END;
  111. extern const secure_string DSA_PRIVATE_BEGIN;
  112. extern const secure_string DSA_PRIVATE_END;
  113. extern const secure_string ELGAMAL_PUBLIC_BEGIN;
  114. extern const secure_string ELGAMAL_PUBLIC_END;
  115. extern const secure_string ELGAMAL_PRIVATE_BEGIN;
  116. extern const secure_string ELGAMAL_PRIVATE_END;
  117. extern const secure_string EC_PUBLIC_BEGIN;
  118. extern const secure_string EC_PUBLIC_END;
  119. extern const secure_string ECDSA_PUBLIC_BEGIN;
  120. extern const secure_string ECDSA_PUBLIC_END;
  121. extern const secure_string EC_PRIVATE_BEGIN;
  122. extern const secure_string EC_PRIVATE_END;
  123. extern const secure_string EC_PARAMETERS_BEGIN;
  124. extern const secure_string EC_PARAMETERS_END;
  125. extern const secure_string DH_PARAMETERS_BEGIN;
  126. extern const secure_string DH_PARAMETERS_END;
  127. extern const secure_string DSA_PARAMETERS_BEGIN;
  128. extern const secure_string DSA_PARAMETERS_END;
  129. extern const secure_string CERTIFICATE_BEGIN;
  130. extern const secure_string CERTIFICATE_END;
  131. extern const secure_string X509_CERTIFICATE_BEGIN;
  132. extern const secure_string X509_CERTIFICATE_END;
  133. extern const secure_string REQ_CERTIFICATE_BEGIN;
  134. extern const secure_string REQ_CERTIFICATE_END;
  135. extern const secure_string PROC_TYPE;
  136. extern const secure_string PROC_TYPE_ENC;
  137. extern const secure_string ENCRYPTED;
  138. extern const secure_string DEK_INFO;
  139. extern const secure_string CONTENT_DOMAIN;
  140. extern const secure_string COMMENT;
  141. NAMESPACE_END // PEM
  142. NAMESPACE_END // CryptoPP
  143. #endif // CRYPTOPP_PEM_COMMON_H