You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by co...@apache.org on 2020/05/08 07:20:29 UTC

[directory-kerby] branch trunk updated: [DIRKRB-741] Asn1Utf8String incorrectly calculates value length

This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/directory-kerby.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 11e06c6  [DIRKRB-741] Asn1Utf8String incorrectly calculates value length
     new c778e28  Merge pull request #46 from michael-o/trunk
11e06c6 is described below

commit 11e06c6aaf453e7aadb60393b87bb28702d066ca
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Wed May 6 22:07:56 2020 +0200

    [DIRKRB-741] Asn1Utf8String incorrectly calculates value length
---
 .../org/apache/kerby/asn1/type/Asn1Utf8String.java | 16 ++++--
 .../org/apache/kerby/asn1/Asn1Utf8StringTest.java  | 61 ++++++++++++++++++++++
 2 files changed, 73 insertions(+), 4 deletions(-)

diff --git a/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Utf8String.java b/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Utf8String.java
index a92f145..c3709fd 100644
--- a/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Utf8String.java
+++ b/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Utf8String.java
@@ -6,16 +6,16 @@
  *  to you under the Apache License, Version 2.0 (the
  *  "License"); you may not use this file except in compliance
  *  with the License.  You may obtain a copy of the License at
- *  
+ *
  *    http://www.apache.org/licenses/LICENSE-2.0
- *  
+ *
  *  Unless required by applicable law or agreed to in writing,
  *  software distributed under the License is distributed on an
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  *  KIND, either express or implied.  See the License for the
  *  specific language governing permissions and limitations
- *  under the License. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.kerby.asn1.type;
 
@@ -39,6 +39,14 @@ public class Asn1Utf8String extends Asn1String {
         setBytes(bytes);
     }
 
+    @Override
+    protected int encodingBodyLength() {
+        if (getValue() != null) {
+            return getValue().getBytes(StandardCharsets.UTF_8).length;
+        }
+        return 0;
+    }
+
     protected void toValue() throws IOException {
         byte[] bytes = getBytes();
         setValue(new String(bytes, StandardCharsets.UTF_8));
diff --git a/kerby-common/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1Utf8StringTest.java b/kerby-common/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1Utf8StringTest.java
new file mode 100644
index 0000000..c5924ae
--- /dev/null
+++ b/kerby-common/kerby-asn1/src/test/java/org/apache/kerby/asn1/Asn1Utf8StringTest.java
@@ -0,0 +1,61 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.asn1;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.kerby.asn1.type.Asn1Utf8String;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class Asn1Utf8StringTest {
+
+    @Test
+    public void testEncodingLength() throws Exception {
+
+        // Testing length <= 127
+        String input = "E-Tuğra EBG Bilişim Teknolojileri ve Hizmetleri A.Ş.";
+        byte[] inputUtf8Bytes = input.getBytes(StandardCharsets.UTF_8);
+        byte[] encoded = new byte[2 + inputUtf8Bytes.length];
+        encoded[0] = 0x0c;
+        encoded[1] = (byte) inputUtf8Bytes.length;
+        System.arraycopy(inputUtf8Bytes, 0, encoded, 2, inputUtf8Bytes.length);
+
+        Asn1Utf8String utf8String = (Asn1Utf8String) Asn1.decode(encoded);
+        assertThat(utf8String.encodingLength()).isEqualTo(encoded.length);
+        assertThat(utf8String.encode()).isEqualTo(encoded);
+
+        // Testing length >= 128
+        input = "Википедия расположена на серверах Фонда Викимедиа "
+                + "— некоммерческой организации, также обеспечивающей работу ряда других проектов";
+        inputUtf8Bytes = input.getBytes(StandardCharsets.UTF_8);
+        encoded = new byte[3 + inputUtf8Bytes.length];
+        encoded[0] = 0x0c;
+        encoded[1] = (byte) ((1 << 7) | 1);
+        encoded[2] = (byte) inputUtf8Bytes.length;
+        System.arraycopy(inputUtf8Bytes, 0, encoded, 3, inputUtf8Bytes.length);
+
+        utf8String = (Asn1Utf8String) Asn1.decode(encoded);
+        assertThat(utf8String.encodingLength()).isEqualTo(encoded.length);
+        assertThat(utf8String.encode()).isEqualTo(encoded);
+    }
+
+}