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
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
use std::collections::HashMap;

use chrono::Duration;
use serde::{Deserialize, Serialize};

use rpki::cert::Cert;
use rpki::crypto::KeyIdentifier;
use rpki::x509::Time;

use crate::commons::api::rrdp::PublishElement;
use crate::commons::api::Base64;
use crate::commons::api::{
    AddedObject, CurrentObject, CurrentObjects, EntitlementClass, HexEncodedHash, IssuanceRequest,
    IssuedCert, ObjectName, ObjectsDelta, ParentHandle, RcvdCert, ReplacedObject, RepoInfo,
    RequestResourceLimit, ResourceClassInfo, ResourceClassName, ResourceSet, Revocation,
    RevocationRequest, RevokedObject, UpdatedObject, WithdrawnObject,
};
use crate::commons::error::Error;
use crate::commons::KrillResult;
use crate::constants::ROA_CERTIFICATE_REISSUE_WEEKS;
use crate::daemon::ca::events::{ChildCertificateUpdates, RoaUpdates};
use crate::daemon::ca::signing::CsrInfo;
use crate::daemon::ca::{
    self, ta_handle, AddedOrUpdated, CertifiedKey, ChildCertificates, CrlBuilder, CurrentKey,
    CurrentObjectSetDelta, EvtDet, KeyState, ManifestBuilder, NewKey, OldKey, PendingKey, RoaInfo,
    Roas, RouteAuthorization, SignSupport, Signer,
};

//------------ ResourceClass -----------------------------------------------

/// A CA may have multiple parents, e.g. two RIRs, and it may not get all its
/// resource entitlements in one set, but in a number of so-called "resource
/// classes".
///
/// Each ResourceClass has a namespace, which can be anything, but for Krill
/// is based on the name of the parent ca, and the name of the resource class
/// under that parent.
///
/// Furthermore a resource class manages the key life cycle, and certificates
/// for each key, as well as products that need to be issued by the 'current'
/// key for this class.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ResourceClass {
    name: ResourceClassName,
    name_space: String,

    parent_handle: ParentHandle,
    parent_rc_name: ResourceClassName,

    roas: Roas,
    certificates: ChildCertificates,

    last_key_change: Time,
    key_state: KeyState,
}

/// # Creating new instances
///
impl ResourceClass {
    /// Creates a new ResourceClass with a single pending key only.
    pub fn create(
        name: ResourceClassName,
        name_space: String,
        parent_handle: ParentHandle,
        parent_rc_name: ResourceClassName,
        pending_key: KeyIdentifier,
    ) -> Self {
        ResourceClass {
            name,
            name_space,
            parent_handle,
            parent_rc_name,
            roas: Roas::default(),
            certificates: ChildCertificates::default(),
            last_key_change: Time::now(),
            key_state: KeyState::create(pending_key),
        }
    }

    pub fn for_ta(parent_rc_name: ResourceClassName, pending_key: KeyIdentifier) -> Self {
        ResourceClass {
            name: parent_rc_name.clone(),
            name_space: parent_rc_name.to_string(),
            parent_handle: ta_handle(),
            parent_rc_name,
            roas: Roas::default(),
            certificates: ChildCertificates::default(),
            last_key_change: Time::now(),
            key_state: KeyState::create(pending_key),
        }
    }
}

/// # Data Access
///
impl ResourceClass {
    pub fn name_space(&self) -> &str {
        &self.name_space
    }

    /// Returns the name of the parent where we got this RC from.
    pub fn parent_handle(&self) -> &ParentHandle {
        &self.parent_handle
    }

    /// Returns the name that the parent uses for this RC.
    pub fn parent_rc_name(&self) -> &ResourceClassName {
        &self.parent_rc_name
    }

    /// Adds a request to an existing key for future reference.
    pub fn add_request(&mut self, key_id: KeyIdentifier, req: IssuanceRequest) {
        self.key_state.add_request(key_id, req);
    }

    /// Returns the current certificate, if there is any
    pub fn current_certificate(&self) -> Option<&RcvdCert> {
        self.current_key().map(|k| k.incoming_cert())
    }

    /// Returns the current resources for this resource class
    pub fn current_resources(&self) -> Option<&ResourceSet> {
        self.current_certificate().map(|c| c.resources())
    }

    /// Returns a reference to current key for this RC, if there is any.
    pub fn current_key(&self) -> Option<&CurrentKey> {
        match &self.key_state {
            KeyState::Active(current)
            | KeyState::RollPending(_, current)
            | KeyState::RollNew(_, current)
            | KeyState::RollOld(current, _) => Some(current),
            _ => None,
        }
    }

