Skip to content

Instantly share code, notes, and snippets.

@mdelete
mdelete / publicKeyHexModulus
Created May 23, 2015
Silly but effective public key modulus extraction in Swift
View publicKeyHexModulus
func publicKeyHexModulus(key: SecKeyRef) -> String {
let s = String(format: "\(key)")
let r1 = s.rangeOfString("modulus: ")!
let r2 = Range<String.Index>(start: advance(r1.startIndex, count(r1)), end: advance(r1.endIndex, (SecKeyGetBlockSize(key) * 2)))
return s.substringWithRange(r2)
}
@mdelete
mdelete / gist:d9dbc320d5de347c2a85
Created May 23, 2015
Swift iOS SSL public key pinning
View gist:d9dbc320d5de347c2a85
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
var localTrust: Unmanaged<SecTrust>?
let serverTrust = challenge.protectionSpace.serverTrust!
let serverPublicKey = SecTrustCopyPublicKey(serverTrust).takeRetainedValue();
let certificateData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("pinning-certificate", ofType: "der")!)
let localCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, certificateData).takeRetainedValue();
let policy = SecPolicyCreateBasicX509().takeRetainedValue()
if SecTrustCreateWithCertificates(localCertificate, policy, &localTrust) == errSecSuccess {
@mdelete
mdelete / Makefile
Created Jul 12, 2014
Makefile for libsodium from andrewtj, with the Xcode ranlib arm64 bug fixed
View Makefile
.PHONY: all clean osx ios
export CFLAGS LDFLAGS
OSX_SDK_VERSION = 10.9
OSX_MIN_VERSION = 10.9
OSX_ARCH = MacOSX-x86_64
OSX_TARGET = $(patsubst %,build/%/lib/libsodium.a,$(OSX_ARCH))
IOS_SDK_VERSION = 7.1
IOS_MIN_VERSION = 7.1
IOS_ARCH = iPhoneSimulator-i386 iPhoneSimulator-x86_64 iPhoneOS-armv7 iPhoneOS-armv7s iPhoneOS-arm64