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
use std::fmt;
use std::str::FromStr;
use openssl::hash as openssl_hash;
use opcua_types::status_code::StatusCode;
use opcua_types::ByteString;
use crate::{
crypto::{
SHA1_SIZE, SHA256_SIZE,
aeskey::AesKey,
pkey::{PrivateKey, PublicKey, RsaPadding, KeySize},
hash,
}
};
pub const SECURITY_POLICY_NONE_URI: &str = "http://opcfoundation.org/UA/SecurityPolicy#None";
pub const SECURITY_POLICY_BASIC_128_RSA_15_URI: &str = "http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15";
pub const SECURITY_POLICY_BASIC_256_URI: &str = "http://opcfoundation.org/UA/SecurityPolicy#Basic256";
pub const SECURITY_POLICY_BASIC_256_SHA_256_URI: &str = "http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256";
pub const SECURITY_POLICY_NONE: &str = "None";
pub const SECURITY_POLICY_BASIC_128_RSA_15: &str = "Basic128Rsa15";
pub const SECURITY_POLICY_BASIC_256: &str = "Basic256";
pub const SECURITY_POLICY_BASIC_256_SHA_256: &str = "Basic256Sha256";
pub mod basic128rsa15 {
use crate::crypto::algorithms::*;
pub const SYMMETRIC_SIGNATURE_ALGORITHM: &str = DSIG_HMAC_SHA1;
pub const SYMMETRIC_ENCRYPTION_ALGORITHM: &str = ENC_AES128_CBC;
pub const ASYMMETRIC_SIGNATURE_ALGORITHM: &str = DSIG_RSA_SHA1;
pub const ASYMMETRIC_KEY_WRAP_ALGORITHM: &str = ENC_RSA_15;
pub const ASYMMETRIC_ENCRYPTION_ALGORITHM: &str = ENC_RSA_15;
pub const KEY_DERIVATION_ALGORITHM: &str = KEY_P_SHA1;
pub const DERIVED_SIGNATURE_KEY_LENGTH: usize = 128;
pub const DERIVED_ENCRYPTION_KEY_LENGTH: usize = 128;
pub const MIN_ASYMMETRIC_KEY_LENGTH: usize = 512;
pub const MAX_ASYMMETRIC_KEY_LENGTH: usize = 2048;
pub const SYMMETRIC_KEY_LENGTH: usize = 128;
pub const CERTIFICATE_SIGNATURE_ALGORITHM: &str = "Sha1";
}
pub mod basic256 {
use crate::crypto::algorithms::*;
pub const SYMMETRIC_SIGNATURE_ALGORITHM: &str = DSIG_HMAC_SHA1;
pub const SYMMETRIC_ENCRYPTION_ALGORITHM: &str = ENC_AES256_CBC;
pub const ASYMMETRIC_SIGNATURE_ALGORITHM: &str = DSIG_RSA_SHA1;
pub const ASYMMETRIC_KEY_WRAP_ALGORITHM: &str = ENC_RSA_OAEP_MGF1P;
pub const ASYMMETRIC_ENCRYPTION_ALGORITHM: &str = ENC_RSA_OAEP;
pub const KEY_DERIVATION_ALGORITHM: &str = KEY_P_SHA1;
pub const DERIVED_SIGNATURE_KEY_LENGTH: usize = 192;
pub const DERIVED_ENCRYPTION_KEY_LENGTH: usize = 192;
pub const MIN_ASYMMETRIC_KEY_LENGTH: usize = 512;
pub const MAX_ASYMMETRIC_KEY_LENGTH: usize = 2048;
pub const SYMMETRIC_KEY_LENGTH: usize = 256;
pub const CERTIFICATE_SIGNATURE_ALGORITHM: &str = "Sha256";
}
pub mod basic256sha256 {
use crate::crypto::algorithms::*;
pub const SYMMETRIC_SIGNATURE_ALGORITHM: &str = DSIG_HMAC_SHA256;
pub const SYMMETRIC_ENCRYPTION_ALGORITHM: &str = ENC_AES256_CBC;
pub const ASYMMETRIC_SIGNATURE_ALGORITHM: &str = DSIG_RSA_SHA256;
pub const ASYMMETRIC_KEY_WRAP_ALGORITHM: &str = ENC_RSA_OAEP_MGF1P;
pub const ASYMMETRIC_ENCRYPTION_ALGORITHM: &str = ENC_RSA_OAEP;
pub const KEY_DERIVATION_ALGORITHM: &str = KEY_P_SHA256;
pub const DERIVED_SIGNATURE_KEY_LENGTH: usize = 256;
pub const DERIVED_ENCRYPTION_KEY_LENGTH: usize = 256;
pub const MIN_ASYMMETRIC_KEY_LENGTH: usize = 1024;
pub const MAX_ASYMMETRIC_KEY_LENGTH: usize = 2048;
pub const SYMMETRIC_KEY_LENGTH: usize = 256;
pub const CERTIFICATE_SIGNATURE_ALGORITHM: &str = "Sha256";
}
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum SecurityPolicy {
Unknown,
None,
Basic128Rsa15,
Basic256,
Basic256Sha256,
}
impl fmt::Display for SecurityPolicy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_str())
}
}
impl FromStr for SecurityPolicy {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
SECURITY_POLICY_NONE | SECURITY_POLICY_NONE_URI => SecurityPolicy::None,
SECURITY_POLICY_BASIC_128_RSA_15 | SECURITY_POLICY_BASIC_128_RSA_15_URI => SecurityPolicy::Basic128Rsa15,
SECURITY_POLICY_BASIC_256 | SECURITY_POLICY_BASIC_256_URI => SecurityPolicy::Basic256,
SECURITY_POLICY_BASIC_256_SHA_256 | SECURITY_POLICY_BASIC_256_SHA_256_URI => SecurityPolicy::Basic256Sha256,
_ => {
error!("Specified security policy {} is not recognized", s);
SecurityPolicy::Unknown
}
})
}
}
impl From<SecurityPolicy> for String {
fn from(v: SecurityPolicy) -> String {
v.to_str().to_string()
}
}
impl SecurityPolicy {
pub fn to_uri(&self) -> &'static str {
match *self {
SecurityPolicy::None => SECURITY_POLICY_NONE_URI,
SecurityPolicy::Basic128Rsa15 => SECURITY_POLICY_BASIC_128_RSA_15_URI,
SecurityPolicy::Basic256 => SECURITY_POLICY_BASIC_256_URI,
SecurityPolicy::Basic256Sha256 => SECURITY_POLICY_BASIC_256_SHA_256_URI,
_ => {
panic!("Shouldn't be turning an unknown policy into a uri");
}
}
}
pub fn to_str(&self) -> &'static str {
match *self {
SecurityPolicy::None => SECURITY_POLICY_NONE,
SecurityPolicy::Basic128Rsa15 => SECURITY_POLICY_BASIC_128_RSA_15,
SecurityPolicy::Basic256 => SECURITY_POLICY_BASIC_256,
SecurityPolicy::Basic256Sha256 => SECURITY_POLICY_BASIC_256_SHA_256,
_ => {
panic!("Shouldn't be turning an unknown policy into a string");
}
}
}
pub fn asymmetric_signature_algorithm(&self) -> &'static str {
match *self {
SecurityPolicy::Basic128Rsa15 => basic128rsa15::ASYMMETRIC_SIGNATURE_ALGORITHM,
SecurityPolicy::Basic256 => basic256::ASYMMETRIC_SIGNATURE_ALGORITHM,
SecurityPolicy::Basic256Sha256 => basic256sha256::ASYMMETRIC_SIGNATURE_ALGORITHM,
_ => {
panic!("Invalid policy");
}
}
}
pub fn symmetric_signature_algorithm(&self) -> &'static str {
match *self {
SecurityPolicy::Basic128Rsa15 => basic128rsa15::SYMMETRIC_SIGNATURE_ALGORITHM,
SecurityPolicy::Basic256 => basic256::SYMMETRIC_SIGNATURE_ALGORITHM,
SecurityPolicy::Basic256Sha256 => basic256sha256::SYMMETRIC_SIGNATURE_ALGORITHM,
_ => {
panic!("Invalid policy");
}
}
}
pub fn symmetric_key_size(&self) -> usize {
let length = match *self {
SecurityPolicy::Basic128Rsa15 => basic128rsa15::SYMMETRIC_KEY_LENGTH,
SecurityPolicy::Basic256 => basic256::SYMMETRIC_KEY_LENGTH,
SecurityPolicy::Basic256Sha256 => basic256sha256::SYMMETRIC_KEY_LENGTH,
_ => {
panic!("Invalid policy");
}
};
length / 8
}
pub fn plain_block_size(&self) -> usize {
match *self {
SecurityPolicy::Basic128Rsa15 | SecurityPolicy::Basic256 | SecurityPolicy::Basic256Sha256 => 16,
_ => {
panic!("Invalid policy");
}
}
}
pub fn symmetric_signature_size(&self) -> usize {
match *self {
SecurityPolicy::None => 0,
SecurityPolicy::Basic128Rsa15 | SecurityPolicy::Basic256 => SHA1_SIZE,
SecurityPolicy::Basic256Sha256 => SHA256_SIZE,
_ => {
panic!("Invalid policy");
}
}
}
pub fn derived_signature_key_size(&self) -> usize {
let length = match *self {
SecurityPolicy::Basic128Rsa15 => basic128rsa15::DERIVED_SIGNATURE_KEY_LENGTH,
SecurityPolicy::Basic256 => basic256::DERIVED_SIGNATURE_KEY_LENGTH,
SecurityPolicy::Basic256Sha256 => basic256sha256::DERIVED_SIGNATURE_KEY_LENGTH,
_ => {
panic!("Invalid policy");
}
};
length / 8
}
pub fn min_asymmetric_key_length(&self) -> usize {
match *self {
SecurityPolicy::Basic128Rsa15 => basic128rsa15::MIN_ASYMMETRIC_KEY_LENGTH,
SecurityPolicy::Basic256 => basic256::MIN_ASYMMETRIC_KEY_LENGTH,
SecurityPolicy::Basic256Sha256 => basic256sha256::MIN_ASYMMETRIC_KEY_LENGTH,
_ => {
panic!("Invalid policy");
}
}
}
pub fn max_asymmetric_key_length(&self) -> usize {
match *self {
SecurityPolicy::Basic128Rsa15 => basic128rsa15::MAX_ASYMMETRIC_KEY_LENGTH,
SecurityPolicy::Basic256 => basic256::MAX_ASYMMETRIC_KEY_LENGTH,
SecurityPolicy::Basic256Sha256 => basic256sha256::MAX_ASYMMETRIC_KEY_LENGTH,
_ => {
panic!("Invalid policy");
}
}
}
pub fn random_nonce(&self) -> ByteString {
match *self {
SecurityPolicy::None => ByteString::null(),
SecurityPolicy::Basic128Rsa15 |
SecurityPolicy::Basic256 |
SecurityPolicy::Basic256Sha256 => ByteString::random(self.symmetric_key_size()),
_ => {
panic!("Can't make a nonce because key size is unknown");
}
}
}
pub fn from_uri(uri: &str) -> SecurityPolicy {
match uri {
SECURITY_POLICY_NONE_URI => SecurityPolicy::None,
SECURITY_POLICY_BASIC_128_RSA_15_URI => SecurityPolicy::Basic128Rsa15,
SECURITY_POLICY_BASIC_256_URI => SecurityPolicy::Basic256,
SECURITY_POLICY_BASIC_256_SHA_256_URI => SecurityPolicy::Basic256Sha256,
_ => {
error!("Specified security policy {} is not recognized", uri);
SecurityPolicy::Unknown
}
}
}
fn prf(&self, secret: &[u8], seed: &[u8], length: usize, offset: usize) -> Vec<u8> {
let message_digest = match *self {
SecurityPolicy::Basic128Rsa15 | SecurityPolicy::Basic256 => openssl_hash::MessageDigest::sha1(),
SecurityPolicy::Basic256Sha256 => openssl_hash::MessageDigest::sha256(),
_ => {
panic!("Invalid policy");
}
};
let result = hash::p_sha(message_digest, secret, seed, offset + length);
result[offset..(offset + length)].to_vec()
}
pub fn make_secure_channel_keys(&self, secret: &[u8], seed: &[u8]) -> (Vec<u8>, AesKey, Vec<u8>) {
let signing_key_length = self.derived_signature_key_size();
let (encrypting_key_length, encrypting_block_size) = match *self {
SecurityPolicy::Basic128Rsa15 => (16, 16),
SecurityPolicy::Basic256 | SecurityPolicy::Basic256Sha256 => (32, 16),
_ => {
panic!("Invalid policy");
}
};
let signing_key = self.prf(secret, seed, signing_key_length, 0);
let encrypting_key = self.prf(secret, seed, encrypting_key_length, signing_key_length);
let encrypting_key = AesKey::new(*self, &encrypting_key);
let iv = self.prf(secret, seed, encrypting_block_size, signing_key_length + encrypting_key_length);
(signing_key, encrypting_key, iv)
}
pub fn asymmetric_sign(&self, signing_key: &PrivateKey, data: &[u8], signature: &mut [u8]) -> Result<usize, StatusCode> {
let result = match *self {
SecurityPolicy::Basic128Rsa15 | SecurityPolicy::Basic256 => {
signing_key.sign_hmac_sha1(data, signature)?
}
SecurityPolicy::Basic256Sha256 => {
signing_key.sign_hmac_sha256(data, signature)?
}
_ => {
panic!("Invalid policy");
}
};
Ok(result)
}
pub fn asymmetric_verify_signature(&self, verification_key: &PublicKey, data: &[u8], signature: &[u8], their_private_key: Option<PrivateKey>) -> Result<(), StatusCode> {
let result = match *self {
SecurityPolicy::Basic128Rsa15 | SecurityPolicy::Basic256 => {
verification_key.verify_hmac_sha1(data, signature)?
}
SecurityPolicy::Basic256Sha256 => {
verification_key.verify_hmac_sha256(data, signature)?
}
_ => {
panic!("Invalid policy");
}
};
if result {
Ok(())
} else {
error!("Signature mismatch");
if let Some(their_key) = their_private_key {
let mut their_signature = vec![0u8; their_key.size()];
self.asymmetric_sign(&their_key, data, &mut their_signature[..])?;
trace!("Using their_key, signature should be {:?}", &their_signature);
}
Err(StatusCode::BadSecurityChecksFailed)
}
}
pub fn padding(&self) -> RsaPadding {
match *self {
SecurityPolicy::Basic128Rsa15 => RsaPadding::PKCS1,
SecurityPolicy::Basic256 | SecurityPolicy::Basic256Sha256 => RsaPadding::OAEP,
_ => {
panic!("Security policy is not supported, shouldn't have gotten here");
}
}
}
pub fn asymmetric_encrypt(&self, encryption_key: &PublicKey, src: &[u8], dst: &mut [u8]) -> Result<usize, StatusCode> {
let padding = self.padding();
if let Ok(encrypted_size) = encryption_key.public_encrypt(src, dst, padding) {
Ok(encrypted_size)
} else {
Err(StatusCode::BadUnexpectedError)
}
}
pub fn asymmetric_decrypt(&self, decryption_key: &PrivateKey, src: &[u8], dst: &mut [u8]) -> Result<usize, StatusCode> {
let padding = self.padding();
if let Ok(decrypted_size) = decryption_key.private_decrypt(src, dst, padding) {
Ok(decrypted_size)
} else {
error!("Asymmetric decryption failed");
Err(StatusCode::BadSecurityChecksFailed)
}
}
pub fn symmetric_sign(&self, key: &[u8], data: &[u8], signature: &mut [u8]) -> Result<(), StatusCode> {
trace!("Producing signature for {} bytes of data into signature of {} bytes", data.len(), signature.len());
match *self {
SecurityPolicy::Basic128Rsa15 | SecurityPolicy::Basic256 => {
hash::hmac_sha1(key, data, signature)
}
SecurityPolicy::Basic256Sha256 => {
hash::hmac_sha256(key, data, signature)
}
_ => {
panic!("Unsupported policy")
}
}
}
pub fn symmetric_verify_signature(&self, key: &[u8], data: &[u8], signature: &[u8]) -> Result<bool, StatusCode> {
let verified = match *self {
SecurityPolicy::Basic128Rsa15 | SecurityPolicy::Basic256 => {
hash::verify_hmac_sha1(key, data, signature)
}
SecurityPolicy::Basic256Sha256 => {
hash::verify_hmac_sha256(key, data, signature)
}
_ => {
panic!("Unsupported policy")
}
};
if verified {
Ok(verified)
} else {
error!("Signature invalid {:?}", signature);
Err(StatusCode::BadSecurityChecksFailed)
}
}
pub fn symmetric_encrypt(&self, key: &AesKey, iv: &[u8], src: &[u8], dst: &mut [u8]) -> Result<usize, StatusCode> {
key.encrypt(src, iv, dst)
}
pub fn symmetric_decrypt(&self, key: &AesKey, iv: &[u8], src: &[u8], dst: &mut [u8]) -> Result<usize, StatusCode> {
key.decrypt(src, iv, dst)
}
}