    pub fn get_current_key(&self) -> KrillResult<&CurrentKey> {
        self.current_key().ok_or_else(|| Error::KeyUseNoCurrentKey)
    }

    /// Gets the new key for a key roll, or returns an error if there is none.
    pub fn get_new_key(&self) -> KrillResult<&NewKey> {
        if let KeyState::RollNew(new_key, _) = &self.key_state {
            Ok(new_key)
        } else {
            Err(Error::KeyUseNoNewKey)
        }
    }

    pub fn current_objects(&self) -> CurrentObjects {
        let mut current_objects = CurrentObjects::default();

        for roa_info in self.roas.current() {
            current_objects.insert(roa_info.name().clone(), roa_info.object().clone());
        }

        for issued in self.certificates.current() {
            let cert = issued.cert();
            current_objects.insert(ObjectName::from(cert), CurrentObject::from(cert));
        }

        fn add_mft_and_crl(objects: &mut CurrentObjects, key: &CertifiedKey) {
            let mft = key.current_set().manifest_info();
            objects.insert(mft.name().clone(), mft.current().clone());

            let crl = key.current_set().crl_info();
            objects.insert(crl.name().clone(), crl.current().clone());
        }

        match &self.key_state {
            KeyState::Pending(_) => {} // nothing to add
            KeyState::Active(current) => {
                add_mft_and_crl(&mut current_objects, current);
            }
            KeyState::RollPending(_, current) => {
                add_mft_and_crl(&mut current_objects, current);
            }
            KeyState::RollNew(new, current) => {
                add_mft_and_crl(&mut current_objects, new);
                add_mft_and_crl(&mut current_objects, current);
            }
            KeyState::RollOld(current, old) => {
                add_mft_and_crl(&mut current_objects, current);
                add_mft_and_crl(&mut current_objects, old);
            }
        }

        current_objects
    }

    /// Returns a ResourceClassInfo for this, which contains all the
    /// same data, but which does not have any behaviour.
    pub fn as_info(&self) -> ResourceClassInfo {
        ResourceClassInfo::new(
            self.name_space.clone(),
            self.parent_handle.clone(),
            self.key_state.as_info(),
            self.current_objects(),
        )
    }
}

/// # Request certificates
///
impl ResourceClass {
    /// Returns event details for receiving the certificate.
    pub fn update_received_cert<S: Signer>(
        &self,
        rcvd_cert: RcvdCert,
        repo_info: &RepoInfo,
        signer: &S,
    ) -> KrillResult<Vec<EvtDet>> {
        // If this is for a pending key, then we need to promote this key

        let rcvd_cert_ki = rcvd_cert.cert().subject_key_identifier();

        fn create_active_key_and_delta<S: Signer>(
            rcvd_cert: RcvdCert,
            repo_info: &RepoInfo,
            name_space: &str,
            signer: &S,
        ) -> KrillResult<(CertifiedKey, ObjectsDelta)> {
            let mut delta = ObjectsDelta::new(rcvd_cert.ca_repository().clone());
            let active_key = CertifiedKey::create(rcvd_cert, repo_info, name_space, signer)?;

            match active_key.current_set().manifest_info().added_or_updated() {
                AddedOrUpdated::Added(added) => delta.add(added),
                _ => panic!("New active key cannot have update."),
            }

            match active_key.current_set().crl_info().added_or_updated() {
                AddedOrUpdated::Added(added) => delta.add(added),
                _ => panic!("New active key cannot have update."),
            }
            Ok((active_key, delta))
        }

        match &self.key_state {
            KeyState::Pending(pending) => {
                if rcvd_cert_ki != pending.key_id() {
                    Err(Error::KeyUseNoMatch(rcvd_cert_ki))
                } else {
                    let (active_key, delta) = create_active_key_and_delta(
                        rcvd_cert,
                        repo_info,
                        self.name_space(),
                        signer,
                    )?;
                    Ok(vec![EvtDet::KeyPendingToActive(
                        self.name.clone(),
                        active_key,
                        delta,
                    )])
                }
            }
            KeyState::Active(current) => {
                self.update_rcvd_cert_current(current, rcvd_cert, repo_info, signer)
            }
            KeyState::RollPending(pending, current) => {
                if rcvd_cert_ki == pending.key_id() {
                    let (active_key, delta) = create_active_key_and_delta(
                        rcvd_cert,
                        repo_info,
                        self.name_space(),
                        signer,
                    )?;
                    Ok(vec![EvtDet::KeyPendingToNew(
                        self.name.clone(),
                        active_key,
                        delta,
                    )])
                } else {
                    self.update_rcvd_cert_current(current, rcvd_cert, repo_info, signer)
                }
            }
            KeyState::RollNew(new, current) => {
                if rcvd_cert_ki == new.key_id() {
                    Ok(vec![EvtDet::CertificateReceived(
                        self.name.clone(),
                        rcvd_cert_ki,
                        rcvd_cert,
                    )])
                } else {
                    self.update_rcvd_cert_current(current, rcvd_cert, repo_info, signer)
                }
            }
            KeyState::RollOld(current, _old) => {
                // We will never request a new certificate for an old key
                self.update_rcvd_cert_current(current, rcvd_cert, repo_info, signer)
            }
        }
    }

