x509_certificate_model.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/strings/utf_string_conversions.h"
10#include "grit/generated_resources.h"
11#include "ui/base/l10n/l10n_util.h"
12
13namespace x509_certificate_model {
14
15std::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
52std::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
63  if (!data_length)
64    return std::string();
65
66  ret.reserve(std::max(kMin, data_length * 3 - 1));
67
68  for (size_t i = 0; i < data_length; ++i) {
69    unsigned char b = data[i];
70    ret.push_back(kHexChars[(b >> 4) & 0xf]);
71    ret.push_back(kHexChars[b & 0xf]);
72    if (i + 1 < data_length) {
73      if ((i + 1) % 16 == 0)
74        ret.push_back(line_separator);
75      else
76        ret.push_back(hex_separator);
77    }
78  }
79  return ret;
80}
81
82std::string ProcessRawBytes(const unsigned char* data, size_t data_length) {
83  return ProcessRawBytesWithSeparators(data, data_length, ' ', '\n');
84}
85
86#if defined(USE_NSS)
87std::string ProcessRawBits(const unsigned char* data, size_t data_length) {
88  return ProcessRawBytes(data, (data_length + 7) / 8);
89}
90#endif  // USE_NSS
91
92}  // x509_certificate_model
93
94