x509_certificate/
rfc3280.rs

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! ASN.1 types defined in RFC 3280.

use {
    crate::rfc4519::{
        OID_COMMON_NAME, OID_COUNTRY_NAME, OID_LOCALITY_NAME, OID_ORGANIZATIONAL_UNIT_NAME,
        OID_ORGANIZATION_NAME, OID_STATE_PROVINCE_NAME,
    },
    bcder::{
        decode::{BytesSource, Constructed, DecodeError, Source},
        encode,
        encode::{PrimitiveContent, Values},
        string::{Ia5String, PrintableString, Utf8String},
        Captured, Mode, OctetString, Oid, Tag,
    },
    std::{
        fmt::{Debug, Display, Formatter},
        io::Write,
        ops::{Deref, DerefMut},
        str::FromStr,
    },
};

pub type GeneralNames = Vec<GeneralName>;

/// General name.
///
/// ```ASN.1
/// GeneralName ::= CHOICE {
///   otherName                       [0]     AnotherName,
///   rfc822Name                      [1]     IA5String,
///   dNSName                         [2]     IA5String,
///   x400Address                     [3]     ORAddress,
///   directoryName                   [4]     Name,
///   ediPartyName                    [5]     EDIPartyName,
///   uniformResourceIdentifier       [6]     IA5String,
///   iPAddress                       [7]     OCTET STRING,
///   registeredID                    [8]     OBJECT IDENTIFIER }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GeneralName {
    OtherName(AnotherName),
    Rfc822Name(Ia5String),
    DnsName(Ia5String),
    X400Address(OrAddress),
    DirectoryName(Name),
    EdiPartyName(EdiPartyName),
    UniformResourceIdentifier(Ia5String),
    IpAddress(OctetString),
    RegisteredId(Oid),
}

impl GeneralName {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        if let Some(name) =
            cons.take_opt_constructed_if(Tag::CTX_0, |cons| AnotherName::take_from(cons))?
        {
            Ok(Self::OtherName(name))
        } else if let Some(name) =
            cons.take_opt_constructed_if(Tag::CTX_1, |cons| Ia5String::take_from(cons))?
        {
            Ok(Self::Rfc822Name(name))
        } else if let Some(name) =
            cons.take_opt_constructed_if(Tag::CTX_2, |cons| Ia5String::take_from(cons))?
        {
            Ok(Self::DnsName(name))
        } else if let Some(name) =
            cons.take_opt_constructed_if(Tag::CTX_3, |cons| OrAddress::take_from(cons))?
        {
            Ok(Self::X400Address(name))
        } else if let Some(name) =
            cons.take_opt_constructed_if(Tag::CTX_4, |cons| Name::take_from(cons))?
        {
            Ok(Self::DirectoryName(name))
        } else if let Some(name) =
            cons.take_opt_constructed_if(Tag::CTX_5, |cons| EdiPartyName::take_from(cons))?
        {
            Ok(Self::EdiPartyName(name))
        } else if let Some(name) =
            cons.take_opt_constructed_if(Tag::CTX_6, |cons| Ia5String::take_from(cons))?
        {
            Ok(Self::UniformResourceIdentifier(name))
        } else if let Some(name) =
            cons.take_opt_constructed_if(Tag::ctx(7), |cons| OctetString::take_from(cons))?
        {
            Ok(Self::IpAddress(name))
        } else if let Some(name) =
            cons.take_opt_constructed_if(Tag::ctx(8), |cons| Oid::take_from(cons))?
        {
            Ok(Self::RegisteredId(name))
        } else {
            Err(cons.content_err("unexpected GeneralName variant"))
        }
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        match self {
            Self::OtherName(name) => (
                Some(name.explicit(Tag::CTX_0)),
                None,
                None,
                None,
                None,
                None,
                None,
                None,
            ),
            Self::Rfc822Name(name) => (
                None,
                Some(name.encode_ref_as(Tag::CTX_1)),
                None,
                None,
                None,
                None,
                None,
                None,
            ),
            Self::DnsName(name) => (
                None,
                None,
                Some(name.encode_ref_as(Tag::CTX_2)),
                None,
                None,
                None,
                None,
                None,
            ),
            Self::X400Address(_name) => {
                unimplemented!()
            }
            Self::DirectoryName(name) => (
                None,
                None,
                None,
                Some(name.encode_ref_as(Tag::CTX_4)),
                None,
                None,
                None,
                None,
            ),
            Self::EdiPartyName(name) => (
                None,
                None,
                None,
                None,
                Some(name.encode_ref_as(Tag::CTX_5)),
                None,
                None,
                None,
            ),
            Self::UniformResourceIdentifier(name) => (
                None,
                None,
                None,
                None,
                None,
                Some(name.encode_ref_as(Tag::CTX_6)),
                None,
                None,
            ),
            Self::IpAddress(name) => (
                None,
                None,
                None,
                None,
                None,
                None,
                Some(name.encode_ref_as(Tag::ctx(7))),
                None,
            ),
            Self::RegisteredId(name) => (
                None,
                None,
                None,
                None,
                None,
                None,
                None,
                Some(name.encode_ref_as(Tag::ctx(8))),
            ),
        }
    }
}