    fn update_rcvd_cert_current<S: Signer>(
        &self,
        current: &CurrentKey,
        rcvd_cert: RcvdCert,
        repo_info: &RepoInfo,
        signer: &S,
    ) -> KrillResult<Vec<EvtDet>> {
        let rcvd_cert_ki = rcvd_cert.cert().subject_key_identifier();
        if rcvd_cert_ki != current.key_id() {
            return Err(ca::Error::KeyUseNoMatch(rcvd_cert_ki));
        }

        let rcvd_resources = rcvd_cert.resources().clone();

        let mut res = vec![];
        res.push(EvtDet::CertificateReceived(
            self.name.clone(),
            rcvd_cert_ki,
            rcvd_cert,
        ));

        if &rcvd_resources != current.incoming_cert().resources() {
            let publish_mode = PublishMode::UpdatedResources(rcvd_resources);
            let authorizations: Vec<RouteAuthorization> =
                self.roas.authorizations().cloned().collect();
            res.append(&mut self.republish(
                authorizations.as_slice(),
                repo_info,
                &publish_mode,
                signer,
            )?)
        }

        Ok(res)
    }

    /// Request certificates for any key that needs it.
    /// Also, create revocation events for any unexpected keys to recover from
    /// issues where the parent believes we have keys that we do not know. This
    /// can happen in corner cases where re-initialisation of Krill as a child
    /// is done without proper revocation at the parent, or as is the case with
    /// ARIN - Krill is sometimes told to just drop all resources.
    pub fn make_entitlement_events<S: Signer>(
        &self,
        entitlement: &EntitlementClass,
        base_repo: &RepoInfo,
        signer: &S,
    ) -> KrillResult<Vec<EvtDet>> {
        self.key_state.make_entitlement_events(
            self.name.clone(),
            entitlement,
            base_repo,
            &self.name_space,
            signer,
        )
    }

    /// Request new certificates for all keys when the base repo changes.
    pub fn make_request_events_new_repo<S: Signer>(
        &self,
        base_repo: &RepoInfo,
        signer: &S,
    ) -> KrillResult<Vec<EvtDet>> {
        self.key_state.request_certs_new_repo(
            self.name.clone(),
            base_repo,
            &self.name_space,
            signer,
        )
    }

    /// This function returns all current certificate requests.
    pub fn cert_requests(&self) -> Vec<IssuanceRequest> {
        self.key_state.cert_requests()
    }

    /// Returns the revocation request for the old key, if it exists.
    pub fn revoke_request(&self) -> Option<&RevocationRequest> {
        self.key_state.revoke_request()
    }
}

/// # Publishing
///
impl ResourceClass {
    /// Applies a publication delta to the appropriate key in this resource class.
    pub fn apply_delta(&mut self, delta: CurrentObjectSetDelta, key_id: KeyIdentifier) {
        self.key_state.apply_delta(delta, key_id);
    }

