I want B to be run only by the private
method A#getSensitiveData()
that uses or does some processing on sensitive data (example: cryptographic keys, national id, whatever).
public final class A{
private transient final B sensitiveHolder; //set at Constructor
public A(B sensitiveHolder){
this.sensitiveHolder = sensitiveHolder;
}
private final byte[] getSensitiveData(){
return sensitiveHolder.getSensitiveData();
}
}
public final class B{
private transient final byte[] sensitiveData;//encrypt and set at Constructor
public final byte[] getSensitiveData(){
//check if it is run by A#getSensitiveData(); if it is, decrypt by DEK and give plaintext.
}
}
Please take into account that the code would be obfuscated, so please refrain from putting in any package names as String
.
What must I write with SecurityManager#checkPrivilege()
and AccessController.doPrivileged()
before I can achieve such an effect?
EDIT: Obviously this is different because the so called "answer" does not contain any CODE. WORKING CODE is worth infinitely more than "oh, just do this and that".
You could do something like this:
And to call the method:
In your
A
class call yourB
class like this:I don't know if this is what you need or it is near that. You could play around the values in the
equals
section of theif
. I got and modified the example from this site.If you're able to use JDK 9+, which introduces
StackWalker
, this sort of thing might work for you. This technique seems to supersede use ofsun.reflect.Reflection#getCallerClass(int)
. (I hope you weren't counting on a SecurityManager-related answer.)