/// A reference to another name.
///
/// ```ASN.1
/// AnotherName ::= SEQUENCE {
///   type-id    OBJECT IDENTIFIER,
///   value      [0] EXPLICIT ANY DEFINED BY type-id }
/// ```
#[derive(Clone, Debug)]
pub struct AnotherName {
    pub type_id: Oid,
    pub value: Captured,
}

impl PartialEq for AnotherName {
    fn eq(&self, other: &Self) -> bool {
        self.type_id == other.type_id && self.value.as_slice() == other.value.as_slice()
    }
}

impl Eq for AnotherName {}

impl AnotherName {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        cons.take_sequence(|cons| {
            let type_id = Oid::take_from(cons)?;
            let value = cons.take_constructed_if(Tag::CTX_0, |cons| cons.capture_all())?;

            Ok(Self { type_id, value })
        })
    }
}

impl Values for AnotherName {
    fn encoded_len(&self, mode: Mode) -> usize {
        encode::sequence((self.type_id.encode_ref(), &self.value)).encoded_len(mode)
    }

    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
        encode::sequence((self.type_id.encode_ref(), &self.value)).write_encoded(mode, target)
    }
}

/// EDI party name.
///
/// ```ASN.1
/// EDIPartyName ::= SEQUENCE {
///   nameAssigner            [0]     DirectoryString OPTIONAL,
///   partyName               [1]     DirectoryString }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EdiPartyName {
    pub name_assigner: Option<DirectoryString>,
    pub party_name: DirectoryString,
}

impl EdiPartyName {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        cons.take_sequence(|cons| {
            let name_assigner =
                cons.take_opt_constructed_if(Tag::CTX_0, |cons| DirectoryString::take_from(cons))?;
            let party_name =
                cons.take_constructed_if(Tag::CTX_1, |cons| DirectoryString::take_from(cons))?;

            Ok(Self {
                name_assigner,
                party_name,
            })
        })
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        encode::sequence((
            self.name_assigner
                .as_ref()
                .map(|name_assigner| name_assigner.encode_ref()),
            self.party_name.encode_ref(),
        ))
    }

    pub fn encode_ref_as(&self, tag: Tag) -> impl Values + '_ {
        encode::sequence_as(
            tag,
            (
                self.name_assigner
                    .as_ref()
                    .map(|name_assigner| name_assigner.encode_ref()),
                self.party_name.encode_ref(),
            ),
        )
    }
}

/// Directory string.
///
/// ```ASN.1
/// DirectoryString ::= CHOICE {
///       teletexString           TeletexString (SIZE (1..MAX)),
///       printableString         PrintableString (SIZE (1..MAX)),
///       universalString         UniversalString (SIZE (1..MAX)),
///       utf8String              UTF8String (SIZE (1..MAX)),
///       bmpString               BMPString (SIZE (1..MAX)) }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DirectoryString {
    // TODO implement.
    TeletexString,
    PrintableString(PrintableString),
    // TODO implement.
    UniversalString,
    Utf8String(Utf8String),
    // TODO implement.
    BmpString,
}