    /// Publish/update/withdraw objects under the key, determined by the
    /// [PublishMode]. Will revoke updated and withdrawn objects under the
    /// correct key as well, i.e. when activating a new key objects will
    /// be re-published and updated in terms of publication, but will only
    /// be revoked under the old key.
    pub fn publish_objects<S: Signer>(
        &self,
        repo_info: &RepoInfo,
        objects_delta: ObjectsDelta,
        new_revocations: Vec<Revocation>,
        mode: &PublishMode,
        signer: &S,
    ) -> KrillResult<EvtDet> {
        let mut key_pub_map = HashMap::new();

        let (publish_key, other_key_opt) = match mode {
            PublishMode::KeyRollActivation => (self.get_new_key()?, Some(self.get_current_key()?)),
            _ => {
                let other_key_opt = match &self.key_state {
                    KeyState::RollNew(new, _) => Some(new),
                    KeyState::RollOld(_, old) => Some(old.key()),
                    _ => None,
                };
                (self.get_current_key()?, other_key_opt)
            }
        };

        let (publish_key_revocations, other_key_revocations) = match mode {
            PublishMode::KeyRollActivation => (vec![], new_revocations),
            _ => (new_revocations, vec![]),
        };

        let publish_key_delta = self
            .make_current_set_delta(
                publish_key,
                repo_info,
                objects_delta,
                publish_key_revocations,
                signer,
            )
            .map_err(Error::signer)?;

        key_pub_map.insert(publish_key.key_id().clone(), publish_key_delta);

        if let Some(other_key) = other_key_opt {
            let ns = self.name_space();
            let delta = ObjectsDelta::new(repo_info.ca_repository(ns));

            let other_delta = self
                .make_current_set_delta(other_key, repo_info, delta, other_key_revocations, signer)
                .map_err(ca::Error::signer)?;

            key_pub_map.insert(other_key.key_id().clone(), other_delta);
        }

        Ok(EvtDet::ObjectSetUpdated(self.name.clone(), key_pub_map))
    }

    fn needs_publication(&self, mode: &PublishMode) -> bool {
        match mode {
            PublishMode::Normal => {
                if let Ok(key) = self.get_current_key() {
                    key.close_to_next_update()
                } else {
                    false
                }
            }
            _ => true,
        }
    }

    /// Republish all keys in this class (that want it). Also update
    /// ROAs as needed.
    pub fn republish<S: Signer>(
        &self,
        authorizations: &[RouteAuthorization],
        repo_info: &RepoInfo,
        mode: &PublishMode,
        signer: &S,
    ) -> KrillResult<Vec<EvtDet>> {
        let mut res = vec![];

        let ns = self.name_space();
        let mut delta = ObjectsDelta::new(repo_info.ca_repository(ns));
        let mut revocations = vec![];

        let roa_updates = self.update_roas(authorizations, mode, signer)?;
        if roa_updates.contains_changes() {
            for added in roa_updates.added().into_iter() {
                delta.add(added);
            }
            for update in roa_updates.updated().into_iter() {
                delta.update(update);
            }
            for withdraw in roa_updates.withdrawn().into_iter() {
                delta.withdraw(withdraw);
            }
            revocations.append(&mut roa_updates.revocations());

            res.push(EvtDet::RoasUpdated(self.name.clone(), roa_updates));
        }

        let child_cert_updates = self.update_child_certificates(mode, signer)?;
        if !child_cert_updates.is_empty() {
            for issued in child_cert_updates.issued() {
                match issued.replaces() {
                    None => delta.add(AddedObject::from(issued.cert())),
                    Some(old) => {
                        delta.update(UpdatedObject::for_cert(issued.cert(), old.hash().clone()))
                    }
                }
            }
            for key in child_cert_updates.removed() {
                if let Some(issued) = self.certificates.get(key) {
                    delta.withdraw(WithdrawnObject::from(issued.cert()));
                    revocations.push(Revocation::from(issued.cert()));
                }
            }
            res.push(EvtDet::ChildCertificatesUpdated(
                self.name.clone(),
                child_cert_updates,
            ));
        }

        if !delta.is_empty() || !revocations.is_empty() || self.needs_publication(mode) {
            res.push(self.publish_objects(&repo_info, delta, revocations, mode, signer)?);
        }

        Ok(res)
    }

    /// Create a publish event details including the revocations, update, withdrawals needed
    /// for updating child certificates.
    pub fn republish_certs<S: Signer>(
        &self,
        issued_certs: &[&IssuedCert],
        removed_certs: &[&Cert],
        repo_info: &RepoInfo,
        signer: &S,
    ) -> KrillResult<HashMap<KeyIdentifier, CurrentObjectSetDelta>> {
        let issuing_key = self.get_current_key()?;
        let name_space = self.name_space();

        let mut revocations = vec![];
        for cert in removed_certs {
            revocations.push(Revocation::from(*cert));
        }
        for issued in issued_certs {
            if let Some(replaced) = issued.replaces() {
                revocations.push(replaced.revocation());
            }
        }

        let ca_repo = repo_info.ca_repository(name_space);
        let mut objects_delta = ObjectsDelta::new(ca_repo);

        for removed in removed_certs {
            objects_delta.withdraw(WithdrawnObject::from(*removed));
        }
        for issued in issued_certs {
            match issued.replaces() {
                None => objects_delta.add(AddedObject::from(issued.cert())),
                Some(replaced) => objects_delta.update(UpdatedObject::for_cert(
                    issued.cert(),
                    replaced.hash().clone(),
                )),
            }
        }

        let set_delta = self
            .make_current_set_delta(issuing_key, repo_info, objects_delta, revocations, signer)
            .map_err(Error::signer)?;

        let mut res = HashMap::new();
        res.insert(issuing_key.key_id().clone(), set_delta);
        Ok(res)
    }

