Skip to content

Commit a217c5c

Browse files
committed
NegotiateSsl implementation for security_framework
1 parent dd301c4 commit a217c5c

7 files changed

Lines changed: 125 additions & 0 deletions

File tree

.travis/server.crt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Certificate:
2+
Data:
3+
Version: 3 (0x2)
4+
Serial Number:
5+
9a:e5:7a:5f:05:5a:2f:e4
6+
Signature Algorithm: sha1WithRSAEncryption
7+
Issuer: C=AU, ST=Some-State, O=Internet Widgits Pty Ltd, CN=localhost
8+
Validity
9+
Not Before: Dec 5 21:50:46 2015 GMT
10+
Not After : Jan 4 21:50:46 2016 GMT
11+
Subject: C=AU, ST=Some-State, O=Internet Widgits Pty Ltd, CN=localhost
12+
Subject Public Key Info:
13+
Public Key Algorithm: rsaEncryption
14+
RSA Public Key: (1024 bit)
15+
Modulus (1024 bit):
16+
00:f1:9b:b6:24:64:66:bf:5e:da:77:2a:39:bd:39:
17+
93:56:28:26:f1:d7:1f:c9:60:1c:e3:82:a4:07:a2:
18+
0f:c8:d6:68:fc:30:2f:17:30:34:69:cd:d8:f1:e7:
19+
c7:84:f9:c5:90:b1:2c:42:d5:23:20:d2:1d:d7:18:
20+
15:70:0a:a3:1d:c7:2e:df:03:c0:9f:5c:cb:02:25:
21+
da:7d:2b:1a:09:78:e5:23:8a:c4:64:39:59:0d:4e:
22+
15:0b:75:7b:75:f9:8a:4c:c3:9d:f9:31:08:d5:da:
23+
00:a5:db:0c:df:09:e5:e4:14:d1:17:0f:bb:f6:cf:
24+
bd:3c:5d:14:6a:cb:c1:dc:e1
25+
Exponent: 65537 (0x10001)
26+
X509v3 extensions:
27+
X509v3 Subject Key Identifier:
28+
9E:09:C0:D1:1E:0E:07:B3:49:57:0A:49:47:F9:8A:5F:4E:FE:23:75
29+
X509v3 Authority Key Identifier:
30+
keyid:9E:09:C0:D1:1E:0E:07:B3:49:57:0A:49:47:F9:8A:5F:4E:FE:23:75
31+
DirName:/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd/CN=localhost
32+
serial:9A:E5:7A:5F:05:5A:2F:E4
33+
34+
X509v3 Basic Constraints:
35+
CA:TRUE
36+
Signature Algorithm: sha1WithRSAEncryption
37+
4c:3b:c6:42:96:75:96:a0:9b:f5:d9:b1:9b:1b:4f:bd:d2:8d:
38+
f1:53:ed:87:80:f5:7b:5d:36:6e:38:c8:ae:1a:58:e5:39:9e:
39+
42:49:12:35:76:ab:0f:fa:b1:1f:4e:b1:85:f3:a3:6f:60:e3:
40+
6c:0e:a8:95:0d:c8:38:7f:e3:e3:ff:64:74:73:50:46:65:83:
41+
5f:1a:72:f9:69:44:07:cd:36:01:90:b9:b3:ed:d8:d7:bc:68:
42+
97:dd:11:ac:2b:ec:5d:a4:d4:d5:e8:8b:60:12:54:b9:c4:5f:
43+
00:f8:ce:5b:72:28:58:43:7c:d5:25:b7:dd:ec:71:da:aa:3a:
44+
f2:6c
45+
-----BEGIN CERTIFICATE-----
46+
MIIC7zCCAligAwIBAgIJAJrlel8FWi/kMA0GCSqGSIb3DQEBBQUAMFkxCzAJBgNV
47+
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
48+
aWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0xNTEyMDUyMTUw
49+
NDZaFw0xNjAxMDQyMTUwNDZaMFkxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21l
50+
LVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNV
51+
BAMTCWxvY2FsaG9zdDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA8Zu2JGRm
52+
v17adyo5vTmTVigm8dcfyWAc44KkB6IPyNZo/DAvFzA0ac3Y8efHhPnFkLEsQtUj
53+
INId1xgVcAqjHccu3wPAn1zLAiXafSsaCXjlI4rEZDlZDU4VC3V7dfmKTMOd+TEI
54+
1doApdsM3wnl5BTRFw+79s+9PF0UasvB3OECAwEAAaOBvjCBuzAdBgNVHQ4EFgQU
55+
ngnA0R4OB7NJVwpJR/mKX07+I3UwgYsGA1UdIwSBgzCBgIAUngnA0R4OB7NJVwpJ
56+
R/mKX07+I3WhXaRbMFkxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRl
57+
MSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMTCWxv
58+
Y2FsaG9zdIIJAJrlel8FWi/kMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQAD
59+
gYEATDvGQpZ1lqCb9dmxmxtPvdKN8VPth4D1e102bjjIrhpY5TmeQkkSNXarD/qx
60+
H06xhfOjb2DjbA6olQ3IOH/j4/9kdHNQRmWDXxpy+WlEB802AZC5s+3Y17xol90R
61+
rCvsXaTU1eiLYBJUucRfAPjOW3IoWEN81SW33exx2qo68mw=
62+
-----END CERTIFICATE-----

.travis/server.der

755 Bytes
Binary file not shown.

