passphrase-encoding.pod 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. =pod
  2. =encoding utf8
  3. =head1 NAME
  4. passphrase-encoding
  5. - How diverse parts of OpenSSL treat pass phrases character encoding
  6. =head1 DESCRIPTION
  7. In a modern world with all sorts of character encodings, the treatment of pass
  8. phrases has become increasingly complex.
  9. This manual page attempts to give an overview over how this problem is
  10. currently addressed in different parts of the OpenSSL library.
  11. =head2 The general case
  12. The OpenSSL library doesn't treat pass phrases in any special way as a general
  13. rule, and trusts the application or user to choose a suitable character set
  14. and stick to that throughout the lifetime of affected objects.
  15. This means that for an object that was encrypted using a pass phrase encoded in
  16. ISO-8859-1, that object needs to be decrypted using a pass phrase encoded in
  17. ISO-8859-1.
  18. Using the wrong encoding is expected to cause a decryption failure.
  19. =head2 PKCS#12
  20. PKCS#12 is a bit different regarding pass phrase encoding.
  21. The standard stipulates that the pass phrase shall be encoded as an ASN.1
  22. BMPString, which consists of the code points of the basic multilingual plane,
  23. encoded in big endian (UCS-2 BE).
  24. OpenSSL tries to adapt to this requirements in one of the following manners:
  25. =over 4
  26. =item 1.
  27. Treats the received pass phrase as UTF-8 encoded and tries to re-encode it to
  28. UTF-16 (which is the same as UCS-2 for characters U+0000 to U+D7FF and U+E000
  29. to U+FFFF, but becomes an expansion for any other character), or failing that,
  30. proceeds with step 2.
  31. =item 2.
  32. Assumes that the pass phrase is encoded in ASCII or ISO-8859-1 and
  33. opportunistically prepends each byte with a zero byte to obtain the UCS-2
  34. encoding of the characters, which it stores as a BMPString.
  35. Note that since there is no check of your locale, this may produce UCS-2 /
  36. UTF-16 characters that do not correspond to the original pass phrase characters
  37. for other character sets, such as any ISO-8859-X encoding other than
  38. ISO-8859-1 (or for Windows, CP 1252 with exception for the extra "graphical"
  39. characters in the 0x80-0x9F range).
  40. =back
  41. OpenSSL versions older than 1.1.0 do variant 2 only, and that is the reason why
  42. OpenSSL still does this, to be able to read files produced with older versions.
  43. It should be noted that this approach isn't entirely fault free.
  44. A pass phrase encoded in ISO-8859-2 could very well have a sequence such as
  45. 0xC3 0xAF (which is the two characters "LATIN CAPITAL LETTER A WITH BREVE"
  46. and "LATIN CAPITAL LETTER Z WITH DOT ABOVE" in ISO-8859-2 encoding), but would
  47. be misinterpreted as the perfectly valid UTF-8 encoded code point U+00EF (LATIN
  48. SMALL LETTER I WITH DIARESIS) I<if the pass phrase doesn't contain anything that
  49. would be invalid UTF-8>.
  50. A pass phrase that contains this kind of byte sequence will give a different
  51. outcome in OpenSSL 1.1.0 and newer than in OpenSSL older than 1.1.0.
  52. 0x00 0xC3 0x00 0xAF # OpenSSL older than 1.1.0
  53. 0x00 0xEF # OpenSSL 1.1.0 and newer
  54. On the same accord, anything encoded in UTF-8 that was given to OpenSSL older
  55. than 1.1.0 was misinterpreted as ISO-8859-1 sequences.
  56. =head2 OSSL_STORE
  57. L<ossl_store(7)> acts as a general interface to access all kinds of objects,
  58. potentially protected with a pass phrase, a PIN or something else.
  59. This API stipulates that pass phrases should be UTF-8 encoded, and that any
  60. other pass phrase encoding may give undefined results.
  61. This API relies on the application to ensure UTF-8 encoding, and doesn't check
  62. that this is the case, so what it gets, it will also pass to the underlying
  63. loader.
  64. =head1 RECOMMENDATIONS
  65. This section assumes that you know what pass phrase was used for encryption,
  66. but that it may have been encoded in a different character encoding than the
  67. one used by your current input method.
  68. For example, the pass phrase may have been used at a time when your default
  69. encoding was ISO-8859-1 (i.e. "naïve" resulting in the byte sequence 0x6E 0x61
  70. 0xEF 0x76 0x65), and you're now in an environment where your default encoding
  71. is UTF-8 (i.e. "naïve" resulting in the byte sequence 0x6E 0x61 0xC3 0xAF 0x76
  72. 0x65).
  73. Whenever it's mentioned that you should use a certain character encoding, it
  74. should be understood that you either change the input method to use the
  75. mentioned encoding when you type in your pass phrase, or use some suitable tool
  76. to convert your pass phrase from your default encoding to the target encoding.
  77. Also note that the sub-sections below discuss human readable pass phrases.
  78. This is particularly relevant for PKCS#12 objects, where human readable pass
  79. phrases are assumed.
  80. For other objects, it's as legitimate to use any byte sequence (such as a
  81. sequence of bytes from `/dev/urandom` that's been saved away), which makes any
  82. character encoding discussion irrelevant; in such cases, simply use the same
  83. byte sequence as it is.
  84. =head2 Creating new objects
  85. For creating new pass phrase protected objects, make sure the pass phrase is
  86. encoded using UTF-8.
  87. This is default on most modern Unixes, but may involve an effort on other
  88. platforms.
  89. Specifically for Windows, setting the environment variable
  90. C<OPENSSL_WIN32_UTF8> will have anything entered on [Windows] console prompt
  91. converted to UTF-8 (command line and separately prompted pass phrases alike).
  92. =head2 Opening existing objects
  93. For opening pass phrase protected objects where you know what character
  94. encoding was used for the encryption pass phrase, make sure to use the same
  95. encoding again.
  96. For opening pass phrase protected objects where the character encoding that was
  97. used is unknown, or where the producing application is unknown, try one of the
  98. following:
  99. =over 4
  100. =item 1.
  101. Try the pass phrase that you have as it is in the character encoding of your
  102. environment.
  103. It's possible that its byte sequence is exactly right.
  104. =item 2.
  105. Convert the pass phrase to UTF-8 and try with the result.
  106. Specifically with PKCS#12, this should open up any object that was created
  107. according to the specification.
  108. =item 3.
  109. Do a naïve (i.e. purely mathematical) ISO-8859-1 to UTF-8 conversion and try
  110. with the result.
  111. This differs from the previous attempt because ISO-8859-1 maps directly to
  112. U+0000 to U+00FF, which other non-UTF-8 character sets do not.
  113. This also takes care of the case when a UTF-8 encoded string was used with
  114. OpenSSL older than 1.1.0.
  115. (for example, C<ï>, which is 0xC3 0xAF when encoded in UTF-8, would become 0xC3
  116. 0x83 0xC2 0xAF when re-encoded in the naïve manner.
  117. The conversion to BMPString would then yield 0x00 0xC3 0x00 0xA4 0x00 0x00, the
  118. erroneous/non-compliant encoding used by OpenSSL older than 1.1.0)
  119. =back
  120. =head1 SEE ALSO
  121. L<evp(7)>,
  122. L<ossl_store(7)>,
  123. L<EVP_BytesToKey(3)>, L<EVP_DecryptInit(3)>,
  124. L<PEM_do_header(3)>,
  125. L<PKCS12_parse(3)>, L<PKCS12_newpass(3)>,
  126. L<d2i_PKCS8PrivateKey_bio(3)>
  127. =head1 COPYRIGHT
  128. Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  129. Licensed under the OpenSSL license (the "License"). You may not use
  130. this file except in compliance with the License. You can obtain a copy
  131. in the file LICENSE in the source distribution or at
  132. L<https://www.openssl.org/source/license.html>.
  133. =cut