| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/internal/cert_errors.h" | 5 #include "net/cert/internal/cert_errors.h" |
| 6 | 6 |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/logging.h" | 7 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_split.h" |
| 12 #include "base/values.h" | 10 #include "net/cert/internal/cert_error_params.h" |
| 13 #include "net/der/input.h" | 11 #include "net/cert/internal/cert_error_scoper.h" |
| 14 | 12 |
| 15 namespace net { | 13 namespace net { |
| 16 | 14 |
| 17 namespace { | 15 namespace { |
| 18 | 16 |
| 19 std::string HexEncodeString(const std::string& bytes) { | 17 // Helpers for pretty-printing CertErrors to a string. |
| 20 return base::HexEncode(bytes.data(), bytes.size()); | 18 void AppendNodeToDebugString(CertErrorNode* node, |
| 19 const std::string& indentation, |
| 20 std::string* out); |
| 21 |
| 22 void AppendChildrenToDebugString(const CertErrorNodes& children, |
| 23 const std::string& indentation, |
| 24 std::string* out) { |
| 25 for (const auto& child : children) |
| 26 AppendNodeToDebugString(child.get(), indentation, out); |
| 27 } |
| 28 |
| 29 void AppendLinesWithIndentation(const std::string& text, |
| 30 const std::string& indentation, |
| 31 std::string* out) { |
| 32 std::vector<base::StringPiece> lines = base::SplitStringPieceUsingSubstr( |
| 33 text, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 34 |
| 35 for (const auto& line : lines) { |
| 36 *out += indentation; |
| 37 line.AppendToString(out); |
| 38 *out += "\n"; |
| 39 } |
| 40 } |
| 41 |
| 42 const char* CertErrorNodeTypeToString(CertErrorNodeType type) { |
| 43 switch (type) { |
| 44 case CertErrorNodeType::TYPE_CONTEXT: |
| 45 return "[Context] "; |
| 46 case CertErrorNodeType::TYPE_WARNING: |
| 47 return "[Warning] "; |
| 48 case CertErrorNodeType::TYPE_ERROR: |
| 49 return "[Error] "; |
| 50 } |
| 51 return nullptr; |
| 52 } |
| 53 |
| 54 void AppendNodeToDebugString(CertErrorNode* node, |
| 55 const std::string& indentation, |
| 56 std::string* out) { |
| 57 std::string cur_indentation = indentation; |
| 58 |
| 59 *out += cur_indentation; |
| 60 *out += CertErrorNodeTypeToString(node->node_type); |
| 61 *out += CertErrorIdToDebugString(node->id); |
| 62 *out += +"\n"; |
| 63 |
| 64 if (node->params) { |
| 65 cur_indentation += " "; |
| 66 AppendLinesWithIndentation(node->params->ToDebugString(), cur_indentation, |
| 67 out); |
| 68 } |
| 69 |
| 70 cur_indentation += " "; |
| 71 |
| 72 AppendChildrenToDebugString(node->children, cur_indentation, out); |
| 21 } | 73 } |
| 22 | 74 |
| 23 } // namespace | 75 } // namespace |
| 24 | 76 |
| 25 CertErrorParams::CertErrorParams() = default; | 77 CertErrorNode::CertErrorNode(CertErrorNodeType node_type, |
| 26 CertErrorParams::~CertErrorParams() = default; | 78 CertErrorId id, |
| 79 std::unique_ptr<CertErrorParams> params) |
| 80 : node_type(node_type), id(id), params(std::move(params)) {} |
| 27 | 81 |
| 28 CertError::CertError() = default; | 82 CertErrorNode::~CertErrorNode() = default; |
| 29 CertError::~CertError() = default; | 83 |
| 30 CertError::CertError(CertError&& other) = default; | 84 void CertErrorNode::AddChild(std::unique_ptr<CertErrorNode> child) { |
| 85 DCHECK_EQ(CertErrorNodeType::TYPE_CONTEXT, node_type); |
| 86 children.push_back(std::move(child)); |
| 87 } |
| 31 | 88 |
| 32 CertErrors::CertErrors() = default; | 89 CertErrors::CertErrors() = default; |
| 33 | 90 |
| 34 CertErrors::~CertErrors() = default; | 91 CertErrors::~CertErrors() = default; |
| 35 | 92 |
| 36 void CertErrors::Add(CertErrorType type) { | 93 void CertErrors::Add(CertErrorNodeType node_type, |
| 37 AddWithParam(type, nullptr); | 94 CertErrorId id, |
| 95 std::unique_ptr<CertErrorParams> params) { |
| 96 AddNode(base::MakeUnique<CertErrorNode>(node_type, id, std::move(params))); |
| 38 } | 97 } |
| 39 | 98 |
| 40 void CertErrors::AddWithParam(CertErrorType type, | 99 void CertErrors::Add(CertErrorId id) { |
| 41 std::unique_ptr<CertErrorParams> params) { | 100 AddError(id); |
| 42 errors_.resize(errors_.size() + 1); | |
| 43 errors_.back().type = type; | |
| 44 errors_.back().params = std::move(params); | |
| 45 } | 101 } |
| 46 | 102 |
| 47 void CertErrors::AddWith1DerParam(CertErrorType type, const der::Input& der1) { | 103 void CertErrors::AddError(CertErrorId id, |
| 48 AddWithParam(type, base::MakeUnique<CertErrorParamsDer1>(der1)); | 104 std::unique_ptr<CertErrorParams> params) { |
| 105 Add(CertErrorNodeType::TYPE_ERROR, id, std::move(params)); |
| 49 } | 106 } |
| 50 | 107 |
| 51 void CertErrors::AddWith2DerParams(CertErrorType type, | 108 void CertErrors::AddError(CertErrorId id) { |
| 52 const der::Input& der1, | 109 AddError(id, nullptr); |
| 53 const der::Input& der2) { | 110 } |
| 54 AddWithParam(type, base::MakeUnique<CertErrorParamsDer2>(der1, der2)); | 111 |
| 112 void CertErrors::AddWarning(CertErrorId id, |
| 113 std::unique_ptr<CertErrorParams> params) { |
| 114 Add(CertErrorNodeType::TYPE_WARNING, id, std::move(params)); |
| 115 } |
| 116 |
| 117 void CertErrors::AddWarning(CertErrorId id) { |
| 118 AddWarning(id, nullptr); |
| 119 } |
| 120 |
| 121 bool CertErrors::empty() const { |
| 122 return nodes_.empty(); |
| 55 } | 123 } |
| 56 | 124 |
| 57 std::string CertErrors::ToDebugString() const { | 125 std::string CertErrors::ToDebugString() const { |
| 58 std::string str; | 126 std::string result; |
| 59 for (const auto& error : errors_) { | 127 AppendChildrenToDebugString(nodes_, std::string(), &result); |
| 60 if (!str.empty()) | 128 return result; |
| 61 str += "\n"; | |
| 62 str += error.type; | |
| 63 } | |
| 64 return str; | |
| 65 } | 129 } |
| 66 | 130 |
| 67 ScopedCertErrorsCertContext::ScopedCertErrorsCertContext( | 131 void CertErrors::AddNode(std::unique_ptr<CertErrorNode> node) { |
| 68 CertErrors* parent, | 132 if (current_scoper_) |
| 69 const ParsedCertificate* cert, | 133 current_scoper_->LazyGetRootNode()->AddChild(std::move(node)); |
| 70 size_t i) { | 134 else |
| 71 // TODO(crbug.com/634443): Implement. | 135 nodes_.push_back(std::move(node)); |
| 72 } | 136 } |
| 73 | 137 |
| 74 ScopedCertErrorsCertContext::~ScopedCertErrorsCertContext() { | 138 CertErrorScoper* CertErrors::SetScoper(CertErrorScoper* scoper) { |
| 75 // TODO(crbug.com/634443): Implement. | 139 CertErrorScoper* prev = current_scoper_; |
| 76 } | 140 current_scoper_ = scoper; |
| 77 | 141 return prev; |
| 78 ScopedCertErrorsTrustAnchorContext::ScopedCertErrorsTrustAnchorContext( | |
| 79 CertErrors* parent, | |
| 80 const TrustAnchor* trust_anchor) { | |
| 81 // TODO(crbug.com/634443): Implement. | |
| 82 } | |
| 83 | |
| 84 ScopedCertErrorsTrustAnchorContext::~ScopedCertErrorsTrustAnchorContext() { | |
| 85 // TODO(crbug.com/634443): Implement. | |
| 86 } | |
| 87 | |
| 88 CertErrorParamsDer1::CertErrorParamsDer1(const der::Input& der1) | |
| 89 : der1_(der1.AsString()) {} | |
| 90 | |
| 91 std::unique_ptr<base::Value> CertErrorParamsDer1::ToValue() const { | |
| 92 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
| 93 dict->SetString("der1", HexEncodeString(der1_)); | |
| 94 return std::move(dict); | |
| 95 } | |
| 96 | |
| 97 CertErrorParamsDer2::CertErrorParamsDer2(const der::Input& der1, | |
| 98 const der::Input& der2) | |
| 99 : der1_(der1.AsString()), der2_(der2.AsString()) {} | |
| 100 | |
| 101 std::unique_ptr<base::Value> CertErrorParamsDer2::ToValue() const { | |
| 102 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
| 103 dict->SetString("der1", HexEncodeString(der1_)); | |
| 104 dict->SetString("der2", HexEncodeString(der2_)); | |
| 105 return std::move(dict); | |
| 106 } | 142 } |
| 107 | 143 |
| 108 } // namespace net | 144 } // namespace net |
| OLD | NEW |