16f621713937c4792d6c349c20b6571d02f18571
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / principal / X509Principal.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
22 package org.onap.aaf.cadi.principal;
23
24 import java.io.IOException;
25 import java.security.cert.CertificateEncodingException;
26 import java.security.cert.X509Certificate;
27 import java.util.regex.Pattern;
28
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.GetCred;
31
32 public class X509Principal extends BearerPrincipal implements GetCred {
33         private static final Pattern pattern = Pattern.compile("[a-zA-Z0-9]*\\@[a-zA-Z0-9.]*");
34         private final X509Certificate cert;
35         private final String name;
36         private TagLookup tagLookup;
37         private byte[] content;  
38
39         public X509Principal(String identity, X509Certificate cert) {
40                 name = identity;
41                 content = null;
42                 this.cert = cert;
43                 tagLookup = null;
44         }
45
46         public X509Principal(String identity, X509Certificate cert, byte[] content) {
47                 name = identity;
48                 this.content = content;
49                 this.cert = cert;
50                 tagLookup = null;
51         }
52
53         public X509Principal(X509Certificate cert, byte[] content) throws IOException {
54                 this.content=content;
55                 this.cert = cert;
56                 String _name = null;
57                 String subj = cert.getSubjectDN().getName();
58                 int cn = subj.indexOf("OU=");
59                 if(cn>=0) {
60                         cn+=3;
61                         int space = subj.indexOf(',',cn);
62                         if(space>=0) {
63                                 String id = subj.substring(cn, space);
64                                 if(pattern.matcher(id).matches()) {
65                                         _name = id;
66                                 }
67                         }
68                 }
69                 if(_name==null) {
70                         throw new IOException("X509 does not have Identity as CN");
71                 }
72                 name = _name;
73                 tagLookup = null;
74         }
75         
76         public String getAsHeader() throws IOException {
77                 try {
78                         if(content==null) {
79                                 content=cert.getEncoded();
80                         }
81                 } catch (CertificateEncodingException e) {
82                         throw new IOException(e);
83                 }
84                 return "X509 " + content;
85         }
86         
87         public String toString() {
88                 return "X509 Authentication for " + name;
89         }
90
91
92         public byte[] getCred() {
93                 try {
94                         return content==null?(content=cert.getEncoded()):content;
95                 } catch (CertificateEncodingException e) {
96                         return null;
97                 }
98         }
99
100         public String getName() {
101                 return name;
102         }
103
104         @Override
105         public String tag() {
106                 return "x509";
107         }
108
109 }