summaryrefslogtreecommitdiffstats
blob: 4270a6fbbc1d5f7de73482d8ba6f54c6dffa9e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (c) 2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/TLS/SecureTransport/SecureTransportCertificate.h>

#include <boost/numeric/conversion/cast.hpp>

#include <Cocoa/Cocoa.h>
#include <Security/Security.h>

#include <Swiften/Base/Log.h>

namespace {

template <typename T, typename S>
T bridge_cast(S source) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
	return (__bridge T)(source);
#pragma clang diagnostic pop
}

}

namespace Swift {

SecureTransportCertificate::SecureTransportCertificate(SecCertificateRef certificate) {
	assert(certificate);
	CFRetain(certificate);
	certificateHandle_ = boost::shared_ptr<SecCertificate>(certificate, CFRelease);
	parse();
}


SecureTransportCertificate::SecureTransportCertificate(const ByteArray& der) {
	CFDataRef derData = CFDataCreateWithBytesNoCopy(NULL, der.data(), static_cast<CFIndex>(der.size()), NULL); 
	SecCertificateRef certificate = SecCertificateCreateWithData(NULL, derData);
	CFRelease(derData);
	if (certificate) {
		certificateHandle_ = boost::shared_ptr<SecCertificate>(certificate, CFRelease);
		parse();
	}
}

SecureTransportCertificate::~SecureTransportCertificate() {

}

#define NS2STDSTRING(a) (a == nil ? std::string() : std::string([a cStringUsingEncoding:NSUTF8StringEncoding]))


void SecureTransportCertificate::parse() {
	assert(certificateHandle_);
	CFErrorRef error = NULL;

	// The SecCertificateCopyValues function is not part of the iOS Secure Transport API.
	CFDictionaryRef valueDict = SecCertificateCopyValues(certificateHandle_.get(), 0, &error);
	if (valueDict) {
		// Handle subject.
		CFStringRef subject = SecCertificateCopySubjectSummary(certificateHandle_.get());
		if (subject) {
			NSString* subjectStr = bridge_cast<NSString*>(subject);
			subjectName_ = NS2STDSTRING(subjectStr);
			CFRelease(subject);
		}

		// Handle a single Common Name.
		CFStringRef commonName = NULL;
		OSStatus error = SecCertificateCopyCommonName(certificateHandle_.get(), &commonName);
		if (!error && commonName) {
			NSString* commonNameStr = bridge_cast<NSString*>(commonName);
			commonNames_.push_back(NS2STDSTRING(commonNameStr));
		}
		if (commonName) {
			CFRelease(commonName);
		}

		// Handle Subject Alternative Names
		NSDictionary* certDict = bridge_cast<NSDictionary*>(valueDict);
		NSDictionary* subjectAltNamesDict = certDict[@"2.5.29.17"][@"value"];

		for (NSDictionary* entry in subjectAltNamesDict) {
			if ([entry[@"label"] isEqualToString:[NSString stringWithUTF8String:ID_ON_XMPPADDR_OID]]) {
				xmppAddresses_.push_back(NS2STDSTRING(entry[@"value"]));
			}
			else if ([entry[@"label"] isEqualToString:[NSString stringWithUTF8String:ID_ON_DNSSRV_OID]]) {
				srvNames_.push_back(NS2STDSTRING(entry[@"value"]));
			}
			else if ([entry[@"label"] isEqualToString:@"DNS Name"]) {
				dnsNames_.push_back(NS2STDSTRING(entry[@"value"]));
			}
		}
		CFRelease(valueDict);
	}

	if (error) {
		CFRelease(error);
	}
}

std::string SecureTransportCertificate::getSubjectName() const {
	return subjectName_;
}

std::vector<std::string> SecureTransportCertificate::getCommonNames() const {
	return commonNames_;
}

std::vector<std::string> SecureTransportCertificate::getSRVNames() const {
	return srvNames_;
}

std::vector<std::string> SecureTransportCertificate::getDNSNames() const {
	return dnsNames_;
}

std::vector<std::string> SecureTransportCertificate::getXMPPAddresses() const {
	return xmppAddresses_;
}

ByteArray SecureTransportCertificate::toDER() const {
	ByteArray der;
	if (certificateHandle_) {
		CFDataRef derData = SecCertificateCopyData(certificateHandle_.get());
		if (derData) {
			try {
				size_t dataSize = boost::numeric_cast<size_t>(CFDataGetLength(derData));
				der.resize(dataSize);
				CFDataGetBytes(derData, CFRangeMake(0,CFDataGetLength(derData)), der.data());
			} catch (...) {
			}
			CFRelease(derData);
		}
	}
	return der;
}

}