Skip to content

Commit

Permalink
X509: make the code more portable (#72)
Browse files Browse the repository at this point in the history
* X509: make the code more portable

`CoreFoundation` is not available on all platforms.  It never was meant
to be available on Linux, but was accidentally pushed into the release
and is now stuck.  Windows intentionally did not make this module
visible.  With the future work with the Foundation rewrite
`CoreFoundation` will disappear in more locations.

Windows does not support `inet_pton` except unless the string is
explicitly ANSI and not UTF-8.  We must use the proper spelling of
`InetPtonW` which takes a UTF-16 string.

The import of `CoreFoundation` was more for the dependency on the C
library as a side-effect.  On Windows, the networking functions are
provided by a separate user space library rather than the C library.
This is now highlighted in the imports.

* Clean up indentation

* Re-add CoreFoundation on Linux

---------

Co-authored-by: Cory Benfield <[email protected]>
  • Loading branch information
compnerd and Lukasa authored Apr 21, 2023
1 parent 0121e24 commit 35a7df2
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Sources/X509/Verifier/RFC5280/URIConstraints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
//
//===----------------------------------------------------------------------===//

import CoreFoundation
import Foundation
#if os(Windows)
import WinSDK
#elseif os(Linux) || os(Android)
import Glibc
import CoreFoundation
#elseif os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
import Darwin
#endif

extension NameConstraintsPolicy {
/// Validates that a URI name matches a name constraint.
Expand Down Expand Up @@ -58,14 +65,22 @@ extension NameConstraintsPolicy {
extension String {
@inlinable
var isIPAddress: Bool {
#if os(Windows)
var v4: IN_ADDR = IN_ADDR()
var v6: IN6_ADDR = IN6_ADDR()
return self.withCString(encodedAs: UTF16.self) {
return InetPtonW(AF_INET, $0, &v4) == 1 ||
InetPtonW(AF_INET6, $0, &v6) == 1
}
#else
// We need some scratch space to let inet_pton write into.
var ipv4Addr = in_addr()
var ipv6Addr = in6_addr()

return self.withCString { ptr in
return inet_pton(AF_INET, ptr, &ipv4Addr) == 1 ||
inet_pton(AF_INET6, ptr, &ipv6Addr) == 1
}
#endif
}
}

0 comments on commit 35a7df2

Please sign in to comment.