Commit 972f1516d04b41c817dc25c2d3737391a9550c1f

Authored by Vladyslav_Prykhodko
Committed by Andrew Shvayka
1 parent 3bf48f0f

UI: Change maxlength key validation LWm2M security in PSK and x509 mode

... ... @@ -28,27 +28,18 @@
28 28 <mat-form-field class="mat-block">
29 29 <mat-label>{{ 'device.lwm2m-security-config.client-publicKey-or-id' | translate }}</mat-label>
30 30 <textarea matInput
31   - #clientPublicKeyOrId
32   - [maxlength]="lenMaxClientPublicKeyOrId"
33 31 cdkTextareaAutosize
34 32 cdkAutosizeMinRows="1"
35 33 cols="1"
36 34 formControlName="clientPublicKeyOrId"
37 35 required>
38 36 </textarea>
39   - <mat-hint align="end">{{clientPublicKeyOrId.value?.length || 0}}/{{lenMaxClientPublicKeyOrId}}</mat-hint>
40 37 <mat-error *ngIf="serverFormGroup.get('clientPublicKeyOrId').hasError('required')">
41 38 {{ 'device.lwm2m-security-config.client-publicKey-or-id-required' | translate }}
42 39 </mat-error>
43 40 <mat-error *ngIf="serverFormGroup.get('clientPublicKeyOrId').hasError('pattern')">
44 41 {{ 'device.lwm2m-security-config.client-publicKey-or-id-pattern' | translate }}
45 42 </mat-error>
46   - <mat-error *ngIf="(serverFormGroup.get('clientPublicKeyOrId').hasError('maxlength') ||
47   - serverFormGroup.get('clientPublicKeyOrId').hasError('minlength'))">
48   - {{ 'device.lwm2m-security-config.client-publicKey-or-id-length' | translate: {
49   - count: lenMaxClientPublicKeyOrId
50   - } }}
51   - </mat-error>
52 43 </mat-form-field>
53 44 <mat-form-field class="mat-block">
54 45 <mat-label>{{ 'device.lwm2m-security-config.client-secret-key' | translate }}</mat-label>
... ... @@ -61,17 +52,18 @@
61 52 formControlName="clientSecretKey"
62 53 required>
63 54 </textarea>
64   - <mat-hint align="end">{{clientSecretKey.value?.length || 0}}/{{lengthClientSecretKey}}</mat-hint>
  55 + <mat-hint [fxShow]="serverFormGroup.get('securityMode').value === securityConfigLwM2MType.PSK" align="end">
  56 + {{clientSecretKey.value?.length || 0}}/{{lengthClientSecretKey}}
  57 + </mat-hint>
65 58 <mat-error *ngIf="serverFormGroup.get('clientSecretKey').hasError('required')">
66 59 {{ 'device.lwm2m-security-config.client-secret-key-required' | translate }}
67 60 </mat-error>
68 61 <mat-error *ngIf="serverFormGroup.get('clientSecretKey').hasError('pattern')">
69 62 {{ 'device.lwm2m-security-config.client-secret-key-pattern' | translate }}
70 63 </mat-error>
71   - <mat-error *ngIf="(serverFormGroup.get('clientSecretKey').hasError('maxlength') ||
72   - serverFormGroup.get('clientSecretKey').hasError('minlength'))">
  64 + <mat-error *ngIf="serverFormGroup.get('clientSecretKey').hasError('length')">
