Unity User SSA CL Certificate Error

Unity에서 공공 API를 사용하는 도중 SSA CL Certificate Error와 직면했다.

원인을 찾으려 구글링 하니 다음과 같은 이유들이 나온다.

Unity takes trusted issuers from device store, so this error means you have to either install updates on your phone and expect the issuer to be added or manually add the issuers certificate (usually not recommended, since not being trusted by OS usually means the issuer is not trustforthy).

The SSL / CA Error that you are receiving is down to the Android version on one of your devices.

Turned out it was a Unity bug and it has been fixed in Unity 2020.3.28f1 and some other patch versions.

밑에 안드로이드 버전과 유니티 버전을 업그레이드 해주었는데도 효과가 없었다.

그래서 직접 UnityWebRequest의 CertificateHandler를 수정하여 사용하려고 한다.

Unity Doc에 보면 CertificateHandler에 있는 ValidateCertifcate 함수를 override해주면 된다고 나와있다.


하지만 이용하려는 사이트의 Encoded RSA Public Key가 있어야 쓸 수 있다.
Key값을 얻기 위해 다음과 같은 코드를 작성하여 UnityWebRequest.CertificateHandler에 작성한 클래스를 넣어주고 키 값을 출력한다.

public class AcceptCeritificates : CertificateHandler
{
    //Public Key
    private static string PUB_KEY= "";
    
    protected override bool ValidateCertificate(byte[] certificateData)
    {
        X509Certificate2 certificate = new X509Certificate2(certificateData);
        string pk = certificate.GetPublicKeyString();
        Debug.Log(pk);
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UsingAPI : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(Validate("PUB KEY 얻으려는 url"));
    }
    IEnumerator Validate(string url)
    {
        UnityWebRequest www = UnityWebRequest.Get(url);
        www.certificateHandler = new AcceptCeritificates();
        yield return www.SendWebRequest();
    }
}


나온 값을 PUB_KEY에 저장해주면 된다. 최종코드는 다음과 같다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AcceptCeritificates : CertificateHandler
{
    private static string PUB_KEY= "가져온 Public Key";
    protected override bool ValidateCertificate(byte[] certificateData)
    {
        X509Certificate2 certificate = new X509Certificate2(certificateData);
        string pk = certificate.GetPublicKeyString();
        if (pk.Equals(PUB_KEY))
            return true;
        return false;
    }
}


그냥 다 true값을 리턴해줘도 작동은 된다. 물론 보안이 최악이라 이 상태로 올리면 바로 짤리겠지만..

public class AcceptCeritificates : CertificateHandler
{
    protected override bool ValidateCertificate(byte[] certificateData)
    {
         return true;
    }
}