    fn make_current_set_delta<S: Signer>(
        &self,
        signing_key: &CertifiedKey,
        repo_info: &RepoInfo,
        mut objects_delta: ObjectsDelta,
        mut new_revocations: Vec<Revocation>,
        signer: &S,
    ) -> KrillResult<CurrentObjectSetDelta> {
        let signing_cert = signing_key.incoming_cert();
        let current_set = signing_key.current_set();
        let current_revocations = current_set.revocations().clone();
        let number = current_set.number() + 1;

        let current_mft = current_set.manifest_info();
        let current_mft_hash = current_mft.current().to_hex_hash();
        let current_crl = current_set.crl_info();
        let current_crl_hash = current_crl.current().to_hex_hash();

        new_revocations.push(Revocation::from(current_mft.current()));

        // Create a new CRL
        let (crl_info, revocations_delta) = CrlBuilder::build(
            current_revocations,
            new_revocations,
            number,
            Some(current_crl_hash),
            signing_cert,
            signer,
        )?;

        match crl_info.added_or_updated() {
            AddedOrUpdated::Added(added) => objects_delta.add(added),
            AddedOrUpdated::Updated(updated) => objects_delta.update(updated),
        }

        // For the new manifest:
        //
        // List all current files, i.e.
        //  - the new CRL
        //  - current ROAs
        //  - current Certs
        //  - applying the delta - which may update the current ROAs and Certs on the MFT
        let issued = self.certificates.current();
        let roas = self.roas.iter();

        let manifest_info = ManifestBuilder::new(&crl_info, issued, roas, &objects_delta).build(
            signing_cert,
            repo_info,
            self.name_space(),
            number,
            Some(current_mft_hash),
            signer,
        )?;

        match manifest_info.added_or_updated() {
            AddedOrUpdated::Added(added) => objects_delta.add(added),
            AddedOrUpdated::Updated(updated) => objects_delta.update(updated),
        }

        Ok(CurrentObjectSetDelta::new(
            number,
            revocations_delta,
            manifest_info,
            crl_info,
            objects_delta,
        ))
    }

    pub fn all_objects(&self, base_repo: &RepoInfo) -> Vec<PublishElement> {
        let mut res = vec![];
        let ns = self.name_space();

        // ROAs
        for info in self.roas.current() {
            let base64 = info.object().content().clone();
            let object_name = info.name().clone();
            let uri = base_repo.resolve(ns, object_name.as_str());
            res.push(PublishElement::new(base64, uri));
        }
        // Certs
        for cert in self.certificates.current() {
            let base64 = Base64::from_content(cert.to_captured().as_slice());
            let uri = cert.uri().clone();
            res.push(PublishElement::new(base64, uri));
        }

        // MFT and CRL for each key
        let sets = match &self.key_state {
            KeyState::Pending(_) => vec![],
            KeyState::Active(current) => vec![current.current_set()],
            KeyState::RollPending(_, current) => vec![current.current_set()],
            KeyState::RollNew(new, current) => vec![new.current_set(), current.current_set()],
            KeyState::RollOld(current, old) => vec![current.current_set(), old.current_set()],
        };

        for set in sets {
            let crl_info = set.crl_info();
            let crl_base64 = crl_info.current().content().clone();
            let crl_uri = base_repo.resolve(ns, crl_info.name());
            res.push(PublishElement::new(crl_base64, crl_uri));

            let mft_info = set.manifest_info();
            let mft_base64 = mft_info.current().content().clone();
            let mft_uri = base_repo.resolve(ns, mft_info.name());
            res.push(PublishElement::new(mft_base64, mft_uri));
        }

        res
    }
}

