~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/Security/Policy.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// 
2
2
// Policy.cs: Implements the managed SecPolicy wrapper.
3
3
//
4
 
// Authors: Miguel de Icaza
5
 
//     
 
4
// Authors: 
 
5
//      Miguel de Icaza
 
6
//  Sebastien Pouliot  <sebastien@xamarin.com>
 
7
//
6
8
// Copyright 2010 Novell, Inc
 
9
// Copyright 2012 Xamarin Inc.
7
10
//
8
11
// Permission is hereby granted, free of charge, to any person obtaining
9
12
// a copy of this software and associated documentation files (the
34
37
        public class SecPolicy : INativeObject, IDisposable {
35
38
                IntPtr handle;
36
39
 
37
 
                internal SecPolicy (IntPtr handle)
 
40
                internal SecPolicy (IntPtr handle) 
 
41
                        : this (handle, false)
38
42
                {
39
 
                        if (handle == IntPtr.Zero)
40
 
                                throw new Exception ("Invalid parameters to context creation");
41
 
 
42
 
                        CFRetain (handle);
43
 
                        this.handle = handle;
44
43
                }
45
44
 
46
45
                [Preserve (Conditional=true)]
47
46
                internal SecPolicy (IntPtr handle, bool owns)
48
47
                {
 
48
                        if (handle == IntPtr.Zero)
 
49
                                throw new Exception ("Invalid handle");
 
50
 
 
51
                        this.handle = handle;
49
52
                        if (!owns)
50
 
                                CFRetain (handle);
51
 
 
52
 
                        this.handle = handle;
 
53
                                CFObject.CFRetain (handle);
 
54
                }
 
55
 
 
56
                [DllImport (Constants.SecurityLibrary)]
 
57
                extern static IntPtr /* SecPolicyRef */ SecPolicyCreateSSL (bool server, IntPtr /* CFStringRef */ hostname);
 
58
 
 
59
                static public SecPolicy CreateSslPolicy (bool server, string hostName)
 
60
                {
 
61
#if false
 
62
                        NSString host = hostName == null ? null : new NSString (hostName);
 
63
                        IntPtr handle = host == null ? IntPtr.Zero : host.Handle; 
 
64
                        SecPolicy policy = new SecPolicy (SecPolicyCreateSSL (server, handle), true);
 
65
                        if (host != null)
 
66
                                host.Dispose ();
 
67
#else
 
68
                        CFString host = hostName == null ? null : new CFString (hostName);
 
69
                        IntPtr handle = host == null ? IntPtr.Zero : host.Handle; 
 
70
                        SecPolicy policy = new SecPolicy (SecPolicyCreateSSL (server, handle), true);
 
71
                        if (host != null)
 
72
                                host.Dispose ();
 
73
#endif
 
74
                        return policy;
 
75
                }
 
76
 
 
77
                [DllImport (Constants.SecurityLibrary)]
 
78
                extern static IntPtr /* SecPolicyRef */ SecPolicyCreateBasicX509 ();
 
79
 
 
80
                static public SecPolicy CreateBasicX509Policy ()
 
81
                {
 
82
                        return new SecPolicy (SecPolicyCreateBasicX509 (), true);
53
83
                }
54
84
 
55
85
                ~SecPolicy ()
67
97
                        get { return handle; }
68
98
                }
69
99
 
70
 
                [DllImport (Constants.CoreFoundationLibrary)]
71
 
                extern static void CFRelease (IntPtr handle);
72
 
 
73
 
                [DllImport (Constants.CoreFoundationLibrary)]
74
 
                extern static void CFRetain (IntPtr handle);
75
 
 
76
100
                protected virtual void Dispose (bool disposing)
77
101
                {
78
102
                        if (handle != IntPtr.Zero){
79
 
                                CFRelease (handle);
 
103
                                CFObject.CFRelease (handle);
80
104
                                handle = IntPtr.Zero;
81
105
                        }
82
106
                }