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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright (C) 2015, Google Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.transport;
import static org.eclipse.jgit.transport.PushCertificateParser.NONCE;
import static org.eclipse.jgit.transport.PushCertificateParser.PUSHEE;
import static org.eclipse.jgit.transport.PushCertificateParser.PUSHER;
import static org.eclipse.jgit.transport.PushCertificateParser.VERSION;
import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
import org.eclipse.jgit.internal.JGitText;
/**
* The required information to verify the push.
* <p>
* A valid certificate will not return null from any getter methods; callers may
* assume that any null value indicates a missing or invalid certificate.
*
* @since 4.0
*/
public class PushCertificate {
/** Verification result of the nonce returned during push. */
public enum NonceStatus {
/** Nonce was not expected, yet client sent one anyway. */
UNSOLICITED,
/** Nonce is invalid and did not match server's expectations. */
BAD,
/** Nonce is required, but was not sent by client. */
MISSING,
/**
* Received nonce matches sent nonce, or is valid within the accepted slop
* window.
*/
OK,
/** Received nonce is valid, but outside the accepted slop window. */
SLOP
}
private final String version;
private final PushCertificateIdent pusher;
private final String pushee;
private final String nonce;
private final NonceStatus nonceStatus;
private final List<ReceiveCommand> commands;
private final String signature;
PushCertificate(String version, PushCertificateIdent pusher, String pushee,
String nonce, NonceStatus nonceStatus, List<ReceiveCommand> commands,
String signature) {
if (version == null || version.isEmpty()) {
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().pushCertificateInvalidField, VERSION));
}
if (pusher == null) {
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().pushCertificateInvalidField, PUSHER));
}
if (nonce == null || nonce.isEmpty()) {
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().pushCertificateInvalidField, NONCE));
}
if (nonceStatus == null) {
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().pushCertificateInvalidField,
"nonce status")); //$NON-NLS-1$
}
if (commands == null || commands.isEmpty()) {
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().pushCertificateInvalidField,
"command")); //$NON-NLS-1$
}
if (signature == null || signature.isEmpty()) {
throw new IllegalArgumentException(
JGitText.get().pushCertificateInvalidSignature);
}
if (!signature.startsWith(PushCertificateParser.BEGIN_SIGNATURE)
|| !signature.endsWith(PushCertificateParser.END_SIGNATURE + '\n')) {
throw new IllegalArgumentException(
JGitText.get().pushCertificateInvalidSignature);
}
this.version = version;
this.pusher = pusher;
this.pushee = pushee;
this.nonce = nonce;
this.nonceStatus = nonceStatus;
this.commands = commands;
this.signature = signature;
}
/**
* Get the certificate version string.
*
* @return the certificate version string.
* @since 4.1
*/
public String getVersion() {
return version;
}
/**
* Get the raw line that signed the cert, as a string.
*
* @return the raw line that signed the cert, as a string.
* @since 4.0
*/
public String getPusher() {
return pusher.getRaw();
}
/**
* Get identity of the pusher who signed the cert.
*
* @return identity of the pusher who signed the cert.
* @since 4.1
*/
public PushCertificateIdent getPusherIdent() {
return pusher;
}
/**
* Get URL of the repository the push was originally sent to.
*
* @return URL of the repository the push was originally sent to.
* @since 4.0
*/
public String getPushee() {
return pushee;
}
/**
* Get the raw nonce value that was presented by the pusher.
*
* @return the raw nonce value that was presented by the pusher.
* @since 4.1
*/
public String getNonce() {
return nonce;
}
/**
* Get verification status of the nonce embedded in the certificate.
*
* @return verification status of the nonce embedded in the certificate.
* @since 4.0
*/
public NonceStatus getNonceStatus() {
return nonceStatus;
}
/**
* Get the list of commands as one string to be feed into the signature
* verifier.
*
* @return the list of commands as one string to be feed into the signature
* verifier.
* @since 4.1
*/
public List<ReceiveCommand> getCommands() {
return commands;
}
/**
* Get the raw signature
*
* @return the raw signature, consisting of the lines received between the
* lines {@code "----BEGIN GPG SIGNATURE-----\n"} and
* {@code "----END GPG SIGNATURE-----\n}", inclusive.
* @since 4.0
*/
public String getSignature() {
return signature;
}
/**
* Get text payload of the certificate for the signature verifier.
*
* @return text payload of the certificate for the signature verifier.
* @since 4.1
*/
public String toText() {
return toStringBuilder().toString();
}
/**
* Get original text payload plus signature
*
* @return original text payload plus signature; the final output will be
* valid as input to
* {@link org.eclipse.jgit.transport.PushCertificateParser#fromString(String)}.
* @since 4.1
*/
public String toTextWithSignature() {
return toStringBuilder().append(signature).toString();
}
private StringBuilder toStringBuilder() {
StringBuilder sb = new StringBuilder()
.append(VERSION).append(' ').append(version).append('\n')
.append(PUSHER).append(' ').append(getPusher())
.append('\n');
if (pushee != null) {
sb.append(PUSHEE).append(' ').append(pushee).append('\n');
}
sb.append(NONCE).append(' ').append(nonce).append('\n')
.append('\n');
for (ReceiveCommand cmd : commands) {
sb.append(cmd.getOldId().name())
.append(' ').append(cmd.getNewId().name())
.append(' ').append(cmd.getRefName()).append('\n');
}
return sb;
}
/** {@inheritDoc} */
@Override
public int hashCode() {
return signature.hashCode();
}
/** {@inheritDoc} */
@Override
public boolean equals(Object o) {
if (!(o instanceof PushCertificate)) {
return false;
}
PushCertificate p = (PushCertificate) o;
return version.equals(p.version)
&& pusher.equals(p.pusher)
&& Objects.equals(pushee, p.pushee)
&& nonceStatus == p.nonceStatus
&& signature.equals(p.signature)
&& commandsEqual(this, p);
}
private static boolean commandsEqual(PushCertificate c1, PushCertificate c2) {
if (c1.commands.size() != c2.commands.size()) {
return false;
}
for (int i = 0; i < c1.commands.size(); i++) {
ReceiveCommand cmd1 = c1.commands.get(i);
ReceiveCommand cmd2 = c2.commands.get(i);
if (!cmd1.getOldId().equals(cmd2.getOldId())
|| !cmd1.getNewId().equals(cmd2.getNewId())
|| !cmd1.getRefName().equals(cmd2.getRefName())) {
return false;
}
}
return true;
}
/** {@inheritDoc} */
@Override
public String toString() {
return getClass().getSimpleName() + '['
+ toTextWithSignature() + ']';
}
}
|