/// # Removing a resource class
///
impl ResourceClass {
    /// Returns withdraws for all current objects, for when this resource class
    /// needs to be removed.
    pub fn withdraw(&self, base_repo: &RepoInfo) -> ObjectsDelta {
        let base_repo = base_repo.ca_repository(self.name_space());
        let mut delta = ObjectsDelta::new(base_repo);

        for withdraw in self.current_objects().withdraw().into_iter() {
            delta.withdraw(withdraw);
        }
        delta
    }

    /// Returns revocation requests for all certified keys in this resource class.
    pub fn revoke<S: Signer>(&self, signer: &S) -> KrillResult<Vec<RevocationRequest>> {
        self.key_state.revoke(self.parent_rc_name.clone(), signer)
    }
}

/// # Key Life Cycle and Receiving Certificates
///
impl ResourceClass {
    /// This function marks a certificate as received.
    pub fn received_cert(&mut self, key_id: KeyIdentifier, cert: RcvdCert) {
        // if there is a pending key, then we need to do some promotions..
        match &mut self.key_state {
            KeyState::Pending(_pending) => panic!("Would have received KeyPendingToActive event"),
            KeyState::Active(current) => {
                current.set_incoming_cert(cert);
            }
            KeyState::RollPending(_pending, current) => {
                current.set_incoming_cert(cert);
            }
            KeyState::RollNew(new, current) => {
                if new.key_id() == &key_id {
                    new.set_incoming_cert(cert);
                } else {
                    current.set_incoming_cert(cert);
                }
            }
            KeyState::RollOld(current, old) => {
                if current.key_id() == &key_id {
                    current.set_incoming_cert(cert);
                } else {
                    old.set_incoming_cert(cert);
                }
            }
        }
    }

    /// Adds a pending key.
    pub fn pending_key_added(&mut self, key_id: KeyIdentifier) {
        match &self.key_state {
            KeyState::Active(current) => {
                let pending = PendingKey::new(key_id);
                self.key_state = KeyState::RollPending(pending, current.clone())
            }
            _ => panic!("Should never create event to add key when roll in progress"),
        }
    }

    /// Moves a pending key to new
    pub fn pending_key_to_new(&mut self, new: CertifiedKey) {
        match &self.key_state {
            KeyState::RollPending(_pending, current) => {
                self.key_state = KeyState::RollNew(new, current.clone());
            }
            _ => panic!("Cannot move pending to new, if state is not roll pending"),
        }
    }

    /// Moves a pending key to current
    pub fn pending_key_to_active(&mut self, new: CertifiedKey) {
        match &self.key_state {
            KeyState::Pending(_pending) => {
                self.key_state = KeyState::Active(new);
            }
            _ => panic!("Cannot move pending to active, if state is not pending"),
        }
    }

    /// Activates the new key
    pub fn new_key_activated(&mut self, revoke_req: RevocationRequest) {
        match &self.key_state {
            KeyState::RollNew(new, current) => {
                let old_key = OldKey::new(current.clone(), revoke_req);
                self.key_state = KeyState::RollOld(new.clone(), old_key);
            }
            _ => panic!("Should never create event to activate key when no roll in progress"),
        }
    }

    /// Removes the old key, we return the to the state where there is one active key.
    pub fn old_key_removed(&mut self) {
        match &self.key_state {
            KeyState::RollOld(current, _old) => {
                self.key_state = KeyState::Active(current.clone());
            }
            _ => panic!("Should never create event to remove old key, when there is none"),
        }
    }

    /// Initiate a key roll
    pub fn keyroll_initiate<S: Signer>(
        &self,
        base_repo: &RepoInfo,
        duration: Duration,
        signer: &mut S,
    ) -> KrillResult<Vec<EvtDet>> {
        if self.last_key_change + duration > Time::now() {
            return Ok(vec![]);
        }

        self.key_state.keyroll_initiate(
            self.name.clone(),
            self.parent_rc_name.clone(),
            base_repo,
            &self.name_space,
            signer,
        )
    }

    /// Activate a new key, if it's been longer than the staging period.
    pub fn keyroll_activate<S: Signer>(
        &self,
        repo_info: &RepoInfo,
        staging: Duration,
        signer: &S,
    ) -> KrillResult<Vec<EvtDet>> {
        if !self.key_state.has_new_key() || self.last_key_change + staging > Time::now() {
            return Ok(vec![]);
        }

        let mut res = vec![];

        let authorizations: Vec<RouteAuthorization> = self.roas.authorizations().cloned().collect();

        res.push(self.key_state.keyroll_activate(
            self.name.clone(),
            self.parent_rc_name.clone(),
            signer,
        )?);

        res.append(&mut self.republish(
            authorizations.as_slice(),
            repo_info,
            &PublishMode::KeyRollActivation,
            signer,
        )?);

        Ok(res)
    }

