You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/12/04 00:08:21 UTC

[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #947: MINIFICPP-1401 Read certificates from the Windows system store

szaszm commented on a change in pull request #947:
URL: https://github.com/apache/nifi-minifi-cpp/pull/947#discussion_r535539954



##########
File path: libminifi/src/utils/tls/DistinguishedName.cpp
##########
@@ -0,0 +1,64 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/tls/DistinguishedName.h"
+
+#include <algorithm>
+
+#include "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace tls {
+
+DistinguishedName::DistinguishedName(const std::vector<std::string>& components) {
+  std::transform(components.begin(), components.end(), std::back_inserter(components_),
+      [](const std::string& component) { return utils::StringUtils::trim(component); });
+  std::sort(components_.begin(), components_.end());
+}
+
+DistinguishedName DistinguishedName::fromCommaSeparated(const std::string& comma_separated_components) {
+  return DistinguishedName{utils::StringUtils::split(comma_separated_components, ",")};
+}
+
+DistinguishedName DistinguishedName::fromSlashSeparated(const std::string &slash_separated_components) {
+  return DistinguishedName{utils::StringUtils::split(slash_separated_components, "/")};
+}
+
+utils::optional<std::string> DistinguishedName::getCN() const {
+  const auto it = std::find_if(components_.begin(), components_.end(),
+      [](const std::string& component) { return component.substr(0, 3) == "CN="; });

Review comment:
       Not sure how often this would be called but this is a way to do the same without a temporary allocation/deallocation:
   ```suggestion
         [](const std::string& component) { return component.compare(0, 3, "CN="); });
   ```

##########
File path: libminifi/src/utils/tls/ExtendedKeyUsage.cpp
##########
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifdef OPENSSL_SUPPORT
+
+#include "utils/tls/ExtendedKeyUsage.h"
+
+#include <openssl/x509v3.h>
+
+#include <array>
+#include <cassert>
+#include <climits>
+
+#include "core/logging/LoggerConfiguration.h"
+#include "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace tls {
+
+namespace {
+
+struct KeyValuePair {
+  const char* key;
+  uint8_t value;
+};
+constexpr std::array<KeyValuePair, 6> EXT_KEY_USAGE_NAME_TO_BIT_POS{{
+    KeyValuePair{"Server Authentication", 1},
+    KeyValuePair{"Client Authentication", 2},
+    KeyValuePair{"Code Signing", 3},
+    KeyValuePair{"Secure Email", 4},
+    KeyValuePair{"Time Stamping", 8},
+    KeyValuePair{"OCSP Signing", 9}
+}};
+
+}  // namespace
+
+void EXTENDED_KEY_USAGE_deleter::operator()(EXTENDED_KEY_USAGE* key_usage) const { EXTENDED_KEY_USAGE_free(key_usage); }
+
+ExtendedKeyUsage::ExtendedKeyUsage() : logger_(core::logging::LoggerFactory<ExtendedKeyUsage>::getLogger()) {}
+
+ExtendedKeyUsage::ExtendedKeyUsage(const EXTENDED_KEY_USAGE& key_usage_asn1) : ExtendedKeyUsage{} {
+  const int num_oids = sk_ASN1_OBJECT_num(&key_usage_asn1);
+  for (int i = 0; i < num_oids; ++i) {
+    const ASN1_OBJECT* const oid = sk_ASN1_OBJECT_value(&key_usage_asn1, i);
+    assert(oid && oid->length > 0);
+    const unsigned char bit_pos = oid->data[oid->length - 1];
+    if (bit_pos < CHAR_BIT * sizeof(bits_)) {
+      bits_ |= (1 << bit_pos);
+    }
+  }
+}
+
+ExtendedKeyUsage::ExtendedKeyUsage(const std::string& key_usage_str) : ExtendedKeyUsage{} {
+  const std::vector<std::string> key_usages = utils::StringUtils::split(key_usage_str, ",");
+  for (const auto& key_usage : key_usages) {
+    const std::string key_usage_trimmed = utils::StringUtils::trim(key_usage);
+    const auto it = std::find_if(EXT_KEY_USAGE_NAME_TO_BIT_POS.begin(), EXT_KEY_USAGE_NAME_TO_BIT_POS.end(),
+                                 [key_usage_trimmed](const KeyValuePair& kv){ return kv.key == key_usage_trimmed; });
+    if (it != EXT_KEY_USAGE_NAME_TO_BIT_POS.end()) {
+      const uint8_t bit_pos = it->value;
+      bits_ |= (1 << bit_pos);
+    } else {
+      logger_->log_error("Ignoring unrecognized extended key usage type %s", key_usage_trimmed);
+    }
+  }
+}
+
+bool operator<=(const ExtendedKeyUsage& left, const ExtendedKeyUsage& right) {
+  return (left.bits_ & right.bits_) == left.bits_;
+}

Review comment:
       Take `A=1011` and `B=1100`. `A <= B` is false and `B <= A` is false. This should be impossible if this is meant to be used as a comparison operator. If this is meant to be some kind of arrow, then it needs a comment explaining the operation, or even better, a name.

##########
File path: libminifi/src/utils/tls/DistinguishedName.cpp
##########
@@ -0,0 +1,64 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/tls/DistinguishedName.h"
+
+#include <algorithm>
+
+#include "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace tls {
+
+DistinguishedName::DistinguishedName(const std::vector<std::string>& components) {
+  std::transform(components.begin(), components.end(), std::back_inserter(components_),
+      [](const std::string& component) { return utils::StringUtils::trim(component); });
+  std::sort(components_.begin(), components_.end());
+}
+
+DistinguishedName DistinguishedName::fromCommaSeparated(const std::string& comma_separated_components) {
+  return DistinguishedName{utils::StringUtils::split(comma_separated_components, ",")};
+}
+
+DistinguishedName DistinguishedName::fromSlashSeparated(const std::string &slash_separated_components) {
+  return DistinguishedName{utils::StringUtils::split(slash_separated_components, "/")};

Review comment:
       If these are used often, I would consider adding a constructor overload that moves from the temporary vector and does in-place trim.

##########
File path: libminifi/include/utils/tls/WindowsCertStoreLocation.h
##########
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+#ifdef WIN32
+
+#include <windows.h>
+
+#include <set>
+#include <string>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace tls {
+
+class WindowsCertStoreLocation {
+ public:
+  explicit WindowsCertStoreLocation(const std::string& location_name);
+
+  DWORD getBitfieldValue() const { return location_bitfield_value_; }
+
+  static std::string defaultLocation();
+  static std::set<std::string> allowedLocations();

Review comment:
       Why `std::set`? An explanatory code comment would be nice.
   
   https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#per7-design-to-enable-optimization
   
   > Compact data: By default, use compact data, such as std::vector and access it in a systematic fashion.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org