73 65 {{ 'device.lwm2m-security-config.client-secret-key-length' | translate: {
74   - count: lengthClientSecretKey
  66 + count: allowLengthKey.join(', ')
75 67 } }}
76 68 </mat-error>
77 69 </mat-form-field>
... ...
... ... @@ -16,6 +16,7 @@
16 16
17 17 import { Component, forwardRef, OnDestroy } from '@angular/core';
18 18 import {
  19 + AbstractControl,
19 20 ControlValueAccessor,
20 21 FormBuilder,
21 22 FormGroup,
... ... @@ -29,8 +30,6 @@ import {
29 30 KEY_REGEXP_HEX_DEC,
30 31 LEN_MAX_PRIVATE_KEY,
31 32 LEN_MAX_PSK,
32   - LEN_MAX_PUBLIC_KEY_RPK,
33   - LEN_MAX_PUBLIC_KEY_X509,
34 33 Lwm2mSecurityType,
35 34 Lwm2mSecurityTypeTranslationMap,
36 35 ServerSecurityConfig
... ... @@ -62,9 +61,8 @@ export class DeviceCredentialsLwm2mServerComponent implements OnDestroy, Control
62 61 securityConfigLwM2MType = Lwm2mSecurityType;
63 62 securityConfigLwM2MTypes = Object.values(Lwm2mSecurityType);
64 63 lwm2mSecurityTypeTranslationMap = Lwm2mSecurityTypeTranslationMap;
65   - lenMinClientPublicKeyOrId = 0;
66   - lenMaxClientPublicKeyOrId = LEN_MAX_PUBLIC_KEY_RPK;
67 64 lengthClientSecretKey = LEN_MAX_PRIVATE_KEY;
  65 + allowLengthKey = [32, 64, LEN_MAX_PSK];
68 66
69 67 private destroy$ = new Subject();
70 68 private propagateChange = (v: any) => {};
... ... @@ -134,21 +132,15 @@ export class DeviceCredentialsLwm2mServerComponent implements OnDestroy, Control
134 132 this.serverFormGroup.get('clientSecretKey').disable();
135 133 break;
136 134 case Lwm2mSecurityType.PSK:
137   - this.lenMinClientPublicKeyOrId = 0;
138   - this.lenMaxClientPublicKeyOrId = LEN_MAX_PUBLIC_KEY_RPK;
139 135 this.lengthClientSecretKey = LEN_MAX_PSK;
140 136 this.setValidatorsSecurity(securityMode);
141 137 break;
142 138 case Lwm2mSecurityType.RPK:
143   - this.lenMinClientPublicKeyOrId = LEN_MAX_PUBLIC_KEY_RPK;
144   - this.lenMaxClientPublicKeyOrId = LEN_MAX_PUBLIC_KEY_RPK;
145   - this.lengthClientSecretKey = LEN_MAX_PRIVATE_KEY;
  139 + this.lengthClientSecretKey = null;
146 140 this.setValidatorsSecurity(securityMode);
147 141 break;
148 142 case Lwm2mSecurityType.X509:
149   - this.lenMinClientPublicKeyOrId = 0;
150   - this.lenMaxClientPublicKeyOrId = LEN_MAX_PUBLIC_KEY_X509;
151   - this.lengthClientSecretKey = LEN_MAX_PRIVATE_KEY;
  143 + this.lengthClientSecretKey = null;
152 144 this.setValidatorsSecurity(securityMode);
153 145 break;
154 146 }
... ... @@ -157,25 +149,28 @@ export class DeviceCredentialsLwm2mServerComponent implements OnDestroy, Control
157 149 }
158 150
159 151 private setValidatorsSecurity = (securityMode: Lwm2mSecurityType): void => {
  152 + const clientSecretKeyValidators = [Validators.required, Validators.pattern(KEY_REGEXP_HEX_DEC)];
  153 + const clientPublicKeyOrIdValidators = [Validators.required];
160 154 if (securityMode === Lwm2mSecurityType.PSK) {
161   - this.serverFormGroup.get('clientPublicKeyOrId').setValidators([Validators.required]);
  155 + clientSecretKeyValidators.push(this.maxLength(this.allowLengthKey));
162 156 } else {
163   - this.serverFormGroup.get('clientPublicKeyOrId').setValidators([
164   - Validators.required,
165   - Validators.pattern(KEY_REGEXP_HEX_DEC),
166   - Validators.minLength(this.lenMinClientPublicKeyOrId),
167   - Validators.maxLength(this.lenMaxClientPublicKeyOrId)
168   - ]);
  157 + clientPublicKeyOrIdValidators.push(Validators.pattern(KEY_REGEXP_HEX_DEC));
169 158 }
170 159
171   - this.serverFormGroup.get('clientSecretKey').setValidators([
172   - Validators.required,
173   - Validators.pattern(KEY_REGEXP_HEX_DEC),
174   - Validators.minLength(this.lengthClientSecretKey),
175   - Validators.maxLength(this.lengthClientSecretKey)
176   - ]);
  160 + this.serverFormGroup.get('clientPublicKeyOrId').setValidators(clientPublicKeyOrIdValidators);
  161 + this.serverFormGroup.get('clientSecretKey').setValidators(clientSecretKeyValidators);
177 162
178 163 this.serverFormGroup.get('clientPublicKeyOrId').enable({emitEvent: false});
179 164 this.serverFormGroup.get('clientSecretKey').enable();
180 165 }
  166 +
  167 + private maxLength(keyLengths: number[]) {
  168 + return (control: AbstractControl): ValidationErrors | null => {
  169 + const value = control.value;
  170 + if (keyLengths.some(len => value.length === len)) {
  171 + return null;
  172 + }
  173 + return {length: true};
  174 + };
  175 + }