impl DirectoryString {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        cons.take_value(|tag, content| {
            if tag == Tag::PRINTABLE_STRING {
                Ok(Self::PrintableString(PrintableString::from_content(
                    content,
                )?))
            } else if tag == Tag::UTF8_STRING {
                Ok(Self::Utf8String(Utf8String::from_content(content)?))
            } else {
                Err(content
                    .content_err("only decoding of PrintableString and UTF8String is implemented"))
            }
        })
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        match self {
            Self::PrintableString(ps) => (Some(ps.encode_ref()), None),
            Self::Utf8String(s) => (None, Some(s.encode_ref())),
            _ => unimplemented!(),
        }
    }
}

impl Display for DirectoryString {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let str = match self {
            Self::PrintableString(s) => s.to_string(),
            Self::Utf8String(s) => s.to_string(),
            _ => unimplemented!(),
        };
        write!(f, "{}", str)
    }
}

impl Values for DirectoryString {
    fn encoded_len(&self, mode: Mode) -> usize {
        self.encode_ref().encoded_len(mode)
    }

    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
        self.encode_ref().write_encoded(mode, target)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Name {
    RdnSequence(RdnSequence),
}

impl Name {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        Ok(Self::RdnSequence(RdnSequence::take_from(cons)?))
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        match self {
            Self::RdnSequence(seq) => seq.encode_ref(),
        }
    }