    /// Finish a key roll, withdraw the old key
    pub fn keyroll_finish(&self, base_repo: &RepoInfo) -> KrillResult<EvtDet> {
        match &self.key_state {
            KeyState::RollOld(_current, old) => {
                let mut objects_delta =
                    ObjectsDelta::new(base_repo.ca_repository(self.name_space()));

                let crl_info = old.current_set().crl_info();
                objects_delta.withdraw(crl_info.withdraw());

                let mft_info = old.current_set().manifest_info();
                objects_delta.withdraw(mft_info.withdraw());

                Ok(EvtDet::KeyRollFinished(self.name.clone(), objects_delta))
            }
            _ => Err(Error::KeyUseNoOldKey),
        }
    }
}

/// # Issuing certificates
///
impl ResourceClass {
    /// Makes a single CA certificate and wraps it in an issuance response.
    ///
    /// Will use the intersection of the requested child resources, and the
    /// resources actually held by the this resource class. An error will be
    /// returned if a ResourceRequestLimit was used that includes resources
    /// that are not in this intersection.
    ///
    /// Note that this certificate still needs to be added to this RC by
    /// calling the update_certs function.
    pub fn issue_cert<S: Signer>(
        &self,
        csr: CsrInfo,
        child_resources: &ResourceSet,
        limit: RequestResourceLimit,
        signer: &S,
    ) -> KrillResult<IssuedCert> {
        let signing_key = self.get_current_key()?;
        let parent_resources = signing_key.incoming_cert().resources();
        let resources = parent_resources.intersection(child_resources);
        let replaces = self
            .certificates
            .get(&csr.key_id())
            .map(ReplacedObject::from);

        let issued =
            SignSupport::make_issued_cert(csr, &resources, limit, replaces, signing_key, signer)?;

        Ok(issued)
    }

    fn re_issue<S: Signer>(
        &self,
        previous: &IssuedCert,
        updated_resources: Option<ResourceSet>,
        signing_key: &CertifiedKey,
        csr_info_opt: Option<CsrInfo>,
        signer: &S,
    ) -> KrillResult<IssuedCert> {
        let (_uri, limit, resource_set, cert) = previous.clone().unpack();
        let csr = csr_info_opt.unwrap_or_else(|| CsrInfo::from(&cert));
        let resource_set = updated_resources.unwrap_or(resource_set);
        let replaced = ReplacedObject::new(Revocation::from(&cert), HexEncodedHash::from(&cert));

        let re_issued = SignSupport::make_issued_cert(
            csr,
            &resource_set,
            limit,
            Some(replaced),
            signing_key,
            signer,
        )?;

        Ok(re_issued)
    }

    fn update_child_certificates<S: Signer>(
        &self,
        mode: &PublishMode,
        signer: &S,
    ) -> KrillResult<ChildCertificateUpdates> {
        let mut updates = ChildCertificateUpdates::default();

        let signing_key = match mode {
            PublishMode::KeyRollActivation => self.get_new_key()?,
            _ => self.get_current_key()?,
        };

        match mode {
            PublishMode::Normal => {
                // re-issue: things about to expire
                // revoke: nothing
                for issued in self.certificates.expiring() {
                    let re_issued = self.re_issue(issued, None, signing_key, None, signer)?;
                    updates.issue(re_issued);
                }
            }
            PublishMode::UpdatedResources(resources) => {
                //    re-issue: overclaiming with remaining
                //    revoke: overclaiming without remaining
                for issued in self.certificates.overclaiming(resources) {
                    let remaining_resources = issued.resource_set().intersection(resources);
                    if remaining_resources.is_empty() {
                        // revoke
                        updates.remove(issued.subject_key_identifier());
                    } else {
                        // re-issue
                        let re_issued = self.re_issue(
                            issued,
                            Some(remaining_resources),
                            signing_key,
                            None,
                            signer,
                        )?;
                        updates.issue(re_issued);
                    }
                }
            }
            PublishMode::KeyRollActivation => {
                for issued in self.certificates.iter() {
                    let re_issued = self.re_issue(issued, None, signing_key, None, signer)?;
                    updates.issue(re_issued);
                }
            }
            PublishMode::NewRepo(info) => {
                for issued in self.certificates.iter() {
                    let csr_info_update = CsrInfo::new(
                        info.ca_repository(self.name_space()),
                        info.rpki_manifest(self.name_space(), &issued.subject_key_identifier()),
                        Some(info.rpki_notify()),
                        issued.subject_public_key_info().clone(),
                    );

                    let re_issued =
                        self.re_issue(issued, None, signing_key, Some(csr_info_update), signer)?;
                    updates.issue(re_issued);
                }
            }
        }

        Ok(updates)
    }