181 176 }
... ...
... ... @@ -53,7 +53,8 @@
53 53 formControlName="key"
54 54 required>
55 55 </textarea>
56   - <mat-hint align="end">{{key.value?.length || 0}}/{{lenMaxKeyClient}}</mat-hint>
  56 + <mat-hint *ngIf="lwm2mConfigFormGroup.get('client.securityConfigClientMode').value === securityConfigLwM2MType.PSK"
  57 + align="end">{{key.value?.length || 0}}/{{lenMaxKeyClient}}</mat-hint>
57 58 <mat-error *ngIf="lwm2mConfigFormGroup.get('client.key').hasError('required')">
58 59 {{ 'device.lwm2m-security-config.client-key-required' | translate }}
59 60 </mat-error>
... ...
... ... @@ -31,7 +31,6 @@ import {
31 31 getDefaultServerSecurityConfig,
32 32 KEY_REGEXP_HEX_DEC,
33 33 LEN_MAX_PSK,
34   - LEN_MAX_PUBLIC_KEY_RPK,
35 34 Lwm2mSecurityConfigModels,
36 35 Lwm2mSecurityType,
37 36 Lwm2mSecurityTypeTranslationMap
... ... @@ -65,7 +64,7 @@ export class DeviceCredentialsLwm2mComponent implements ControlValueAccessor, Va
65 64 securityConfigLwM2MTypes = Object.keys(Lwm2mSecurityType);
66 65 credentialTypeLwM2MNamesMap = Lwm2mSecurityTypeTranslationMap;
67 66 lenMaxKeyClient = LEN_MAX_PSK;
68   - allowLengthKey: number[];
  67 + allowLengthKey = [32, 64, LEN_MAX_PSK];
69 68
70 69 private destroy$ = new Subject();
71 70 private propagateChange = (v: any) => {};
... ... @@ -137,13 +136,11 @@ export class DeviceCredentialsLwm2mComponent implements ControlValueAccessor, Va
137 136 break;
138 137 case Lwm2mSecurityType.PSK:
139 138 this.lenMaxKeyClient = LEN_MAX_PSK;
140   - this.allowLengthKey = [32, 64, LEN_MAX_PSK];
141 139 this.setValidatorsPskRpk(mode);
142 140 this.lwm2mConfigFormGroup.get('client.identity').enable({emitEvent: false});
143 141 break;
144 142 case Lwm2mSecurityType.RPK:
145   - this.lenMaxKeyClient = LEN_MAX_PUBLIC_KEY_RPK;
146   - this.allowLengthKey = [LEN_MAX_PUBLIC_KEY_RPK];
  143 + this.lenMaxKeyClient = null;
147 144 this.setValidatorsPskRpk(mode);
148 145 this.lwm2mConfigFormGroup.get('client.identity').disable({emitEvent: false});
149 146 break;
... ... @@ -160,16 +157,14 @@ export class DeviceCredentialsLwm2mComponent implements ControlValueAccessor, Va
160 157 }
161 158
162 159 private setValidatorsPskRpk = (mode: Lwm2mSecurityType): void => {
  160 + const keyValidators = [Validators.required, Validators.pattern(KEY_REGEXP_HEX_DEC)];
163 161 if (mode === Lwm2mSecurityType.PSK) {
164 162 this.lwm2mConfigFormGroup.get('client.identity').setValidators([Validators.required]);
  163 + keyValidators.push(this.maxLength(this.allowLengthKey));
165 164 } else {
166 165 this.lwm2mConfigFormGroup.get('client.identity').clearValidators();
167 166 }
168   - this.lwm2mConfigFormGroup.get('client.key').setValidators([
169   - Validators.required,
170   - Validators.pattern(KEY_REGEXP_HEX_DEC),
171   - this.maxLength(this.allowLengthKey)
172   - ]);
  167 + this.lwm2mConfigFormGroup.get('client.key').setValidators(keyValidators);
173 168 this.lwm2mConfigFormGroup.get('client.key').enable({emitEvent: false});
174 169 this.lwm2mConfigFormGroup.get('client.cert').disable({emitEvent: false});
175 170 }
... ...
... ... @@ -95,24 +95,17 @@
95 95 <mat-form-field class="mat-block">
96 96 <mat-label>{{ 'device-profile.lwm2m.server-public-key' | translate }}</mat-label>
97 97 <textarea matInput
98   - #serverPublicKey
99   - maxlength="{{maxLengthPublicKey}}"
100 98 cdkTextareaAutosize
101 99 cdkAutosizeMinRows="1"
102 100 cols="1" required
103 101 formControlName="serverPublicKey"
104 102 ></textarea>
105   - <mat-hint align="end">{{serverPublicKey.value?.length || 0}}/{{maxLengthPublicKey}}</mat-hint>
106 103 <mat-error *ngIf="serverFormGroup.get('serverPublicKey').hasError('required')">
107 104 {{ 'device-profile.lwm2m.server-public-key-required' | translate }}
108 105 </mat-error>
109 106 <mat-error *ngIf="serverFormGroup.get('serverPublicKey').hasError('pattern')">
110 107 {{ 'device-profile.lwm2m.server-public-key-pattern' | translate }}
111 108 </mat-error>
112   - <mat-error *ngIf="serverFormGroup.get('serverPublicKey').hasError('maxlength') ||
113   - serverFormGroup.get('serverPublicKey').hasError('minlength')">
114   - {{ 'device-profile.lwm2m.server-public-key-length' | translate: {count: maxLengthPublicKey } }}
115   - </mat-error>
116 109 </mat-form-field>
117 110 </div>
118 111 </section>
... ...
... ... @@ -29,8 +29,6 @@ import {
29 29 DEFAULT_PORT_BOOTSTRAP_NO_SEC,
30 30 DEFAULT_PORT_SERVER_NO_SEC,
31 31 KEY_REGEXP_HEX_DEC,
32   - LEN_MAX_PUBLIC_KEY_RPK,
33   - LEN_MAX_PUBLIC_KEY_X509,
34 32 securityConfigMode,
35 33 securityConfigModeNames,
36 34 ServerSecurityConfig
... ... @@ -68,7 +66,6 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc
68 66 securityConfigLwM2MType = securityConfigMode;
69 67 securityConfigLwM2MTypes = Object.keys(securityConfigMode);
70 68 credentialTypeLwM2MNamesMap = securityConfigModeNames;
71   - maxLengthPublicKey = LEN_MAX_PUBLIC_KEY_RPK;
72 69 currentSecurityMode = null;
73 70
74 71 @Input()
... ... @@ -147,12 +144,10 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc
147 144 this.clearValidators();
148 145 break;
149 146 case securityConfigMode.RPK:
150   - this.maxLengthPublicKey = LEN_MAX_PUBLIC_KEY_RPK;
151   - this.setValidators(LEN_MAX_PUBLIC_KEY_RPK);
  147 + this.setValidators();
152 148 break;
153 149 case securityConfigMode.X509:
154   - this.maxLengthPublicKey = LEN_MAX_PUBLIC_KEY_X509;
155   - this.setValidators(0);
  150 + this.setValidators();
156 151 break;
157 152 }
158 153 this.serverFormGroup.get('serverPublicKey').updateValueAndValidity({emitEvent: false});
... ... @@ -162,12 +157,10 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc
162 157 this.serverFormGroup.get('serverPublicKey').clearValidators();
163 158 }
164 159
165   - private setValidators(minLengthKey: number): void {
  160 + private setValidators(): void {
166 161 this.serverFormGroup.get('serverPublicKey').setValidators([
167 162 Validators.required,
168   - Validators.pattern(KEY_REGEXP_HEX_DEC),
169   - Validators.minLength(minLengthKey),
170   - Validators.maxLength(this.maxLengthPublicKey)
  163 + Validators.pattern(KEY_REGEXP_HEX_DEC)
171 164 ]);
172 165 }
173 166
... ...
... ... @@ -34,8 +34,6 @@ export const DEFAULT_MIN_PERIOD = 1;
34 34 export const DEFAULT_NOTIF_IF_DESIBLED = true;
35 35 export const DEFAULT_BINDING = 'UQ';
36 36 export const DEFAULT_BOOTSTRAP_SERVER_ACCOUNT_TIME_OUT = 0;
37   -export const LEN_MAX_PUBLIC_KEY_RPK = 182;
38   -export const LEN_MAX_PUBLIC_KEY_X509 = 3000;
39 37 export const KEY_REGEXP_HEX_DEC = /^[-+]?[0-9A-Fa-f]+\.?[0-9A-Fa-f]*?$/;
40 38 export const INSTANCES_ID_VALUE_MIN = 0;
41 39 export const INSTANCES_ID_VALUE_MAX = 65535;
... ...
... ... @@ -16,8 +16,6 @@
16 16
17 17 export const LEN_MAX_PSK = 128;
18 18 export const LEN_MAX_PRIVATE_KEY = 134;
19   -export const LEN_MAX_PUBLIC_KEY_RPK = 182;
20   -export const LEN_MAX_PUBLIC_KEY_X509 = 3000;
21 19 export const KEY_REGEXP_HEX_DEC = /^[-+]?[0-9A-Fa-f]+\.?[0-9A-Fa-f]*?$/;
22 20
23 21 export enum Lwm2mSecurityType {
... ...
... ... @@ -974,7 +974,6 @@
974 974 "client-publicKey-or-id": "Veřejný klíč nebo Id klienta",
975 975 "client-publicKey-or-id-required": "Veřejný klíč nebo Id klienta jsou povinné.",
976 976 "client-publicKey-or-id-pattern": "Veřejný klíč klienta nebo Id musí být v hexadecimálním formátu.",
977   - "client-publicKey-or-id-length": "Veřejný klíč klienta nebo Id musí mít {{ count }} znaků.",
978 977 "client-secret-key": "Tajný klíč klienta",
979 978 "client-secret-key-required": "Tajný klíč klienta je povinný.",
980 979 "client-secret-key-pattern": "Tajný klíč klienta musí být v hexadecimálním formátu.",
... ... @@ -1301,7 +1300,6 @@
1301 1300 "server-public-key": "Veřejný klíč serveru",
1302 1301 "server-public-key-required": "Veřejný klíč serveru je povinný.",
1303 1302 "server-public-key-pattern": "Veřejný klíč serveru musí být v hexadecimálním formátu.",
1304   - "server-public-key-length": "Veřejný klíč serveru musí být {{ count }} znaků.",
1305 1303 "client-hold-off-time": "Čas odložení",
1306 1304 "client-hold-off-time-required": "Čas odloženíje povinný.",
1307 1305 "client-hold-off-time-pattern": "Čas odložení musí být kladné celé číslo.",
... ...
... ... @@ -978,7 +978,6 @@
978 978 "client-publicKey-or-id": "Client Public Key or Id",
979 979 "client-publicKey-or-id-required": "Client Public Key or Id is required.",
980 980 "client-publicKey-or-id-pattern": "Client Public Key or Id must be hexadecimal format.",
981   - "client-publicKey-or-id-length": "Client Public Key or Id must be {{ count }} characters.",
982 981 "client-secret-key": "Client Secret Key",
983 982 "client-secret-key-required": "Client Secret Key is required.",
984 983 "client-secret-key-pattern": "Client Secret Key must be hexadecimal format.",
... ... @@ -1315,7 +1314,6 @@
1315 1314 "server-public-key": "Server Public Key",
1316 1315 "server-public-key-required": "Server Public Key is required.",
1317 1316 "server-public-key-pattern": "Server Public Key must be hex decimal format.",
1318   - "server-public-key-length": "Server Public Key must be {{ count }} characters.",
1319 1317 "client-hold-off-time": "Hold Off Time",
1320 1318 "client-hold-off-time-required": "Hold Off Time is required.",
1321 1319 "client-hold-off-time-pattern": "Hold Off Time must be a positive integer.",
... ...