~ubuntu-branches/debian/squeeze/f-spot/squeeze

« back to all changes in this revision

Viewing changes to extensions/Exporters/TabbloExport/ApplicationCentricCertificatePolicy.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane, Mirco Bauer, Iain Lane
  • Date: 2009-02-07 20:23:32 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: james.westby@ubuntu.com-20090207202332-oc93rfjo1st0571s
Tags: 0.5.0.3-2
[ Mirco Bauer]
* Upload to unstable.
* debian/control:
  + Lowered GNOME# build-deps to 2.0 ABI as that transition didn't happen
    yet in unstable.

[ Iain Lane ]
* debian/patches/svn-r4545_locales-import.dpatch: Patch backported from SVN
  trunk revision 4545 - initialize the translation catalog earlier (LP: #293305)
  (Closes: #514457). Thanks to Florian Heinle for finding the patch and to
  Chris Coulson for preparing the update.
* debian/control: Build-depend on libmono-dev (>= 1.2.4) to match configure
  checks.
* debian/rules: Pass CSC=/usr/bin/csc to configure for gio-sharp to fix FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// FSpotTabbloExport.ApplicationCentricCertificatePolicy
 
3
//
 
4
// Authors:
 
5
//      Wojciech Dzierzanowski (wojciech.dzierzanowski@gmail.com)
 
6
//
 
7
// (C) Copyright 2008 Wojciech Dzierzanowski
 
8
//
 
9
 
 
10
// Permission is hereby granted, free of charge, to any person obtaining
 
11
// a copy of this software and associated documentation files (the
 
12
// "Software"), to deal in the Software without restriction, including
 
13
// without limitation the rights to use, copy, modify, merge, publish,
 
14
// distribute, sublicense, and/or sell copies of the Software, and to
 
15
// permit persons to whom the Software is furnished to do so, subject to
 
16
// the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
28
//
 
29
 
 
30
using System;
 
31
using System.Collections.Generic;
 
32
using System.Diagnostics;
 
33
using System.IO;
 
34
using System.IO.IsolatedStorage;
 
35
using System.Net;
 
36
using System.Runtime.Serialization;
 
37
using System.Runtime.Serialization.Formatters.Binary;
 
38
using System.Security.Cryptography.X509Certificates;
 
39
 
 
40
using FSpot.Utils;
 
41
 
 
42
namespace FSpotTabbloExport {
 
43
 
 
44
        class ApplicationCentricCertificatePolicy : ICertificatePolicy {
 
45
 
 
46
                protected enum Decision {
 
47
                        DontTrust,
 
48
                        TrustOnce,
 
49
                        TrustAlways
 
50
                };
 
51
 
 
52
                private Dictionary<string, int> cert_hashes;
 
53
 
 
54
                private static readonly IsolatedStorageFile isolated_store =
 
55
                                IsolatedStorageFile.GetUserStoreForAssembly ();
 
56
 
 
57
                private const string StoreName = "cert_hashes";
 
58
 
 
59
 
 
60
                public bool CheckValidationResult (ServicePoint service_point,
 
61
                                                   X509Certificate certificate,
 
62
                                                   WebRequest request,
 
63
                                                   int problem)
 
64
                {
 
65
                        Log.DebugFormat ("Checking validation result for {0}: problem={1}", request.RequestUri, problem);
 
66
 
 
67
                        if (0 == problem) {
 
68
                                return true;
 
69
                        }
 
70
 
 
71
                        // Only try to deal with the problem if it is a trust
 
72
                        // failure.
 
73
                        if (-2146762486 != problem) {
 
74
                                return false;
 
75
                        }
 
76
 
 
77
                        LoadCertificates ();
 
78
 
 
79
                        string hash = certificate.GetCertHashString ();
 
80
                        Log.DebugFormat ("Certificate hash: " + hash);
 
81
 
 
82
                        int stored_problem = 0;
 
83
                        if (cert_hashes.TryGetValue (hash, out stored_problem)
 
84
                                        && problem == stored_problem) {
 
85
                                Log.DebugFormat ("We already trust this site");
 
86
                                return true;
 
87
                        }
 
88
 
 
89
                        Decision decision = GetDecision (certificate, request);
 
90
                        Log.DebugFormat ("Decision: " + decision);
 
91
 
 
92
                        switch (decision) {
 
93
                        case Decision.DontTrust:
 
94
                                return false;
 
95
                        case Decision.TrustOnce:
 
96
                                return true;
 
97
                        case Decision.TrustAlways:
 
98
                                SaveCertificate (hash, problem);
 
99
                                return true;
 
100
                        default:
 
101
                                Debug.Assert (false, "Unknown decision");
 
102
                                return false;
 
103
                        }
 
104
                }
 
105
 
 
106
 
 
107
                protected virtual Decision GetDecision (
 
108
                                X509Certificate certificate,
 
109
                                WebRequest request)
 
110
                {
 
111
                        Decision decision = Decision.DontTrust;
 
112
                        Log.DebugFormat ("Making the default decision: " + decision);
 
113
                        return decision;
 
114
                }
 
115
 
 
116
 
 
117
                private void LoadCertificates ()
 
118
                {
 
119
                        using (IsolatedStorageFileStream isol_stream =
 
120
                                        new IsolatedStorageFileStream (
 
121
                                                        StoreName,
 
122
                                                        FileMode.OpenOrCreate,
 
123
                                                        FileAccess.Read,
 
124
                                                        isolated_store)) {
 
125
                                try {
 
126
                                        BinaryFormatter formatter =
 
127
                                                        new BinaryFormatter ();
 
128
                                        cert_hashes = (Dictionary<string, int>)
 
129
                                                        formatter.Deserialize (
 
130
                                                                isol_stream);
 
131
                                } catch (SerializationException e) {
 
132
                                        // FIXME: handle
 
133
                                        Log.Exception (e);
 
134
                                }
 
135
                        }
 
136
 
 
137
                        if (null == cert_hashes) {
 
138
                                cert_hashes = new Dictionary<string,int> ();
 
139
                        }
 
140
                }
 
141
 
 
142
 
 
143
                private void SaveCertificate (string hash, int problem)
 
144
                {
 
145
                        cert_hashes.Add (hash, problem);
 
146
 
 
147
                        using (IsolatedStorageFileStream isolated_stream =
 
148
                                        new IsolatedStorageFileStream (
 
149
                                                        StoreName,
 
150
                                                        FileMode.OpenOrCreate,
 
151
                                                        FileAccess.Write,
 
152
                                                        isolated_store)) {
 
153
                                try {
 
154
                                        BinaryFormatter formatter =
 
155
                                                        new BinaryFormatter ();
 
156
                                        formatter.Serialize (isolated_stream,
 
157
                                                        cert_hashes);
 
158
                                } catch (SerializationException e) {
 
159
                                        // FIXME: handle
 
160
                                        Log.Exception (e);
 
161
                                }
 
162
                        }
 
163
                }
 
164
        }
 
165
}