    /// Stores an [IssuedCert](krill_commons.api.ca.IssuedCert)
    pub fn certificate_issued(&mut self, issued: IssuedCert) {
        self.certificates.certificate_issued(issued);
    }

    /// Returns an issued certificate for a key, if it exists
    pub fn issued(&self, ki: &KeyIdentifier) -> Option<&IssuedCert> {
        self.certificates.get(ki)
    }

    /// Removes a revoked key.
    pub fn key_revoked(&mut self, key: &KeyIdentifier) {
        self.certificates.key_revoked(key);
    }
}

/// # ROAs
///
impl ResourceClass {
    /// Updates the ROAs in accordance with the current authorizations, and
    /// the target resources and key determined by the PublishMode.
    pub fn update_roas<S: Signer>(
        &self,
        auths: &[RouteAuthorization],
        mode: &PublishMode,
        signer: &S,
    ) -> KrillResult<RoaUpdates> {
        let mut updates = RoaUpdates::default();

        let key = match mode {
            PublishMode::KeyRollActivation => self.get_new_key()?,
            _ => self.get_current_key()?,
        };

        let resources = match mode {
            PublishMode::Normal | PublishMode::NewRepo(_) => key.incoming_cert().resources(),
            PublishMode::UpdatedResources(resources) => resources,
            PublishMode::KeyRollActivation => self.get_current_key()?.incoming_cert().resources(),
        };

        let new_repo = match &mode {
            PublishMode::NewRepo(info) => Some(info.ca_repository(self.name_space())),
            _ => None,
        };

        // Remove any ROAs no longer in auths, or no longer in resources.
        for (current_auth, roa_info) in self.roas.iter() {
            if !auths.contains(current_auth) || !resources.contains(&current_auth.prefix().into()) {
                updates.remove(*current_auth, RevokedObject::from(roa_info.object()));
            }
        }

        for auth in auths {
            // if the auth is not in this resource class, just skip it.
            if !resources.contains(&auth.prefix().into()) {
                continue;
            }

            match self.roas.get(auth) {
                None => {
                    // NO ROA yet, so create one.
                    let roa = Roas::make_roa(auth, key, new_repo.as_ref(), signer)?;
                    let name = ObjectName::from(auth);
                    updates.update(*auth, RoaInfo::new_roa(&roa, name));
                }
                Some(roa) => {
                    // Re-issue if the ROA is getting close to its expiration time, or if we are
                    //  activating the new key.
                    let expiring = roa.object().expires()
                        < Time::now() + Duration::weeks(ROA_CERTIFICATE_REISSUE_WEEKS);
                    let activating = mode == &PublishMode::KeyRollActivation;

                    if expiring || activating || new_repo.is_some() {
                        let new_roa = Roas::make_roa(auth, key, new_repo.as_ref(), signer)?;
                        let name = ObjectName::from(auth);
                        updates.update(*auth, RoaInfo::updated_roa(roa, &new_roa, name));
                    }
                }
            }
        }

        Ok(updates)
    }

    /// Marks the ROAs as updated from a RoaUpdated event.
    pub fn roas_updated(&mut self, updates: RoaUpdates) {
        self.roas.updated(updates);
    }
}

//------------ PublishMode -------------------------------------------------

/// Describes which kind of publication we're after:
///
/// Normal: Use the current key and resources. ROAs are re-issued and revoked
///         under the current key - if needed.
///
/// UpdatedResources: Use the current key, but with the new resource set that
///         this key is about to be updated with.
///
/// PendingKeyActivation: The pending key will be activated, to either a new
///         (init) or the current (roll) key, and needs to be published.
///
/// KeyActivation: Publish ROAs and certificates under the new key, and revoke
///         them under the old key - which will be revoked shortly.
///
#[derive(Clone, Eq, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum PublishMode {
    Normal,
    UpdatedResources(ResourceSet),
    KeyRollActivation,
    NewRepo(RepoInfo),
}