|
1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Implementation for OCSP Transaction object. |
|
15 // |
|
16 // |
|
17 |
|
18 #include "transaction.h" |
|
19 #include "requestencoder.h" |
|
20 #include "responsedecoder.h" |
|
21 #include "ocsp.h" |
|
22 #include "panic.h" |
|
23 |
|
24 #include <x509cert.h> |
|
25 #include <x509certchain.h> |
|
26 |
|
27 COCSPTransaction* COCSPTransaction::NewL(const TDesC8& aURI, |
|
28 MOCSPTransport& aTransport, const TUint aRetryCount, const TInt aTimeout) |
|
29 { |
|
30 COCSPTransaction* self = new (ELeave) COCSPTransaction(aTransport, aRetryCount, aTimeout); |
|
31 CleanupStack::PushL(self); |
|
32 self->ConstructL(aURI); |
|
33 CleanupStack::Pop(self); |
|
34 return self; |
|
35 } |
|
36 |
|
37 COCSPTransaction::COCSPTransaction(MOCSPTransport& aTransport, const TUint aRetryCount, const TInt aTimeout) : |
|
38 CActive(EPriorityNormal), |
|
39 iTransport(aTransport), |
|
40 iRetryCount(aRetryCount), |
|
41 iTimeout(aTimeout) |
|
42 { |
|
43 CActiveScheduler::Add(this); |
|
44 } |
|
45 |
|
46 |
|
47 void COCSPTransaction::ConstructL(const TDesC8& aURI) |
|
48 { |
|
49 iURI = aURI.AllocL(); |
|
50 } |
|
51 |
|
52 |
|
53 COCSPTransaction::~COCSPTransaction() |
|
54 { |
|
55 Cancel(); |
|
56 delete iURI; |
|
57 delete iRequestEncoder; |
|
58 delete iResponseDecoder; |
|
59 } |
|
60 |
|
61 |
|
62 // Create the request, then send it off |
|
63 void COCSPTransaction::SendRequest(COCSPRequest& aRequest, TRequestStatus& aStatus) |
|
64 { |
|
65 __ASSERT_ALWAYS(!iRequest, Panic(KErrInUse)); |
|
66 iClientRequestStatus = &aStatus; |
|
67 aStatus = KRequestPending; |
|
68 |
|
69 iRequest = &aRequest; |
|
70 iState = ESendRequest; |
|
71 SetActive(); |
|
72 |
|
73 TRequestStatus* status = &iStatus; |
|
74 User::RequestComplete(status, KErrNone); |
|
75 } |
|
76 |
|
77 |
|
78 // Safe regardless of whether we have a request outstanding |
|
79 void COCSPTransaction::CancelRequest() |
|
80 { |
|
81 Cancel(); |
|
82 } |
|
83 |
|
84 |
|
85 COCSPResponse* COCSPTransaction::TakeResponse() |
|
86 { |
|
87 __ASSERT_ALWAYS(iResponseDecoder, Panic(KErrNotReady)); |
|
88 return iResponseDecoder->TakeResponse(); |
|
89 } |
|
90 |
|
91 |
|
92 void COCSPTransaction::CompleteClient(const TInt aStatus) |
|
93 { |
|
94 User::RequestComplete(iClientRequestStatus, aStatus); |
|
95 } |
|
96 |
|
97 |
|
98 void COCSPTransaction::RunL() |
|
99 { |
|
100 TInt err = iStatus.Int(); |
|
101 switch (iState) |
|
102 { |
|
103 case ESendRequest: |
|
104 __ASSERT_ALWAYS(err == KErrNone, Panic(KErrCorrupt)); |
|
105 iRequestEncoder = COCSPRequestEncoder::NewL(*iRequest); |
|
106 iTransport.SendRequest(*iURI, iRequestEncoder->Encoding(), iTimeout, iStatus); |
|
107 iState = EFinished; |
|
108 SetActive(); |
|
109 break; |
|
110 |
|
111 case EFinished: |
|
112 { |
|
113 // Depending on the error retry the request |
|
114 // Retry if |
|
115 // - More attempts remaining AND |
|
116 // - error is OCSP::KErrTransportTimeout OR |
|
117 // - error is OCSP::KErrTransportFailure (HTTP response malformed) OR |
|
118 // - invalid response received (OCSPReponse malformed) OR |
|
119 // - OCSPResponseStatus is OCSP::EServerInternalError OR |
|
120 // - OCSPResponseStatus is OCSP::ETryLater |
|
121 // |
|
122 // We won't retry if |
|
123 // - error is OCSP::KErrInvalidURI OR |
|
124 // - error is OCSP::KErrServerNotFound |
|
125 |
|
126 TBool retryNeeded = EFalse; |
|
127 TInt errDecode = KErrNone; |
|
128 if ((err == OCSP::KErrTransportTimeout) || (err == OCSP::KErrTransportFailure)) |
|
129 { |
|
130 retryNeeded = ETrue; |
|
131 } |
|
132 else if (err == KErrNone) |
|
133 { |
|
134 TRAP(errDecode, iResponseDecoder = COCSPResponseDecoder::NewL(iTransport.GetResponse())); |
|
135 if ((errDecode == OCSP::EMalformedResponse) || |
|
136 (errDecode == OCSP::EServerInternalError) || (errDecode == OCSP::ETryLater)) |
|
137 { |
|
138 retryNeeded = ETrue; |
|
139 } |
|
140 } |
|
141 |
|
142 --iRetryCount; |
|
143 if (retryNeeded && iRetryCount > 0) |
|
144 { |
|
145 __ASSERT_ALWAYS(iResponseDecoder == NULL, Panic(KErrCorrupt)); |
|
146 iTransport.SendRequest(*iURI, iRequestEncoder->Encoding(), iTimeout, iStatus); |
|
147 SetActive(); |
|
148 } |
|
149 else |
|
150 { |
|
151 // If retry is not needed and there was an error (including +ve) then pass it up else complete normally |
|
152 if (err != KErrNone) |
|
153 { |
|
154 User::Leave(err); |
|
155 } |
|
156 if (errDecode != KErrNone) |
|
157 { |
|
158 User::Leave(errDecode); |
|
159 } |
|
160 CompleteClient(KErrNone); |
|
161 } |
|
162 } |
|
163 break; |
|
164 |
|
165 default: |
|
166 Panic(KErrCorrupt); |
|
167 } |
|
168 } |
|
169 |
|
170 |
|
171 TInt COCSPTransaction::RunError(TInt aError) |
|
172 { |
|
173 CompleteClient(aError); |
|
174 return KErrNone; |
|
175 } |
|
176 |
|
177 |
|
178 void COCSPTransaction::DoCancel() |
|
179 { |
|
180 iTransport.CancelRequest(); |
|
181 if (iClientRequestStatus) |
|
182 { |
|
183 CompleteClient(KErrCancel); |
|
184 } |
|
185 } |
|
186 |