Skip to content

Commit 09f0812

Browse files
Apply password normalisation using ICU4J
RES-465
1 parent cceb3c1 commit 09f0812

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

NOTICE.txt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,45 @@ Are you using this icon set? Send me an email
9292
mjames at gmail dot com
9393

9494
Any other questions about this icon set please
95-
contact mjames at gmail dot com
95+
contact mjames at gmail dot com
96+
97+
--------------------------------------------
98+
99+
ICU4J under the following license:
100+
101+
102+
COPYRIGHT AND PERMISSION NOTICE (ICU 58 and later)
103+
104+
Copyright © 1991-2020 Unicode, Inc. All rights reserved.
105+
Distributed under the Terms of Use in https://www.unicode.org/copyright.html.
106+
107+
Permission is hereby granted, free of charge, to any person obtaining
108+
a copy of the Unicode data files and any associated documentation
109+
(the "Data Files") or Unicode software and any associated documentation
110+
(the "Software") to deal in the Data Files or Software
111+
without restriction, including without limitation the rights to use,
112+
copy, modify, merge, publish, distribute, and/or sell copies of
113+
the Data Files or Software, and to permit persons to whom the Data Files
114+
or Software are furnished to do so, provided that either
115+
(a) this copyright and permission notice appear with all copies
116+
of the Data Files or Software, or
117+
(b) this copyright and permission notice appear in associated
118+
Documentation.
119+
120+
THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF
121+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
122+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
123+
NONINFRINGEMENT OF THIRD PARTY RIGHTS.
124+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
125+
NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
126+
DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
127+
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
128+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
129+
PERFORMANCE OF THE DATA FILES OR SOFTWARE.
130+
131+
Except as contained in this notice, the name of a copyright holder
132+
shall not be used in advertising or otherwise to promote the sale,
133+
use or other dealings in these Data Files or Software without prior
134+
written authorization of the copyright holder.
135+
136+
For transitive 3rd-party dependencies through ICU4J, see https://raw.githubusercontent.com/unicode-org/icu/46861a5c78367f7c720559243d6bf96146ee070f/icu4c/LICENSE

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>dom4j</artifactId>
8787
<version>${dom4j.version}</version>
8888
</dependency>
89+
<dependency>
90+
<groupId>com.ibm.icu</groupId>
91+
<artifactId>icu4j</artifactId>
92+
<version>69.1</version>
93+
</dependency>
8994
<dependency>
9095
<groupId>com.itextpdf</groupId>
9196
<artifactId>pdftest</artifactId>

src/main/java/com/itextpdf/rups/model/PdfFile.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.kernel.pdf.PdfWriter;
5050
import com.itextpdf.kernel.pdf.ReaderProperties;
5151

52+
import com.ibm.icu.text.StringPrepParseException;
53+
import com.ibm.icu.text.StringPrep;
5254
import javax.swing.*;
5355
import java.io.*;
5456
import java.nio.charset.StandardCharsets;
@@ -88,6 +90,8 @@ public class PdfFile {
8890

8991
protected boolean readOnly = false;
9092

93+
public static final int MAX_PASSWORD_BYTE_LENGTH = 127;
94+
9195
/**
9296
* Constructs a PdfFile object.
9397
*
@@ -125,6 +129,25 @@ public PdfFile(byte[] file, boolean readOnly) throws IOException, PdfException {
125129
}
126130
}
127131

132+
private static byte[] preparePasswordForOpen(String inputPassword) {
133+
StringPrep prep = StringPrep.getInstance(StringPrep.RFC4013_SASLPREP);
134+
String prepped;
135+
try {
136+
// we're invoking StringPrep to open a document -> pass ALLOW_UNASSIGNED
137+
prepped = prep.prepare(inputPassword, StringPrep.ALLOW_UNASSIGNED);
138+
} catch (StringPrepParseException e) {
139+
throw new PdfException("Failed to process password", e);
140+
}
141+
byte[] resultingBytes = prepped.getBytes(StandardCharsets.UTF_8);
142+
if (resultingBytes.length <= MAX_PASSWORD_BYTE_LENGTH) {
143+
return resultingBytes;
144+
} else {
145+
byte[] trimmed = new byte[MAX_PASSWORD_BYTE_LENGTH];
146+
System.arraycopy(resultingBytes, 0, trimmed, 0, trimmed.length);
147+
return trimmed;
148+
}
149+
}
150+
128151
private static byte[] requestPassword() {
129152
final JPasswordField passwordField = new JPasswordField(32);
130153

@@ -137,8 +160,8 @@ public void selectInitialValue() {
137160

138161
pane.createDialog(null, "Enter the User or Owner Password of this PDF file").setVisible(true);
139162

140-
// TODO RES-427: SASLprep & truncate this
141-
return new String(passwordField.getPassword()).getBytes(StandardCharsets.UTF_8);
163+
String passwordString = new String(passwordField.getPassword());
164+
return preparePasswordForOpen(passwordString);
142165
}
143166

144167
/**

0 commit comments

Comments
 (0)