.travis/server.key

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICXQIBAAKBgQDxm7YkZGa/Xtp3Kjm9OZNWKCbx1x/JYBzjgqQHog/I1mj8MC8X
3+
MDRpzdjx58eE+cWQsSxC1SMg0h3XGBVwCqMdxy7fA8CfXMsCJdp9KxoJeOUjisRk
4+
OVkNThULdXt1+YpMw535MQjV2gCl2wzfCeXkFNEXD7v2z708XRRqy8Hc4QIDAQAB
5+
AoGBAIBsJuWzJFYmQfNDU4t8Fg+eqgy0LyYn21Mm9q9D+iXjqcwahH1L1yBCFUWH
6+
0Kqi5NujAQbJKbHhXZEeMQ7r6IT8HjAW800F+M3eRLaMGVbh02L/EpEgUspb8VH+
7+
SZDolJvxCGmkBBgglJwYpFQG6ANXaEU0/uS+aHz0Wptip2NNAkEA+UdCmpY7whXS
8+
5F3LrZE8qjwjEs86RxQoe7+wF7eT4CbXmxvQBwgxMO9ZUhwdUJ3Cm5T4Qu943gp/
9+
hiRIXunrdwJBAPgfgWNE1KpmJALr3opq+mu92D6YWk2aLFQj01kJI1lomRq/ptXB
10+
niMPzzvauiFuNgpGtKKoxzBPM3l8Ii5E4GcCQCBTuHR5tSg3UlEhRM+ufRKKl/XR
11+
f/pFx/Y8Zqa8vOWdw+oukizHSDHTaF74nGie/OTWTdfIXIFXFTCdNfFxHoMCQQDs
12+
k2WT1/IJkp/tZSXnxn6Esht3+13GtiRkCVCfiRX6TsAEgA27rANynMVT5YYpD+NY
13+
wvfCS7i4OBv1TkVs5mErAkAQmGseTKaye5ABFxBOEHT00hRtIE0yojuL6oPEDhkk
14+
SJIBC5XE0vzmMKq9sQ7foqgPork9O4VYBo0q//BO0RWG
15+
-----END RSA PRIVATE KEY-----

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ serde_json = { version = "0.6", optional = true }
3737
time = { version = "0.1.14", optional = true }
3838
unix_socket = { version = ">= 0.3, < 0.5", optional = true, features = ["socket_timeout"] }
3939
uuid = { version = "0.1", optional = true }
40+
security-framework = { version = "0.1.1", optional = true }
4041

4142
[dev-dependencies]
4243
url = "0.2"

src/io/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::io::prelude::*;
66

77
#[cfg(feature = "openssl")]
88
mod openssl;
9+
#[cfg(feature = "security-framework")]
10+
mod security_framework;
911

1012
/// A trait implemented by SSL adaptors.
1113
pub trait StreamWrapper: Read+Write+Send {
@@ -20,6 +22,9 @@ pub trait StreamWrapper: Read+Write+Send {
2022
///
2123
/// If the `openssl` Cargo feature is enabled, this trait will be implemented
2224
/// for `openssl::ssl::SslContext`.
25+
///
26+
/// If the `security-framework` Cargo feature is enabled, this trait will be
27+
/// implemented for `security_framework::secure_transport::ClientBuilder`.
2328
pub trait NegotiateSsl {
2429
/// Negotiates an SSL session, returning a wrapper around the provided
2530
/// stream.

src/io/security_framework.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
extern crate security_framework;
2+
3+
use self::security_framework::secure_transport::{SslStream, ClientBuilder};
4+
use io::{Stream, StreamWrapper, NegotiateSsl};
5+
use std::error::Error;
6+
7+
impl StreamWrapper for SslStream<Stream> {
8+
fn get_ref(&self) -> &Stream {
9+
self.get_ref()
10+
}
11+
12+
fn get_mut(&mut self) -> &mut Stream {
13+
self.get_mut()
14+
}
15+
}
16+
17+
impl NegotiateSsl for ClientBuilder {
18+
fn negotiate_ssl(&self,
19+
domain: &str,
20+
stream: Stream)
21+
-> Result<Box<StreamWrapper>, Box<Error + Send + Sync>> {
22+
let stream = try!(self.handshake(domain, stream));
23+
Ok(Box::new(stream))
24+
}
25+
}

tests/test.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ extern crate rustc_serialize as serialize;
33
extern crate url;
44
#[cfg(feature = "openssl")]
55
extern crate openssl;
6+
#[cfg(feature = "security-framework")]
7+
extern crate security_framework;
68

79
#[cfg(feature = "openssl")]
810
use openssl::ssl::{SslContext, SslMethod};
@@ -671,6 +673,21 @@ fn test_prefer_ssl_conn() {
671673
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
672674
}
673675

676+
#[test]
677+
#[cfg(feature = "security-framework")]
678+
fn security_framework_ssl() {
679+
use security_framework::certificate::SecCertificate;
680+
use security_framework::secure_transport::ClientBuilder;
681+
682+
let certificate = include_bytes!("../.travis/server.der");
683+
let certificate = or_panic!(SecCertificate::from_der(certificate));
684+
let mut builder = ClientBuilder::new();
685+
builder.anchor_certificates(&[certificate]);
686+
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
687+
&mut SslMode::Require(Box::new(builder))));
688+
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
689+
}
690+
674691
#[test]
675692
fn test_plaintext_pass() {
676693
or_panic!(Connection::connect("postgres://pass_user:password@localhost/postgres", &SslMode::None));

0 commit comments

Comments
 (0)