    pub fn encode_ref_as(&self, tag: Tag) -> impl Values + '_ {
        match self {
            Self::RdnSequence(seq) => seq.encode_ref_as(tag),
        }
    }

    /// Iterate over relative distinguished name entries in this instance.
    pub fn iter_rdn(&self) -> impl Iterator<Item = &RelativeDistinguishedName> {
        self.0.iter()
    }

    /// Iterate over all attributes in this Name.
    pub fn iter_attributes(&self) -> impl Iterator<Item = &AttributeTypeAndValue> {
        self.0.iter().flat_map(|rdn| rdn.iter())
    }

    /// Iterate over all attributes, yielding mutable entries.
    pub fn iter_mut_attributes(&mut self) -> impl Iterator<Item = &mut AttributeTypeAndValue> {
        self.0.iter_mut().flat_map(|rdn| rdn.iter_mut())
    }

    /// Iterate over all attributes in this Name having a given OID.
    pub fn iter_by_oid(&self, oid: Oid) -> impl Iterator<Item = &AttributeTypeAndValue> {
        self.iter_attributes().filter(move |atv| atv.typ == oid)
    }

    /// Iterate over all attributes in this Name having a given OID, yielding mutable instances.
    pub fn iter_mut_by_oid(
        &mut self,
        oid: Oid,
    ) -> impl Iterator<Item = &mut AttributeTypeAndValue> {
        self.iter_mut_attributes().filter(move |atv| atv.typ == oid)
    }

    /// Iterate over all Common Name (CN) attributes.
    pub fn iter_common_name(&self) -> impl Iterator<Item = &AttributeTypeAndValue> {
        self.iter_by_oid(Oid(OID_COMMON_NAME.as_ref().into()))
    }

    /// Iterate over all Country (C) attributes.
    pub fn iter_country(&self) -> impl Iterator<Item = &AttributeTypeAndValue> {
        self.iter_by_oid(Oid(OID_COUNTRY_NAME.as_ref().into()))
    }

    /// Iterate over all Locality Name (L) attributes.
    pub fn iter_locality(&self) -> impl Iterator<Item = &AttributeTypeAndValue> {
        self.iter_by_oid(Oid(OID_LOCALITY_NAME.as_ref().into()))
    }

    /// Iterate over all State or Province attributes.
    pub fn iter_state_province(&self) -> impl Iterator<Item = &AttributeTypeAndValue> {
        self.iter_by_oid(Oid(OID_STATE_PROVINCE_NAME.as_ref().into()))
    }

    /// Iterate over all Organization (O) attributes.
    pub fn iter_organization(&self) -> impl Iterator<Item = &AttributeTypeAndValue> {
        self.iter_by_oid(Oid(OID_ORGANIZATION_NAME.as_ref().into()))
    }

    /// Iterate over all Organizational Unit (OU) attributes.
    pub fn iter_organizational_unit(&self) -> impl Iterator<Item = &AttributeTypeAndValue> {
        self.iter_by_oid(Oid(OID_ORGANIZATIONAL_UNIT_NAME.as_ref().into()))
    }

    /// Find the first attribute given an OID search.
    pub fn find_attribute(&self, oid: Oid) -> Option<&AttributeTypeAndValue> {
        self.iter_by_oid(oid).next()
    }

    /// Attempt to obtain a string attribute given
    pub fn find_first_attribute_string(
        &self,
        oid: Oid,
    ) -> Result<Option<String>, DecodeError<<BytesSource as Source>::Error>> {
        if let Some(atv) = self.find_attribute(oid) {
            Ok(Some(atv.to_string()?))
        } else {
            Ok(None)
        }
    }

    /// Obtain a user friendly string representation of this instance.
    ///
    /// This prints common OIDs like common name and organization unit in a way
    /// that is similar to how tools like OpenSSL render certificates.
    ///
    /// Not all attributes are printed. Do not compare the output of this
    /// function to test equality.
    pub fn user_friendly_str(&self) -> Result<String, DecodeError<<BytesSource as Source>::Error>> {
        let mut fields = vec![];

        for cn in self.iter_common_name() {
            fields.push(format!("CN={}", cn.to_string()?));
        }
        for ou in self.iter_organizational_unit() {
            fields.push(format!("OU={}", ou.to_string()?));
        }
        for o in self.iter_organization() {
            fields.push(format!("O={}", o.to_string()?));
        }
        for l in self.iter_locality() {
            fields.push(format!("L={}", l.to_string()?));
        }
        for state in self.iter_state_province() {
            fields.push(format!("S={}", state.to_string()?));
        }
        for c in self.iter_country() {
            fields.push(format!("C={}", c.to_string()?));
        }

        Ok(fields.join(", "))
    }

    /// Appends a PrintableString value for the given OID.
    ///
    /// The attribute will always be written to a new RDN.
    pub fn append_printable_string(
        &mut self,
        oid: Oid,
        value: &str,
    ) -> Result<(), bcder::string::CharSetError> {
        let mut rdn = RelativeDistinguishedName::default();
        rdn.push(AttributeTypeAndValue::new_printable_string(oid, value)?);
        self.0.push(rdn);

        Ok(())
    }

    /// Appends a Utf8String value for the given OID.
    ///
    /// The attribute will always be written to a new RD.
    pub fn append_utf8_string(
        &mut self,
        oid: Oid,
        value: &str,
    ) -> Result<(), bcder::string::CharSetError> {
        let mut rdn = RelativeDistinguishedName::default();
        rdn.push(AttributeTypeAndValue::new_utf8_string(oid, value)?);
        self.0.push(rdn);

        Ok(())
    }

    /// Append a Common Name (CN) attribute to the first RDN.
    pub fn append_common_name_utf8_string(
        &mut self,
        value: &str,
    ) -> Result<(), bcder::string::CharSetError> {
        self.append_utf8_string(Oid(OID_COMMON_NAME.as_ref().into()), value)
    }

    /// Append a Country (C) attribute to the first RDN.
    pub fn append_country_utf8_string(
        &mut self,
        value: &str,
    ) -> Result<(), bcder::string::CharSetError> {
        self.append_utf8_string(Oid(OID_COUNTRY_NAME.as_ref().into()), value)
    }

    /// Append an Organization Name (O) attribute to the first RDN.
    pub fn append_organization_utf8_string(
        &mut self,
        value: &str,
    ) -> Result<(), bcder::string::CharSetError> {
        self.append_utf8_string(Oid(OID_ORGANIZATION_NAME.as_ref().into()), value)
    }

    /// Append an Organizational Unit (OU) attribute to the first RDN.
    pub fn append_organizational_unit_utf8_string(
        &mut self,
        value: &str,
    ) -> Result<(), bcder::string::CharSetError> {
        self.append_utf8_string(Oid(OID_ORGANIZATIONAL_UNIT_NAME.as_ref().into()), value)
    }
}

