@@ -565,142 +565,6 @@ def _get_keyid(keytype, scheme, key_value, hash_algorithm="sha256"):
565
565
return keyid
566
566
567
567
568
- def create_signature (key_dict , data ):
569
- """
570
- <Purpose>
571
- Return a signature dictionary of the form:
572
- {'keyid': 'f30a0870d026980100c0573bd557394f8c1bbd6...',
573
- 'sig': '...'}.
574
-
575
- The signing process will use the private key in
576
- key_dict['keyval']['private'] and 'data' to generate the signature.
577
-
578
- The following signature schemes are supported:
579
-
580
- 'RSASSA-PSS'
581
- RFC3447 - RSASSA-PSS
582
- http://www.ietf.org/rfc/rfc3447.
583
-
584
- 'ed25519'
585
- ed25519 - high-speed high security signatures
586
- http://ed25519.cr.yp.to/
587
-
588
- Which signature to generate is determined by the key type of 'key_dict'
589
- and the available cryptography library specified in 'settings'.
590
-
591
- >>> ed25519_key = generate_ed25519_key()
592
- >>> data = 'The quick brown fox jumps over the lazy dog'
593
- >>> signature = create_signature(ed25519_key, data)
594
- >>> securesystemslib.formats.SIGNATURE_SCHEMA.matches(signature)
595
- True
596
- >>> len(signature['sig'])
597
- 128
598
- >>> rsa_key = generate_rsa_key(2048)
599
- >>> signature = create_signature(rsa_key, data)
600
- >>> securesystemslib.formats.SIGNATURE_SCHEMA.matches(signature)
601
- True
602
- >>> ecdsa_key = generate_ecdsa_key()
603
- >>> signature = create_signature(ecdsa_key, data)
604
- >>> securesystemslib.formats.SIGNATURE_SCHEMA.matches(signature)
605
- True
606
-
607
- <Arguments>
608
- key_dict:
609
- A dictionary containing the keys. An example RSA key dict has the
610
- form:
611
-
612
- {'keytype': 'rsa',
613
- 'scheme': 'rsassa-pss-sha256',
614
- 'keyid': 'f30a0870d026980100c0573bd557394f8c1bbd6...',
615
- 'keyval': {'public': '-----BEGIN RSA PUBLIC KEY----- ...',
616
- 'private': '-----BEGIN RSA PRIVATE KEY----- ...'}}
617
-
618
- The public and private keys are strings in PEM format.
619
-
620
- data:
621
- Data to be signed. This should be a bytes object; data should be
622
- encoded/serialized before it is passed here. The same value can be be
623
- passed into securesystemslib.verify_signature() (along with the public
624
- key) to later verify the signature.
625
-
626
- <Exceptions>
627
- securesystemslib.exceptions.FormatError, if 'key_dict' is improperly
628
- formatted.
629
-
630
- securesystemslib.exceptions.UnsupportedAlgorithmError, if 'key_dict'
631
- specifies an unsupported key type or signing scheme.
632
-
633
- securesystemslib.exceptions.CryptoError, if the signature cannot be
634
- generated.
635
-
636
- TypeError, if 'key_dict' contains an invalid keytype.
637
-
638
- <Side Effects>
639
- The cryptography library specified in 'settings' is called to perform the
640
- actual signing routine.
641
-
642
- <Returns>
643
- A signature dictionary conformant to
644
- 'securesystemslib_format.SIGNATURE_SCHEMA'.
645
- """
646
-
647
- # Does 'key_dict' have the correct format?
648
- # This check will ensure 'key_dict' has the appropriate number of objects
649
- # and object types, and that all dict keys are properly named.
650
- # Raise 'securesystemslib.exceptions.FormatError' if the check fails.
651
- # The key type of 'key_dict' must be either 'rsa' or 'ed25519'.
652
- formats .ANYKEY_SCHEMA .check_match (key_dict )
653
-
654
- # Signing the 'data' object requires a private key. Signing schemes that are
655
- # currently supported are: 'ed25519', 'ecdsa-sha2-nistp256',
656
- # 'ecdsa-sha2-nistp384' and rsa schemes defined in
657
- # `securesystemslib.keys.RSA_SIGNATURE_SCHEMES`.
658
- # RSASSA-PSS and RSA-PKCS1v15 keys and signatures can be generated and
659
- # verified by rsa_keys.py, and Ed25519 keys by PyNaCl and PyCA's
660
- # optimized, pure python implementation of Ed25519.
661
- signature = {}
662
- keytype = key_dict ["keytype" ]
663
- scheme = key_dict ["scheme" ]
664
- public = key_dict ["keyval" ]["public" ]
665
- private = key_dict ["keyval" ]["private" ]
666
- keyid = key_dict ["keyid" ]
667
- sig = None
668
-
669
- if keytype == "rsa" :
670
- if scheme in RSA_SIGNATURE_SCHEMES :
671
- private = private .replace ("\r \n " , "\n " )
672
- sig , scheme = rsa_keys .create_rsa_signature (private , data , scheme )
673
-
674
- else :
675
- raise exceptions .UnsupportedAlgorithmError (
676
- "Unsupported" " RSA signature scheme specified: " + repr (scheme )
677
- )
678
-
679
- elif keytype == "ed25519" :
680
- public = binascii .unhexlify (public .encode ("utf-8" ))
681
- private = binascii .unhexlify (private .encode ("utf-8" ))
682
- sig , scheme = ed25519_keys .create_signature (
683
- public , private , data , scheme
684
- )
685
-
686
- # Continue to support keytypes of ecdsa-sha2-nistp256 and ecdsa-sha2-nistp384
687
- # for backwards compatibility with older securesystemslib releases
688
- elif keytype in ["ecdsa" , "ecdsa-sha2-nistp256" , "ecdsa-sha2-nistp384" ]:
689
- sig , scheme = ecdsa_keys .create_signature (public , private , data , scheme )
690
-
691
- # 'securesystemslib.formats.ANYKEY_SCHEMA' should have detected invalid key
692
- # types. This is a defensive check against an invalid key type.
693
- else : # pragma: no cover
694
- raise TypeError ("Invalid key type." )
695
-
696
- # Build the signature dictionary to be returned.
697
- # The hexadecimal representation of 'sig' is stored in the signature.
698
- signature ["keyid" ] = keyid
699
- signature ["sig" ] = binascii .hexlify (sig ).decode ()
700
-
701
- return signature
702
-
703
-
704
568
def verify_signature (
705
569
key_dict , signature , data
706
570
): # pylint: disable=too-many-branches
0 commit comments