File: ASN1Any.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (100 lines) | stat: -rw-r--r-- 3,854 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftASN1 open source project
//
// Copyright (c) 2021 Apple Inc. and the SwiftASN1 project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftASN1 project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

/// An ASN1 ANY represents...well, anything.
///
/// In this case we store the ASN.1 ANY as a serialized representation. This is a bit limiting,
/// but it's the only safe way to manage this data, as we cannot arbitrarily parse it.
///
/// The only things users can do with ASN.1 ANYs is to try to decode them as something else,
/// to create them from something else, or to serialize them.
public struct ASN1Any: DERParseable, DERSerializable, Hashable, Sendable {
    @usableFromInline
    var _serializedBytes: ArraySlice<UInt8>

    /// Create an ``ASN1Any`` from a serializable ASN1 type.
    ///
    /// - parameters:
    ///     erasing: The type to be represented as an ASN1 ANY.
    @inlinable
    public init<ASN1Type: DERSerializable>(erasing: ASN1Type) throws {
        var serializer = DER.Serializer()
        try erasing.serialize(into: &serializer)
        self._serializedBytes = ArraySlice(serializer._serializedBytes)
    }

    /// Create an ``ASN1Any`` from a serializable implicitly taggable ASN1 type.
    ///
    /// - parameters:
    ///     erasing: The type to be represented as an ASN1 ANY.
    ///     identifier: The tag to use with this node.
    @inlinable
    public init<ASN1Type: DERImplicitlyTaggable>(erasing: ASN1Type, withIdentifier identifier: ASN1Identifier) throws {
        var serializer = DER.Serializer()
        try erasing.serialize(into: &serializer, withIdentifier: identifier)
        self._serializedBytes = ArraySlice(serializer._serializedBytes)
    }

    @inlinable
    public init(derEncoded rootNode: ASN1Node) {
        // This is a bit sad: we just re-serialize this data. In an ideal world
        // we'd update the parse representation so that all nodes can point at their
        // complete backing storage, but for now this is better.
        var serializer = DER.Serializer()
        serializer.serialize(rootNode)
        self._serializedBytes = ArraySlice(serializer._serializedBytes)
    }

    @inlinable
    public func serialize(into coder: inout DER.Serializer) throws {
        // Dangerous to just reach in there like this, but it's the right way to serialize this.
        coder.serializeRawBytes(self._serializedBytes)
    }
}

extension ASN1Any: CustomStringConvertible {
    @inlinable
    public var description: String {
        "ASN1Any(\(self._serializedBytes))"
    }
}

extension DERParseable {
    /// Construct this node from an ASN.1 ANY object.
    ///
    /// This operation works by asking the type to decode itself from the serialized representation
    /// of this ASN.1 ANY node.
    ///
    /// - parameters:
    ///     asn1Any: The ASN.1 ANY object to reinterpret.
    @inlinable
    public init(asn1Any: ASN1Any) throws {
        try self.init(derEncoded: asn1Any._serializedBytes)
    }
}

extension DERImplicitlyTaggable {
    /// Construct this node from an ASN.1 ANY object.
    ///
    /// This operation works by asking the type to decode itself from the serialized representation
    /// of this ASN.1 ANY node.
    ///
    /// - parameters:
    ///     asn1Any: The ASN.1 ANY object to reinterpret.
    ///     identifier: The tag to use with this node.
    @inlinable
    public init(asn1Any: ASN1Any, withIdentifier identifier: ASN1Identifier) throws {
        try self.init(derEncoded: asn1Any._serializedBytes, withIdentifier: identifier)
    }
}