impl Default for Name {
    fn default() -> Self {
        Self::RdnSequence(RdnSequence::default())
    }
}

impl Deref for Name {
    type Target = RdnSequence;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::RdnSequence(seq) => seq,
        }
    }
}

impl DerefMut for Name {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            Self::RdnSequence(seq) => seq,
        }
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct RdnSequence(Vec<RelativeDistinguishedName>);

impl Deref for RdnSequence {
    type Target = Vec<RelativeDistinguishedName>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for RdnSequence {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl RdnSequence {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        cons.take_sequence(|cons| {
            let mut values = Vec::new();

            while let Some(value) = RelativeDistinguishedName::take_opt_from(cons)? {
                values.push(value);
            }

            Ok(Self(values))
        })
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        encode::sequence(&self.0)
    }

    pub fn encode_ref_as(&self, tag: Tag) -> impl Values + '_ {
        encode::sequence_as(tag, &self.0)
    }
}

pub type DistinguishedName = RdnSequence;

/// Relative distinguished name.
///
/// ```ASN.1
/// RelativeDistinguishedName ::=
///   SET OF AttributeTypeAndValue
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct RelativeDistinguishedName(Vec<AttributeTypeAndValue>);

impl Deref for RelativeDistinguishedName {
    type Target = Vec<AttributeTypeAndValue>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for RelativeDistinguishedName {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl RelativeDistinguishedName {
    pub fn take_opt_from<S: Source>(
        cons: &mut Constructed<S>,
    ) -> Result<Option<Self>, DecodeError<S::Error>> {
        cons.take_opt_set(|cons| {
            let mut values = Vec::new();

            while let Some(value) = AttributeTypeAndValue::take_opt_from(cons)? {
                values.push(value);
            }

            Ok(Self(values))
        })
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        encode::set(&self.0)
    }
}

impl Values for RelativeDistinguishedName {
    fn encoded_len(&self, mode: Mode) -> usize {
        self.encode_ref().encoded_len(mode)
    }

    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
        self.encode_ref().write_encoded(mode, target)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OrAddress {}

impl OrAddress {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        Err(cons.content_err("parsing of OrAddress not implemented"))
    }
}

/// Attribute type and its value.
///
/// ```ASN.1
/// AttributeTypeAndValue ::= SEQUENCE {
///   type     AttributeType,
///   value    AttributeValue }
/// ```
#[derive(Clone)]
pub struct AttributeTypeAndValue {
    pub typ: AttributeType,
    pub value: AttributeValue,
}

impl Debug for AttributeTypeAndValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut s = f.debug_struct("AttributeTypeAndValue");
        s.field("type", &format_args!("{}", self.typ));
        s.field("value", &self.value);
        s.finish()
    }
}

impl AttributeTypeAndValue {
    pub fn take_opt_from<S: Source>(
        cons: &mut Constructed<S>,
    ) -> Result<Option<Self>, DecodeError<S::Error>> {
        cons.take_opt_sequence(|cons| {
            let typ = AttributeType::take_from(cons)?;
            let value = cons.capture_all()?;

            Ok(Self {
                typ,
                value: value.into(),
            })
        })
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        encode::sequence((self.typ.encode_ref(), self.value.deref()))
    }

    /// Attempt to coerce the stored value to a Rust string.
    pub fn to_string(&self) -> Result<String, DecodeError<<BytesSource as Source>::Error>> {
        self.value.to_string()
    }

    /// Construct a new instance with a PrintableString given an OID and Rust string.
    pub fn new_printable_string(oid: Oid, s: &str) -> Result<Self, bcder::string::CharSetError> {
        Ok(Self {
            typ: oid,
            value: AttributeValue::new_printable_string(s)?,
        })
    }

