• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/common/net/x509_certificate_model.h"
6 
7 #include <unicode/uidna.h>
8 
9 #include "base/utf_string_conversions.h"
10 #include "grit/generated_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
12 
13 namespace x509_certificate_model {
14 
ProcessIDN(const std::string & input)15 std::string ProcessIDN(const std::string& input) {
16   // Convert the ASCII input to a string16 for ICU.
17   string16 input16;
18   input16.reserve(input.length());
19   input16.insert(input16.end(), input.begin(), input.end());
20 
21   string16 output16;
22   output16.resize(input.length());
23 
24   UErrorCode status = U_ZERO_ERROR;
25   int output_chars = uidna_IDNToUnicode(input16.data(), input.length(),
26                                         &output16[0], output16.length(),
27                                         UIDNA_DEFAULT, NULL, &status);
28   if (status == U_ZERO_ERROR) {
29     output16.resize(output_chars);
30   } else if (status != U_BUFFER_OVERFLOW_ERROR) {
31     return input;
32   } else {
33     output16.resize(output_chars);
34     output_chars = uidna_IDNToUnicode(input16.data(), input.length(),
35                                       &output16[0], output16.length(),
36                                       UIDNA_DEFAULT, NULL, &status);
37     if (status != U_ZERO_ERROR)
38       return input;
39     DCHECK_EQ(static_cast<size_t>(output_chars), output16.length());
40     output16.resize(output_chars);  // Just to be safe.
41   }
42 
43   if (input16 == output16)
44     return input;  // Input did not contain any encoded data.
45 
46   // Input contained encoded data, return formatted string showing original and
47   // decoded forms.
48   return l10n_util::GetStringFUTF8(IDS_CERT_INFO_IDN_VALUE_FORMAT,
49                                    input16, output16);
50 }
51 
ProcessRawBytesWithSeparators(const unsigned char * data,size_t data_length,char hex_separator,char line_separator)52 std::string ProcessRawBytesWithSeparators(const unsigned char* data,
53                                           size_t data_length,
54                                           char hex_separator,
55                                           char line_separator) {
56   static const char kHexChars[] = "0123456789ABCDEF";
57 
58   // Each input byte creates two output hex characters + a space or newline,
59   // except for the last byte.
60   std::string ret;
61   size_t kMin = 0U;
62   ret.reserve(std::max(kMin, data_length * 3 - 1));
63 
64   for (size_t i = 0; i < data_length; ++i) {
65     unsigned char b = data[i];
66     ret.push_back(kHexChars[(b >> 4) & 0xf]);
67     ret.push_back(kHexChars[b & 0xf]);
68     if (i + 1 < data_length) {
69       if ((i + 1) % 16 == 0)
70         ret.push_back(line_separator);
71       else
72         ret.push_back(hex_separator);
73     }
74   }
75   return ret;
76 }
77 
ProcessRawBytes(const unsigned char * data,size_t data_length)78 std::string ProcessRawBytes(const unsigned char* data, size_t data_length) {
79   return ProcessRawBytesWithSeparators(data, data_length, ' ', '\n');
80 }
81 
82 #if defined(USE_NSS)
ProcessRawBits(const unsigned char * data,size_t data_length)83 std::string ProcessRawBits(const unsigned char* data, size_t data_length) {
84   return ProcessRawBytes(data, (data_length + 7) / 8);
85 }
86 #endif  // USE_NSS
87 
88 }  // x509_certificate_model
89 
90