Automation adds INFO.yaml
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / aaf / certservice / cmpv2client / external / Rdn.java
1 /*
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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====================================================
21  *
22  */
23
24 package org.onap.aaf.certservice.cmpv2client.external;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.stream.Collectors;
30
31 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
32 import org.bouncycastle.asn1.x500.style.BCStyle;
33 import org.bouncycastle.cert.CertException;
34
35 public class Rdn {
36
37     private String tag;
38     private String value;
39     private ASN1ObjectIdentifier aoi;
40
41     public String getValue() {
42         return value;
43     }
44
45     public Rdn(final String tag, final String value) throws CertException {
46         this.tag = tag;
47         this.value = value;
48         this.aoi = getAoi(tag);
49     }
50
51     public Rdn(final String tagValue) throws CertException {
52         List<String> tv = parseRdn("=", tagValue);
53         this.tag = tv.get(0);
54         this.value = tv.get(1);
55         this.aoi = getAoi(this.tag);
56     }
57
58     /**
59      * Splits RDN as string by given delimiter, then trims every part.
60      *
61      * @param splitBy Delimiter which splits value
62      * @param value Value to be splitted
63      * @return List of splitted and trimmed strings
64      */
65     static List<String> parseRdn(String splitBy, String value) {
66         String[] splitted = value.split(splitBy);
67         return Arrays.stream(splitted)
68                 .map(String::trim)
69                 .collect(Collectors.toList());
70     }
71     /**
72      * Parse various forms of DNs into appropriate RDNs, which have the ASN1ObjectIdentifier
73      *
74      * @param delim
75      * @param dnString
76      * @return
77      * @throws CertException
78      */
79
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);
86             if (inQuotes) {
87                 if ('"' == currentCharacter) {
88                     inQuotes = false;
89                 } else {
90                     sb.append(dnString.charAt(i));
91                 }
92             } else {
93                 if ('"' == currentCharacter) {
94                     inQuotes = true;
95                 } else if (delim == currentCharacter) {
96                     if (sb.length() > 0) {
97                         lrnd.add(new Rdn(sb.toString()));
98                         sb.setLength(0);
99                     }
100                 } else {
101                     sb.append(dnString.charAt(i));
102                 }
103             }
104         }
105         if (sb.indexOf("=") > 0) {
106             lrnd.add(new Rdn(sb.toString()));
107         }
108         return lrnd;
109     }
110
111     @Override
112     public String toString() {
113         return tag + '=' + value;
114     }
115
116     ASN1ObjectIdentifier getAoi() {
117         return aoi;
118     }
119
120     ASN1ObjectIdentifier getAoi(String tag) throws CertException {
121         switch (tag.toLowerCase()) {
122             case "cn":
123                 return BCStyle.CN;
124             case "c":
125                 return BCStyle.C;
126             case "st":
127                 return BCStyle.ST;
128             case "l":
129                 return BCStyle.L;
130             case "o":
131                 return BCStyle.O;
132             case "ou":
133                 return BCStyle.OU;
134             case "dc":
135                 return BCStyle.DC;
136             case "gn":
137                 return BCStyle.GIVENNAME;
138             case "sn":
139                 return BCStyle.SN;
140             case "email":
141             case "e":
142             case "emailaddress":
143                 return BCStyle.EmailAddress;
144             case "initials":
145                 return BCStyle.INITIALS;
146             case "pseudonym":
147                 return BCStyle.PSEUDONYM;
148             case "generationqualifier":
149                 return BCStyle.GENERATION;
150             case "serialnumber":
151                 return BCStyle.SERIALNUMBER;
152             default:
153                 throw new CertException(
154                         "Unknown ASN1ObjectIdentifier for tag " + tag);
155         }
156     }
157 }