    /// Construct a new instance with a Utf8String given an OID and Rust string.
    pub fn new_utf8_string(oid: Oid, s: &str) -> Result<Self, bcder::string::CharSetError> {
        Ok(Self {
            typ: oid,
            value: AttributeValue::new_utf8_string(s)?,
        })
    }

    /// Set the captured value to a Utf8String.
    pub fn set_utf8_string_value(&mut self, s: &str) -> Result<(), bcder::string::CharSetError> {
        self.value.set_utf8_string_value(s)
    }
}

impl PartialEq for AttributeTypeAndValue {
    fn eq(&self, other: &Self) -> bool {
        self.typ == other.typ && self.value.as_slice() == other.value.as_slice()
    }
}

impl Eq for AttributeTypeAndValue {}

impl Values for AttributeTypeAndValue {
    fn encoded_len(&self, mode: Mode) -> usize {
        self.encode_ref().encoded_len(mode)
    }

    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
        self.encode_ref().write_encoded(mode, target)
    }
}

pub type AttributeType = Oid;

#[derive(Clone)]
pub struct AttributeValue(Captured);

impl Debug for AttributeValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{}", hex::encode(self.0.as_slice())))
    }
}

impl AttributeValue {
    /// Construct a new instance containing a PrintableString given a Rust string.
    pub fn new_printable_string(s: &str) -> Result<Self, bcder::string::CharSetError> {
        let mut slf = Self(Captured::empty(Mode::Der));

        slf.set_printable_string_value(s)?;

        Ok(slf)
    }

    /// Construct a new instance containing a Utf8String given a Rust string.
    pub fn new_utf8_string(s: &str) -> Result<Self, bcder::string::CharSetError> {
        let mut slf = Self(Captured::empty(Mode::Der));

        slf.set_utf8_string_value(s)?;

        Ok(slf)
    }

    /// Attempt to convert the inner value to a Rust string.
    ///
    /// The inner value can be any number of different types. This will try
    /// a lot of them and hopefully yield a working result.
    ///
    /// If the inner type isn't a known string, a decoding error occurs.
    pub fn to_string(&self) -> Result<String, DecodeError<<BytesSource as Source>::Error>> {
        self.0.clone().decode(|cons| {
            if let Some(s) = cons.take_opt_value_if(Tag::NUMERIC_STRING, |content| {
                bcder::NumericString::from_content(content)
            })? {
                Ok(s.to_string())
            } else if let Some(s) = cons.take_opt_value_if(Tag::PRINTABLE_STRING, |content| {
                bcder::PrintableString::from_content(content)
            })? {
                Ok(s.to_string())
            } else if let Some(s) = cons.take_opt_value_if(Tag::UTF8_STRING, |content| {
                bcder::Utf8String::from_content(content)
            })? {
                Ok(s.to_string())
            } else if let Some(s) = cons.take_opt_value_if(Tag::IA5_STRING, |content| {
                bcder::Ia5String::from_content(content)
            })? {
                Ok(s.to_string())
            } else {
                Ok(DirectoryString::take_from(cons)?.to_string())
            }
        })
    }

    /// Set the captured value to a PrintableString.
    pub fn set_printable_string_value(
        &mut self,
        value: &str,
    ) -> Result<(), bcder::string::CharSetError> {
        let ps = DirectoryString::PrintableString(PrintableString::from_str(value)?);
        let captured = bcder::Captured::from_values(Mode::Der, ps);
        self.0 = captured;

        Ok(())
    }

    /// Set the captured value to a Utf8String.
    pub fn set_utf8_string_value(
        &mut self,
        value: &str,
    ) -> Result<(), bcder::string::CharSetError> {
        let ds = DirectoryString::Utf8String(Utf8String::from_str(value)?);
        let captured = bcder::Captured::from_values(Mode::Der, ds);
        self.0 = captured;

        Ok(())
    }
}

impl Deref for AttributeValue {
    type Target = Captured;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl From<Captured> for AttributeValue {
    fn from(v: Captured) -> Self {
        Self(v)
    }
}