Skip to content

Commit

Permalink
Port LDAP code to swift and fix memory leaks
Browse files Browse the repository at this point in the history
Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma committed Feb 1, 2025
1 parent e681f36 commit c50478e
Show file tree
Hide file tree
Showing 25 changed files with 282 additions and 649 deletions.
118 changes: 36 additions & 82 deletions CryptoLib/CryptoLib.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions CryptoLib/CryptoLib/CryptoLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ FOUNDATION_EXPORT const unsigned char CryptoLibVersionString[];
#import <CryptoLib/Decrypt.h>
#import <CryptoLib/CdocParser.h>
#import <CryptoLib/AbstractSmartToken.h>
#import <CryptoLib/MoppLdapConfiguration.h>
#import <CryptoLib/OpenLdap.h>
13 changes: 0 additions & 13 deletions CryptoLib/CryptoLib/LDAPResponse.h

This file was deleted.

51 changes: 0 additions & 51 deletions CryptoLib/CryptoLib/LDAPResponse.m

This file was deleted.

33 changes: 0 additions & 33 deletions CryptoLib/CryptoLib/Ldap/Attribute.h

This file was deleted.

61 changes: 0 additions & 61 deletions CryptoLib/CryptoLib/Ldap/Attribute.m

This file was deleted.

33 changes: 0 additions & 33 deletions CryptoLib/CryptoLib/Ldap/AttributeSet.h

This file was deleted.

48 changes: 0 additions & 48 deletions CryptoLib/CryptoLib/Ldap/AttributeSet.m

This file was deleted.

90 changes: 90 additions & 0 deletions CryptoLib/CryptoLib/Ldap/LDAPResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// LDAPResponse.swift
// CryptoLib
/*
* Copyright 2017 - 2024 Riigi Infosüsteemi Amet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

import Foundation
import LDAP

public class LDAPResponse: NSObject {
@objc public var userCertificate: [Data] = []
@objc public var cn: String = ""

typealias BerElement = OpaquePointer

init(ldap: LDAP, msg: LDAPMessage) {
var ber: BerElement?
var attrPointer = ldap_first_attribute(ldap, msg, &ber)
while let attr = attrPointer {
defer { ldap_memfree(attr) }
let tag = String(cString: attr)
switch tag {
case "cn": cn = (LDAPResponse.values(ldap: ldap, msg: msg, tag: tag) as [String]).first ?? ""
case "userCertificate;binary": userCertificate = LDAPResponse.values(ldap: ldap, msg: msg, tag: tag)
default: break
}
attrPointer = ldap_next_attribute(ldap, msg, ber)
}
if let ber = ber {
ber_free(ber, 0)
}

if let namePointer = ldap_get_dn(ldap, msg) {
print("Result (\(userCertificate.count)) \(String(cString: namePointer))")
ldap_memfree(namePointer)
}
}

static func from(ldap: LDAP, msg: LDAPMessage) -> [LDAPResponse] {
var result: [LDAPResponse] = []
var message = ldap_first_message(ldap, msg)
while let currentMessage = message {
if ldap_msgtype(currentMessage) == LDAP_RES_SEARCH_ENTRY {
let response = LDAPResponse(ldap: ldap, msg: currentMessage)
if !response.userCertificate.isEmpty {
result.append(response)
}
}
message = ldap_next_message(ldap, currentMessage)
}
return result
}

static func values<T>(ldap: LDAP, msg: LDAPMessage, tag: String) -> [T] {
var result: [T] = []
guard let bvals = ldap_get_values_len(ldap, msg, tag) else {
return result
}
defer { ldap_value_free_len(bvals) }

var i = 0
while let bval = bvals[i] {
let value = bval.pointee.bv_val
let length = bval.pointee.bv_len
if T.self == Data.self {
result.append(Data(bytes: value!, count: Int(length)) as! T)
} else if T.self == String.self, let stringValue = String(validatingUTF8: value!) {
result.append(stringValue as! T)
}
i += 1
}
return result
}
}
34 changes: 0 additions & 34 deletions CryptoLib/CryptoLib/Ldap/MoppLdapConfiguration.h

This file was deleted.

Loading

0 comments on commit c50478e

Please sign in to comment.