|
| 1 | +package keeper |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + |
| 6 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 7 | + pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/types/pki" |
| 8 | + authTypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types" |
| 9 | + "github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types" |
| 10 | +) |
| 11 | + |
| 12 | +func (k Keeper) CertificateApprovalsCount(ctx sdk.Context, authKeeper types.DclauthKeeper) int { |
| 13 | + return int(math.Ceil(types.RootCertificateApprovalsPercent * |
| 14 | + float64(authKeeper.CountAccountsWithRole(ctx, authTypes.Trustee)))) |
| 15 | +} |
| 16 | + |
| 17 | +func (k Keeper) CertificateRejectApprovalsCount(ctx sdk.Context, authKeeper types.DclauthKeeper) int { |
| 18 | + return authKeeper.CountAccountsWithRole(ctx, authTypes.Trustee) - k.CertificateApprovalsCount(ctx, authKeeper) + 1 |
| 19 | +} |
| 20 | + |
| 21 | +func (k Keeper) EnsureVidMatches(ctx sdk.Context, owner string, signer string) error { |
| 22 | + // get signer VID |
| 23 | + signerAddr, err := sdk.AccAddressFromBech32(signer) |
| 24 | + if err != nil { |
| 25 | + return pkitypes.NewErrInvalidAddress(err) |
| 26 | + } |
| 27 | + |
| 28 | + signerAccount, _ := k.dclauthKeeper.GetAccountO(ctx, signerAddr) |
| 29 | + signerVid := signerAccount.VendorID |
| 30 | + |
| 31 | + // get owner VID |
| 32 | + ownerAddr, err := sdk.AccAddressFromBech32(owner) |
| 33 | + if err != nil { |
| 34 | + return pkitypes.NewErrInvalidAddress(err) |
| 35 | + } |
| 36 | + |
| 37 | + ownerAccount, _ := k.dclauthKeeper.GetAccountO(ctx, ownerAddr) |
| 38 | + ownerVid := ownerAccount.VendorID |
| 39 | + |
| 40 | + if signerVid != ownerVid { |
| 41 | + return pkitypes.NewErrUnauthorizedCertVendor(ownerVid) |
| 42 | + } |
| 43 | + |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func RemoveCertFromList(issuer string, serialNumber string, certs *[]*types.Certificate) { |
| 48 | + certIndex := -1 |
| 49 | + |
| 50 | + for i, cert := range *certs { |
| 51 | + if cert.SerialNumber == serialNumber && cert.Issuer == issuer { |
| 52 | + certIndex = i |
| 53 | + |
| 54 | + break |
| 55 | + } |
| 56 | + } |
| 57 | + if certIndex == -1 { |
| 58 | + return |
| 59 | + } |
| 60 | + *certs = append((*certs)[:certIndex], (*certs)[certIndex+1:]...) |
| 61 | +} |
| 62 | + |
| 63 | +func FindCertificateInList(serialNumber string, certificates *[]*types.Certificate) (*types.Certificate, bool) { |
| 64 | + for _, cert := range *certificates { |
| 65 | + if cert.SerialNumber == serialNumber { |
| 66 | + return cert, true |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return nil, false |
| 71 | +} |
| 72 | + |
| 73 | +func FilterCertificateList(certificates *[]*types.Certificate, predicate CertificatePredicate) []*types.Certificate { |
| 74 | + var result []*types.Certificate |
| 75 | + |
| 76 | + for _, s := range *certificates { |
| 77 | + if predicate(s) { |
| 78 | + result = append(result, s) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return result |
| 83 | +} |
| 84 | + |
| 85 | +func (k msgServer) AddCertificateToGlobalCertificateIndexes( |
| 86 | + ctx sdk.Context, |
| 87 | + certificate types.Certificate, |
| 88 | +) { |
| 89 | + // add to the global list of certificates |
| 90 | + k.AddAllCertificate(ctx, certificate) |
| 91 | + // add to the global list of certificates indexed by subject key id |
| 92 | + k.AddAllCertificateBySubjectKeyID(ctx, certificate) |
| 93 | + // add to the global list of certificates indexed by subject |
| 94 | + k.AddAllCertificateBySubject(ctx, certificate.Subject, certificate.SubjectKeyId) |
| 95 | +} |
| 96 | + |
| 97 | +func (k msgServer) RemoveCertificateFromGlobalCertificateIndexes( |
| 98 | + ctx sdk.Context, |
| 99 | + subject string, |
| 100 | + subjectKeyID string, |
| 101 | +) { |
| 102 | + // remove from the global list of certificates |
| 103 | + k.RemoveAllCertificates(ctx, subject, subjectKeyID) |
| 104 | + // remove from the global list of certificates indexed by subject key id |
| 105 | + k.RemoveAllCertificatesBySubjectKeyID(ctx, subject, subjectKeyID) |
| 106 | + // remove from the global list of certificates indexed by subject |
| 107 | + k.RemoveAllCertificateBySubject(ctx, subject, subjectKeyID) |
| 108 | +} |
| 109 | + |
| 110 | +func (k msgServer) StoreDaCertificate( |
| 111 | + ctx sdk.Context, |
| 112 | + certificate types.Certificate, |
| 113 | + isRoot bool, |
| 114 | +) { |
| 115 | + // add to Global certificates indexes |
| 116 | + k.AddCertificateToGlobalCertificateIndexes(ctx, certificate) |
| 117 | + |
| 118 | + // add to list of certificates with the same Subject/SubjectKeyID combination and store updated list |
| 119 | + k.AddApprovedCertificate(ctx, certificate) |
| 120 | + |
| 121 | + // add to list of certificates indexed by subject |
| 122 | + k.AddApprovedCertificateBySubject(ctx, certificate.Subject, certificate.SubjectKeyId) |
| 123 | + |
| 124 | + // add to list of certificates indexed by subject key id |
| 125 | + k.AddApprovedCertificateBySubjectKeyID(ctx, certificate) |
| 126 | + |
| 127 | + if isRoot { |
| 128 | + // add to root certificates index |
| 129 | + k.AddApprovedRootCertificate(ctx, certificate) |
| 130 | + } else { |
| 131 | + // add the certificate identifier to the issuer's Child Certificates record |
| 132 | + k.AddChildCertificate(ctx, certificate) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (k msgServer) RemoveDaCertificate( |
| 137 | + ctx sdk.Context, |
| 138 | + subject string, |
| 139 | + subjectKeyID string, |
| 140 | + isRoot bool, |
| 141 | +) { |
| 142 | + // remove from global list |
| 143 | + k.RemoveCertificateFromGlobalCertificateIndexes(ctx, subject, subjectKeyID) |
| 144 | + // remove from approved certificates map |
| 145 | + k.RemoveApprovedCertificates(ctx, subject, subjectKeyID) |
| 146 | + // remove from subject -> subject key ID map |
| 147 | + k.RemoveApprovedCertificateBySubject(ctx, subject, subjectKeyID) |
| 148 | + // remove from subject key ID -> certificates map |
| 149 | + k.RemoveApprovedCertificatesBySubjectKeyID(ctx, subject, subjectKeyID) |
| 150 | + if isRoot { |
| 151 | + k.RemoveApprovedRootCertificate(ctx, subject, subjectKeyID) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func (k msgServer) RemoveDaCertificateBySerialNumber( |
| 156 | + ctx sdk.Context, |
| 157 | + subject string, |
| 158 | + subjectKeyID string, |
| 159 | + certificates *types.ApprovedCertificates, |
| 160 | + serialNumber string, |
| 161 | + issuer string, |
| 162 | +) { |
| 163 | + RemoveCertFromList(issuer, serialNumber, &certificates.Certs) |
| 164 | + |
| 165 | + if len(certificates.Certs) == 0 { |
| 166 | + k.RemoveDaCertificate(ctx, subject, subjectKeyID, false) |
| 167 | + } else { |
| 168 | + k.RemoveAllCertificatesBySerialNumber(ctx, subject, subjectKeyID, serialNumber) |
| 169 | + k.RemoveAllCertificatesBySubjectKeyIDBySerialNumber(ctx, subject, subjectKeyID, serialNumber) |
| 170 | + k.RemoveApprovedCertificatesBySerialNumber(ctx, subject, subjectKeyID, serialNumber) |
| 171 | + k.RemoveApprovedCertificatesBySubjectKeyIDBySerialNumber(ctx, subject, subjectKeyID, serialNumber) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func (k msgServer) StoreNocCertificate( |
| 176 | + ctx sdk.Context, |
| 177 | + certificate types.Certificate, |
| 178 | + isRoot bool) { |
| 179 | + // add to Global certificates indexes |
| 180 | + k.AddCertificateToGlobalCertificateIndexes(ctx, certificate) |
| 181 | + |
| 182 | + // add to the list of all NOC certificates |
| 183 | + k.AddNocCertificate(ctx, certificate) |
| 184 | + |
| 185 | + // add to certificates map indexed by vid/skid |
| 186 | + k.AddNocCertificateByVidAndSkid(ctx, certificate) |
| 187 | + |
| 188 | + // add to certificates map indexed by subject |
| 189 | + k.AddNocCertificateBySubject(ctx, certificate) |
| 190 | + |
| 191 | + // add to certificates map indexed by subject key id |
| 192 | + k.AddNocCertificateBySubjectKeyID(ctx, certificate) |
| 193 | + |
| 194 | + if isRoot { |
| 195 | + // add to the list of NOC root certificates with the same VID |
| 196 | + k.AddNocRootCertificate(ctx, certificate) |
| 197 | + } else { |
| 198 | + // add to the list of NOC ica certificates with the same VID |
| 199 | + k.AddNocIcaCertificate(ctx, certificate) |
| 200 | + // add the certificate identifier to the issuer's Child Certificates record |
| 201 | + k.AddChildCertificate(ctx, certificate) |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func (k msgServer) RemoveNocCertificate( |
| 206 | + ctx sdk.Context, |
| 207 | + subject string, |
| 208 | + subjectKeyID string, |
| 209 | + accountVid int32, |
| 210 | + isRoot bool, |
| 211 | +) { |
| 212 | + // remove from global list |
| 213 | + k.RemoveCertificateFromGlobalCertificateIndexes(ctx, subject, subjectKeyID) |
| 214 | + // remove from noc certificates map |
| 215 | + k.RemoveNocCertificates(ctx, subject, subjectKeyID) |
| 216 | + // remove from vid, subject key id map |
| 217 | + k.RemoveNocCertificatesByVidAndSkid(ctx, accountVid, subjectKeyID) |
| 218 | + // remove from subject -> subject key ID map |
| 219 | + k.RemoveNocCertificateBySubject(ctx, subject, subjectKeyID) |
| 220 | + // remove from subject key ID -> certificates map |
| 221 | + k.RemoveNocCertificatesBySubjectAndSubjectKeyID(ctx, subject, subjectKeyID) |
| 222 | + if isRoot { |
| 223 | + // remove from noc root certificates map |
| 224 | + k.RemoveNocRootCertificate(ctx, subject, subjectKeyID, accountVid) |
| 225 | + } else { |
| 226 | + // remove from noc ica certificates map |
| 227 | + k.RemoveNocIcaCertificate(ctx, subject, subjectKeyID, accountVid) |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +func (k msgServer) RemoveNocCertBySerialNumber( |
| 232 | + ctx sdk.Context, |
| 233 | + subject string, |
| 234 | + subjectKeyID string, |
| 235 | + certificates *types.NocCertificates, |
| 236 | + accountVid int32, |
| 237 | + serialNumber string, |
| 238 | + issuer string, |
| 239 | + isRoot bool, |
| 240 | +) { |
| 241 | + RemoveCertFromList(issuer, serialNumber, &certificates.Certs) |
| 242 | + |
| 243 | + if len(certificates.Certs) == 0 { |
| 244 | + k.RemoveNocCertificate(ctx, subject, subjectKeyID, accountVid, isRoot) |
| 245 | + } else { |
| 246 | + k.RemoveAllCertificatesBySerialNumber(ctx, subject, subjectKeyID, serialNumber) |
| 247 | + k.RemoveAllCertificatesBySubjectKeyIDBySerialNumber(ctx, subject, subjectKeyID, serialNumber) |
| 248 | + k.RemoveNocCertificatesBySerialNumber(ctx, subject, subjectKeyID, serialNumber) |
| 249 | + k.RemoveNocCertificatesBySubjectKeyIDBySerialNumber(ctx, subject, subjectKeyID, serialNumber) |
| 250 | + k.RemoveNocCertificatesByVidAndSkidBySerialNumber(ctx, accountVid, subject, subjectKeyID, serialNumber) |
| 251 | + if isRoot { |
| 252 | + k.RemoveNocRootCertificateBySerialNumber(ctx, subject, subjectKeyID, accountVid, serialNumber) |
| 253 | + } else { |
| 254 | + k.RemoveNocIcaCertificateBySerialNumber(ctx, subject, subjectKeyID, accountVid, serialNumber) |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments