androidcertificateadbx509certificate

Install User Certificate Via ADB


Is there a way to install CA certificate (.crt file) under the Security -> Trusted Credential -> User tab via ADB? or any other "scriptable" way.


Solution

  • I figured out a way to do this, thus i was able to trust charles proxy certificate. it will be added as trusted SSL root certificate.

    First you need to get the certificate hash

    openssl x509 -inform PEM -subject_hash_old -in charles-proxy-ssl-proxying-certificate.pem | head -1>hashedCertFile
    

    i use windows, store it in a var in a matter to automate the process

    set /p certHash=<hashedCertFile
        
    
    set certHash=%certHash%.0 && DEL toto
    cat charles-proxy-ssl-proxying-certificate.pem > %certHash%
    
    openssl x509 -inform PEM -text -in charles-proxy-ssl-proxying-certificate.pem -out nul >> %certHash%
    
    adb shell mount -o rw,remount,rw /system
    
    adb push %certHash% /system/etc/security/cacerts/
    
    adb shell mount -o ro,remount,ro /system
    
    adb reboot
    

    This is the unix version copied from this answer:

    PEM_FILE_NAME=logger-charles-cert.pem
    hash=$(openssl x509 -inform PEM -subject_hash_old -in $PEM_FILE_NAME | head -1)
    OUT_FILE_NAME="$hash.0"
    
    cp $PEM_FILE_NAME $OUT_FILE_NAME
    openssl x509 -inform PEM -text -in $PEM_FILE_NAME -out /dev/null >> $OUT_FILE_NAME
    
    echo "Saved to $OUT_FILE_NAME"
    adb shell mount -o rw,remount,rw /system
    adb push $OUT_FILE_NAME /system/etc/security/cacerts/
    adb shell mount -o ro,remount,ro /system
    adb reboot