1 // swiftlint:disable all
2 //
3 //  X509Extension.swift
4 //
5 //  Copyright © 2019 Filippo Maguolo.
6 //
7 //  Permission is hereby granted, free of charge, to any person obtaining a copy
8 //  of this software and associated documentation files (the "Software"), to deal
9 //  in the Software without restriction, including without limitation the rights
10 //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 //  copies of the Software, and to permit persons to whom the Software is
12 //  furnished to do so, subject to the following conditions:
13 //
14 //  The above copyright notice and this permission notice shall be included in all
15 //  copies or substantial portions of the Software.
16 //
17 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 //  SOFTWARE.
24 
25 import Foundation
26 
27 public class X509Extension {
28 
29     let block: ASN1Object
30 
31     required init(block: ASN1Object) {
32         self.block = block
33     }
34 
35     public var oid: String? {
36         return block.sub(0)?.value as? String
37     }
38 
39     public var name: String? {
40         return OID.description(of: oid ?? "")
41     }
42 
43     public var isCritical: Bool {
44         if block.sub?.count ?? 0 > 2 {
45             return block.sub(1)?.value as? Bool ?? false
46         }
47         return false
48     }
49 
50     public var value: Any? {
51         if let valueBlock = block.sub?.last {
52             return firstLeafValue(block: valueBlock)
53         }
54         return nil
55     }
56 
57     var valueAsBlock: ASN1Object? {
58         return block.sub?.last
59     }
60 
61     var valueAsStrings: [String] {
62         var result: [String] = []
63         for item in block.sub?.last?.sub?.last?.sub ?? [] {
64             if let name = item.value as? String {
65                 result.append(name)
66             }
67         }
68         return result
69     }
70 }
71 // swiftlint:enable all
72