AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-certman / src / main / java / org / onap / aaf / auth / cm / cert / RDN.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21 package org.onap.aaf.auth.cm.cert;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
27 import org.bouncycastle.asn1.x500.style.BCStyle;
28 import org.onap.aaf.cadi.cm.CertException;
29 import org.onap.aaf.cadi.util.Split;
30
31 public class RDN {
32         public String tag;
33         public String value;
34         public ASN1ObjectIdentifier aoi;
35         public RDN(final String tagValue) throws CertException {
36                 String[] tv = Split.splitTrim('=',tagValue);
37                 switch(tv[0]) {
38                         case "cn":case "CN":                    aoi = BCStyle.CN; break;
39                         case "c":case "C":                      aoi = BCStyle.C;break;
40                         case "st":case "ST":                    aoi = BCStyle.ST;break;
41                         case "l":case "L":                      aoi = BCStyle.L;break;
42                         case "o":case "O":                      aoi = BCStyle.O;break;
43                         case "ou":case "OU":                    aoi = BCStyle.OU;break;
44                         case "dc":case "DC":                    aoi = BCStyle.DC;break;
45                         case "gn":case "GN":                    aoi = BCStyle.GIVENNAME; break;
46                         case "sn":case "SN":                    aoi = BCStyle.SN; break;  // surname
47                         case "email":case "EMAIL":
48                         case "emailaddress":
49                         case "EMAILADDRESS":                    aoi = BCStyle.EmailAddress;break; // should be SAN extension
50                         case "initials":                                aoi = BCStyle.INITIALS; break; 
51                         case "pseudonym":                       aoi = BCStyle.PSEUDONYM; break;
52                         case "generationQualifier":     aoi = BCStyle.GENERATION; break;
53                         case "serialNumber":                    aoi = BCStyle.SERIALNUMBER; break;
54                         default:
55                                 throw new CertException("Unknown ASN1ObjectIdentifier for " + tv[0]);
56                 }
57                 tag = tv[0];
58                 value = tv[1];
59         }
60         
61         /**
62          * Parse various forms of DNs into appropriate RDNs, which have the ASN1ObjectIdentifier
63          * @param delim
64          * @param dnString
65          * @return
66          * @throws CertException
67          */
68         public static List<RDN> parse(final char delim, final String dnString ) throws CertException {
69                 List<RDN> lrnd = new ArrayList<RDN>();
70                 StringBuilder sb = new StringBuilder();
71                 boolean inQuotes = false;
72                 for(int i=0;i<dnString.length();++i) {
73                         char c = dnString.charAt(i);
74                         if(inQuotes) {
75                                 if('"' == c) {
76                                         inQuotes=false;
77                                 } else {
78                                         sb.append(dnString.charAt(i));
79                                 }
80                         } else {
81                                 if('"' == c) {
82                                         inQuotes=true;
83                                 } else if(delim==c) {
84                                         lrnd.add(new RDN(sb.toString()));
85                                         sb.setLength(0);
86                                 } else {
87                                         sb.append(dnString.charAt(i));
88                                 }
89                         }
90                 }
91                 if(sb.indexOf("=")>0) {
92                         lrnd.add(new RDN(sb.toString()));
93                 }
94                 return lrnd;
95         }
96         
97         @Override
98         public String toString() {
99                 return tag + '=' + value;
100         }
101 }