2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ===========================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END====================================================
24 package org.onap.aaf.certservice.cmpv2client.external;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.stream.Collectors;
31 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
32 import org.bouncycastle.asn1.x500.style.BCStyle;
33 import org.bouncycastle.cert.CertException;
39 private ASN1ObjectIdentifier aoi;
41 public String getValue() {
45 public Rdn(final String tag, final String value) throws CertException {
48 this.aoi = getAoi(tag);
51 public Rdn(final String tagValue) throws CertException {
52 List<String> tv = parseRdn("=", tagValue);
54 this.value = tv.get(1);
55 this.aoi = getAoi(this.tag);
59 * Splits RDN as string by given delimiter, then trims every part.
61 * @param splitBy Delimiter which splits value
62 * @param value Value to be splitted
63 * @return List of splitted and trimmed strings
65 static List<String> parseRdn(String splitBy, String value) {
66 String[] splitted = value.split(splitBy);
67 return Arrays.stream(splitted)
69 .collect(Collectors.toList());
72 * Parse various forms of DNs into appropriate RDNs, which have the ASN1ObjectIdentifier
77 * @throws CertException
80 public static List<Rdn> parse(final char delim, final String dnString) throws CertException {
81 List<Rdn> lrnd = new ArrayList<>();
82 StringBuilder sb = new StringBuilder();
83 boolean inQuotes = false;
84 for (int i = 0; i < dnString.length(); ++i) {
85 char currentCharacter = dnString.charAt(i);
87 if ('"' == currentCharacter) {
90 sb.append(dnString.charAt(i));
93 if ('"' == currentCharacter) {
95 } else if (delim == currentCharacter) {
96 if (sb.length() > 0) {
97 lrnd.add(new Rdn(sb.toString()));
101 sb.append(dnString.charAt(i));
105 if (sb.indexOf("=") > 0) {
106 lrnd.add(new Rdn(sb.toString()));
112 public String toString() {
113 return tag + '=' + value;
116 ASN1ObjectIdentifier getAoi() {
120 ASN1ObjectIdentifier getAoi(String tag) throws CertException {
121 switch (tag.toLowerCase()) {
137 return BCStyle.GIVENNAME;
143 return BCStyle.EmailAddress;
145 return BCStyle.INITIALS;
147 return BCStyle.PSEUDONYM;
148 case "generationqualifier":
149 return BCStyle.GENERATION;
151 return BCStyle.SERIALNUMBER;
153 throw new CertException(
154 "Unknown ASN1ObjectIdentifier for tag " + tag);