|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * <p>Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.example.appengine.appidentity; |
| 17 | + |
| 18 | +import com.google.appengine.api.appidentity.PublicCertificate; |
| 19 | +import com.google.appengine.api.appidentity.AppIdentityService; |
| 20 | +import com.google.appengine.api.appidentity.AppIdentityServiceFactory; |
| 21 | +import com.google.apphosting.api.ApiProxy; |
| 22 | +import com.google.apphosting.api.ApiProxy.Environment; |
| 23 | + |
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.PrintWriter; |
| 28 | +import java.io.UnsupportedEncodingException; |
| 29 | +import java.security.cert.Certificate; |
| 30 | +import java.security.cert.CertificateException; |
| 31 | +import java.security.cert.CertificateFactory; |
| 32 | +import java.security.InvalidKeyException; |
| 33 | +import java.security.NoSuchAlgorithmException; |
| 34 | +import java.security.PublicKey; |
| 35 | +import java.security.Signature; |
| 36 | +import java.security.SignatureException; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Collection; |
| 39 | + |
| 40 | +import javax.servlet.http.HttpServlet; |
| 41 | +import javax.servlet.http.HttpServletRequest; |
| 42 | +import javax.servlet.http.HttpServletResponse; |
| 43 | + |
| 44 | +@SuppressWarnings("serial") |
| 45 | +public class SignForAppServlet extends HttpServlet { |
| 46 | + private final AppIdentityService appIdentity; |
| 47 | + |
| 48 | + public SignForAppServlet() { |
| 49 | + appIdentity = AppIdentityServiceFactory.getAppIdentityService(); |
| 50 | + } |
| 51 | + |
| 52 | + // [START asserting_identity_to_other_services] |
| 53 | + // Note that the algorithm used by AppIdentity.signForApp() and |
| 54 | + // getPublicCertificatesForApp() is "SHA256withRSA" |
| 55 | + |
| 56 | + private byte[] signBlob(byte[] blob) { |
| 57 | + AppIdentityService.SigningResult result = appIdentity.signForApp(blob); |
| 58 | + return result.getSignature(); |
| 59 | + } |
| 60 | + |
| 61 | + private byte[] getPublicCertificate() throws UnsupportedEncodingException { |
| 62 | + Collection<PublicCertificate> certs = appIdentity.getPublicCertificatesForApp(); |
| 63 | + PublicCertificate publicCert = certs.iterator().next(); |
| 64 | + return publicCert.getX509CertificateInPemFormat().getBytes("UTF-8"); |
| 65 | + } |
| 66 | + |
| 67 | + private Certificate parsePublicCertificate(byte[] publicCert) |
| 68 | + throws CertificateException, NoSuchAlgorithmException { |
| 69 | + InputStream stream = new ByteArrayInputStream(publicCert); |
| 70 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 71 | + return cf.generateCertificate(stream); |
| 72 | + } |
| 73 | + |
| 74 | + private boolean verifySignature(byte[] blob, byte[] blobSignature, PublicKey pk) |
| 75 | + throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { |
| 76 | + Signature signature = Signature.getInstance("SHA256withRSA"); |
| 77 | + signature.initVerify(pk); |
| 78 | + signature.update(blob); |
| 79 | + return signature.verify(blobSignature); |
| 80 | + } |
| 81 | + |
| 82 | + private String simulateIdentityAssertion() |
| 83 | + throws CertificateException, UnsupportedEncodingException, NoSuchAlgorithmException, |
| 84 | + InvalidKeyException, SignatureException { |
| 85 | + // Simulate the sending app. |
| 86 | + String message = "abcdefg"; |
| 87 | + byte[] blob = message.getBytes(); |
| 88 | + byte[] blobSignature = signBlob(blob); |
| 89 | + byte[] publicCert = getPublicCertificate(); |
| 90 | + |
| 91 | + // Simulate the receiving app, which gets the certificate, blob, and signature. |
| 92 | + Certificate cert = parsePublicCertificate(publicCert); |
| 93 | + PublicKey pk = cert.getPublicKey(); |
| 94 | + boolean isValid = verifySignature(blob, blobSignature, pk); |
| 95 | + |
| 96 | + return String.format( |
| 97 | + "isValid=%b for message: %s\n\tsignature: %s\n\tpublic cert: %s", |
| 98 | + isValid, |
| 99 | + message, |
| 100 | + Arrays.toString(blobSignature), |
| 101 | + Arrays.toString(publicCert)); |
| 102 | + } |
| 103 | + // [END asserting_identity_to_other_services] |
| 104 | + |
| 105 | + @Override |
| 106 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 107 | + resp.setContentType("text/plain"); |
| 108 | + try { |
| 109 | + resp.getWriter().println(simulateIdentityAssertion()); |
| 110 | + } catch (Exception e) { |
| 111 | + throw new RuntimeException(e); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments