atecc_csr.py
1 import board 2 import busio 3 from adafruit_atecc.adafruit_atecc import ATECC, _WAKE_CLK_FREQ, CFG_TLS 4 5 import adafruit_atecc.adafruit_atecc_cert_util as cert_utils 6 7 # -- Enter your configuration below -- # 8 9 # Lock the ATECC module when the code is run? 10 LOCK_ATECC = False 11 # 2-letter country code 12 MY_COUNTRY = "US" 13 # State or Province Name 14 MY_STATE = "New York" 15 # City Name 16 MY_CITY = "New York" 17 # Organization Name 18 MY_ORG = "Adafruit" 19 # Organizational Unit Name 20 MY_SECTION = "Crypto" 21 # Which ATECC slot (0-4) to use 22 ATECC_SLOT = 0 23 # Generate new private key, or use existing key 24 GENERATE_PRIVATE_KEY = True 25 26 # -- END Configuration, code below -- # 27 28 # Initialize the i2c bus 29 i2c = busio.I2C(board.SCL, board.SDA, frequency=_WAKE_CLK_FREQ) 30 31 # Initialize a new atecc object 32 atecc = ATECC(i2c) 33 34 print("ATECC Serial Number: ", atecc.serial_number) 35 36 if not atecc.locked: 37 if not LOCK_ATECC: 38 raise RuntimeError( 39 "The ATECC is not locked, set LOCK_ATECC to True in code.py." 40 ) 41 print("Writing default configuration to the device...") 42 atecc.write_config(CFG_TLS) 43 print("Wrote configuration, locking ATECC module...") 44 # Lock ATECC config, data, and otp zones 45 atecc.lock_all_zones() 46 print("ATECC locked!") 47 48 print("Generating Certificate Signing Request...") 49 # Initialize a certificate signing request with provided info 50 csr = cert_utils.CSR( 51 atecc, 52 ATECC_SLOT, 53 GENERATE_PRIVATE_KEY, 54 MY_COUNTRY, 55 MY_STATE, 56 MY_CITY, 57 MY_ORG, 58 MY_SECTION, 59 ) 60 # Generate CSR 61 my_csr = csr.generate_csr() 62 print("-----BEGIN CERTIFICATE REQUEST-----\n") 63 print(my_csr.decode("utf-8")) 64 print("-----END CERTIFICATE REQUEST-----")