fe4e20ff180d36c6765a5bba6e8835fa1dee178e
[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.configure.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; 
39             break;
40             case "c":case "C":            aoi = BCStyle.C;
41             break;
42             case "st":case "ST":            aoi = BCStyle.ST;
43             break;
44             case "l":case "L":              aoi = BCStyle.L;
45             break;
46             case "o":case "O":            aoi = BCStyle.O;
47             break;
48             case "ou":case "OU":            aoi = BCStyle.OU;
49             break;
50             case "dc":case "DC":            aoi = BCStyle.DC;
51             break;
52             case "gn":case "GN":            aoi = BCStyle.GIVENNAME; 
53             break;
54             case "sn":case "SN":            aoi = BCStyle.SN; 
55             break;  // surname
56             case "email":case "EMAIL":
57             case "emailaddress":
58             case "EMAILADDRESS":            aoi = BCStyle.EmailAddress;
59             break; // should be SAN extension
60             case "initials":                aoi = BCStyle.INITIALS; 
61             break; 
62             case "pseudonym":            aoi = BCStyle.PSEUDONYM; 
63             break;
64             case "generationQualifier":    aoi = BCStyle.GENERATION; 
65             break;
66             case "serialNumber":            aoi = BCStyle.SERIALNUMBER; 
67             break;
68             default:
69                 throw new CertException("Unknown ASN1ObjectIdentifier for " + tv[0] + " in " + tagValue);
70         }
71         tag = tv[0];
72         value = tv[1];
73     }
74     
75     /**
76      * Parse various forms of DNs into appropriate RDNs, which have the ASN1ObjectIdentifier
77      * @param delim
78      * @param dnString
79      * @return
80      * @throws CertException
81      */
82     public static List<RDN> parse(final char delim, final String dnString ) throws CertException {
83         List<RDN> lrnd = new ArrayList<>();
84         StringBuilder sb = new StringBuilder();
85         boolean inQuotes = false;
86         for (int i=0;i<dnString.length();++i) {
87             char c = dnString.charAt(i);
88             if (inQuotes) {
89                 if ('"' == c) {
90                     inQuotes=false;
91                 } else {
92                     sb.append(dnString.charAt(i));
93                 }
94             } else {
95                 if ('"' == c) {
96                     inQuotes=true;
97                 } else if (delim==c) {
98                     if (sb.length()>0) {
99                         lrnd.add(new RDN(sb.toString()));
100                         sb.setLength(0);
101                     }
102                 } else {
103                     sb.append(dnString.charAt(i));
104                 }
105             }
106         }
107         if (sb.indexOf("=")>0) {
108             lrnd.add(new RDN(sb.toString()));
109         }
110         return lrnd;
111     }
112     
113     @Override
114     public String toString() {
115         return tag + '=' + value;
116     }
117 }